-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.cpp
More file actions
86 lines (74 loc) · 2.07 KB
/
Copy pathCalc.cpp
File metadata and controls
86 lines (74 loc) · 2.07 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
#include <iostream>
#include "Calc.h"
#include "stddef.h"
#include <cmath>
Calc::Calc(int values):storedExpressions(new std::string[values]), stored(0){
}
Calc::Calc(const Calc &rhs) {
if(storedExpressions != nullptr){
storedExpressions = new std::string[rhs.stored];
}
}
Calc::~Calc() {
delete[] storedExpressions;
storedExpressions = nullptr;
stored = 0;
}
const Calc &Calc::operator=(const Calc &rhs) {
if(this != &rhs){
Calc temp(rhs);
std::swap(storedExpressions, temp.storedExpressions);
std::swap(stored, temp.stored);
}
return *this;
}
double Calc::process(std::string userInput, double prevValue) {
//First cleanse
std::string cleansedString = "";
for(size_t i = 0; i < userInput.size(); ++i) {
if (userInput[i] == '=' || userInput[i] == ' ' || userInput[i] == '(' || userInput[i] == ')') {}
else {
cleansedString += userInput[i];
}
}
double value = std::stod(cleansedString.substr(1, cleansedString.length() - 1));
if(cleansedString[0] == '^'){
return pow(prevValue, value);
}
else if(cleansedString[0] == '*' || cleansedString[0] == '/'){
if(cleansedString[0] == '*'){
return prevValue * value;
}
else{
if(value != 0){
std::exit(0);
}
}
}
else if(cleansedString[0] == '+' || cleansedString[0] == '-'){
if(cleansedString[0] == '+'){
return prevValue + value;
}
else{
return prevValue - value;
}
}
else{
std::cout << "ERROR: Must start with operator " << std::endl;
return -10000000;
}
return -10000000;
}
void Calc::print() {
std::cout << "You have " << stored << " expressions stored: ";
for(size_t i = 0; i < stored; ++i){
std::cout << "Expression " << i+1 << " result is:\n";
std::cout << storedExpressions[i] << std::endl;
}
}
void Calc::setStored(size_t newStored) {
stored = newStored;
}
size_t Calc::getStored() {
return stored;
}