![]() |
Need some help for calc code
:D, so I have this calc code, its accepts strings then calculates it. Can anyone explain what does the function. I can understand the bool check function though!
double value_of_num(const string &Expr) and double Value_Of_Expr(const string &Expr) Code:
class Calculator : protected base |
Re: Need some help for calc code
It's unlikely anyone will give you a line by line breakdown of the entire function. What precisely do you want to know? Have you tried asking the person who wrote the code?
|
Re: Need some help for calc code
Sadly, the author of this code did not leave his contact email. =(
double Value_Of_Num(const string &Expr) this function is the one that's making my head spin, its only 4 lines long, thanks in advance for explaining! |
Re: Need some help for calc code
As the name suggests it gets the value of a number. So for "27" it would return 27.
|
Re: Need some help for calc code
My question will be, why does it need to create an object 'is' with istringstream.
And what does this line mean Code:
is >> value; |
Re: Need some help for calc code
Presumably because istringstream implements a conversion to double, whereas string doesn't. So you convert the string Expr to an istringstream with "istringstream is(Expr)", then invoke istringstream::operator>>(double), which performs the conversion, with "is >> value".
A lot of C++ is about knowing your standard library. |
Re: Need some help for calc code
So you're saying its essentially converting the value of Expr into an istringstream. Then it converts the 'is' which is created by the string 'Expr' and in the line of the code
Code:
is >> value |
Re: Need some help for calc code
As pointed by my friend, we noticed that the calculator does not take in negative numbers, is there any code that I should change to let it accept negative numbers?
ie : (5*5)-(2*2)*-5 |
Re: Need some help for calc code
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" |
Re: Need some help for calc code
Quote:
Code:
class Calculator : protected base |
| All times are GMT +5.5. The time now is 09:50. |