-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp.cpp
More file actions
108 lines (93 loc) · 2.26 KB
/
calculator.cpp.cpp
File metadata and controls
108 lines (93 loc) · 2.26 KB
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
#include <iostream>
#include <math.h>
using namespace std;
// Class calculator
class Calculator
{
float a, b;
public:
// Function to take input
// from user
void result()
{
cout << "Enter First Number: ";
cin >> a;
cout << "Enter Second Number: ";
cin >> b;
}
// Function to add two numbers
float add()
{
return a + b;
}
// Function to subtract two numbers
float sub()
{
return a - b;
}
// Function to multiply two numbers
float mul()
{
return a * b;
}
// Function to divide two numbers
float div()
{
if (b == 0)
{
cout << "Division By Zero" <<
endl;
return INFINITY;
}
else
{
return a / b;
}
}
};
// Driver code
int main()
{
int ch;
Calculator c;
cout << "Enter 1 to Add 2 Numbers" <<
"\nEnter 2 to Subtract 2 Numbers" <<
"\nEnter 3 to Multiply 2 Numbers" <<
"\nEnter 4 to Divide 2 Numbers" <<
"\nEnter 0 To Exit";
do
{
cout << "\nEnter Choice: ";
cin >> ch;
switch (ch)
{
case 1:
// result function invoked
c.result();
// add function to calculate sum
cout << "Result: " <<
c.add() << endl;
break;
case 2:
// sub function to calculate
// difference
c.result();
cout << "Result: " <<
c.sub() << endl;
break;
case 3:
c.result();
// mul function to calculate product
cout << "Result: " <<
c.mul() << endl;
break;
case 4:
c.result();
// div function to calculate division
cout << "Result: " <<
c.div() << endl;
break;
}
} while (ch >= 1 && ch <= 4);
return 0;
}