Code #include #include #include #include #include #include using namespace std; bool isNumber(string line); void evolutePostfixExpr(string,ostream&); int operation(int , int , string); int main() {    ifstream inFile;    ofstream outFile;    string filename;    string expression;    int choice;    cout << "Enter the file name: ";    cin >> filename;    inFile.open(filename);    cout << "\n1. Ouput on console." << endl;    cout << "2. Ouput on file." << endl<<"Your choice: ";    cin >> choice;    if (choice == 2)    {        cout << "\nEnter the output file name: ";        cin >> filename;        outFile.open(filename);        cout << "\nWrite to " << filename << endl;    }    while (!inFile.eof())    {               getline(inFile, expression);        if (choice == 2)            evolutePostfixExpr(expression,outFile);        else            evolutePostfixExpr(expression, cout);           }    return 1; } void evolutePostfixExpr(string expr,ostream &out) {    stack stk;    string value,errorMsg;    bool invalid = false;    stringstream ss(expr);    int ans = 0;    int a, b;    while (ss >> value)    {        if (isNumber(value))            stk.push(stoi(value));        else if (value == "+" || value == "-" || value == "*" || value == "/")        {            if (stk.empty() || stk.size() == 1)            {                invalid = true;                errorMsg = "To many operators";                break;            }                       a = stk.top();            stk.pop();            b = stk.top();            stk.pop();            ans = operation(a, b, value);            stk.push(ans);        }        else        {            invalid = true;            errorMsg = "Illegal Operation";            break;        }    }    if (invalid)    {        out.width(30); out << left << expr;        out << errorMsg << endl;        return;    }    if (stk.size() > 1)    {        invalid = true;        errorMsg = "To few operators.";    }    if (invalid)    {        out.width(30); out << left << expr;        out << errorMsg << endl;        return;    }    else    {        out.width(30); out << left << expr;        out << errorMsg << stk.top()<= '0' && s[i] <= '9') == false) {            return false;        }    }    return true; } int operation(int a, int b, string op) {    //Perform operation    if (op == "+")        return b + a;    else if (op == "-")        return b - a;    else if (op == "*")        return b * a;    else if (op == "/")        return b / a;    return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

please is there another to answer this 

Code

#include<iostream>
#include<cmath>
#include<stack>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
bool isNumber(string line);
void evolutePostfixExpr(string,ostream&);
int operation(int , int , string);
int main()
{
   ifstream inFile;
   ofstream outFile;
   string filename;
   string expression;
   int choice;
   cout << "Enter the file name: ";
   cin >> filename;
   inFile.open(filename);

   cout << "\n1. Ouput on console." << endl;
   cout << "2. Ouput on file." << endl<<"Your choice: ";
   cin >> choice;

   if (choice == 2)
   {
       cout << "\nEnter the output file name: ";
       cin >> filename;
       outFile.open(filename);
       cout << "\nWrite to " << filename << endl;
   }

   while (!inFile.eof())
   {
      
       getline(inFile, expression);
       if (choice == 2)
           evolutePostfixExpr(expression,outFile);
       else
           evolutePostfixExpr(expression, cout);
      
   }
   return 1;
}

void evolutePostfixExpr(string expr,ostream &out)
{
   stack<int> stk;
   string value,errorMsg;
   bool invalid = false;
   stringstream ss(expr);
   int ans = 0;
   int a, b;
   while (ss >> value)
   {
       if (isNumber(value))
           stk.push(stoi(value));
       else if (value == "+" || value == "-" || value == "*" || value == "/")
       {
           if (stk.empty() || stk.size() == 1)
           {
               invalid = true;
               errorMsg = "To many operators";
               break;
           }
          
           a = stk.top();
           stk.pop();
           b = stk.top();
           stk.pop();
           ans = operation(a, b, value);
           stk.push(ans);
       }
       else
       {
           invalid = true;
           errorMsg = "Illegal Operation";
           break;
       }
   }
   if (invalid)
   {
       out.width(30); out << left << expr;
       out << errorMsg << endl;
       return;
   }
   if (stk.size() > 1)
   {
       invalid = true;
       errorMsg = "To few operators.";
   }
   if (invalid)
   {
       out.width(30); out << left << expr;
       out << errorMsg << endl;
       return;
   }
   else
   {
       out.width(30); out << left << expr;
       out << errorMsg << stk.top()<<endl;
   }
}

bool isNumber(string s) {
   if (s.size() == 0) return false;
   for (int i = 0; i<s.size(); i++) {
       if ((s[i] >= '0' && s[i] <= '9') == false) {
           return false;
       }
   }
   return true;
}
int operation(int a, int b, string op) {
   //Perform operation
   if (op == "+")
       return b + a;
   else if (op == "-")
       return b - a;
   else if (op == "*")
       return b * a;
   else if (op == "/")
       return b / a;
   return 0;
}

Use the C++ Standard Template Library's stack class to write a program for processing a file of postfix
expressions.
Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix
expression (w +x) * (v-2) becomes wx+ys-* in postfix.
A postfix expression can be efficiently evaluated using a stack as follows.
1. Read the expression from the left to the right.
2. When an operand is encountered, push it on the stack.
3. When an operator is encountered, pop the top two elements from the stack, perform the
operation, and then push the result.
4. There should be exactly one element on the stack at the end and this element is the value of the
еxpression.
For example, if the expression 20 5 - 3 * is entered, the evaluation should proceed as follows.
Symbol read
Actions taken
20
Push 20
5
Push 5
Pop 5, Pop 20, Push 20 - 5
Push 3
3
Pop 3, Pop 15, Push 15 * 3
The result of 45 should then be output.
Your program should ask the user for the name of an input file. The input file will contain postfix
expressions one per line. The program should open the file and evaluate the postfix expressions in the
file. Your program should give the user the option of writing the results to an output file or to the screen.
Your program should also check the entered postfix expression for validity. A postfix expression is
invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not|
enough operands on the stack when an operation is performed.
E.g. If the input file contained:
The output would be:
20 5 - 3 *
45
100 50 60 + +
210
3 45 +
3 4 + +
3 4 ?
Too few operators.
Too many operators.
Illegal operation
Limit your postfix expression to the arithmetic operations of +, -, *, and / on integer values.
Transcribed Image Text:Use the C++ Standard Template Library's stack class to write a program for processing a file of postfix expressions. Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix expression (w +x) * (v-2) becomes wx+ys-* in postfix. A postfix expression can be efficiently evaluated using a stack as follows. 1. Read the expression from the left to the right. 2. When an operand is encountered, push it on the stack. 3. When an operator is encountered, pop the top two elements from the stack, perform the operation, and then push the result. 4. There should be exactly one element on the stack at the end and this element is the value of the еxpression. For example, if the expression 20 5 - 3 * is entered, the evaluation should proceed as follows. Symbol read Actions taken 20 Push 20 5 Push 5 Pop 5, Pop 20, Push 20 - 5 Push 3 3 Pop 3, Pop 15, Push 15 * 3 The result of 45 should then be output. Your program should ask the user for the name of an input file. The input file will contain postfix expressions one per line. The program should open the file and evaluate the postfix expressions in the file. Your program should give the user the option of writing the results to an output file or to the screen. Your program should also check the entered postfix expression for validity. A postfix expression is invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not| enough operands on the stack when an operation is performed. E.g. If the input file contained: The output would be: 20 5 - 3 * 45 100 50 60 + + 210 3 45 + 3 4 + + 3 4 ? Too few operators. Too many operators. Illegal operation Limit your postfix expression to the arithmetic operations of +, -, *, and / on integer values.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education