Question

Please help in C++ each input transaction will comprise an mathematics expression in post-fix layout.  expect that...

Please help in C++

each input transaction will comprise an mathematics expression in post-fix layout.  expect that each operand is a single man or woman in each arithmetic expression.  you could expect that each arithmetic expression is correctly formatted.  but, your program ought to not permit an try to divide by zero.  rather than dividing via 0, your application need to show an errors message and then start comparing a new line of code

An enter transaction containing “end-of-file” shows there are not any extra transactions to be processed.  implement a stack to assess every expression and show the result.  Use the C++ built-in elegance to put in force stack functions.

sample

  1. 23+ (result = 5)
  2. 34*2/ (result = 6)
  3. 832*6-/ (result= “error: division by zero”)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

To solve a postfix expression

1)make a stack

2)start traversing the expression if the character is an operand pop two elements from stack and push their result in stack

3)else push the number in stack

4)at last pop the element from stack this is the result

#include <bits/stdc++.h>
using namespace std;

int main() {
   cout<<"Enter the file name"<<endl;
   string fileName;
   cin>>fileName;
   ifstream file(fileName);
   while(!file.eof())
   {
       string expression;
       getline(file,expression);
       bool result=false;
       stack<int> s;
       for(int i=0;i<expression.length();i++)
       {
           if(expression[i]=='+')
           {
               int a=s.top();
               s.pop();
               int b=s.top();
               s.pop();
               s.push(a+b);
           }
           else if(expression[i]=='-')
           {
               int a=s.top();
               s.pop();
               int b=s.top();
               s.pop();
               s.push(b-a);
           }
           else if(expression[i]=='*')
           {
               int a=s.top();
               s.pop();
               int b=s.top();
               s.pop();
               s.push(b*a);
           }
           else if(expression[i]=='/')
           {
               int a=s.top();
               s.pop();
               int b=s.top();
               s.pop();
               if(a==0)
               {
                   result=true;
                   cout<<"result=error: division by zero"<<endl;
                   break;
               }
               else
               s.push(b/a);
           }
           else
           s.push(expression[i]-'0');
       }
       if(result==false)
       {
          
           int x;
           if(s.empty()==false)
           {
               x=s.top();
               s.pop();
               cout<<"result="<<x<<endl;
           }
       }
   }
   file.close();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please help in C++ each input transaction will comprise an mathematics expression in post-fix layout.  expect that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT