I have written an infix calculator based on the algorithm which is coded below. It is using two stacks, one for the integer values, and one for the operators. However, it is not doing what I think it is supposed to be doing. Once it executes the Execute() function, it turns out that opStack is empty, but I have not been able to isolate the problem. If anyone is able to point me in the right direction, I would be grateful. Thanks.
Code:
#include <iostream>
#include <stack>
#include <fstream>
#include <string>
using namespace std;
class Calculator
{
public:
void Open(ifstream& f, string afile);
void Process(ifstream& f);
void Execute ();
void Display ();
void DisplayOp ();
private:
stack <int> valStack ;
stack <char> opStack ;
};
int main()
{
Calculator Infix ;
ifstream INFILE;
string filename ;
cout << "Enter filename to open: " ;
cin >> filename ;
Infix.Open(INFILE, filename) ;
Infix.Process(INFILE);
system ("Pause");
return 0;
}
void Calculator :: Open(ifstream& f, string afile)
{
f.open (afile.c_str());
if(f.fail())
{
cout <<"Error: File not Opened!" << "\n" ;
cout <<"Exiting Program!" << "\n" ;
system("Pause");
exit(1);
}
}
void Calculator :: Process(ifstream& f)
{
int result ;
string infixExpr;
string achar[infixExpr.length()];
while(f && f.peek() != EOF)
{
getline(f, infixExpr);
}
for(int i=0; i < infixExpr.length(); i++)
{
//*********************************************************
//achar = 'infixExpr[i]' ;
//First Attempt Not Working
//achar = infixExpr;
/*
//Handles cases where the operand is more than one digit
// j is used as a temporary index iterator as to not affect the i from the loop
int j = i;
while(isdigit(infixExpr[j+1]))
{
achar = achar[j] ;
j++;
}
*/
//***********************************************************
if (isdigit(infixExpr[i]))
valStack.push(infixExpr[i]) ;
if (infixExpr[i] == '(')
opStack.push(infixExpr[i]) ;
if(infixExpr[i] == '+' || '-' || '*' || '/' || '^')
{
if(opStack.empty())
opStack.push(infixExpr[i]) ;
}
else if (infixExpr[i] > opStack.top())
opStack.push(infixExpr[i]) ;
else
{
while ( !opStack.empty() && infixExpr[i] <= opStack.top() )
{
Execute() ;
opStack.push(infixExpr[i]) ;
}
}
if(infixExpr[i] == ')')
{
while (opStack.top() != '(')
{
Execute();
opStack.pop() ;
}
}
}//end for
while(!opStack.empty())
{
Execute();
result = valStack.top();
}
}
void Calculator :: Execute ()
{
int operand2, operand1, result ;
char op;
operand2 = valStack.top();
valStack.pop();
operand1 = valStack.top();
valStack.pop();
op = opStack.top();
opStack.pop();
if(op == '^')
result = operand1 + operand2 ;
if(op == '*')
{
result = operand1 * operand2 ;
cout << operand1 << operand2 ;
}
if(op == '/')
{
if(operand2 == '0')
cout <<"Division by zero error!";
else
result = operand1 / operand2 ;
}
if(op == '+')
result = operand1 + operand2 ;
if(op == '-')
result = operand1 - operand2 ;
valStack.push(result) ;
}
void Calculator :: Display()
{
while(!valStack.empty())
{
cout <<valStack.top();
valStack.pop();
}
}
void Calculator :: DisplayOp()
{
while(!opStack.empty())
{
cout <<opStack.top();
opStack.pop();
}
}