C question Please use C style string Char to code Please giv

C++ question. Please use C- style string (Char) to code. Please give me a complete code.

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<double> (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.

Solution

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

using namespace std;

// evaluate Postfix i.e input expression and return output
double EvaluatePostfix(string expression);

//Performing an operation and return output.
double PerformOperation(char operation, double operand1, double operand2);

// verifying whether character is operator symbol or not?.
bool IsOperator(char C);

//verify whether a character is number.
bool IsNumericDigit(char C);

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

double EvaluatePostfix(string expression)
{
   // Declaring a Stack from Standard template library in C++.
   stack<double> S;

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

       //you can use strtok -- applied simple logic
       if(expression[i] == \' \' || expression[i] == \',\') continue;

       //algorithm starts operator then pop first 2 symbols
       else if(IsOperator(expression[i])) {
           //first store top elements into operand then delete or pop
           double operand2 = S.top(); S.pop();
           double operand1 = S.top(); S.pop();
           // calculation of expression
           double result = PerformOperation(expression[i], operand1, operand2);
           //if attained intermediate result push back in stack for continuation of expression evaluation
           S.push(result);
       }
       else if(IsNumericDigit(expression[i])){
          
           //Extract number till digit in string expression
           double operand = 0;
           while(i<expression.length() && IsNumericDigit(expression[i])) {
              
               // for number more than unit digit
               operand = (operand*10) + (expression[i] - \'0\');
               i++;
           }
          
           // out of loop as we not want to skip the non-numeric character by incrementing i twice as in loop extra increment before exiting.
           i--;

           // pushing operand in stack.
           S.push(operand);
       }
   }
   // if expression is in postfix format, Stack must have one element after all calculation which will be the output
   return S.top();
}

bool IsNumericDigit(char C)
{
   if(C >= \'0\' && C <= \'9\') return true;
   return false;
}

bool IsOperator(char C)
{
   if(C == \'+\' || C == \'-\' || C == \'*\' || C == \'/\')
       return true;

   return false;
}

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 \ \";
   return -1;
}

C++ question. Please use C- style string (Char) to code. Please give me a complete code. We normally write arithmetical expressions using infix notation, meanin
C++ question. Please use C- style string (Char) to code. Please give me a complete code. We normally write arithmetical expressions using infix notation, meanin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site