使用真实(正面和负面)数字加密后缀 [英] Infix to postfix with real (postive and negative) numbers

查看:156
本文介绍了使用真实(正面和负面)数字加密后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个允许从中缀到后缀表达式进行翻译的程序,但它仅适用于一位 [A-Z] [a-z] [0-9] 。如何才能使真实(正面和负面)数字成为可能?

I've written a program that allows translation from infix to postfix expression but it works only for one digit [A-Z][a-z][0-9]. How can I do to make it possible for real (positive and negative) numbers?

Example: (50 + 3.75) + 50 --> 50 3.75 + 50 +

doTrans()允许从中缀到后缀的翻译

doTrans() which allows translation from infix to postfix

public String doTrans() {
        for (int j = 0; j < input.length(); j++) {
            char ch = input.charAt(j);
            theStack.displayStack("For " + ch + " ");
            switch (ch) {
            case '+':
            case '-':
                gotOper(ch, 1);
                break;
            case '*':
            case '/':
                gotOper(ch, 2);
                break;
            case '(':
                theStack.push(ch);
                break;
            case ')':
                gotParen(ch);
                break;
            default:
                output = output + ch;
                break;
            }
        }
        while (!theStack.isEmpty()) {
            theStack.displayStack("While ");
            output = output + theStack.pop();
        }
        theStack.displayStack("End ");
        return output;
    }

此行中的问题 output = output + ch ; 。我找不到如何获得整数而不是一个数字的解决方案

The problem in this line output = output + ch;. I can't find a solution for how to get the whole number instead of only one digit

推荐答案

你必须做一个< a href =http://en.wikipedia.org/wiki/Lexical_analysis =nofollow>词法分析。您必须将输入流转换为标记并对其进行分类。目前您的程序已经完成,但它非常基础,那么您的令牌只包含一位数。虽然令牌可能更复杂,从大于9的简单整数文字,浮点数字面值,字符串文字,简单运算符等开始。

You have to do a Lexical analysis. You have to convert your input stream to tokens and classify them. At the moment your program already does, but it is very very basic then your tokens consist only of one digit. Though tokens can be more complex, starting from simple integer number literals greater than 9, floating point number literal, string literals, simple operators and so on.

你可能有什么现在可以让你在后续调用时使用下一个令牌来分析处理它并逐步进入下一个令牌。类似于:

What you might have now is something that gives you on subsequent calls the next token to analyze process it and step to the next. something like:

String input = "1 + 2";
int actPos = 0 ;

....

char getNextToken() {
    return input.charAt(actPos++);
}

你需要做的是重写 getNextToken ()这样它就会给你一个复杂的令牌(由多个数字/字符组成,因此是一个字符串),你必须在下一步中进行分类。

What you need to do is to rewrite the getNextToken() so that it gives you a "complex" token back (consisting of more than one digit/char, thus a string) witch you have to classify in the next step.

String getNextToken() {
    String StringBuilder token = new StirngBuilde();

    // extract the next token from the input stream
    // using a state automata witch comes to a final
    // state when a token was recognized or an erroneous input

    return token.toString();
}

EIDT

您需要编写有限状态机来分析输入流并且生成令牌。

You need to write finite state machine to analyze the input stream and produce tokens.

为了给你一个非常简单的例子(假设你已经阅读了上面链接中的定义),假设你有字母 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} 并且想要构建没有符号的整数标记。您将整数定义为 [0-9] +

Just to give you a very simple example (assuming you have read the definition in the link above), let's say you have the alphabet {'0','1','2','3','4','5','6','7','8','9'} and want to build tokens that are integers without a sign. You define integer as [0-9]+

您的状态机至少需要三个状态 START 女巫是开始状态, INTEGER 女巫是最终和接受状态, ERROR witch表示输入中有错误。

Your state machine will need at least three states START witch is the start state, INTEGER witch is a final and accept state, and ERROR witch denotes that there is an error in the input.

现在你需要一个转换函数和一个状态转换表,这是实际输入并根据实际状态产生下一个状态。

Now You need a transition function and a state transition table, this is what takes the actual input and produces the next state according the actual state.

状态转换表看起来像那样(非常基本,在没有输入时无法处理)

The state transition table could look something like that (very basic and cannot handle when given no input)

              i n p u t
              +----------+----------+
              | [0-9]    | else     |
s  +----------+----------+----------+
t  | START    | INTEGER  | ERROR    |
a  +----------+----------+----------+
t  | INTEGER  | INTEGER  | ERROR    |
e  +----------+----------+----------+
   | ERROR    | ERROR    | ERROR    |
   +----------+----------+----------+

因此,假设您有以下输入: 27

So let's say you have the following input: 27


  • 开始时状态机的状态为 START (实际状态)

  • 您读取第一个字符 2 并将其与实际状态一起传递给转换函数

  • 由于 2 是一个整数,你的函数将你带到状态 INTEGER ,因为这不是 ERROR 你将它放在缓冲区中,用char来构建令牌char。

  • 现在你读取下一个char 7 并传递它以及转换函数的实际状态 INTEGER

  • 7 也是一个整数,与 2 相同。

  • 现在,令牌缓冲区的内容 27 ,您的状态机仍然具有状态 INTEGER 巫婆是接受状态,没有输入。此时,您可以将令牌缓冲区的内容作为下一个令牌返回。

  • At the start the state machine has the state START (actual state)
  • You read the first char 2 and pass it along with the actual state to the transition function
  • Since 2 is an integer your function brings you to the state INTEGER and since this is not an ERROR you put it in the buffer where you build the token char by char.
  • Now you read the next char 7 and pass it along with the actual state INTEGER to the transition function.
  • 7 is also an integer, the same thing happens as with 2.
  • Now the token buffer has the content 27 and your state machine still has the state INTEGER witch is an accept state and there is no input left. At this moment you can return the content of the token buffer as being the next token.

现在假设您有输入: 2e7

您的状态机如上所述,第一个字符 2 和州 INTEGER ,然后来 e 女巫不符合上面定义的整数的规则( [0-9] + )。将 e 与实际状态 INTEGER 一起传递给转换函数结果转换状态 ERROR 。此时,您可以通知 e 的位置/索引处的输入错误。

Your state machine proceeds like above with the first char 2 and the state INTEGER, then comes e witch does not meet the rule for integer numbers defined above ([0-9]+). Passing e with the actual state INTEGER to the transition function result in the transition the state ERROR. At this point you can notify that there is an error in the input at position/index of e.

我的建议您阅读更多关于词法分析以及如何编写有限状态机来实现它。

My advise is that you read more about lexical analysis and how you can code a finite state machine the achieve it.

有人说过,你总是可以使用像 JLex 为您生成此分析代码,但您仍需要为其生成一些规则以生成执行您希望它执行的操作的代码,这会导致您回到阅读更多关于词汇分析的信息:))

This been said, you could always use some tool like JLex that generates this analysis code for you, but you still have to define some rules for it to have generate code that does what you want it to do, witch leads you back to reading more about lexical analysis :)

祝你好运!

这篇关于使用真实(正面和负面)数字加密后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆