Infix to Post fix Expression and solving it



Design an arithmetic expression evaluator. Your program should have following features.
  • Upon execution your program will prompt for an arithmetic equation in infix form. Once user enters the input, your program should check the balancing of brackets using the algorithm you’ve studied in Lecture 6 (using stacks). If the brackets in equation are balanced, your program should proceed to step 2; otherwise it will ask user to reenter the expression.
  • Once the equation has been checked for brackets, your program should convert the equation to postfix notation using the algorithm you’ve studied in Lecture 6 (using stacks).
  • Step 2 should be proceeded by evaluation of the expression. At the time of evaluation, your program should prompt the user for the values of each variable in equation. For evaluation, use algorithm you’ve studied in Lecture 6 (using stacks).

Header file:
InfixtoPostfix.h

#include<iostream>
#include<stack>
class InfixtoPostfix
{
bool isOperator(char a);
bool isOperand(char a);
bool isBracket(char a);
bool isOBracket(char a);
bool isSNumber(char a);
float arithOperation(float a, float b, char f);
public:
bool bracketCheck(std::string a);
std::string toPostfix(std::string a);
float evaluateExp(std::string a);
};


InfixtoPostfix.cpp

#include"InfixtoPostfix.h"
#include<sstream>
#include<string>
bool InfixtoPostfix::isOperator(char a)
{
if (a == '+' || a == '-' || a == '*' || a == '/' || a == '^')
return true;
return false;
}
bool InfixtoPostfix::isOperand(char a)
{
int b = a;
if ((b <= 90 && b >= 65) || (b <= 122 && b >= 97))
return true;
return false;
}
bool InfixtoPostfix::isBracket(char a)
{
if (a == '(' || a == '{' || a == '[' || a == ')' || a == '}' || a == ']')
return true;
return false;
}
bool InfixtoPostfix::isOBracket(char a)
{
if (a == '(' || a == '{' || a == '[')
return true;
return false;
}
bool InfixtoPostfix::isSNumber(char a)
{
int x = a;
if (x > 48 && x < 57)
return true;
return false;
}
float InfixtoPostfix::arithOperation(float a, float b, char f)
{
float r;
switch (f)
{
case '+':
r = a + b;
break;
case '-':
r = a - b;
break;
case '*':
r = a * b;
break;
case '/':
r = a / b;
break;
case '^':
r = pow(a, b);
break;
}
return r;
}
bool InfixtoPostfix::bracketCheck(std::string a)
{
std::stack<char> s;
char temp;
for (int i = 0; i < a.length(); i++)
{
if (a[i] == '(' || a[i] == '{' || a[i] == '[')
s.push(a[i]);
else if (a[i] == ')' || a[i] == '}' || a[i] == ']')
{
if (s.empty())
return false;
else
temp = s.top();
if ((temp == '(' && a[i] == ')') || (temp == '{' && a[i] == '}') || (temp == '[' && a[i] == ']'))
{
s.pop();
}
else
{
return false;
}
}
}
if (s.empty())
return true;
else
return false;
}
std::string InfixtoPostfix::toPostfix(std::string a)
{
std::stack<char> s;
std::string output;
for (int i = 0; i < a.length(); i++)
{
if (isOperand(a[i]))
{
output = output + a[i];
}
else if (isBracket(a[i]))
{
if (a[i] == '(' || a[i] == '{' || a[i] == '[')
{
s.push(a[i]);
}
else
{
while (!s.empty() && !isOBracket(s.top()))
{
output = output + s.top();
s.pop();
}
s.pop();
}
}
else
{
if (a[i] == '^')
{
while (!s.empty() && !isOBracket(s.top()))
{
output = output + s.top();
s.pop();
}
s.push(a[i]);
}
else if (a[i] == '+' || a[i] == '-')
{
while (!s.empty() && (s.top() != '^' && !isOBracket(s.top())))
{
output = output + s.top();
s.pop();
}
s.push(a[i]);
}
else 
{
while (!s.empty() && (s.top() != '^' && s.top() != '+' && s.top() != '-' && !isOBracket(s.top())))
{
output = output + s.top();
s.pop();
}
s.push(a[i]);
}
}
}
while (!s.empty())
{
output = output + s.top();
s.pop();
}
return output;
}
float InfixtoPostfix::evaluateExp(std::string a)
{
std:: stack<float> s;
std::string vs;
std::string val;
for (int i = 0; i < a.length(); i++)
{
if (isOperand(a[i]))
{
std::cout << a[i] << ": ";
std::cin >> val;
vs = vs + val + " ";
}
else
{
vs = vs + a[i] + " ";
}
}
int ni, pi = 0;
std::string sst;
float f, x, y;
for (int i = 0; i < vs.length(); i++)
{
if (vs[i] == ' ')
{
ni = i - pi;
sst = vs.substr(pi, ni);
if (isOperator(sst[0]))
{
y = s.top();
s.pop();
x = s.top();
s.pop();
s.push(arithOperation(x, y, sst[0]));
}
else
{
std::stringstream ss(sst);
ss >> f;
s.push(f);
}
pi = i;
pi++;
}
}
return s.top();
}

Output:

Post a Comment

0 Comments