calculator

how can i do a program that will input a 4 different numbers and operators??..
like this ( 7 + 2 - 6 * 4 / 3 )please help me... thank you for your cooperation...
Do you want to do the whole operation in one line, or just to be able to perform many operations in a row using the previous outcome?
i want to do the MDAS operation to this programs using c++
" Multiplication Division Addition Subtraction "
how can i be able to perform 4 different operations in a row, even though in different position of the operation,
Basically you have to use nested switches, I guess you can do something like:

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
double num1, num2, num3 ...
char operator1, operator2, operator3 ...
double answer;
int values;
int maxvalues = //set this to how many values you can handle

cout << "How many values would you like to use (at least 2) ? ";
cin >> values;

if (values < 2)
{ cout << "Invalid number of values } // You have to have at least 2 values

else if (values > maxvalues)
{ cout << "Please use less values, this calculator is rather limited" }

else if (values = 2)
{
cout << "Enter your first number: \n";
cin >> num1;
cout << "Enter the operator (+, -, * or /): \n";
cin >> operator1;
cout << "Enter your second number: \n";
cin >> num 2;

switch(operator1)
{
case '+':
num1 + num2 = answer;
break;

... //do something like that for other operators
}

else if (values = 3)
{
cout << "Enter your first number: \n";
cin >> num1;
cout << "Enter the first operator (+, -, * or /): \n";
cin >> operator1;
cout << "Enter your second number: \n";
cin >> num 2;
cout << "Enter the second operator (+, -, * or /): \n";
cin >> operator2;
cout << "Enter your third number: \n";
cin >> num3;

switch(operator1)
{
case '+':
switch(operator2)
{
case '+':
num1 + num2 + num3 = answer;
break;
...
}
... //do something like that for other operators
}

cout << "The answer is: " << answer << "\n";

//You can add a do while loop for bool again 


This is just the basic solution, but it's not very good and I'm sure you can improve it, I'll leave you at least a bit of work :)
Last edited on
You do realize that even if you code this "correctly" it will not calculate the right answers don't you? You need to consider operator precedence. If you just perform the operation as they are typed in the user will have to be the one to remember the proper order.

Your example: ( 7 + 2 - 6 * 4 / 3 ) = 4 if you ignore the precedence.

The correct way to solve this equation is
(7 + 2 - ((6 * 4) / 3))) =
(7 + 2 - (24 / 3)) =
(7 + 2 - 8) =
1.
Hah, thanks, I know. Everything works, you just have to add parentheses to the code, like so :

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
switch(operator1)
{
case '+':
switch(operator2)
{
case '+':
num1 + num2 + num3 = answer;
break;
// case '-':

case '*':
num1 + (num2 * num3) = answer;
break;
//same for division
}
break;

... //do something like that for other operators
//Here's the example for multiplication:
case '*':
switch(operator2)
{
case '+':
(num1 * num2) + num3 = answer;
break;
// case '-':

case '*':
(num1 * num2)  + num3 = answer;
break;
//same for division
}
break;
}
Last edited on
Topic archived. No new replies allowed.