infix expression converted to suffix expression swust-oj

Source: Internet
Author: User
Tags arithmetic
Infix expression to postfix expression (MS) 10000 (KB) 1754/4417 infix expression is a general arithmetic or logical formula representation method, operator is in infix form in the middle of the operand (example: 3 + 4), infix expression is commonly used in arithmetic representation. The suffix expression does not contain parentheses, the operator is placed after two operands, and all calculations are made strictly from left to right in the order in which the operators appear (no longer consider precedence rules for operators, such as: (2 + 1) * 3, which is 2 1 + 3 *. Using the stack structure, the infix expression is converted to a suffix expression. (Test data element is a single character) input
Infix expression
Output
Suffix expression
Sample Input
A + (B-C/D) *e
Sample Output
abcd/-e*+

#include <iostream> #include <stdio.h> using namespace std;
	int main () {int top=-1;
	Char a[50];//array simulation stack char ch;
		while ((Ch=getchar ()) = ' \ n ')//one character reads {if (' a ' <=ch&&ch<= ' z ') cout<<ch;//if the character is directly output.
		else if (top==-1) a[++top]=ch;//if the stack is empty, enter the operator directly into the else if (ch== ' (') a[++top]=ch;
			else  if (ch== ') ')      //Consider parentheses, the parentheses change the precedence of the operator {while (a[top]!= ' (') {cout<<a[top--];
		} top--; } else if (ch== ' * ' | | ch== '/')//multiplication The priority of the method is greater than the addition and subtraction {if (a[top]== ' + ' | | a[top]== '-' | |
			a[top]== ' (')//if the stack top is added or subtracted or parentheses into the stack.
			A[++top]=ch; else if (a[top]== ' * ' | |
			a[top]== '/')//if the stack top is the multiplication method, the output stack top, and then into the stack.
		{cout<<a[top--];a[++top]=ch; }} else if (ch== ' + ' | |
			ch== '-')//plus subtraction priority is small {if (a[top]== ')//Consider the stack top is not the parentheses a[++top]=ch; else if (a[top]== ' + ' | |
				a[top]== '-')//stack top is add and subtract, on the output, and then into the stack {cout<<a[top--];
			A[++top]=ch; } else if (a[top]== ' * ' | | a[top]== '/')//stack top is multiplication, consider whether the previous bit of the top of the stack is empty {cout<<a[top--];//first output stack top if (top!=0) cout<<a[top--];//continue output stack top a[++top]=ch;//stack}}} while (top>=0) if it is not empty cout<<a[top--];
return 0; }
 stack: same idea 
#include <iostream> #include <cstdio> #include <stdlib.h> using namespace std;
	typedef struct {char data[105];
int top;
}sqlist; void push (char e,sqlist *&l) {l->data[++l->top]=e;} void Pop (SqList *&l) {if (l->data[l->top]== ' ('
	) l->top--;
else cout<<l->data[l->top--];
	} int main () {char ch;
	SqList *l;
	l->top=-1;
		while ((Ch=getchar ()) = ' \ n ') {if (' A ' <=ch&&ch<= ' z ') cout<<ch;
		else if (l->top==-1) push (ch,l);
		else if (ch== ' (') push (ch,l);
			else if (ch== ') ') {while (l->data[l->top]!= ' (') {pop (L);
		} pop (L); } else if (ch== ' * ' | | ch== '/') {if (l->data[l->top]== ' + ' | | l->data[l->top]== '-' | |
			l->data[l->top]== ' (') push (ch,l); else if (l->data[l->top]== ' * ' | |
				l->data[l->top]== '/') {pop (L);
			Push (CH,L); }} else if (ch== ' + ' | | |
			ch== '-') {if (l->data[l->top]== ' (') push (ch,l);
				else {pop (L);
			Push (CH,L);
	}	}} while (L->top!=-1) pop (l);
return 0; }  


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.