-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleCal.cpp
More file actions
167 lines (153 loc) · 4.32 KB
/
simpleCal.cpp
File metadata and controls
167 lines (153 loc) · 4.32 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<iostream>
using namespace std;
string s; // used to ask for yes.
/**
* @brief this function do addition between two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long long
*/
long long sum(long long num1, long long num2){ // O(1)
long long res = num1+num2;
return res;
}
/**
* @brief this function subtraction between two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long long
*/
long long sub(long long num1, long long num2){ // O(1)
long long res = num1-num2;
return res;
}
/**
* @brief this function do multiplication between two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long double
*/
long long mul(long long num1, long long num2){ // O(1)
long long res = num1*num2;
return res;
}
/**
* @brief this function do division between two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long double
*/
long double divi(long long num1, long long num2){ // O(1)
long double res = num1/(long double)num2;
return res;
}
/**
* @brief this function do mod of two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long long
*/
long long mod(long long num1, long long num2){ // O(1)
long long res = num1%num2;
return res;
}
/**
* @brief this function do power of two numbers.
*
* @param num1 number 1
* @param num2 number 2
* @return long long
*/
long long power(long long num1, long long num2){ // O(logn2)
long long res = 1;
while (num2 > 0) {
if (num2 & 1) // if odd
res *= num1;
num1 *= num1;
num2 >>= 1; // divid by 2
}
// while(num2--){ // O(num2)
// res *= num1;
// }
return res;
}
/**
* @brief this function do operation between two numbers.
*
* @param op operator
* @param num1 number 1
* @param num2 number 2
*/
void operation(long long op, long long num1, long long num2){
if(op == 1){
cout << " "<<num1 << " + " << num2 << " = " << sum(num1, num2) << endl;
}else if(op == 2){
cout << " "<< num1 << " - " << num2 << " = " << sub(num1, num2) << endl;
}else if(op == 3){
cout << " "<<num1 << " × " << num2 << " = " << mul(num1, num2) << endl;
}else if(op == 4){
if(num2 != 0){
cout << " "<<num1 << " ÷ " << num2 << " = " << divi(num1, num2) << endl;
}else{
cout << "[Error]: divid by 0" << endl;
}
}else if(op == 5){
cout << " "<<num1 << " % " << num2 << " = " << mod(num1, num2) << endl;
}else if(op == 6){
long long res = power(num1, num2);
cout << " "<<num1 << " ^ " << num2 << " = ";
if(res>>63 && num2 > 1){
cout << "[Error]: the output is too big for compliler to handle" << endl;
}else{
cout << res << endl;
}
}else{
cout << "invalid input" << endl;
}
cout << "Do you want to do another operation? y/n: ";
cin >> s;
if(tolower(s[0]) == 'y'){
cout << "\nEnter your choice: ";
cin >> op;
operation(op, num1, num2);
}
return;
}
/**
* @brief ask the user for two numbers & ask for operator to do between two numbers & perform the operation and print result.
*
*/
void Cal(){
long long num1, num2, op;
// ask the user for number 1 & number 2.
cout << " -First enter number 1 & number 2: ";
cin >> num1 >> num2;
// ask the user for the operator he/she want to preform on the number 1 & number 2.
cout << " -Second select an operator:" << endl;
cout << " 1 : + (addition)" << endl;
cout << " 2 : - (subtraction)" << endl;
cout << " 3 : × (multiplication)" << endl;
cout << " 4 : ÷ (division)" << endl;
cout << " 5 : % (mod)" << endl;
cout << " 6 : ^ (power)" << endl;
cout << "\nEnter your choice: ";
cin >> op;
operation(op, num1, num2);
// ask user if he want to start the function over.
cout << "Do you want to start over? y/n : ";
cin >> s;
if(tolower(s[0]) == 'y'){
cout << endl;
Cal();
}
}
int main(){
cout << "This is a Simple Calculator.\n" << endl;
Cal();
return 0;
}