1.000 2 4/+ = 1 2 + 5 * 1 + 4/=
First analysis of the next infix change suffix idea:
get an infix expression string and scan from the beginning:
A--the suffix expression is added to the number.
B--Operator:
A. If you are ' (', directly into the stack.
B. If ') ', then add the operator in the stack to the suffix expression and out of the stack, until the top of the stack appears ' (', and delete ' (') from the stack.
C. If it is an operator other than parentheses (that is, +-*/), when its precedence is higher thanexcept ' (' )the top of the stack operator, directly into the stack. Otherwise, start from the top of the stack and popthan the currently processed operatorAn operator with a high priority and equal precedence until a lower priority is reached or an opening parenthesis is encountered.
when the scanned infix expression ends, all the operators in the stack are out of the stack, and the suffix expression is added.
/* code using the array simulation stack, the variable s is equivalent to the stack top pointer */#include <stdio.h> #include <string.h> #define N 1000int Main () {int s,t,t,i,len; Char stack[n+1],str[n+1],ch[n+1];scanf ("%d", &t), while (t--) {scanf ("%s", str);//Read source data Len=strlen (str);// Find the string length s=-1;//"stack top pointer" t=0;/* under the idea to start traversing */for (i=0;i<len-1;i++)//minus one is to not the last = operation {/* in the conversion process first consider the operator, The advantage is that in the presence of multiple digits can be processed directly with the loop, and to meet (adjacent operand operator with a space separated) of the topic requirements to bring convenience */if (str[i]== ' (')//If for ' (' directly into the stack {stack[++s]=str[i];} else if (str[i]== ') ') ', then add the operator in the stack to the suffix expression, until ' (', and delete ' (' {') ' (' {') ' (' {s>=0&&stack[s]!= ') {ch[t++]= stack[s];ch[t++] = "; s--;} Delete occurrences of s--;//stack (}/* the following branches are ingenious */else if (str[i]== '/' | | str[i]== ' * ')//if the top element of the/{/* stack is * or/or the priority is higher or equal than the current operator, because the operator is only +-*/and () */while (stack[s]== '/' | | stack[s]== ' * ') {ch[t++]=stack[s];//stack top element join suffix expression ch[t++] = "; s--;//the stack at the end of the}//loop, indicating that the top element is lower than the current operator Stack[++s]=str[i] ;//Current operator}else if (str[i]== ' + ' | | str[i]== '-')//If the operator in the + or-{while (s>=0&&stack[s]!= ') {//stack is above or equal to the current operator, the operator in the stack joins the suffix expression ch[t++]=stack[s]; ch[t++] = '; s--;} stack[++s]=str[I];} else{/* processing numbers (including floating-point) */while (str[i]<= ' 9 ' &&str[i]>= ' 0 ' | | str[i] = = '. ') {ch[t++] = str[i++];} Ch[t++]= ';//number plus a space i--;}} while (s>=0)//The operators in the stack are added to the suffix expression {ch[t++]=stack[s];ch[t++] = "; s--;} ch[t++]= ' = ';//Join unhandled = ch[t++]= '; ch[t]= ';//end suffix expression printf ("%s\n", ch);} return 0;}