Calculator

I've created this calculator, which I'm using arrays in to accept any amount of numbers. For example, you can enter " 5 - 6 - 3 - 5 " then subtract them all. However, what I really want to do is using different operators in the same operation. For example, " 5 + 3 * 9 " which I have no clue how to do it.
How can I do it?

Thanks.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <new>
#include <climits>

using namespace std;
int main(int argc, char **argv)
{	
        double x[50] = { };
	double gm3=0, drb=1;
	char yesawy = '\0';

	for(int i=0; i<50; i++)
	{
			
		cout << "Enter Number or \"=\"" << endl;
		cin >> x[i];
		
		if(cin.good() && cin.peek() == '\n')
		{
			gm3 += x[i];
			drb *= x[i];
			continue;
		}
		else
		{
			cin.clear();
			cin >> yesawy;
			if(yesawy == '=')
			{
				break;
			}
			else
			{
				
				cin.ignore(INT_MAX, '\n');
				cout << "invalid char" << endl;
				i--;
			}
		}
	}	
		char op;
		start:
		cout << endl << "Enter Operator(+ - * /)" << endl;
		cin >> op;

		switch(op)
		{
			case '+':
				cout << gm3 << endl; break;
			case '-':
			{
				double z;
				z = x[0];
				for(int i=1; i<50; i++)
				{
					z = z - x[i]; 
				}
				cout << z;
				break;
			}
			case '*':
				cout << drb << endl; break;
			case '/':
			{
				double z;
				z = x[0];
				for(int i=1; i<50; i++)
				{
					z /= x[i]; 
				}
				cout << z;
				cout << "TT";
				break;
				
			}
			default :
			{
				cout << "Invalid Operator";
				cin.ignore(INT_MAX, '\n');
				goto start;
			}
		}
	}
Last edited on
Here is how I would approach this problem:
1. Store all numbers in an array (x), and all operations in a different array(op). The reason is that you would need to apply * and / before + or -. Note that the length of op is equal to length of x +1
2. Loop over the operator array. If op[i]=='*' then replace op[i] with '+', x[i+1] is going to be replaced by x[i]*x[i+1] and replace x[i] with 0. Similarly, if you have op[i]=='/' then x[i+1] is replaced by x[i]/x[i+1]
3. At this point your op array will have only + or -. You start with result=x[0], and loop over the op array. If op[i]=='+', you add x[i+1] to the result, else you subtract it from the result
That's kinda confusing, could you give me a scripted example please?
Edit:

Ideally you need to extend your code so the user can enter a whole equation in one go (using getline()) and then split it up into numbers and operators (storing these in your array(s))

And replace your goto + label with a do-while loop!!

And then...

You might want to read up about the shunting yard algorithm

Shunting-yard algorithm
http://en.wikipedia.org/wiki/Shunting-yard_algorithm

In computer science, the shunting-yard algorithm is a method for parsing mathematical expressions specified in infix notation. ...


Andy
Last edited on
Hesham0, that explanation makes perfect sense and is very elegant. It sounds like you want someone to write code for you.

Ideally you need to extend your code so the user can enter a whole equation in one go (using getline()) and then split it up into numbers and operators (storing these in your array(s))


how about using scanf()?
Note that the length of op is equal to length of x +1

Thinking of 1 / 4 + 2 * 7, aren't there more operands than operators?

Andy

PS Regarding:

how about using scanf()?

Only if you're forced to use C, otherwise why wouldn't you use the std::getline(std::istream&, std::string&) ??
Last edited on
Topic archived. No new replies allowed.