-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.cpp
66 lines (54 loc) · 1.35 KB
/
calculate.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
#include "calculate.h"
std::string calculate::getAnswer(std::vector<std::string> input)
{
std::string send;
send = doCalculations(input);
return send;
}
std::string calculate::doCalculations(std::vector<std::string> input)
{
std::stack<mixedNumber> mixed;
std::stack<char> ops;
for(int i = 0; i < input.size(); i ++)
{
std::string check = input[i];
char next = check[0];
if(isdigit(next) || (next == '-' && check.size() > 1))
{
std::stringstream ss;
ss<<check;
mixedNumber A;
ss>>A;
mixed.push(A);
}
else if(next == '+' || next == '-' || next == '*' || next == '/')
{
mixedNumber B = mixed.top();
mixed.pop();
mixedNumber A = mixed.top();
mixed.pop();
switch(next)
{
case '+':
A = A + B;
break;
case '-':
A = A - B;
break;
case '*':
A = A * B;
break;
case '/':
A = A / B;
break;
}
mixed.push(A);
}
}
mixedNumber give = mixed.top();
std::string send;
std::stringstream ss;
ss<<give;
getline(ss, send);
return send;
}