0% found this document useful (0 votes)
50 views

Source Code: Abdullah Arshad FA19-BSE-104

The document describes a C++ program that evaluates mathematical expressions. It defines an ExpEvaluator class with methods to check if an expression is valid, convert it to postfix notation, and evaluate the postfix expression. The main function gets an input expression, checks its validity, converts it to postfix if valid, and outputs the evaluated value. Key methods include isExpressionValid() to check brackets and precedence, postfixConversion() to convert to postfix, and postfixEvaluation() to evaluate the postfix expression.

Uploaded by

Hamza Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Source Code: Abdullah Arshad FA19-BSE-104

The document describes a C++ program that evaluates mathematical expressions. It defines an ExpEvaluator class with methods to check if an expression is valid, convert it to postfix notation, and evaluate the postfix expression. The main function gets an input expression, checks its validity, converts it to postfix if valid, and outputs the evaluated value. Key methods include isExpressionValid() to check brackets and precedence, postfixConversion() to convert to postfix, and postfixEvaluation() to evaluate the postfix expression.

Uploaded by

Hamza Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ABDULLAH ARSHAD

FA19-BSE-104

ASSIGNMENT EXPRESSION EVALUATOR

SUBMITTED TO : DR. FARUKH ZEESHAN

SOURCE CODE
#include <iostream>

using namespace std;

const int ArySize = 100;

class ExpEvaluator

private:

int top, pfEcounter;

char postFixExpression[ArySize];

public:

ExpEvaluator()

top = 0;

}
bool isExpressionValid(char Expression[])

char Stack[ArySize] = {NULL};

for(int i = 0; i < top; i++)

if(Expression[i] == '(' || Expression[i] == '{' || Expression[i] == '[')

Stack[top++] = Expression[i];

continue;

if(Expression[i] == ')' && Stack[top--] != '(')

return false;

if(Expression[i] == '}' && Stack[top--] != '{' )

return false;

if(Expression[i] == '[' && Stack[top--] != ']')

return false;

}
}

return true;

bool postfixConversion(char Expression[])

char Stack[ArySize] = {NULL};

int j;

for(int i = 0; i < top; i++)

if(Expression[i] == '(' || Expression[i] == '{' || Expression[i] == '[')

Stack[top++] = Expression[i];

continue;

if(

char Stack[ArySize] = {NULL};

for(int i = 0; i < top; i++)

if(Expression[i] >= '0' && Expression[i] <= '9')

{
postFixExpression[j++] = Expression[i];

continue;

top = top-1;

if(Expression[i] == ')' || Expression[i] == '}' || Expression[i] == ']')

while(Stack[top] != '(' && Stack[top] != '{' && Stack[top] != ']')

postFixExpression[j++] = Stack[top--];

continue;

if(Expression[i] == '+' || Expression[i] == '-' || Expression[i] == '*' ||


Expression[i] == '/')

while(precedence(Stack[top]) >= precedence(Expression[i]))

postFixExpression[j++] = Stack[top--];

top++;

Stack[top++] = Expression[i];
}

pfEcounter = j;

return true;

int precedence(char ch)

if(ch == '-')

return 1;

if(ch == '+')

return 2;

if(ch == '*')

return 3;

if(ch == '/')

return 4;

int postfixEvaluation()

int numStack[ArySize], numStktop = 0, i = 0, num1, num2;

for(i = 0; i < pfEcounter; i++)


{

if(postFixExpression[i] >= '0' && postFixExpression[i] <= '9')

numStack[numStktop++] = charToNum( postFixExpression[i]);

else

num1 = numStack[--numStktop];

num2 = numStack[--numStktop];

if( postFixExpression[i] == '-')

numStack[numStktop++] = num1 - num2;

if(postFixExpression[i] == '+')

numStack[numStktop++] = num1 + num2;

if(postFixExpression[i] == '*')

numStack[numStktop++] = num1 * num2;

if(postFixExpression[i] == '/')

numStack[numStktop++] = num1 / num2;

return(numStack[--numStktop]);

}
int charToNum(char ch)

if(ch == '0') return 0;

if(ch == '1') return 1;

if(ch == '2') return 2;

if(ch == '3') return 3;

if(ch == '4') return 4;

if(ch == '5') return 5;

if(ch == '6') return 6;

if(ch == '7') return 7;

if(ch == '8') return 8;

if(ch == '9') return 9;

};

int main()

ExpEvaluator NE;

char ch, Expression[];

cout<< "Enter an Expression to check and evaluate";


cin>> Expression[];

if(NE.isExpressionValid( Expression[]))

cout<<"Expression is valid";

if(NE.postfixConversion(Expression[]))

cout<<"Expression converted to post fix";

cout<<"Evaluated value is"<<NE.postfixEvaluation();

else

cout<< "not post fixed";

else

cout<<"Expression is valid";

}
}

You might also like