7.1 stack
codeup 1918
Ideas: 1. Interfix to Suffix
2. calculated values
The steps of suffix conversion are as follows:
1. The read-in data is stored in the string, and the preprocessing deletes all the blanks.
2. Read characters sequentially and store them in suffix queues if they are operands.
3. If it is an operator, it is compared with the priority of the stack top operator of the operator stack:
1) If the current operator priority is higher than the top operator priority, then the operator is placed on the operator stack.
2) If the current operator is lower than the top operator priority, pop the operator stack to the suffix queue until the top operator priority is lower than the current operator, and then put the operator on the stack;
4. Repeat 3 until the infix expression traverses. If the stack is not empty, pop the remaining operators into the suffix queue one by one.
Attention: 1. As for digital reading, since it is read by character, after reading a digit, it goes into a loop and reads to 10 digits until it is not a digit.
2. About calculating suffix expression: every time you read the first element of the queue, and always pay attention to reading one, you have to pop(), which is easy to miss. If it is a number, you can put it on the stack. If it is an operator, you can pop out two numbers from the stack. Note that the pop after pop is the operand, and the pop after pop is the operand!!! Then the calculation is performed according to the operator and the results are push ed back into the stack! Repeat operation, the final number in the stack is the final result!
4. In addition, this topic defines a struct to save num/op, which is determined by flag!!!
The following is the code in the book:
#include <iostream> #include <cstdio> #include <string> #include <stack> #include <queue> #include <map> using namespace std; struct node{ double num; char op; bool flag; }; string str; stack<node> s; queue<node> q; map<char, int> op; void change() { double num; node temp; for(int i=0; i<str.length();) { if(str[i]>='0' && str[i]<='9') { temp.flag=true; temp.num=str[i++]-'0'; while(i<str.length() && str[i]>='0' &&str[i]<='9') { temp.num=temp.num*10+str[i]-'0'; i++; } q.push(temp); } else { temp.flag=false; while(!s.empty() && op[str[i]]<=op[s.top().op]) { q.push(s.top()); s.pop(); } temp.op=str[i]; s.push(temp); i++; } } while(!s.empty()) { q.push(s.top()); s.pop(); } } double Cal() { double temp1, temp2; node cur, temp; while(!q.empty()) { cur=q.front(); q.pop(); if(cur.flag==true) s.push(cur); else { temp2=s.top().num; s.pop(); temp1=s.top().num; s.pop(); temp.flag=true; if(cur.op=='+') temp.num=temp1+temp2; else if(cur.op=='-') temp.num=temp1-temp2; else if(cur.op=='*') temp.num=temp1*temp2; else temp.num=temp1/temp2; s.push(temp); } } return s.top().num; } int main() { op['+']=op['-']=1; op['*']=op['/']=2; while(getline(cin, str), str!="0") { for(string::iterator it=str.end(); it!=str.begin(); it--) { if(*it==' ') str.erase(it); } while(!s.empty()) s.pop(); change(); printf("%.2f\n", Cal()); } return 0; }