i am trying to make a calculator, I need some hints and guidance

Hi guys, i am am a beginner

i am trying to write a calculator
that gives op like
>>2+6
>>>>ANS=8
in general num1 operator num2 should give the answer

i have written some code regarding this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    char op;
    int i;
    double inNum1,inNum2;
    double ans_n=0;//,ans_o=0;

    while(1)
    {
        cout << ">> ";
        cin >> inNum1 >> op >> inNum2;

        if(op == '+')
            ans_n=inNum1+inNum2;
        else if(op=='-')
            ans_n=inNum1-inNum2;
        else if(op=='*')
            ans_n=inNum1*inNum2;
        else if(op=='/')
            ans_n=inNum1/inNum2;
        else if(op=='%')
            ans_n=inNum1*inNum2;
        else if(op=='^')
            ans_n=pow(inNum1,inNum2);
        else
            cout << "Invalid operator";

        cout << ">>>> ANS = " << ans_n << endl;
       // ans_o=ans_n;
    }
}


next i want to add a feature such that
1) If the use inputs quit or exit the program should terminate
2) in the program the previous result cannot be used again
like

>>2+3
>>>>ANS=5
>>5^2
>>>>ANS=25                   


now i cannot use 5 unless i manually type 5 again
i want it to be like this

>>2+3
>>>>ANS=5
>>^2
>>>>5^2
>>>>ANS=25                   


also the previous session or history should be cleared if use gives input as clear


3) i want it to be able to handle inputs like this

>>2+(3-5)*6


Last edited on
To be able to do ^2 isn't hard given what you have, but the problem is that to get to where
you want to go, you need to parse the input, which is considerably more difficult than what
you've written so far, and you'll end up throwing away what you've done anyway. You might
try googling "infix calculator c++" and going from there.

You can do with switch. It's better for me :)
from what i understood from <b>infix calculator</b>
1) take input in form of string
2) separate all numbers and operators according to their priorities
3) convert string to float or int.. and then process the data

i have written this can you guys guide me further
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>
#include <cstring>
#include <stdlib.h>

#define max_input 2
using namespace std;
int main()
{
    int i=0,j,k;
    string input,num[max_input];
    int op_pri=0;
    double dnum[max_input];
    char op;

    cout << ">> ";
    getline(cin,input);
    cout << input << endl;
    k=input.length();
    cout << k << endl;

    for(; i<k; i++)
    {
        switch(input[i])
        {
        case '+':
            op_pri=1;
            break;
        case '-':
            op_pri=1;
            break;
        case '*':
            op_pri=2;
            break;
        case '/':
            op_pri=2;
            break;
        case '%':
            op_pri=2;
            break;
        case '^':
            op_pri=3;
            break;
        default:
            op_pri=0;
            break;
        }
        if(op_pri!=0)
        {
            break;
        }
    }

    num[0]=input.substr(0,i);
    cout << num[0] << endl;
    num[1]=input.substr(i+1,k);
    cout << num[1] << endl;
    op=input[i];
    cout << op << endl;
}



>> 111*999
 7                    //string length 
 111                 //first digit
 999               //second digit
 *                   //operator



it cannot handle '-' at the beginning
and only 1 operator
No, it's much more complicated than that. Infix calculators need, at some level, a state machine, because you
can't just evaluate left-to-right. For example:

5 * ( 3 + 4 )

You need to evaluate 3 + 4 first to get 7, then multiply 5 by 7 to get the right answer. And even without
parens:

3 + 4 * 5

should yield 23, because multiplication has higher precedence than addition, and so again evaluation is not
necessarily left to right.

And then getting arbitrarily complex:

( 5 + 3 / ( 4 - 1 ) ) / ( ( 4 - 2 ) / 2 )

Means you need to evaluate the terms of the expression in the following order:

(a) 4 - 1 = 3
(b) 3 / (a) = 1
(c) 5 + (b) = 6
(d) 4 - 2 = 2
(e) (d) / 2 = 1
(f) (c) / (e) = 6

Answer = 6.

Here evaluation is neither left to right nor right to left; rather, the syntax of the expression
(ie, the parentheses and the operators you use) "directs" your calculator how to evaluate
the expression.

You could ask for postfix input.
So later you could simply do a conversor.
thanks guys but i think this program is beyond me at the moment, first i need to read state machines and also understand c++ better. but if you guys can tell me any ideas which are bit more challenging than the text book problems but a bit easier than the above problem it might be helpful.
also some links on learning the abc's of finite state machines could be useful.
(should i mark this post as solved)
Last edited on
I recently shamed myself with my ignorance on the application of recursive descent parser for exactly this kind of problems. (Parsing something in infix notation)
Read here: http://en.wikipedia.org/wiki/Recursive_descent_parser

You can also use the shunting-yard algorithm to convert infix notation to postfix notation.
Read here: http://en.wikipedia.org/wiki/Shunting-yard_algorithm

And the evaluation of expressions in postfix notation is relatively simple.
Read here: http://en.wikipedia.org/wiki/Reverse_Polish_notation

Now, you can either demand that the input is in postfix notation and just experiment with the back-end (the engine) of the calculator for fun. Or you can use one of the two methods above to evaluate expressions in infix notation. The shunting-yard is inferior in being the more complex method, but can be somewhat more optimal if the number precedence levels is significant (a few wasted bytes really), or if you plan to evaluate the same expression many times with different input variables. The latter will apparently matter only if your calculator allows symbolic expressions of some sort.

Regards
Topic archived. No new replies allowed.