Infix expression: the operator is placed in the middle of two calculation objects, for example: (2 + 1) * 3;
Suffix expression: contains no parentheses. Operators are placed behind two calculation objects. All calculation operations are performed strictly from left to right according to the order in which operators appear (the priority rules of operators are not considered, for example: 2 1 + 3 *;
Prefix expression: Like a suffix expression, it does not contain parentheses. The operator is placed before two calculation objects, for example, * + 2 1 3.
Lab Purpose
1. Familiar with Stack applications
2. Familiar with the transformation from an infix expression to a prefix
Lab content
1. Write a program to convert an infix expression to a prefix.
For example, 2*3/(2-1) + 5 * (4-1) is converted to +/* 23-21*5-41
// Replace /*
Reference Algorithm
1) Calculate the reverse order of the input string.
2) Check the next element of the input.
3) Add the operand to the output string.
4) if the brackets are closed, press them on the stack.
5) if it is an operator, then
I) If the stack is empty, this operator is used to import data to the stack.
Ii) if the top of the stack is closed brackets, this operator is used to import the stack.
Iii) if its priority is higher than or equal to the stack top operator, this operator is added to the stack.
Iv) otherwise, the stack top operator goes out of the stack and is added to the output string. Repeat Step 5.
6) For example, the operators in the stack exit the stack one by one and output them until brackets are closed. Close the brackets out of the stack and discard them.
7) if the input is not complete, jump to step 2.
8) If the input is complete, all operators in the stack are output from the stack and added to the output string.
9) returns the reverse order of the output string. */
Code:
- # Include <iostream>
- # Include <string>
- Using namespace STD;
- Char stack [50]; // defines the ordered stack. The first element indicates that the stack is empty;
- Int Top = 0; // stack top pointer. If it is 0, the stack is empty;
- Int level (char OP) // determines the operator level function. The */level is 2, and the +-level is 1;
- {
- Int level;
- Switch (OP)
- {
- Case '+ ':
- Case '-': Level = 1; break;
- Case '*':
- Case '/': Level = 2; break;
- Default: Level = 0; break;
- }
- Return level;
- }
- Bool isoperator (char OP) // determines whether the character in the input string is an operator. If yes, true is returned.
- {
- If (OP = '+' | op = '-' | op = '*' | op = '/')
- Return true;
- Else
- Return false;
- }
- String convert (string s) // converts an infix string to a suffix string,
- {
- String output = ""; // output string
- For (INT I = S. Length ()-1; I> = 0;) // 1) returns the reverse order of the input string.
- {
- If (s [I]> = 48 & S [I] <= 57)
- Output = output + s [I]; // 3) Add the operand to the output string.
- If (s [I] = ') // 4) if it is a closed bracket, press it on the stack.
- {
- Top ++;
- Stack [Top] = s [I];
- }
- While (isoperator (s [I]) // if it is an operator, execute the corresponding operation of the algorithm (5;
- {
- If (Top = 0 | stack [Top] = ')' | level (s [I])> = level (stack [Top])
- {
- Top ++;
- Stack [Top] = s [I];
- Break;
- }
- Else
- {
- Output = output + stack [Top];
- Top --;
- }
- }
- If (s [I] = '(') // 6) for example, if it is an open bracket, the operators in the stack are output one by one and output until parentheses are closed. Close the brackets out of the stack and discard them.
- {
- While (stack [Top]! = ')')
- {
- Output = output + stack [Top];
- Top --;
- }
- Top --;
- }
- I --; // 7) if the input is not complete, jump to step 2.
- }
- While (top! = 0) // 8) If the input is complete, all the operators remaining in the stack go out of the stack and add them to the output string.
- {
- Output = output + stack [Top];
- Top --;
- }
- Return output;
- }
- Int main ()
- {
- String input, output;
- Cout <"Enter the string :";
- Cin> input;
- Output = convert (input );
- Cout <"Suffix :";
- For (INT I = output. Length ()-1; I> = 0; I --) // 9) returns the reverse order of the output string.
- Cout <output [I];
- Cout <Endl;
- Return 0;
- }