-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.cpp
170 lines (163 loc) · 6.64 KB
/
calculator.cpp
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
168
169
170
#include "header.h"
//constructs history class 'hist'
history hist = history();
//Assigns values to be used by the calculator.
double assigner(string input, int index, int type, int previous_value){
double value;
if (type == 0 || type == 2){
/*Try catch is still an ugly function.
Half the reason I wrote this method is so I wouldn't need to look at it unless I needed to.*/
try{
value = stoi(input.substr(0, index));
} catch (invalid_argument const &e){
if (type == 2) value = 2;
else value = previous_value;
}
} else {
try{
if (type == 1) value = stoi(input.substr(index+1, input.length()));
else value = stoi(input.substr(index+4, input.length()));
} catch (invalid_argument const &e){
value = previous_value;
}
}
return value;
}
//clears whitespace from user input so spaces won't break the machine
string stripper(string unstripped){
string stripped;
int head = unstripped.length();
for (int i = 0; i < head; i++){
if(unstripped[i] != ' ') stripped += unstripped[i];
}
return stripped;
}
/*See below. The only difference is that, true to the name, this one handles complex inputs.
Those require more than one letter, so it first verifies that the input is valid.
To be honest, there's probably a much better way to do this.*/
double complex_operation_switch(int previous_value, string input, int i){
switch(tolower(input[i])){
case 'h':
if(input.substr(i+1, 3) == "ist") previous_value = hist.display_history();
else if(input.substr(i+1, 3) == "elp") hist.help_menu();
break;
case 'r':
//Side note: C++ substrings are different from java ones.
//They don't go from x to y, they go from x and read to the length of y.
if(input.substr(i+1, 3) == "oot"){
hist.commit_to_history(input);
int index = assigner(input, i, 2, previous_value);
int radicand = assigner(input, i, 3, previous_value);
previous_value = take_root(index, radicand);
if (previous_value == -1) cout << "Error! Imaginary value!" "\n";
else cout << previous_value << ".\n";
}
break;
}
return previous_value;
}
/*Reads for a SPECIFIC operator and runs the appropriate function.
The functions have return types so the result
can be used as a temporary history value.
I should probably reduce the number of arguments I'm passing here, at least if I can.*/
double standard_operation_switch(double previous_value, char input, int first_number, int second_number){
switch(input){
case '+':
previous_value = addition(first_number,second_number);
cout << previous_value << ".\n";
break;
case '-':
previous_value = subtraction(first_number,second_number);
cout << previous_value << ".\n";
break;
case '*':
previous_value = multiplication(first_number,second_number);
cout << previous_value << ".\n";
break;
case '/':
previous_value = division(first_number, second_number);
if(previous_value == INT_MIN){
cout << "Error: Undefined! Cannot Divide by Zero!" << "\n";
previous_value = 0;
} else cout << previous_value << ".\n";
break;
case '^':
previous_value = exponent(first_number, second_number);
cout << previous_value << ".\n";
break;
case '=':
comparator(first_number, second_number);
break;
}
return previous_value;
}
//This searches the entire input for some kind of operator.
double operator_finder(string input, double previous_value){
string operator_list = "+-*/^=hr&";
for (int i = 0; i < input.length(); i++){
bool breaker = false;
/*if a valid operator is found, the calculator commits the equation to history and gets to work
This is just as ugly as the equivalent if statement, but at least I don't feel like yanderedev*/
switch(tolower(input[i])){
case '-':
/*This is how I handle leading negatives.
If - is the first value, check and see if any of the values proceeding are an operator.
If so, break. */
if (i == 0){
for (int j = 2; j < input.length(); j++){
for(int k = 0; k < operator_list.length(); k++){
if (input[j] == operator_list[k]){
breaker = true;
}
}
}
}
if(breaker){
break;
}
case '+':
case '*':
case '/':
case '^':
case '=':
hist.commit_to_history(input);
previous_value = standard_operation_switch(previous_value, input[i],
assigner(input, i, 0, previous_value), assigner(input, i, 1, previous_value));
return previous_value;
/*side note: I honest to god had a near panic attack
because I thought the previous value function completely stopped working.
Lo and behold I just forgot to put a fucking break*/
break;
case 'h':
case 'r':
previous_value = complex_operation_switch(previous_value, input, i);
return previous_value;
break;
/* This is just so I can exit the loop,
because the terminal doesn't have a big red x button. I'll comment this
out for release. This is an absolute hack but if it works, it works. */
case '&':
return INT_MAX;
break;
}
}
return previous_value;
}
//Main function, asks for and recieves input.
int main(){
hist.clear_history();
double previous_value = 0;
string input_unstripped;
cout << "Skullculator v1.0" << "\n";
while(previous_value != INT_MAX){
cout << "." << "\n" << "." << "\n";
cout << "Please enter an equation or type 'help' to view available list: " << "\n";
getline(cin, input_unstripped);
string input_stripped = stripper(input_unstripped);
previous_value = operator_finder(input_stripped, previous_value);
//This makes it so & can exit the program properly
if (previous_value == INT_MAX){
return 0;
}
}
}