Infix to Postfix Expression Converter, Study Guides, Projects, Research of Applications of Computer Sciences

This c++ code implements a function to convert an infix mathematical expression to its postfix notation using a stack. The user inputs the infix expression, and the program converts it to postfix and displays both the infix and postfix expressions. This is a useful tool for those studying computer science, mathematics, or engineering, as it helps in understanding the concept of expression conversion and the implementation of such algorithms.

Typology: Study Guides, Projects, Research

2018/2019

Uploaded on 04/07/2019

saud
saud 🇸🇦

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream
#include <string
#include <stack
void GetInfixExpr(std::string& input)
void AppendAndPush(std::string& input#
std::stack<char& aux_stack)
void ConvertToPostFix(std::string const& INPUT#
std::string& output# std::stack<char& aux_stack)
void Display(std::string const& INPUT#
std::string const& OUTPUT)
int main()
-
std::string input
std::string output
std::stack<char aux_stack
GetInfixExpr(input)
AppendAndPush(input# aux_stack)
ConvertToPostFix(input# output# aux_stack)
Display(input# output)
.
void GetInfixExpr(std::string& input)
-
std::cout << "Enter an infix expression: "
std::getline(std::cin# input)
.
void AppendAndPush(std::string& input#
std::stack<char& aux_stack)
-
input.push_back(')')
aux_stack.push('(')
.
void Display(std::string const& INPUT#
std::string const& OUTPUT)
-
std::cout << "input: " << INPUT << std::endl
std::cout << "output: " << OUTPUT << std::endl
.
void ConvertToPostFix(std::string const& INPUT#
std::string& output# std::stack<char& aux_stack)
-
while (!aux_stack.empty())
-
for (auto i = INPUT.begin()
i != INPUT.end() ++i)
-
switch (*i)
-
case ' ':
continue
case '+':
case '-':
while (true)
-
if (aux_stack.top() != '(')
-
output.push_back(aux_stack.top())
aux_stack.pop()
pf2

Partial preview of the text

Download Infix to Postfix Expression Converter and more Study Guides, Projects, Research Applications of Computer Sciences in PDF only on Docsity!

#include #include #include void GetInfixExpr(std::string& input); void AppendAndPush(std::string& input, std::stack & aux_stack); void ConvertToPostFix(std::string const& INPUT, std::string& output, std::stack & aux_stack); void Display(std::string const& INPUT, std::string const& OUTPUT); int main() { std::string input; std::string output; std::stack aux_stack; GetInfixExpr(input); AppendAndPush(input, aux_stack); ConvertToPostFix(input, output, aux_stack); Display(input, output); } void GetInfixExpr(std::string& input) { std::cout << "Enter an infix expression: "; std::getline(std::cin, input); } void AppendAndPush(std::string& input, std::stack & aux_stack) { input.push_back(')'); aux_stack.push('('); } void Display(std::string const& INPUT, std::string const& OUTPUT) { std::cout << "input: " << INPUT << std::endl; std::cout << "output: " << OUTPUT << std::endl; } void ConvertToPostFix(std::string const& INPUT, std::string& output, std::stack & aux_stack) { while (!aux_stack.empty()) { for (auto i = INPUT.begin(); i != INPUT.end(); ++i) { switch (*i) { case ' ': continue; case '+': case '-': while (true) { if (aux_stack.top() != '(') { output.push_back(aux_stack.top()); aux_stack.pop();

else break; } aux_stack.push(i); break; case '': case '/': case '%': while (true) { if (aux_stack.top() == '(' || aux_stack.top() == '+' || aux_stack.top() == '-') break; else { output.push_back(aux_stack.top()); aux_stack.pop(); } } aux_stack.push(i); break; case '^': case '(': aux_stack.push(i); break; case ')': while (true) { if (aux_stack.top() == '(') { aux_stack.pop(); break; } else { output.push_back(aux_stack.top()); aux_stack.pop(); } } break; default: output.push_back(*i); break; } } } }