Write a program write a program called \" infix.cpp \", that uses a stack to con
ID: 3544844 • Letter: W
Question
Write a program write a program called "infix.cpp", that uses a stack to convert a postfix expression to the corresponding fully-parenthesized infix expression. Consider the following examples:
Your program should ask the user for a postfix expression as input, and it should output the corresponding fully-parenthesized infix expression. The program should also ask the user if he/she would like to do another conversion. If so, the user should be able to enter another posfix expression; otherwise the program should terminate. Also, the stack must be implemented using a singly-linked list. Your driver, infix.cpp, should include the definition and declaration files for the class STACK, stack.cpp and stack.h, respectively.
**Using CodePad is helpful to understand your code.
Otherwise just submit your code neatly (understandable)
THANK YOU!
Explanation / Answer
// stack.h#include <string>
using namespace std;
struct node { string data; struct node *next; };
class stack { struct node *top; public: stack() { top = 0; }
void push(string data); string pop(); };
// stack.cpp
#include "stack.h"
void stack::push(string data) { struct node *pn; pn = new node(); pn->data = data; pn->next = (top != 0) ? top : 0; top = pn; }
string stack::pop() { struct node *pn; if (top == 0) return ""; // slight alteration on this line pn = top; string result = pn->data; top = top->next; delete pn; return result; }
// infix.cpp
#include "stack.cpp" #include <iostream> #include <string> #include <vector>
using namespace std;
int main() // a lot of alterations in here { int error; stack s; string postfix, infix, operand1, operand2, expr, yn; do { cout << "Enter postfix expression" << endl; getline(cin, postfix); error = 0; for(int i = 0; i < postfix.length(); i++) { string c = postfix.substr(i, 1); if (c == "+" || c == "-" || c == "*" || c == "/") { operand2 = s.pop(); if (operand2 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } operand1 = s.pop(); if (operand1 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } expr = "(" + operand1 + " " + c + " " + operand2 + ")"; s.push(expr); } else if (c == " ") { continue; } else { s.push(c); } }
if (!error) { infix = s.pop(); if (s.pop() == "") { cout << "The infix expression is" << endl << infix << endl; } else { cout << "Too many operands and not enough operators" << endl; // make sure stack is clear while(s.pop() != ""); } } cout << "Convert another y/n : "; cin >> yn; getchar(); // swallow new line character cout<< endl; } while(yn == "y" || yn == "Y"); return 0; } // stack.cpp
#include "stack.h"
void stack::push(string data) { struct node *pn; pn = new node(); pn->data = data; pn->next = (top != 0) ? top : 0; top = pn; }
string stack::pop() { struct node *pn; if (top == 0) return ""; // slight alteration on this line pn = top; string result = pn->data; top = top->next; delete pn; return result; }
// infix.cpp
#include "stack.cpp" #include <iostream> #include <string> #include <vector>
using namespace std;
int main() // a lot of alterations in here { int error; stack s; string postfix, infix, operand1, operand2, expr, yn; do { cout << "Enter postfix expression" << endl; getline(cin, postfix); error = 0; for(int i = 0; i < postfix.length(); i++) { string c = postfix.substr(i, 1); if (c == "+" || c == "-" || c == "*" || c == "/") { operand2 = s.pop(); if (operand2 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } operand1 = s.pop(); if (operand1 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } expr = "(" + operand1 + " " + c + " " + operand2 + ")"; s.push(expr); } else if (c == " ") { continue; } else { s.push(c); } }
if (!error) { infix = s.pop(); if (s.pop() == "") { cout << "The infix expression is" << endl << infix << endl; } else { cout << "Too many operands and not enough operators" << endl; // make sure stack is clear while(s.pop() != ""); } } cout << "Convert another y/n : "; cin >> yn; getchar(); // swallow new line character cout<< endl; } while(yn == "y" || yn == "Y"); return 0; } // infix.cpp
#include "stack.cpp" #include <iostream> #include <string> #include <vector>
using namespace std;
int main() // a lot of alterations in here { int error; stack s; string postfix, infix, operand1, operand2, expr, yn; do { cout << "Enter postfix expression" << endl; getline(cin, postfix); error = 0; for(int i = 0; i < postfix.length(); i++) { string c = postfix.substr(i, 1); if (c == "+" || c == "-" || c == "*" || c == "/") { operand2 = s.pop(); if (operand2 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } operand1 = s.pop(); if (operand1 == "") { cout << "Too many operators and not enough operands" << endl; error = 1; break; } expr = "(" + operand1 + " " + c + " " + operand2 + ")"; s.push(expr); } else if (c == " ") { continue; } else { s.push(c); } }
if (!error) { infix = s.pop(); if (s.pop() == "") { cout << "The infix expression is" << endl << infix << endl; } else { cout << "Too many operands and not enough operators" << endl; // make sure stack is clear while(s.pop() != ""); } } cout << "Convert another y/n : "; cin >> yn; getchar(); // swallow new line character cout<< endl; } while(yn == "y" || yn == "Y"); return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.