Solution technique of UVA problem 727 Equqtion

Problem description:

Write a program that changes an infix expression to a postfix expression according to the following specifications.

 Input condition:

  1.  The infix expression to be converted is in the input file in the format of one character per line, with a maximum of 50 lines in the file. For example, (3+2)*5 would be in the form: ( 3 + 2 ) * 5 
  2.  The input starts with an integer on a line by itself indicating the number of test cases. Several infix expressions follows, preceded by a blank line. 
  3.  The program will only be designed to handle the binary operators +, -, *, /. 
  4.  The operands will be one digit numerals. 
  5. The operators * and / have the highest priority. The operators + and - have the lowest priority. Operators at the same precedence level associate from left to right. Parentheses act as grouping symbols that over-ride the operator priorities. 
  6.  Each testcase will be an expression with valid syntax
Expected uotput:

The output file will have each postfix expression all on one line. Print a blank line between different expressions.

Sample Input 
(
 3
 +
 2
 )
 *
 5 

Sample Output 

32+5*

Solution Technique

This is a simple problem. You can solve this problem by using stack. Just follow this stapes.
  1.  Get input for test case.
  2. Get input expression that need to change infix to postfix                                                                                N.B: When you will give input expression follow that one character per line or more. When get empty line then input is complete
  3. Read the expression from left to right or top to bottom
  4. if input character is opening bracket or operator then push into stack.
  5. if input character is digit then print this character.
  6.  if input character is closing bracket then print operator from stack and pop this operator until get opening bracket.
  7.  pop this opening bracket from stack.                                                                                                                N.B: Do not need to insert closing bracket into stack and check next character.
  8. . This process running until all input are checked. 
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience