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

Implement a C++ program that converts an infix expression into a postfix express

ID: 663849 • Letter: I

Question

Implement a C++ program that converts an infix expression into a postfix expression and then evaluate the postfix expression.

     Print out the infix expression, the postfix expression and the calculated results of the expression.

     You are to handle the (), + - * / and ^ (exponential/power) operators. The ^ operator precedence is higher than * and / operators.

     To test your program, use the following input expressions. Put the input data into a file to be read in. Test your program with some simple expressions before using the data below and make sure each of your operations work correctly.

Input File.txt:

5 + 7

6 ^ 2 + 1

3 * 8 + 6

5 ^ 4 - 6 ^ 4

5 - 3 * 7 / 4 - 9

8 * ( 9 / 3 - 2 ) / 4 + 5 * 6

5 ^ 3 * 4 + ( 2 + ( 9 * 8 / ( 2 * 6 * ( 8 / 4 ) ) ) ^ 2 * 8 - 5 ) / 5 ^ 2 - 4

5 - 3 * 8 / 2 ^ 3

3 ^ 3 ^ 2 *3

8 * ( 6 / 3 - 2 ) / 4 + 5 * 6 +3

( ( ( ( ( 9 * 5 ) ) ) ) )

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stack>
using namespace std;

// Method to determine if character is one of the four standard operators.
bool isOperator(char chr) {
    if (chr == '+' || chr == '-' || chr == '*' || chr == '/') {
        return true;
    }
    return false;
}


// Method to check character is operand.
bool isOperand(char chr) {
    if (!isOperator(chr) && chr != '(' && chr != ')') {
        return true;
    }
    return false;
}


// Method to compare operator precedence
int compareOperators(char op1, char op2) {
    if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) { return -1; }
    else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/')) { return 1; }
    return 0;
}

// Method to evaluate postfix expression
void calc(string postfixExp)
{
   int i=0;
  
    char token;
    float value, value1, value2;
    stack<float> s;
   //i = 0;
    token = postfixExp[i];
    for(i=0;i <postfixExp.size() && token != '=';i++)
    {
        if(isdigit(token))
        {
            value = token - '0';
            s.push(value);
        }
        else
        {
            value2 = s.top();
            s.pop();
            value1 = s.top();
            s.pop();
            switch(token)
            {
                case '+': value = value1 + value2;
                            break;
                case '-': value = value1 - value2;
                            break;
                case '*': value = value1*value2;
                            break;
                case '/': value = value1/value2;
                            break;
               case '^':
                         {
                           value=1;
                           for(int k = 0; k < value2; k++)
                           value *=value1;
                           break;
                       }
            }
            s.push(value);
        }
       i++;
       token = postfixExp[i];
      
   }
   value = s.top();
   s.pop();
   cout << "Postfix Expression:" <<postfixExp << " Evaluted value :" << value << endl;
}

// Methdo to convert infix into post fix
string express(string str)
{
   stack<char> opStack;
   string postFixString = "";
   const char *cPtr=str.c_str();
   while (*cPtr != '') {
        if (isOperand(*cPtr)) { postFixString += *cPtr; }
        else if (isOperator(*cPtr)) {
            while (!opStack.empty() && opStack.top() != '(' && compareOperators(opStack.top(),*cPtr) <= 0) {
                postFixString += opStack.top();
                opStack.pop();
            }
            opStack.push(*cPtr);
        }
        else if (*cPtr == '(') { opStack.push(*cPtr); }
        else if (*cPtr == ')') {
            while (!opStack.empty()) {
                if (opStack.top() == '(') { opStack.pop(); break; }
                postFixString += opStack.top();
                opStack.pop();
            }
        }
       // Advance our pointer to next character in string.
        cPtr++;
    }
      
    while (!opStack.empty())
   {
        postFixString += opStack.top();
        opStack.pop();      
    }
   return postFixString;  
}
int main()
{
   string str;
   ifstream infile;
  
    string postFix = "";
   char input[100];
   infile.open("InputFile.txt");
   while(infile.eof() == false)
    {
       getline(infile,str);
       postFix=express(str);
       cout<<"Infix expression:"<<str;
       calc(postFix);
   }
   infile.close();
   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote