-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
72 lines (59 loc) · 1.63 KB
/
main.cc
File metadata and controls
72 lines (59 loc) · 1.63 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
/*
Name: Calculator
Author: David Lange
Date: 25.07.2024
Description: Simple one file calculator program to add, subtract, multiply and divide user input
*/
#include <iostream>
#include <string>
#include <cctype>
#include <limits>
using namespace std;
bool isNumber(string s) {
for(int i = 0; i < s.length(); ++i) {
if( !isdigit(s[i]) ) {
return false;
}
}
return true;
}
bool isOperator(string s) {
if(s == "+" || s == "-" || s == "*" || s == "/") {
return true;
}
return false;
}
double calculate(double a, double b, char op) {
switch (op) {
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
}
return numeric_limits<double>::quiet_NaN();
}
int main() {
string inputNum1, inputOperator, inputNum2 = "";
double num1, num2, result;
cout << "Basic Calculator. Type q to quit" << endl;
while(true) {
cin >> inputNum1 >> inputOperator >> inputNum2;
if(inputNum1 == "q" || inputNum1 == "quit" || inputNum2 == "q" || inputNum2 == "quit" || inputOperator == "q" || inputOperator == "quit") {
return 1;
}
// cout << inputNum1 << ' ' << inputNum2 << ' ' << inputOperator << endl;
// cout << isNumber(inputNum1) << ' ' << isNumber(inputNum2) << ' ' << isOperator(inputOperator) << endl;
if(!isNumber(inputNum1) || !isNumber(inputNum2) || !isOperator(inputOperator)) {
cout << "Please enter valid numbers and operators (+ - * /)" << endl;
continue;
}
num1 = stod(inputNum1);
num2 = stod(inputNum2);
result = calculate(num1, num2, inputOperator[0]);
cout << result << endl;
}
}