如何正确解析算术表达式中的数字,以区分正数和负数? [英] How to properly parse numbers in an arithmetic expression, differentiating positive and negative ones?

查看:198
本文介绍了如何正确解析算术表达式中的数字,以区分正数和负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据结构类中有一个分配,其中我必须编程一个计算器,该计算器使用4个基本运算和括号来求解算术表达式,输入是通过stdin缓冲区完成的,而输出则是相同的.

I have an assignment in my Data Structures class in which I have to program a calculator that solves arithmetic expressions with the 4 basic operations and parenthesis, the input is done via the stdin buffer and the same with the output.

一开始很容易,老师为我们提供了算法(如何将表达式从中缀转换为后缀以及如何对其进行求值),唯一的目标是我们实现自己的堆栈并使用它,但是计算器本身不能很好地工作,我认为这是因为我的解析器.

It was easy at the beginning, the teacher provided us the algorithms (how to transform the expression from the infix to the postfix and how to evaluate it) and the only goal was for us to implement our own stack and use it, but the calculator itself does not work quite well, and I think it is because of my parser.

这是算法,还有我的代码,用于解析数字,运算符和括号,然后将它们放入数组中以更易于稍后求值的方式存储表达式.

This is the algorithm, and my code, used to parse the numbers, operators and parenthesis while putting them into an array to store the expression in a way that it is easier to evaluate later.

// saida is an array of pairs of integers, the first value of the pair is the value of the info (the number itself or the ASCII value of the operator)
// The second value is an indicator of whether it is a number or a operator
for (i = 0; i < exp_size; i++) {
    c = expression[i];

    // If the current char is a digit, store it into a helper string and keep going until a non-digit is found
    // Then atoi() is used to transform this string into an int and then store it.
    if (c >= '0' && c <= '9') {
        j = 1, k = i+1;
        tempInt[0] = c;
        while(expression[k] >= '0' && expression[k] <= '9') {
            tempInt[j++] = expression[k];
            k++;
        }
        tempInt[j] = '\0';
        saida[saidaIndex][0] = atoi(tempInt);
        saida[saidaIndex++][1] = 0;
        i = k-1;
    }

    // If the character is an operator, the algorithm is followed.
    else if (c == '+' || c == '-' || c == '*' || c == '/') {
        while(pilha->size > 0 && isOpBigger( stack_top(pilha), c )) {
            saida[saidaIndex][0] = stack_pop(pilha);
            saida[saidaIndex++][1] = 1;
        }
        stack_push(c, pilha);
    }
    else if (c == '(') stack_push(c, pilha);
    else if (c == ')') {
        j = stack_pop(pilha);
        while(j != '(') {
            saida[saidaIndex][0] = j;
            saida[saidaIndex++][1] = 1;
            j = stack_pop(pilha);
        }
    }
}

问题是,在这段代码中,我无法确定减号是否表示减法运算符或负数(我知道减号运算符是具有负数的和,但并没有帮助我解决这个问题),我想到了以下问题,但没有成功:

The problem is, in this code I can't tell if a minus sign indicates a subtraction operator or a negative number (I know that a minus operator is a sum with a negative number, but it didn't helped me solving this problem), I thought of the following, none with success:

  • 每次都有减号时,请存储一个加号,并让下一个数字本身为* -1.如果下一个数字已经是负数,或者减号在带括号的表达式(例如1 + 2-(3 * 4))之前,则无法使用.
  • 如果减号后有空格,则为运算符,否则为负数.无法使用,因为输入中没有这些规则.

我在口译员方面没有任何经验,我真的不知道该如何进行.该代码可以完美地处理没有负数的有效表达式,但不能与()+ 3-()之类的怪异表达式配合使用,但这是另一个问题.

I have no experience whatsoever in interpreters and I really don't know how to proceed. The code works perfectly with valid expressions without negative numbers, but not with weird ones like () + 3 - (), but that's another problem.

感谢您的帮助.

推荐答案

这就是称为一元减"的问题,在您的情况下(无变量)可以通过替换来解决.

That is the problem called "unary minus" and can in your case (no variables) get solved by substitution.

运算符-是一元负号

  • 在左括号之前
  • 由另一个运算符
  • 之前
  • 输入的第一个字符

现在,您不用存储-而是存储一个不同的字符,例如说m,并为其赋予比其他运算符更高的优先级(如果有,则与幂运算符相同).

Now instead of storing a - you store a different character like, say m and assign it a higher precedence than the other operators (or the same as the exponentiation operator if you have one).

另一个提示:请勿使用空格来表示任何内容,算术表达式必须在没有任何空格的情况下起作用,否则它是不正确的.

Another tip: don't use spaces to indicate anything, an arithmetic expression must work without any spaces or it is not correct.

这篇关于如何正确解析算术表达式中的数字,以区分正数和负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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