Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using this c++ code....create a program that evaluates an arithmetic expression

ID: 3674444 • Letter: U

Question

Using this c++ code....create a program that evaluates an arithmetic expression in postfix notation. Assume the input contains numbers (but no variables) as well as the arithmetic operations +,-,/,* . Your program should allow the user to evaluate additional expressions until the user wants to end the program. It will read from a txt file called HomeworkInput.txt with these in ethe file,

2 3 + 7 * ------->> line 1
2 6 + 9 / ------>> line 2 and etc.....
14 2 + 160 /
99 2 + 50 / 24 -
100 4 / 3 * 20 +
14 3 * 19 + 12 / 3 + 7 *
23 7 + 10 * 14 + 19 - 22 * 16 /

each of those is a line in the txt file meant to be read by the program.

this is a program that evaluates a file of arithmetic expressions in postfix notation.

Please implement this program using the following guidelines:

The input file should be HomeworkSixInput.txt, provided by the assignment. This file has one postfix notation expression per line.

The program should read each line and evaluate the expression.

The program should ignore any extra white space characters.

The output of the program should be the expression and its evaluated value.

The program should use the template class Stack2 as provided by our textbook to implement necessary

stacks

The program can read the file into character arrays or into Strings, your choice

FILE: stack2.template // TEMPLATE CLASS IMPLEMENTED: stack (see stack2.h for documentation) // This file is included in the header file, and not compiled separately. // INVARIANT for the stack class: // 1. The items in the stack are stored in a linked list, with the top of the // stack stored at the head node, down to the bottom of the stack at the // final node. // 2. The member variable top_ptr is the head pointer of the linked list. #include // Provides assert #include "node2.h" // Node template class namespace main_savitch_7B { template stack::stack(const stack& source) // Library facilities used: node2.h { main_savitch_6B::node *tail_ptr; // Needed for argument of list_copy list_copy(source.top_ptr, top_ptr, tail_ptr); } template void stack::push(const Item& entry) // Library facilities used: node2.h { list_head_insert(top_ptr, entry); } template void stack::pop( ) // Library facilities used: cassert, node2.h { assert(!empty( )); list_head_remove(top_ptr); } template void stack::operator =(const stack& source) // Library facilities used: node2.h { main_savitch_6B::node *tail_ptr; // Needed for argument of list_copy if (this == &source) // Handle self-assignment return; list_clear(top_ptr); list_copy(source.top_ptr, top_ptr, tail_ptr); } template Item stack::top( ) const // Library facilities used: cassert { assert(!empty( )); return top_ptr->data( ); } }

Explanation / Answer

Hi, I have done all coding.

Just you cahnge- Instead of reading from console, you can read from file.

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
using namespace std;
const int MAX = 50 ;
class Stack
{
   private :

       int stack[MAX] ;
       int top, nn ;
       char *s ;
   public :
       Stack( ) ;
       void setexpr ( char *str ) ;
       void push ( int item ) ;
       int pop( ) ;
       void calculate( ) ;
       void show( ) ;
} ;
Stack :: Stack( )
{
   top = -1 ;
}
void Stack :: setexpr ( char *str )
{
   s = str ;
}
void Stack :: push ( int item )
{
   if ( top == MAX - 1 )
       cout << endl << "Stack is full" ;
   else
   {
       top++ ;
       stack[top] = item ;
   }
}
int Stack :: pop( )
{
   if ( top == -1 )
   {
       cout << endl << "Stack is empty" ;
       return -999 ;
   }
   int data = stack[top] ;
   top-- ;
   return data ;
}
void Stack :: calculate( )
{
   int n1, n2, n3 ;
   while ( *s )
   {
       if ( *s == ' ' || *s == ' ' )
       {
           s++ ;
           continue ;
       }
       if ( isdigit ( *s ) )
       {
           nn = *s - '0' ;
           push ( nn ) ;
       }
       else
       {
           n1 = pop( ) ;
           n2 = pop( ) ;
           switch ( *s )
           {
               case '+' :
                   n3 = n2 + n1 ;
                   break ;
               case '-' :
                   n3 = n2 - n1 ;
                   break ;
               case '/' :
                   n3 = n2 / n1 ;
                   break ;
               case '*' :
                   n3 = n2 * n1 ;
                   break ;
               case '%' :
                   n3 = n2 % n1 ;
                   break ;
               case '$' :
                   n3 = pow ( n2 , n1 ) ;
                   break ;
               default :
                   cout << "Unknown operator" ;
                   exit ( 1 ) ;
           }

           push ( n3 ) ;
       }
       s++ ;
   }
}
void Stack :: show( )
{
   nn = pop ( ) ;
   cout << "Result is: " << nn ;
}

int main( )
{
   char expr[MAX] ;
   cout << " Enter postfix expression to be evaluated : " ;
   cin.getline ( expr, MAX ) ;
   Stack q ;
   q.setexpr ( expr ) ;
   q.calculate( ) ;
   q.show( ) ;
   return 0;
}

/*

Output:

Enter postfix expression to be evaluated : 14 3 * 19 + 12 / 3 + 7 *
Result is: 21

*/