Question

In: Computer Science

C++ •Write a program that evaluates a postfix expression (assume it’s valid) such as 6 2...

C++

•Write a program that evaluates a postfix expression (assume it’s valid) such as

6 2 + 5 * 8 4 / -

•The program should read a postfix expression consisting of digits and operators into a string.

•The algorithm is as follows:

1.While you have not reached the end of the string, read the expression from left to right.

–If the current character is a digit, Push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set).

–Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y.

–Calculate y operator x.

Push the result of the calculation onto the stack.

2.When you reach the end of the string, pop the top value of the stack. This is the result of the postfix expression.

[Example: In Step 2 above, if the operator is '/', the top of the stack is 2 and the next element in the stack is 8, then pop 2 into x, pop 8 into y, evaluate 8 / 2 and push the result, 4, back onto the stack. This note also applies to operator '–'.]

–The arithmetic operations allowed in an expression are

+ addition

– subtraction

* multiplication

/ division

^ exponentiation

% modulus

•[Note: We assume left-to-right associativity for all operators for the purpose of this exercise.] The stack should be maintained with stack nodes that contain an int data member and a pointer to the next stack node. You may want to provide the following functional capabilities:

a.function evaluatePostfixExpression that evaluates the postfix expression

b.function calculate that evaluates the expression (op1 operator op2)

c.function push that pushes a value onto the stack

d.function pop that pops a value off the stack

e.function isEmpty that determines if the stack is empty

f.function printStack that prints the stack

SAMPLE OUTPUT:

INPUT POSTFIX NOTATION: 23+2*

INFIX NOTATION: (2+3)*2

RESULT: 10

Solutions

Expert Solution

#include <iostream>
#include <string.h>

using namespace std;

//declaring Stack
struct Stack
{
   int top;
   unsigned capacity;
   int* array;
};

// Stack Operations
struct Stack* createStack( unsigned capacity )
{
   struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

   if (!stack) return NULL;

   stack->top = -1;
   stack->capacity = capacity;
   stack->array = (int*) malloc(stack->capacity * sizeof(int));

   if (!stack->array) return NULL;

   return stack;
}
// isEmpty function
int isEmpty(struct Stack* stack)
{
   return stack->top == -1 ;
}
// peek function
char peek(struct Stack* stack)
{
   return stack->array[stack->top];
}
// pop function
char pop(struct Stack* stack)
{
   if (!isEmpty(stack))
       return stack->array[stack->top--] ;
   return '$';
}
// push function
void push(struct Stack* stack, char op)
{
   stack->array[++stack->top] = op;
}


// The evaluatePostfix function which returns value of The postfix expression
int evaluatePostfix(char* exp)
{
   // Create a stack of capacity equal to expression size
   struct Stack* stack = createStack(strlen(exp));
   int i;

   // to check either stack is created or not
   if (!stack) return -1;

   // for scanning each character
   for (i = 0; exp[i]; ++i)
   {
       // If the scanned character is a number push it to the stack.
       if (isdigit(exp[i]))
           push(stack, exp[i] - '0');

       // If the scanned character is an operator, pop two
       // elements from stack apply the operator
       else
       {
           int val1 = pop(stack);
           int val2 = pop(stack);
           switch (exp[i])
           {
           case '+': push(stack, val2 + val1); break;
           case '-': push(stack, val2 - val1); break;
           case '*': push(stack, val2 * val1); break;
           case '/': push(stack, val2/val1); break;
           }
       }
   }
   return pop(stack);
}

// Driver program
int main()
{
   char exp[50];
   cin >> exp;
   cout<<"postfix evaluation: "<< evaluatePostfix(exp);
   return 0;
}
// this program is valid only right postfix expressions,in case expression is wrong it will not work correctly


Related Solutions

C++ OOP •Write a program that evaluates a postfix expression (assume it’s valid) such as 6...
C++ OOP •Write a program that evaluates a postfix expression (assume it’s valid) such as 6 2 + 5 * 8 4 / - •The program should read a postfix expression consisting of digits and operators into a string. •The algorithm is as follows: 1.While you have not reached the end of the string, read the expression from left to right. –If the current character is a digit, Push its integer value onto the stack (the integer value of a...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer choices -1 1 -4 4 this is not a postfix expression In an array implementation of the StackInterface where push() and pop() are O(1), the top of the stack is Group of answer choices the first entry in the array the last occupied entry in the array the second entry in the array the entry before the last ocuppied entry in the array the...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
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...
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)
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis...
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix...
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points) (Should remove parentheses) infix.dat 5 * 6 + -10 3 - 2 + 4 7 ( 3 * 4 - (2 + 5)) * 4 / 2 10 + 6 * 11 -(3 * 2 + 14) / 2 2...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will print to
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT