-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
60 lines (53 loc) · 1.51 KB
/
calculator.cpp
File metadata and controls
60 lines (53 loc) · 1.51 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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <limits>
#include <unistd.h>
using namespace std;
int main() {
double num1, num2, result;
char op;
cout<<"Hello World\n";
while (true) {
// Display the prompt and get user input
cout << "Enter an operation (+, -, *, /, ^, %): ";
cin >> op;
if (op == 'q') {
break;
}
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '^' || op == '%') {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
// Perform the requested operation
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
case '^':
result = pow(num1, num2);
break;
case '%':
result = fmod(num1, num2);
break;
default:
cerr << "Invalid operation." << endl;
continue;
}
// Print the result
cout << "Result: " << result << endl;
} else {
cerr << "Invalid operation." << endl;
}
}
return 0;
}