Hello!
I am making a postfix calculator and I want to add in some equation integrity checking. Everything seems to work ( it needs to be done some little improvements, but overall it is working), except when I am checking for the blank spaces the program do not work the way as it should.
I enclosed the snippet of that part of code. Compile it and enter something like 9 * 5 + 2. The result should be 9*5+2, but it is not even near as that.
What could be wrong?
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
bool IsOperator(char ch);
bool IsOperand(char ch);
void Check(string & infix, double & result, int & i);
double strToDbl(string const& str);
int main()
{
string infix, postfix;
double result = 0.0;
for( int i = 1; i <= 2 ; i++ )
{
switch(i)
{
case 1:
postfix.erase();
infix.erase();
cout << "Enter equation (\"q\" for quit): ";
cin >> infix;
cout << endl;
if (!(infix.compare("q"))) return 0;
break;
case 2:
Check(infix, result, i);
cout << infix << endl;
i = 0;
break;
}
}
return 0;
}
bool IsOperand(char ch)
{
if ((ch >= '0') && (ch <= '9') || (ch == '.'))
return true;
else
return false;
}
bool IsOperator(char ch)
{
if (((ch == '*') || (ch == '/')) ||
((ch == '+') || (ch == '-')) || (ch == '^'))
return true;
else
return false;
}
void Check(string &infix, double & result, int &st)
{
short counter = 0;
for ( int x = 1; x <= 5; x++ )
{
switch (x)
{
case 1:
{
cout << "Checking if there is any letters in the equation...";
for ( string::size_type i = 0; i < infix.size(); i++ )
{
if ( isalpha(infix[i]) )
{
infix.erase(i, 1);
i--;
counter++;
}
}
if (counter != 0)
{
cout << "Error!\nThere were letters in the equation! They were deleted." << endl;
counter = 0;
}
else cout << "Passed!" << endl;
break;
}
case 2:
{
cout << "Checking if there is any blank space...";
string temp;
temp.clear();
for ( string::size_type i = 0; i < infix.size(); i++ )
{
if ( isspace(infix[i]) )
counter++;
else
temp.append(1, infix[i]);
}
if (counter != 0)
{
infix.clear();
infix.assign(temp);
cout << "Error!\nThere were blank space in equation! They were deleted." << endl;
counter = 0;
}
else cout << "Passed!" << endl;
break;
}
case 3:
{
cout << "Checking if there is any parenthesis misspelling...";
for ( string::size_type i = 0; i < infix.size(); i++ )
{
if ( infix[i] == '(' || infix[i] == ')' )
counter++;
}
if (counter % 2 != 0)
{
cout << "Error!\nThere were error placing parenthesis!" << endl;
counter = 0;
st = 0;
}
else cout << "Passed!" << endl;
break;
}
case 4:
{
cout << "Checking if there is any operator misspelling...";
for ( string::size_type i = 0; i < infix.size(); i++ )
{
if ( IsOperator(infix[i]) && IsOperator(infix[i+1]) )
counter++;
else if (IsOperand(infix[i]) && infix[i+1] == '(' || infix[i+1] == ')' )
counter++;
else if (IsOperator(infix[infix.size()-1]) || IsOperator(infix[0]) )
counter++;
}
if (counter != 0)
{
cout << "Error!\nThere were error using operators!" << endl;
counter = 0;
st = 0;
}
else cout << "Passed!" << endl;
break;
}
case 5:
{
// if there is only a number entered
for ( string::size_type i = 0; i < infix.size(); i++ )
if ( !IsOperand(infix[i]) )counter++;
if (counter == 0)
{
result = strToDbl(infix);
st = 0;
}
}
}
}
cout << endl;
}
double strToDbl(string const& str)
{
std::istringstream ss(str);
double d;
ss >> d;
return d;
}