c ++ realize calculate arithmetic expressions Stack

With a stack converts the arithmetic expression into postfix form of expression it should not be unfamiliar, but I found quite a few pit in the realization calculations.

Description Title: read only contains a +, -, *, / nonnegative integer calculation expression, the expression of the calculated value.

Input Description: test input comprising a plurality of test cases, each test case per line, each line of no more than 200 characters, separated by a space between the integer and the operator. No illegal expression. When a line input end only 0:00, not output the corresponding results.

Output Description: row 1 output for each test case, i.e. the value of the expression, 2 decimal place.

Example 1 Input

1 + 2 4 + 2 * 5 - 7 / 11 0

Export

3.00 13.36

This problem must do pay attention to several issues:

  • When the stack is no operator, on top of the stack and direct the operator to read the string compared to be wrong, so here go out at the beginning of the stack is pressed into our design a symbol '#'
  • When we traverse the string, digital experience to be extra careful, because this time everything is in the form of a character, such as input 1 11 will become two characters, so when we read the judge to ensure that All numeric characters are read together, rather than to read only the first character.
  • When we finished traverse the string, probably also left the stack did not deal with the operators, so we need to continue to deal with the stack of operators, here joined the symbols behind the string \ $ is very clever, \ $ priority than '#' high, lower than other symbols, so this ensures that when read characters \ $ when all the characters than it appears certain the stack of high priority (in addition to its own and '#'), so We can let the rest of the stack operator with a pop up to the end of the process, the stack is the '#', pressed into the \ $.

Specific code as follows:

#include<iostream>
#include<stack>
#include<string>
#include<stdio.h>
#include<cctype>
using namespace std;


/**
 * 比较操作符优先级的函数

 */
int OperPrecedence(char a){
    if(a == '/' || a == '*'){
        return 3;
    }else if(a == '+' || a == '-' ){
        return 2;
    }else if(a == '$'){
        return 1;
    }else if(a == '#'){
        return 0;
    }

}

/**
 * 实现运算
 */

double operNums(char oper, double num1, double num2){
    if(oper == '+'){
        return num1 + num2;
    }else if(oper == '-'){
        return num1 - num2;
    }else if(oper == '*'){
        return num1 * num2;
    }else if(oper == '/'){
        return num1 / num2;
    }
}

/**
 * 实现遇到数字往后移动直到数字确定
 */

int getNum(string str, int &index){
    string num = "";
    while(isdigit(str[index])){
        num += str[index];
        index ++;
    }

    return atoi(num.c_str());
}
int main(){
    stack<char> operStack;
    stack<double> numStack;
    string str;
    char oper;
    double num1;
    double num2;
    double sum = 0;
    int index = 0, preceResult;
    operStack.push('#');
    while (getline(cin, str))
    {

        str += '$';
        if(str[0] == '0'){
           
            break;
        }

        while(index < str.size()){
          
        if(str[index] == ' '){
            index++;
        }
        else if(isdigit(str[index])){
            //数字
            numStack.push(getNum(str, index));
        }else
        {
            //操作符
            //比较优先级

            if(OperPrecedence(operStack.top()) < OperPrecedence(str[index])){
                operStack.push(str[index]);
                index++;
            }else{
                num2 = numStack.top();
                numStack.pop();
                num1 = numStack.top();
                numStack.pop();
                oper = operStack.top();
                operStack.pop();
                numStack.push(operNums(oper, num1 ,num2));

            }
            
        }
        
        
        }
        // std::cout << "sum="<<sum << endl;
        printf("%.2f\n", numStack.top());
        index = 0;
        
        
    }
  


}


Guess you like

Origin www.cnblogs.com/yuyuan-bb/p/12614656.html