Depressed C Xiaojia (2)
- describe
-
Smart you helped C Xiaojia solve the conversion of infix expressions to postfix expressions (for details, please refer to "Depressed C Xiaojia (1)"), C Xiaojia is very happy. But C Xiaojia is a thinking person, and he wants to calculate the value of an expression in this way. That is, the expression is converted to a postfix expression first, and then evaluated. At this time, it is necessary to consider the case where the operand is a decimal and a multi-digit number.
- enter
-
Input an integer T in the first line, there are T groups of test data (T<10).
Each set of test data has only one line, which is a character string with a length of no more than 1000, indicating this expression, and each expression ends with "=". This expression contains only the symbols +-*/ and parentheses. The parentheses can be nested. The data guarantees that the input operand will not have negative numbers and be less than 1,000,000.
The data guarantees that the divisor will not be 0.
- output
- For each set of test data, the output result includes two lines. The converted suffix expression is output first, and then the calculation result is output. The result is kept to two decimal places. Separate the two sets of test data with a blank line.
- sample input
-
21+2=(19+21)*3-4/5=
- Sample output
-
12+=3.001921+3*45/-=119.20
- source
- Adapted from NYOJ
- Uploaded by
This question is really... Alas, a lot of bitter tears, I don't love it anymore, I used a queue, I used a queue to read other people's code, but there was always an error in the program, and then I ignored the loop of the array and added i directly, But the queue yields the queue
#include<stdio.h> #include<string.h> #include<stack> #include<queue> #include<ctype.h> #include<stdlib.h> using namespace std; char str[1010]; stack<char>s;//Define a stack operator queue<char>q;//Define a queue to store the converted suffix expression int priority(char ch){ switch(ch){ case '+': case '-': return 1; break; case '*': case '/': return 2; break; case '(':return 0; default:return -1; } } int trans(){ while(!s.empty()) s.pop(); while(!q.empty()) q.pop(); s.push('='); int len = strlen (str); for(int i=0;str[i]!='=';i++){ if(str[i]>='0'&&str[i]<='9'||str[i]=='.'){ q.push(str[i]); } else{ q.push('#');//Separate multiple consecutive operands if(str[i]=='(') s.push(str[i]); else if(str[i]==')'){ while(s.top()!='('){ q.push(s.top()); s.pop(); } s.pop();//Finally pop the left parenthesis from the stack (no parentheses are required in the output) } else{ while(priority(str[i])<=priority(s.top())){ q.push(s.top()); s.pop(); } s.push(str[i]);//Wait until the priority of the element in the stack is less than the current element to be pushed, push the element onto the stack } } } while(s.top()!='='){ q.push(s.top()); s.pop(); } q.push('='); return 0;//remember to return here } double Calculate(){//Calculation (Node: Every step here must be dequeued) char zs[1010];//Convert character array, need to store characters with decimal points stack<double>num;//Define a stack to store operands int k=0; int flag=0; while(q.front()!='='){ if(q.front()>='0'&&q.front()<='9'||q.front()=='.'){ zs [k ++] = q.front (); q.pop(); flag=1; } else{ if(flag){ zs [k] = '\ 0'; num.push(atof(zs)); k=0; flag=0; } else if(q.front()=='#'){//This must be judged, and it needs to be out of the queue, otherwise it will be endless q.pop(); continue; } else if(q.front()!='#'){//For addition, subtraction, multiplication and division double num1=num.top(); num.pop(); double num2=num.top(); num.pop(); switch(q.front()){ case '+': num.push(num2+num1); q.pop(); break; case '-': num.push(num2-num1); q.pop(); break; case '*': num.push(num2*num1); q.pop(); break; case '/': num.push(num2/num1); q.pop(); break; } } } } return num.top(); } int main(){ int T; char str2[1010]; scanf("%d",&T); while(T--){ scanf("%s",str); trans(); int t=0; while(!q.empty()){ str2[t++]=q.front(); q.pop(); if(str2[t-1]!='#') printf("%c",str2[t-1]); } printf("\n"); for(int i=0;i<t;i++){ q.push(str2[i]); } printf("%.2f\n",Calculate()); } return 0; }