My friend gave this code about a stack base calculator where the user enter the mathematical expression as infix and then it converts it into postfix and gives out the result. Now his program works but there is stuff that I don't need like printing the postfix convertion. This is the code:
Code:
#include "Calculator.h"
Calculator::Calculator()
{
infix = " ";
}
Calculator::~Calculator()
{
}
void Calculator::set(string a)
{
infix = a;
}
bool Calculator::Operand(char character)//check each char is a operand
{ //eg. numbers,x and y which represent a function in this program
if (((character >= '0') && (character <= '9'))||(character == 'x') ||(character == 'y') ||(character == '.'))
return true;
else
return false;
}
bool Calculator::Number(char character)//check each char for a number
{
if (((character >= '0') && (character <= '9'))||(character == '.'))//include . for decimals numbers
return true;
else
return false;
}
bool Calculator::Operator(char character)//check each char for a operator
{
if ((character == '+') ||(character == '-') ||(character == '*') ||(character == '/'))
return true;
else
return false;
}
bool Calculator::Precedence(char Operator1, char Operator2)//check the order of precedence
{ //in reference to the infix notation
if (Operator1 == '(')
return false;
else if (Operator2 == '(')
return false;
else if (Operator2 == ')')
return true;
else if ((Operator1 == '*') || (Operator1 == '/'))
return true;
else if ((Operator2 == '*') || (Operator2 == '/'))
return false;
else
return true;
}
void Calculator::InfixtoPostfix(const string &infix, string &postfix)
{
Stack <char> Operator;
char top, symbol;
for (int k = 0; k < infix.size(); k++)//size() for loop to go until the lenght of the infix notation
{ //part of the library <string>
symbol = infix[k];
if (Operand(symbol))// if char is an operand just add them
postfix = postfix + symbol;
else//delete char that is not operand and add it to Postfix
{
while ((! Operator.isEmpty()) &&(Precedence(Operator.top(), symbol)))
{
top = Operator.top();
Operator.pop(top);
postfix = postfix + top;
}
if ((! Operator.isEmpty()) && (symbol == ')'))
Operator.pop(top); // skip ( ) matching
else
Operator.push(symbol);
}
}
while (! Operator.isEmpty())
{
top = Operator.top();
Operator.pop(top);
postfix = postfix + top;
}
}
string Calculator::Change(string tmp)
{
for(int i = 0; i <tmp.size(); i++)
{
if(Number(tmp[i])==true)
{
if(Number(tmp[i+1])==false)
{
tmp.insert(i+1, "x");
}
}
}// -ve * -ve case
for (int i = 0; i < tmp.size(); i++)
{
if(tmp[i]=='-')
{
if((tmp[i-1]!='x')&&(tmp[i-1]!=')'))
{
tmp.replace(i,1,"y");
}
}
}
return tmp;
}
string Calculator::Space(string tmp)//replace the x and y inserted in Change()
{
for(int i = 0; i < tmp.size(); i++)
{
if (Operator(tmp[i])==true)
{
tmp.insert(i+1, " ");
//Insert a space after all
//found operators
}
else if( tmp[i]=='x' )
{
tmp.replace(i,1," ");
//replace the x with a space
//for clarity
}
}
for (int i = 0; i < tmp.size(); i++)
{
if(tmp[i]=='y')
{
tmp.replace(i,1,"-");
}
}
return tmp;
}
bool Calculator::Check(string tmp)
{
//incomplete Changed check that consecutive '+', '-' signs do not exist
for (int i = 0; i < tmp.size(); i++)//lenght() for loop until lenght of infix notation in this case tmp
{
if((tmp[i]=='+')||(tmp[i]=='-'))
{
if((tmp[i+1]=='+')||(tmp[i+1]=='-'))
{
return false;
}
}
}
string array1 = "+-*/().0123456789";
int counter = 0;
for (int i = 0; i < tmp.size(); i++)
{
for(int j = 0; j < array1.size(); j++)
{
if(tmp[i]==array1[j])
{
counter++;
}
}
}
if (counter == tmp.size())
{
return true;
}
else
{
return false;
}
}
void Calculator::Next(string tmp)
{
vector <string> array1;
int spaces = 0;
for ( int a = 0; a < tmp.size(); a++ )
{
if(tmp[a]==' ')
{
spaces++;
}
}
string whatever;
istringstream input(tmp);
while ( getline(input, whatever, ' ') )
{
array1.push_back(whatever);
}
Stack <string> stack;//initialize stack
vector <string> temp;
string character;
for (int i = 0; i < spaces; i++)
{
string s;
s = array1[i]; //make it easier to read
if ((s!="+")&&(s!="*")&&(s!="-")&&(s!="/"))
{
stack.push(s);//push numbers onto the stack
}
else //i.e if it encounters an operator
{
stack.push(s);//push operator onto stack
for ( int i = 0; i < 3; i++ )
{
temp.push_back(stack.top());
stack.pop(character); //erase from the stack
}
double z;
z = Operation(temp);
ostringstream output;//so i can copy and convert example taken from www.cplusplus.com/reference/iostream/ostringstream/ostringstream.html
output << z; // Convert value into a string.
character = output.str();//copy string of outs and assign it to character
stack.push(character);
temp.clear();
}
}
cout << character;
}
double Calculator::Operation(vector <string> &temp)
{
string a,b,c;
a = temp[2];
b = temp[0];
c = temp[1];
double x,y,z;
istringstream insert,insert2;
insert.str(a); insert2.str(c);
insert >> x;
insert2 >> y;
if (b == "+")
{
z = x + y;
return z;
}
else if (b == "-")
{
z = x - y;
return z;
}
else if (b == "*")
{
z = x * y;
return z;
}
else if (b == "/")
{
z = x / y;
return z;
}
}
void Calculator::result()
{
string postfix;
if(Check(infix)==true)
{
string temp;
temp = Change(infix);
InfixtoPostfix(temp, postfix);
cout << "\nThe Postfix is: "<< Space(postfix);//show the user the postfix
//for me to know the convertion from infix to postfix went ok
string hold;
hold = Space(postfix);
cout << "\n\nThe result is: ";
Next(hold);
cout << "\n\n";
}
else
{
cout << "Expression is not valid.\n";
}
}
I already try deleting the Space and Change me part of the program but it wont give the result it prints out just blank space. Thanks. I think it has to do something in the Next function but I can't figure it out.