Writing a program that reads a string of arithmetic expression from the keyboard and
evaluates the arithmetic operation in the string. Some example runs are given below.
program should accept only
* any real or integer numbers
* four basic arithmetic operators, +, -, *, /
* paranthesis ( , )

I'm just not getting paranthesis, I mean, how I get this expression "(5 + (11-5) * 2) * 4 + 9.9" into the program.
I have an idea about it, but I am not sure it's going to work. My idea is read this expression from the command line to a string and than read that string into a function, like cout<<"Result is:" << A (not the whole thing but I just express myself)

Example 2
Input an aritmetic expression:
2 - 3 * 4 + 2
The result is -8

Example 3
Input an aritmetic expression:
(5 + (11-5) * 2) * 4 + 9.9
The result is 77.9

Example 4
Input an aritmetic expression:
2 + 3 = 5
Error: Undefined symbol '='.

Example 5
Input an aritmetic expression:
(5 + (11-5 * 2) * 4 + 9
Error: Missing paranthesis ')'.

Example 6
Input an aritmetic expression:
(5 + 11-5) * 2) * 4 + 9
Error: Missing paranthesis '('.

Example 7
Input an aritmetic expression:
2 - p 4 + 2
Error: Undefined symbol 'p'.

Example 8
Input an aritmetic expression:
2 - [3.0 / 4] + 2.5
Error: Undefined symbol '

Recommended Answers

All 9 Replies

OK, you don't get the parentheses part. So how about writing a program to do the rest of it and posting it when you're done? If you do that, I'm sure someone will give you a hint about how to do the rest of it.

As it is, you're just asking people to do your homework for you, without evidence that you've done any of it yourself.

OK, you don't get the parentheses part. So how about writing a program to do the rest of it and posting it when you're done? If you do that, I'm sure someone will give you a hint about how to do the rest of it.

As it is, you're just asking people to do your homework for you, without evidence that you've done any of it yourself.

You missunderstand me, sir. I just ask an opinion, not to do homework. I am asking "which parameter that I will use for defining the parantesis." For example; "Use switch-case statement..." Than I can write something.
I am not asking you to do my homework, just ask for help(opinion). Thank you.

Switch-case statements will work fine.

However, if you think that the hard part of the problem is deciding what kind of statement to use to detect parentheses, then I think you do not understand the problem.

I also think that before you start worrying about parentheses, you write a program that can handle the first of the examples correctly. In other words, write a program that can accept "2 - 3 * 4 + 2" as its input and give -8 as its result.

commented: That's what I am talking about. Thank you +2

I did a project like this in Java. The goal was to write the entire calculator using the functional programming style. I ran into quite a few problems involving parsing the negative numbers versus minus operators, and ended up adding a bunch of if statements checking for "-" after "e" and silly things like that. I probably should have re-thought the entire application. I did parsing of parentheses recursively until string had no more parentheses. That was the easy part. The difficult part was figuring out every possible input for error checking. Maybe you could persuade this community to post some input for your program. Also, I'm interested in seeing your approach. Would you attach some source code to your next post if you have some?

Start at the beginning of the string and look at each character.
Every time you find a ( remember it's position in the string.
When you find a ), evaluate the equation from the last ( to the just found )
Remove that equation replacing it with the answer just found.
Continue...

commented: Nice, you just express the algorithm +2

Start at the beginning of the string and look at each character.
Every time you find a ( remember it's position in the string.
When you find a ), evaluate the equation from the last ( to the just found )
Remove that equation replacing it with the answer just found.
Continue...

You forgot one step: If a "(" is found instead of a ")" after the first "(", then remember the new position and look for the next ")".

You forgot one step: If a "(" is found instead of a ")" after the first "(", then remember the new position and look for the next ")".

No I didn't. I said:
"Every time you find a ( remember it's position in the string.." You may have to remember 2 or more positions.

No I didn't. I said:
"Every time you find a ( remember it's position in the string.." You may have to remember 2 or more positions.

Yes, that's right. My mistake.

I continue to believe that the first example that the OP cited is the most interesting.

Once you understand enough to be able to write a program that evaluates "2 - 3 * 4 + 2" as -8 instead of -2, parentheses are easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.