infix-type variable suffix

Source: Internet
Author: User

Topic source infix-type variable suffix time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe
People's Daily habits are written in the arithmetic expression infix, but for the machine is more "accustomed to" suffix, about the arithmetic expression of the infix and suffix of the general data structure of the book has relevant content to see, here no longer repeat, now your task is to change infix type suffix.
Input
The first line enters an integer n, and there are n sets of test data (N<10).
Each set of test data has only one row, is a string of not more than 1000, indicating the infix of the expression, each expression is "=" end. This expression contains only +-*/and parentheses. Where parentheses can be used in a nested set. The data guarantees that no negative numbers appear in the operands entered.
Data guarantee Divisor is not 0
Output
each group outputs the corresponding suffix of the group infix, requiring the adjacent operand operators to be separated by a space.
Sample input
21.000+2/4= ((1+2) *5+1)/4=
Sample output
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;}


infix-type variable suffix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.