Implement a C++ program that converts infix expressions to postfix expressions. Requirement: - Design a function called
Posted: Tue Jul 12, 2022 8:10 am
Implement a C++ program that converts infix expressions topostfix expressions.
Requirement:
- Design a function called I2PConvertor, which use followingprototype:
std::string I2PConvertor(const std::string& infix_expr);
- The above function takes in a valid infix expression andreturns its corresponding postfix expression.
- Use provided main function to test the program.
Note:
- Infix expressions may have binary operators: +, -, *, /
- Infix expressions may have round parentheses: ()
- Infix expressions uses lower case letters to representoperands, such as a, b, c, …
Please use provided main function to test your program:
int main(){
std::string infix1 {"a*b+c"};
std::string infix2 {"a-b*c"};
std::string infix3 {"(a+b)/c"};
std::string infix4 {"(a+b)*(c-d)"};
// expected: ab*c+
std::cout << I2PConvertor(infix1) << std::endl;
// expected: abc*-
std::cout << I2PConvertor(infix2) << std::endl;
// expected: ab+c/
std::cout << I2PConvertor(infix3) << std::endl;
// expected: ab+cd-*
std::cout << I2PConvertor(infix4) << std::endl;
return 0; }
Requirement:
- Design a function called I2PConvertor, which use followingprototype:
std::string I2PConvertor(const std::string& infix_expr);
- The above function takes in a valid infix expression andreturns its corresponding postfix expression.
- Use provided main function to test the program.
Note:
- Infix expressions may have binary operators: +, -, *, /
- Infix expressions may have round parentheses: ()
- Infix expressions uses lower case letters to representoperands, such as a, b, c, …
Please use provided main function to test your program:
int main(){
std::string infix1 {"a*b+c"};
std::string infix2 {"a-b*c"};
std::string infix3 {"(a+b)/c"};
std::string infix4 {"(a+b)*(c-d)"};
// expected: ab*c+
std::cout << I2PConvertor(infix1) << std::endl;
// expected: abc*-
std::cout << I2PConvertor(infix2) << std::endl;
// expected: ab+c/
std::cout << I2PConvertor(infix3) << std::endl;
// expected: ab+cd-*
std::cout << I2PConvertor(infix4) << std::endl;
return 0; }