Question

In: Computer Science

We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two...

We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4 5 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses.

Write a function named postfixEval that uses a stack (from the Standard Template Library) to evaluate postfix expressions. It should take a C-style string parameter that represents a postfix expression. The only symbols in the string will be +, -, *, /, digits and spaces. '+' and '-' will only appear in the expression string as binary operators - not as unary operators that indicate the sign of a number. The return type should be double. You may find the isdigit() function useful in parsing the expression. You may also use strtok() and atof().

Hint: Read a postfix expression from left to right. When you read a number, push it on the stack. When you read an operand, pop the top two numbers off the stack, apply the operator to them, and push the result on top of the stack. At the end, the result of the expression should be the only number on the stack.

**File must be called: postfixEval.cpp**

Solutions

Expert Solution

This code implements postfix evaluation. It takes an input and provides the calculated result in double. I hope this surely helps you. Do rate me with a thumbs up.

//CODE in C++:-

#include<iostream>
#include<stack>
#include<string>

using namespace std;

// This function evaluates Postfix expression and returns output
double postfixEval(string expression);

// This function performs an operation on two operands and returns output.
double PerformOperation(char operation, double operand1, double operand2);

// This function verifies whether a character is an operator symbol or not.
bool IsOperator(char C);

// This function verifies whether a character is numeric digit.
bool IsNumericDigit(char C);

int main()
{
   string expression;
   cout<<"Enter Postfix Expression \n";
   getline(cin,expression);
   double result = postfixEval(expression);
   cout<<"Output = "<<result<<"\n";
}

// this function evaluates the given Postfix expression and returns output
double postfixEval(string expression)
{
   // Declaring a Stack from Standard template library in C++.
   stack<double> S;

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

       // Scanning each character from left.
       // If character is a delimitter, simply move on.
       if(expression[i] == ' ' || expression[i] == ',') continue;

       // If character is operator, pop two elements from stack, perform operation and push the result back.
       else if(IsOperator(expression[i])) {
           // Pop two operands.
           double operand2 = S.top(); S.pop();
           double operand1 = S.top(); S.pop();
           // Perform operation
           double result = PerformOperation(expression[i], operand1, operand2);
           //Push back result of operation on stack.
           S.push(result);
       }
       else if(IsNumericDigit(expression[i])){
           // Extract the numeric operand from the string
           // Keep incrementing i as long as you are getting a numeric digit.
           double operand = 0;
           while(i<expression.length() && IsNumericDigit(expression[i])) {
               // For a number with more than one digits, as we are scanning from left to right.
               // Everytime , we get a digit towards right, we can multiply current total in operand by 10
               // and add the new digit.
               operand = (operand*10) + (expression[i] - '0');
               i++;
           }
           // Finally, you will come out of while loop with i set to a non-numeric character or end of string
           // decrement i because it will be incremented in increment section of loop once again.
           // We do not want to skip the non-numeric character by incrementing i twice.
           i--;

           // Push operand on stack.
           S.push(operand);
       }
   }
   // If expression is in correct format, Stack will finally have one element. This will be the output.
   return S.top();
}

// Function to verify whether a character is numeric digit.
bool IsNumericDigit(char C)
{
   if(C >= '0' && C <= '9') return true;
   return false;
}

// Function to verify whether a character is operator symbol or not.
bool IsOperator(char C)
{
   if(C == '+' || C == '-' || C == '*' || C == '/')
       return true;

   return false;
}

// Function to perform an operation and return output.
double PerformOperation(char operation, double operand1, double operand2)
{
   if(operation == '+') return operand1 +operand2;
   else if(operation == '-') return operand1 - operand2;
   else if(operation == '*') return operand1 * operand2;
   else if(operation == '/') return operand1 / operand2;

   else cout<<"Unexpected Error \n";
   return -1;
}

// output:-

Enter Postfix Expression
25 12 7 - 2 * /
Output = 2.5

Related Solutions

C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
Reverse Polish notation is a notation where every operator follows all of its operands. For example,...
Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated using...
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...
(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...
Write a function to concatenate two strings using pointer notation
Write a function to concatenate two strings using pointer notation
We will use shorthand notation and probability notation for random variables when working with normally distributed...
We will use shorthand notation and probability notation for random variables when working with normally distributed random variables. Suppose the vitamin C content of a particular variety of orange is distributed normally with mean 720 IU and standard deviation 46 IU. If we designate X = the vitamin C content of a randomly selected orange, then our shorthand notation is X~N(720 IU, 46 IU). Use this distribution of vitamin C content to answer the following questions: 1) What is the...
What is the advantage of using scientific notation over decimal notation? What is the difference between...
What is the advantage of using scientific notation over decimal notation? What is the difference between weight and mass?
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 an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT