-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicMathOperations.cpp
128 lines (106 loc) · 2.5 KB
/
basicMathOperations.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// This program will perform the chosen operation on two numbers
// entered by the user
#include <iostream>
using namespace std;
// Function prototype
float addNumbers(float, float);
float subtractNumbers(float, float);
float multiplyNumbers(float, float);
float divideNumbers(float, float);
void selection(int, float, float);
float result;
int main()
{
// Variables
int choice;
float number1;
float number2;
do
{
// Display Menu
cout << "Math Program:\n";
cout << "\t 1. Add two numbers\n";
cout << "\t 2. Subtract two numbers\n";
cout << "\t 3. Multiply two numbers\n";
cout << "\t 4. Divide two numbers\n";
cout << "\t 5. Quit\n";
cin >> choice;
// Inputs validation for the menu
while (choice < 1 || choice > 5)
{
cout << "\nYou must choose between 1 and 5!\n";
// Display Menu
cout << "\n";
cout << "Math Program:\n";
cout << "\t 1. Add two numbers\n";
cout << "\t 2. Subtract two numbers\n";
cout << "\t 3. Multiply two numbers\n";
cout << "\t 4. Divide two numbers\n";
cout << "\t 5. Quit\n";
cin >> choice;
}
if (choice == 5)
{
selection(choice, 0, 0);
cout << "Thanks for using the program!\n";
}
else
{
// Get numbers and function call
cout << "Enter two numbers: ";
cin >> number1 >> number2;
selection(choice, number1, number2);
// Display results
if (choice == 1)
cout << "The numbers added are: " << result << "\n\n";
else if (choice == 2)
cout << "The numbers subtracted are: " << result << "\n\n";
else if (choice == 3)
cout << "The numbers multiplied are: " << result << "\n\n";
else if (choice == 4)
cout << "The numbers divided are: " << result << "\n\n";
}
} while (choice != 5);
return 0;
}
// Addition
float addNumbers(float number1, float number2)
{
result = number1 + number2;
return result;
}
// Subtraction
float subtractNumbers(float number1, float number2)
{
result = number1 - number2;
return result;
}
//Multiplication
float multiplyNumbers(float number1, float number2)
{
result = number1 * number2;
return result;
}
// Division
float divideNumbers(float number1, float number2)
{
result = number1 / number2;
return result;
}
void selection(int choice,float number1, float number2)
{
// Choose the right operation
switch (choice)
{
case 1: addNumbers(number1, number2);
break;
case 2: subtractNumbers(number1, number2);
break;
case 3: multiplyNumbers(number1, number2);
break;
case 4: divideNumbers(number1, number2);
break;
case 5:
break;
}
}