-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
161 lines (147 loc) · 3.06 KB
/
Copy pathCalculator.cpp
File metadata and controls
161 lines (147 loc) · 3.06 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
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
#include "Calculator.h"
#include "Check.h"
Calculator::Calculator()
{
polishNotation = new PolishNotation();
}
void Calculator::SetExpression(std::string expression)
{
polishNotation->CleanStr();
std::string revPolNot = polishNotation->DoRevPolNot(expression);
Calculate(revPolNot);
}
Calculator::~Calculator()
{
delete polishNotation;
}
// calculation expression for Polish Notation
void Calculator::Calculate(std::string revPolNot)
{
// parsing the string at whitespace
std::vector<std::string> vectorStr;
std::istringstream iss(revPolNot);
std::string temp;
while (iss >> temp)
{
vectorStr.push_back(temp); // get vector with elements without whitespace
}
CheckDots(vectorStr);
for (size_t i = 0; i < vectorStr.size(); i++)
{
try
{
float temp = stof(vectorStr.at(i));
stackFloat.push(temp);
}
catch (...)
{
if (Check::IsOperator(vectorStr.at(i).at(0)))
{
std::string a = vectorStr.at(i);
GetOperator(vectorStr.at(i));
}
}
}
// if the stack have one element, this is answer
if (stackFloat.size() == 1)
{
answer = stackFloat.top();
stackFloat.pop();
}
else
{
while (!stackFloat.empty())
{
stackFloat.pop();
}
throw (Exception("not enough operators!"));
}
}
// get operator in the float
void Calculator::GetOperator(std::string str)
{
int encodedData = (int)(str.at(0)); // ascii code of elem
try
{
switch (encodedData)
{
case 42: // " * "
{
float a, b;
PopStack(b);
PopStack(a);
stackFloat.push(a*b);
break;
}
case 43:// " + "
{
float a, b;
PopStack(b);
PopStack(a);
stackFloat.push(a + b);
break;
}
case 45:// " - "
{
float a, b;
PopStack(b);
PopStack(a);
stackFloat.push(a - b);
break;
}
case 47:// " / "
{
float a, b;
PopStack(b);
PopStack(a);
if (b == 0) throw (Exception("divide by 0 is forbidden!"));
else stackFloat.push(a / b);
break;
}
default:
throw (Exception("missing an closing bracket"));
}
}
catch (Exception& e)
{
throw (Exception(e.what()));
}
}
// pulls out a stack element
void Calculator::PopStack(float& num)
{
if (!stackFloat.empty())
{
num = stackFloat.top();
stackFloat.pop();
}
else
{
throw (Exception("not enough operands!"));
}
}
// error checking with dots / commas
void Calculator::CheckDots(std::vector<std::string>& vectorStr)
{
int counter;
size_t pos; // deleted item position
for (size_t i = 0; i < vectorStr.size(); i++)
{
counter = 0;
// replase the first "," on "."
if ((pos = vectorStr.at(i).find(",")) != vectorStr.at(i).npos)
vectorStr.at(i).replace(pos, 1, ".");
// if we found "," or "." we increase the counter
for (size_t j = 0; j < vectorStr.at(i).length(); j++)
if (vectorStr.at(i).at(j) == '.' || vectorStr.at(i).at(j) == ',')
{
counter++;
if (counter > 1) throw (Exception("incorrect expression"));
}
}
}
// the output answer
float Calculator::GetAnswer()
{
return answer;
}