Command Line Calculator

Hi everyone, i just wanted some advice as to solve my assignment.

i want to write command line calculator that accepts a formula from the user and respond with the calculated result. An example would be:
33.2+2*150/5^2
Which would display:
The answer is : 45.2

So i am considering different things in order to do this:

cin >> user_input ;
int length = user_input.length();
int i;
string array_1[100];
for (i = 0; i<length ; i++)
{
array_1[i] = user_input.substr(i,1);
cout << array_1[i]<< endl;
}


I am placing each character in an array.

I will use "atoi" to convrt char to int , for the calculation
( a problem i face with this is that atoi only accept const char*, how can i resolve this).
And use .find() to give the location of operators (+ - x /).

Do you think that i am heading in the right direction with, this or id there a simple but shorter way?

atoi() returns an int, which means you cannot enter floating point values.

better would be to use strtod() [string-to-double].

string foo;

foo.c_str() gets you a const char*.

are you honoring operator precedence or just evaluating left to right?

have you learned in class about Reverse Polish Notation?
Thanks jsmith,

Yes i am honoring operator precendence, and am also taking into consideration the use of () brackets in the user input.

I know what the RPN is, but i dont know how to implement it in this case.

I am also working on another method of doing this. So hopefully that works out better.
It seems to me like implementing RPN would be easier to handle operator precedence and parentheses correctly.

I don't see how using find() to locate the operators would help you; the bottom line is that you have to parse the input left to right.
Topic archived. No new replies allowed.