-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment2Classesdraft.cpp
400 lines (333 loc) · 15 KB
/
Assignment2Classesdraft.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include <map>
#include <cmath>
#include <sstream>
using namespace std;
class Calculator //Class calculator
{
public:
char c = ' '; //Char the tracks the entered keyboard input
char op = '?'; //Char that tracks what operator is being applied
string calcDisplay = ""; //String that is put onto the calculators display
string currentCalc = ""; //String to keep track of working equation
vector<string> sVector; //Vector to hold each equation as a string
map<string, string> myMap; //Map to store memory
//Memory function, adds given key value pair into calculator map.
string memoryFunction(char storeRetrieve, char memKey, string memVal)
{
string key = "M";
key = key + memKey; //Add number entered into key string
switch (storeRetrieve)
{
case 'M': //If user entered char M (for storing to memory)
myMap[key] = memVal; //Set key memKey to memVal
return memVal; //Return memVal (this is so that our calculator display doesn't change when you store a value to memory)
case 'R': //If user entered char R (for retrieving from memory)
return myMap[key]; //Return value in key memKey
default: //If neither work report error
cout << "Memory function Error" << endl;
return "";
}
}
//Function that controls how the calculator display reacts to key presses. Takes char as keypress
void displaySequence(char c)
{
c = (toupper(c)); //Convert lowercase inputs to uppercase
if (c == '\t' || c == '\n' || c == '\r' || c == ' ') return; //Continue if any whitespace is entered
if (c == '\b') //If backspace is entered remove last char in string calcDisplay
{
if (calcDisplay == "") return; //Avoid crash if s is already empty
calcDisplay.pop_back(); //Remove last char in string s and continue if backspace is entered
op = '?';
}
if ((c == '+' || c == '-' || c == '*' || c == '/' || c == 'M') && calcDisplay != "") //Turn operator display into user entered operator by changing char op into entered char
{
op = c;
}
if (c == 'R') //If user entered R (access memory), we have to handle if they also are adding the number in memory to the number on the display
{
switch (op)
{
case '?': case 'M': //If operator is ? (blank) or M (save to memory)
op = c; //Set operator to 'R'
break;
default: //If operator is anything else (+ - / *)
currentCalc += (calcDisplay + op); //Add display number and correspoding operator to our currentCalc string
op = c; //Set our operator to R
calcDisplay = ""; //Wipe display num
break;
}
}
if (c == 'C') { currentCalc = ""; calcDisplay = ""; } //If c is entered, clear display and the calculation that has been building
if (c == 'K') { calcDisplay = ""; } //If k is entered, only clear the display
if (c == 'S' && calcDisplay != "")calcDisplay = to_string(sqrt(stod(calcDisplay))); //Square root method !note i probably should make this 2dp, but i also think it's worth keeping more precision for sqrt
if (c == '!' && calcDisplay != "")calcDisplay = to_string(stod(calcDisplay) * -1); //Negate method !same as above but i am lazy
if ((op == 'M' || op == 'R') && (c >= '0' && c <= '9')) //If M or R (memory and retrive from memory) are the current operator, AND the next key pressed is a number
{
calcDisplay = memoryFunction(op, c, calcDisplay); //Send the operator (m or r), the char (0-9) and the current calue of display to memoryFunction()
op = '?'; //Set operator back to ?
c = ' '; //Set char entered to ' '
}
//Adds user entered number into display
if (c >= '0' && c <= '9' || c == '.') //If user inputs a number (or decimal)
{
if (op != '?' && op != 'M' && op != 'R') //If operator isn't ? M or R
{
currentCalc += (calcDisplay + op); //Add numbers on display and the operator into currentCalc string
op = '?';
calcDisplay = "";
}
int displayLen = calcDisplay.length(); //Set displaylength to equal length of calcDispaly string as int
if (displayLen <= 9) calcDisplay = calcDisplay + c; //Append entered char into string if the displaylength doesn't exceed 9 characters
}
//If user enters =, calculate the working equation and add it to display
if (c == '=' && op == '?' && calcDisplay != "")
{
currentCalc += (calcDisplay + c); //Add display into working equation
sVector.push_back(currentCalc); //Store string of working equation into a new element in sVector
currentCalc = ""; //Reset the working calculation
ostringstream streamObj3; //Create a stream object
streamObj3 << std::fixed; //Set precision to fixed
streamObj3 << std::setprecision(2); //Set precision to 2DP
streamObj3 << calcSequence(sVector); //Send vector of strings to calculation function. Feed input into streamObj
string strObj3 = streamObj3.str(); //Declare new string as streamObj3 converted to string
calcDisplay = strObj3; //Set calculator display as answer from calculation with 2DP
}
//Display formatting
//If user has stored anything, display storage
if (!myMap.empty())
{
cout << "Memory Values" << endl;
cout << "*-----------------*" << endl;
map<string, string>::iterator iter; //Construct an iterator
for (iter = myMap.begin(); iter != myMap.end(); ++iter) //Loop through each key-value pair in map
{
cout << "| " << iter->first << " | " << setw(10) << iter->second << " | " << '\n';
cout << "*-----------------*" << endl;
}
}
//Show user display and working calculations and operator
cout << currentCalc << endl;
cout << "______________________" << endl;
cout << "|" << setw(20) << calcDisplay << "|" << " (" << op << ")" << " <- Operator" << endl;
cout << "----------------------" << endl;
cout << endl;
}
//Function for running the caluclator using a given vector of strings
void inputSequence(vector<string> sequence)
{
//For each line, for each character in that line, run calculator inputting that char
for (int i = 0; i < sequence.size(); i++)
{
for (char const& c : sequence[i])
{
if (c == ';') return; //if ; is entered then return
cout << "Key entered: [" << c << "]" << endl;
cout << endl;
displaySequence(c);
}
}
}
//Function for running the calculator using user inputs
void createSequence()
{
//Show instance of display so user knows they can begin
cout << "______________________" << endl;
cout << "|" << setw(20) << "|" << " (" << '?' << ")" << " <- Operator" << endl;
cout << "----------------------" << endl;
cout << endl;
while (c != ';') //Exit on ;
{
c = _getch(); //Store keystroke into c
displaySequence(c); //Send keystroke into calculator function
}
return;
}
//Function that takes a vector of strings, where each element is a string representing an equation. Returns the result of running all equations line by line (this allows run on equations)
double calcSequence(vector<string> sequence)
{
double result = 0;
vector<double> sequenceResult; //Vector to store result of each line
//First, format vector of strings, where each element in the vector represents a mathematical expression
for (int i = 0; i < sequence.size(); i++) //For each equation in our sequence
{
vector<double> terms; //Vector of terms
vector<double> multiplier; //Vector of multipliers
vector<char> operations; //vector of operations
string tempNum = ""; //String to hold temp numbers
int o = -1; //iterator counter
for (char const& c : sequence[i])
{
o++;
//If the first char in the string is -, add that to operation list and continue
if (c == '-' && o == 0)
{
operations.push_back(c);
continue;
}
if (c != '+' && o == 0) { operations.push_back('+'); } //If first char is + add it to operation list (doesn't continue)
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '=') //If any operator is pressed
{
terms.push_back(stod(tempNum)); //Push tempNum into vector of terms
if (c != '=') operations.push_back(c); //Push our operator into vector of operator (unless it's =)
tempNum = ""; //Reset temp num
continue;
}
//Adds char to tempNum to build a number longer than 1 character
if (c >= '0' && c <= '9' || c == '.') //if c is a number)
{
tempNum += c;
continue;
}
else //If character isn't accepted previously to this than it must be invalid for our program
{
cout << "invalid character has been found: " << c << " ";
break;
}
}
//Second, take a line and find out it's result.
vector<double> newTerms; //Vector to store new terms, any terms that come after a multiplier will NOT be stored into this vector
//Iterates through each element (number) in vector of terms, if the corrosponding operation for that number is a + or - then add it into list of new terms
//then add a multiplier of 1 for that term into our vector of multipliers
//If the corresponding oepration is * or / then we take that term, DON'T add it to new terms, but instead multiply the corresponding multiplier in our multiplier vector by that term
//This thing is maybe stupid and complicated but it means i follow bodmas!!!!!
for (int a = 0; a < terms.size(); a++)
{
if (operations[a] == '+')
{
multiplier.push_back(1);
newTerms.push_back(terms[a]);
}
if (operations[a] == '-')
{
multiplier.push_back(-1);
newTerms.push_back(terms[a]);
}
if (operations[a] == '*')
{
multiplier.back() = multiplier.back() * terms[a];
}
if (operations[a] == '/')
{
multiplier.back() = multiplier.back() / terms[a];
}
}
sequenceResult.push_back(0); //Create a new element in sequence of result starting at 0
for (int z = 0; z < newTerms.size(); z++) //For each term in newTerms
{
sequenceResult[i] += newTerms[z] * multiplier[z]; //Take the term, multiply it by it's multilier (meaning we follow bodmas!) then add it our curret result element
}
}
result = sequenceResult[sequenceResult.size() - 1]; //result = the result of the last sequence
return result;
}
//Reads a txt file and returns vector of strings where each line is an equation
vector<string> readSequence(string fileName)
{
vector<string>sequence;
fstream myFile; //Declare variable as fstream
myFile.open(fileName, ios::in); //Open file in read mode
if (myFile.is_open())
{
string line; //Store each line into a string
while (getline(myFile, line))
{
sequence.push_back(line); //Add that line into new element in vector
}
myFile.close();
}
else
{
cout << "Failed to open file!" << endl;
cout << endl;
}
return sequence; //Return vector of lines
}
};
class Directory
{
public:
//File handling code from module
#define DEFDIR "E:\\A_ST2\\ST2_2022\\Assignment2\\"
string dir = DEFDIR;//
string filename;
string fullFileName;
bool getFileName()
{
cout << "enter filename:";
cin >> filename;
cout << endl;
fullFileName = dir + filename;
cout << "Full FileName is:'" << fullFileName << "'" << endl;
if (fileExists(fullFileName)) return true;
cout << "** ERROR ** File '" << fullFileName << "'Does not exist";
return false;
}
bool fileExists(string fullFileName)
{
fstream myFile;
myFile.open(fullFileName, ios::in);
if (myFile.is_open())
{
myFile.close();
return true;
}
return false;
}
void do_S()
{
cout << "enter directory:";
cin >> dir;
cout << endl;
cout << "\nCurrent Directory :" << dir << endl;
}
void do_R()
{
cout << "\nCurrent Directory :" << dir << endl;
if (!getFileName()) return;
}
};
int main()
{
char select = '0';
vector<string> sequence;
Calculator calc;
Directory dir;
while (select != 'E' && select != 'e')
{
//Reset calculator
calc.c = ' ';
calc.op = '?';
calc.calcDisplay = "";
calc.currentCalc = "";
calc.sVector.clear();
calc.myMap.clear();
cout << "Select one of the following options:" << endl;
cout << "E - Exit \nS - Set Directory \n4 - Run 4 function calculator using keyboard input \nR - Read a test file and run test" << endl;
cin >> select;
switch (select) {
case '4':
calc.createSequence();
break;
case 'R':
dir.do_R();
calc.inputSequence(calc.readSequence(dir.fullFileName));
break;
case 'E': case 'e':
return 0;
case 'C': case 'c': //might add a controls menu?
break;
case 'S': case 's':
dir.do_S();
break;
default:
break;
}
}
return 0;
}