PROGRAM TO SIMPLIFY INFIX EXPRESSION.
#include <iostream>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <cctype>
#include <stdlib.h>
using namespace std;
template <class Type>
class Simplify
{
private:
Type *Stack;
int Top, Size;
public:
Simplify(int Siz)
{
Size = Siz;
Stack = new Type[Size];
for(int I =0; I <Size; I++)
{
Stack[I] = 0;
}
Top = -1;
}
void Push(Type Ch)
{
if(IsFull())
{
cout << "\n\n\nSTACK OVERFLOWED!!!!!!\n\n\n" << endl;
exit(1);
}
Stack[++Top] = Ch;
}
Type Pop()
{
if(IsEmpty())
{
cout << "\n\n\nSTACK UNDERFLOWED!!!!!!!!!\n\n\n" << endl;
exit(1);
}
return Stack[Top--];
}
Type Result(string Ptr)
{
Simplify<char> Obj(100);
int Oprnd1, Oprnd2, Rezult, Count = 0;
char Symb;
for(int I = 0; I <Ptr.length(); I++)
{
Symb = Ptr[I];
if( isdigit(Symb) )
Obj.Push(Symb - '0');
else
{
Push(Symb);
Count++;
}
}
for(int I =0; I < Count; I++)
{
Symb = Pop();
Oprnd2 = Obj.Pop();
Oprnd1 = Obj.Pop();
if(Symb == '+')
Rezult = Oprnd1 + Oprnd2;
else if(Symb == '-')
Rezult = Oprnd1 - Oprnd2;
else if(Symb == '*')
Rezult = Oprnd1 * Oprnd2;
else if(Symb == '/')
Rezult = Oprnd1 / Oprnd2;
else if(Symb == '^')
Rezult = pow(Oprnd1, Oprnd2);
else
exit(1);
Obj.Push(Rezult);
}
return Obj.Pop();
}
inline bool IsFull()
{
return (Top == Size -1);
}
inline bool IsEmpty()
{
return (Top == -1);
}
~Simplify()
{
cout << "\n\n\n***********PROGRAM ENDED************\n\n\n" << endl;
}
};
int main()
{
Simplify<int> Obj(100);
string Statement;
cout << "Enter Any Expression: ";
cin >> Statement;
cout << "\nResults Of INFIX Expression is: " << Obj.Result(Statement) << endl;
getch();
return 0;
}
0 comments:
Post a Comment