# Simple Calculator (input problem)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-17-2011
grache28
Simple Calculator (input problem)
I'm trying to make a simple calculator, but it has to receive input like this:

operand-operator-operand

i.e.

6*5
76+2
670/10

My problem is, how I can I write it so it knows when the first operand has ended (when it could be 1 digit or 5 for example) and then see what the operation is (multiplication, division, etc) and then start on the second operand?

Sorry for the rather silly question, and yes I need it to work this way, not enter the first operand, then enter the second, all together with the operator in one line.

• 11-17-2011
manasij7479
Like this:
Code:

`std::cin>>operand1>>operator>>operand2;`
...where operands are numbers and operator is a char.
• 11-17-2011
Elkvis
@manasij that only works if there are spaces or other whitespace between operators and operands.

if you make the assumption that the first operand will be all digits, you can just read in single characters, appending them to a string, until you find the first non-digit. then read that in, and then read the remainder of the line into another string. convert both strings to integer values, and apply the operator.
• 11-17-2011
manasij7479
Quote:

Originally Posted by Elkvis
@manasij that only works if there are spaces or other whitespace between operators and operands.

No..(with some conditions)
The following works even if there is no space between the tokens.
Code:

```#include<iostream> using namespace std; int main() {         int op1,op2;char opr;         cin>>op1>>opr>>op2;         cout<<op1<<endl<<opr<<endl<<op2;         return 0; }```
• 11-18-2011
grache28
doh *facepalm*

Thanks
• 11-19-2011
Elysia
Quote:

Originally Posted by Elkvis
@manasij that only works if there are spaces or other whitespace between operators and operands.

Or, I suppose, a non-integer.
Although, don't quote me on that.
• 11-19-2011
manasij7479
Quote:

Originally Posted by Elysia
Or, I suppose, a non-integer.
Although, don't quote me on that.

Quoted.. :P
Anyway.. It works when whitespaces are absent...for integers ( and in some cases floats and doubles)... The only way it'd fails in absence of whitespace is when the operators + or - are confused as parts of the floating points.
• 11-27-2011
grache28
Ok I got it working via a swtich that does this:

std::cin>> op1 >> opr >> op2;

switch(opr){

case '+':
std::cout << op1+op2 << "\n";
break;

case '-':
std::cout << op1-op2 << "\n";
break;.......................

Now here's the million dollar question, how can I handle this:

2*2*2-0.5
??

Or any input that has multiple operands and multiple operators

(Sorry if this isn't formatted pretty)
• 11-27-2011
laserlight
Quote:

Originally Posted by grache28
Now here's the million dollar question, how can I handle this:

2*2*2-0.5
??

Or any input that has multiple operands and multiple operators

There are a few ways, but one approach that could work as a starting point is to look up the Shunting Yard algorithm and the use of reverse Polish notation.
• 11-28-2011
grache28
Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?
• 11-28-2011
manasij7479
Quote:

Originally Posted by grache28
Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?

No..(Unless the expression is so short that you can afford numerous passes over it/)
(Well...there is.. but I think you'll find it more complicated..) You could always build a tree... but Shunting Yard is way more easy to implement and understand ..IMHO.
• 11-28-2011
grache28
Ok, thanks to you both. I'll work out a way to do it with the Shunting-yard algorithm and come back if I have any problems.
• 12-02-2011
grache28
What if I don't need the calculator to perform the correct order of operations, for example:

5-3*5
=10

It just reads from left to right, but the amount of operators and operands is unknown. So the user could input:
5-4

or

5-4+3+3+2/2-2*5

Again the expression would be evaluated from left to right without using any order of operations, but the number of operators and operands is unknown.

Thanks
• 12-02-2011
manasij7479
Just read the input character by character and perform the operation with a switch-case when you have two operands.
• 12-02-2011
grache28
Ok, great. The problem is I only know how to get input with cin. So I wouldn't have to make a lot of variables, or? How do I know how many variables to make?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last