Quote:
Originally Posted by xpi0t0s
The code currently treats consecutive operators an error, so 3+-5 would throw an error because of the +-. It would need to be modified to throw an error only when this doesn't make sense, so for example 3+-5 is fine because it means three plus (-5), but 3+*5 would still throw an error.

Have a look at the code after the comment "// several important validation checks on the input"
I have changed several codes of it, but it still wouldn't take a negative number. Here is the code, can you give me some suggestions?

Code:
```class Calculator : protected base
{
enum {PLUS='+',MINUS='-',MUL='*',DIV='/'};
int precision;
double Value_Of_Num(const string &Expr)

{
istringstream is(Expr);
double value;
is >> value;
return value;
}
double Value_Of_Expr(const string &Expr)

{
int i=0,p=0,pos_mul=0,pos_div=0;

if(Expr.at(0)=='('&&Expr.at(Expr.length()-1)==')')
{
for(i=0;i < Expr.length();i++)
{

if(Expr.at(i)=='(') p++;
else if(Expr.at(i)==')') p--;
if(p==0) break;

}

if(i==Expr.length()-1)
return Value_Of_Expr(Expr.substr(1,Expr.length()-2));
}

for(i=0;i < Expr.length();i++)
{
if(Expr.at(i)=='(') p++;
else if(Expr.at(i)==')') p--;
else if(p==0&&ispunct(Expr.at(i)))
{

switch(Expr.at(i))
{
case PLUS:
return Value_Of_Expr(Expr.substr(0,i))+Value_Of_Expr(Expr.substr(i+1,Expr.length()-i-1));
case MINUS:
return Value_Of_Expr(Expr.substr(0,i))-Value_Of_Expr(Expr.substr(i+1,Expr.length()-i-1));
case MUL: pos_mul=i; break;
case DIV: pos_div=i; break;

}
}
}

if(pos_mul)
return Value_Of_Expr(Expr.substr(0,pos_mul))*Value_Of_Expr(Expr.substr(pos_mul+1,Expr.length()-pos_mul-1));

if(pos_div)
return Value_Of_Expr(Expr.substr(0,pos_div))/Value_Of_Expr(Expr.substr(pos_div+1,Expr.length()-pos_div-1));

return Value_Of_Num(Expr);
}

// several important validation checks on the input
bool Check(string input_string)
{

for (int r = 0; r <= input_string.length(); r++)
{
if((input_string[r]=='+')||(input_string[r]=='-'))
{
if((input_string[r+1]=='+')||(input_string[r+1]=='-')||(input_string[r+1]=='*')||(input_string[r+1]=='/'))
{
return false;
}
}
}
for (int r = 0; r <= input_string.length(); r++)
{
if((input_string[r]=='*')||(input_string[r]=='/'))
{
if((input_string[r+1]=='*')||(input_string[r+1]=='/'))
{
return false;
}
}
}
string array="0123456789+-*/().";
int count=0;
for (int s=0; s < input_string.length(); s++)
{
for(int z=0; z < array.length(); z++)
{
if(input_string[s]==array[z])
{
count++;
}
}
}
if (count == input_string.length())
{
if(input_string.at(0) != '*' && input_string.at(0) != '/' && input_string.at(0) != '+' && input_string.at(0) != '-')
{
if(input_string.at(input_string.length()-1) != '*' && input_string.at(input_string.length()-1) != '/' && input_string.at(input_string.length()-1) != '+' && input_string.at(input_string.length()-1) != '-')
{
return true;
}
}
}
else
{
return false;
}
}
public :
Calculator()
{
string expression, choice;

system("CLS");
system("COLOR 03");
cout << "\t\t\tWelcome to the calculator function!" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "With this calculator, your problem solving is made simple." << endl;
cout << "You do not have to calculate each part of an expression one at a time \n";
cout << "like you do with you simple calculators." << endl;
cout << "All you have to do is simply input an expression \n";
cout << "and the calculator will give you the answer." << endl;
cout << "" << endl;
cin >> expression;
if(Check(expression)==true)
{
cout << "Does your expression require precision of significant figures?[y/n]: ";
cin >> choice;
cout << endl;
if(choice == "y" || choice == "Y")
{
cout << "How many significant figures do you want it to display?" << endl;
cin >> precision;
cout << setprecision(precision) << Value_Of_Expr(expression)<<endl;
}
else
{
cout << "The answer is: ";
cout << Value_Of_Expr(expression)<<endl;
}
}
else
{
cout << "\a" <<endl;
cout << "Error in the input" << endl;
}
cout << endl << endl << endl;
base calculator1;
calculator1.display();

}
};```