Question

In: Computer Science

Your assignment for this program is to evaluate a numeric expression in postfix notation using a...

Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout.

Your program will read and evaluate expressions stored in an input file (infile.txt). The process will continue until the end of file is read. After each expression is read and evaluated, the expression and its value should be output to a text file using the following format.

                       

                        EXPRESSION: XXXXXXXXXXXXXXXXXXXXXXXXXXX

                        VALUE:XXXXXX

You may assume all expressions in the file will be in postfix notation. You may also assume that the expressions will only have the following operators: +, -, *, /. The variables found in the expressions will be either A, B, C or D. No other variables will be used. The variables should be assigned the following values in your program:

                                    A = 14.0, B = 16.0, C = 20.0, D = 30.0

Assume that all variables will be uppercase. You may store the expression in a string and extract one character at a time. Please use the data file shown below for the program run.

Output your run to another file.

Please note: Your stack should include a working destructor.

infile.dat

DCA-+

ABC-+

ABCD*+A/-

AB-CD+*

Hint: You can use this code to help you input the string and process each character in the string.

while(!in.eof()){

            in>>s; // s is a string, read the entire string on the line

                // process each character in the string using s.length() and a for loop

            for (int i = 0; i < s.length(); i++)

                    //check to see if the character is an operator (+,-,*,/)

                    // or operand A,B,C,D

                    if(s.substr(i,1)==                  ) // use the handout algorithm for the rest

                       

           

           

           

    }

Given s, declared as a string then s.length() returns the number of characters in the string; s.substr(i,1) returns the character in the ith position of the string. The string library contains the length and substr methods; substr(i,n), another method, will return n characters starting at position i of a string.

Solutions

Expert Solution

// C++ program to evaluate the value of postfix expressions

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

// structure of Node of class Stack

struct Node{

               double data;

               Node *next;

};

class Stack{

               Node *top;

public:

               Stack()

               {

                              top = NULL;

               }

               void push(double data)

               {

                              if(top == NULL)

                              {

                                             top = new Node;

                                             top->data = data;

                                             top->next = NULL;

                              }else

                              {

                                             Node *node = new Node;

                                             node->data = data;

                                             node->next = top;

                                             top = node;

                              }

               }

               double pop()

               {

                              if(!isEmpty())

                              {

                                             double data = top->data;

                                             top = top->next;

                                             return data;

                              }

                              return -1;

               }

               bool isEmpty()

               {

                              return(top == NULL);

               }

};

int main() {

               double A = 14.0, B = 16.0, C = 20.0, D = 30.0;

               string s;

               bool invalid;

               ifstream in("infile.dat"); // provide path to input file

               if(in.is_open())

               {

                              Stack stk; // create an object of class Stack

                              ofstream out("outfile.dat"); // provide the path to output file

                              while(!in.eof()){

                                             invalid = false;

                                             in>>s; // s is a string, read the entire string on the line

           // process each character in the string using s.length() and a for loop

                                             for (int i = 0; i < s.length(); i++)

                                             {

                                                            //check to see if the character is an operator (+,-,*,/)

                                                            // or operand A,B,C,D

// use the handout algorithm for the rest

                                                            if(s.substr(i,1)=="A" || s.substr(i,1) == "B" || s.substr(i,1) == "C" || s.substr(i,1) == "D" ) // if operand, push the corresponding value on the stack

                                                            {

                                                                           if(s.substr(i,1) == "A")

                                                                                          stk.push(A);

                                                                           else if(s.substr(i,1) == "B")

                                                                                          stk.push(B);

                                                                           else if(s.substr(i,1) == "C")

                                                                                          stk.push(C);

                                                                           else

                                                                                          stk.push(D);

                                                            }

                                                            else { // if operator

                                                                           if(stk.isEmpty()) // check if stack is empty

                                                                           {

                                                                                          cout<<" Invalid postfix expression"<<endl;

                                                                                          invalid = true;

                                                                                          break;

                                                                           }

                                                                           // pop the top two operands from stack

                                                                           double op2 = stk.pop();

                                                                           double op1 = stk.pop();

                                                                           // perform the operation and push the value back to stack

                                                                           if(s.substr(i,1) == "+")

                                                                                          stk.push(op1+op2);

                                                                           else if(s.substr(i,1) == "-")

                                                                                          stk.push(op1-op2);

                                                                           else if(s.substr(i,1) == "*")

                                                                                          stk.push(op1*op2);

                                                                           else {

                                                                                          if(op2 != 0)

                                                                                                         stk.push(op1/op2);

                                                                                          else{

                                                                                                         cout<<" Divide by zero error "<<endl;

                                                                                                         invalid = true;

                                                                                                         break;

                                                                                          }

                                                                           }

                                                            }

                                  }

                                             // output to file

                                             if(!stk.isEmpty() && (!invalid))

                                             {

                                                            out<<" EXPRESSION : "<<s<<endl;

                                                            out<<" VALUE : "<<stk.pop()<<endl;

                                             }else{

                                                            out<<" EXPRESSION : "<<s<<endl;

                                                            out<<" Invalid postfix expression"<<endl;

                                             }

                              }

                              //close the files

                              in.close();

                              out.close();

               }else

                              cout<<" Unable to open file : infile.dat"<<endl;

}

//end of program

Output:

Input file:

Output file:


Related Solutions

Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in which operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4 (infix notation). If there are multiple operations, operators are given immediately after their second operands; so, the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in postfix notation: 4 is first...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab 10: 1 5 4 – 3 + * 3.   Computer Science
For this assignment you will write a class that transforms a Postfix expression (interpreted as a...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. We will test this using our own program that instantiates your class and calls the expected methods. Do not use another class besides the tester and the ExpressionTree class. All work must be done in the class ExpressionTree. Your class must be called ExpressionTree and have...
the postfix notation for (7-4)+2 is
the postfix notation for (7-4)+2 is
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example,...
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT