-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab3.cpp
More file actions
103 lines (87 loc) · 2.41 KB
/
lab3.cpp
File metadata and controls
103 lines (87 loc) · 2.41 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
/*Author: Autumn Henderson
Date: September 11th, 2018
Description: This program allows the user to enter operands and an operator and will perform
the operation.
*/
#include <iostream>
#include <string>
using namespace std;
int main () {
//Declared Variables
double leftOperand, rightOperand, result;
char operation;
bool noAbsResult = false;
bool noResult = false;
string otherOperations = "Aa+Ss-Mm*Dd/";
bool invldOperand = false;
cout << "Enter left operand: ";
if (cin >> leftOperand) { //Validates left operand
cout << "Enter operation: ";
cin >> operation;
switch (operation) { //Checks for absolute value operand
case 'B':
case 'b':
case '|':
if (leftOperand < 0) { //Performs absolute value without calling function
result = leftOperand * (-1);
}
else {
result = leftOperand;
}
break;
default:
noAbsResult = true; //Tells us absolute value is not being performed.
break;
}
//Checks if absolute value was not performed and the operator does not match the rest
if ((otherOperations.find(operation) == string::npos) && (noAbsResult == true)) {
cout << "Unknown operation.\n";
invldOperand = true;
}
//Will enter if result was not assigned because it was not absolute value.
//Asks for right operand and performs the rest of the operations.
if ((noAbsResult == true) && (invldOperand != true)) {
cout << "Enter right operand: ";
if (cin >> rightOperand) { //Checks for operations
switch (operation) {
case 'A':
case 'a':
case '+':
result = leftOperand + rightOperand;
break;
case 'S':
case 's':
case '-':
result = leftOperand - rightOperand;
break;
case 'M':
case 'm':
case '*':
result = leftOperand * rightOperand;
break;
case 'D':
case 'd':
case '/':
result = leftOperand / rightOperand;
break;
default:
noResult = true;
break;
}
}
else { //Hits this statement if right operand was not correct
cout << "Invalid right operand.\n";
noResult = true;
}
}
}
else { //Hits this statement if left operand was not correct
cout << "Invalid left operand.\n";
noResult = true;
}
//Prints out result if a result was obtained and noResult was never assigned "true"
if ((noResult != true && invldOperand !=true)) {
cout << "Result = " << result << "\n";
}
return 0;
}