-
Notifications
You must be signed in to change notification settings - Fork 13
/
calculator.cpp
64 lines (59 loc) · 1.93 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
#include <bits/stdc++.h>
typedef enum { NUMS, OPS, EVS } MODS;
int main()
{
std::vector<std::string> inputs{ ++std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>() };
std::string cur;
double mem { 0 }, useless;
char op { ' ' };
MODS mode { NUMS };
bool start{ true };
auto ops = [&op](double a, double b) {
switch (op) {
case '+' : return a + b;
case '-' : return a - b;
case 'x' : return a * b;
case '/' : return a / b;
default : return b;
}
};
for ( const auto& c : inputs ) {
if ( std::isdigit(c[0]) ) {
if ( EVS == mode ) { mem = 0, cur.clear(), op = ' '; }
if ( start ) { start = false; cur.clear(); }
mode = NUMS;
cur += c;
std::cout << cur << '\n';
continue;
}
else if ( "AC" == c ) {
mem = 0;
op = ' ';
mode = NUMS;
start = true;
cur.clear();
}
else if ( "=" == c ) {
mem = ops(mem, std::stod(cur));
mode = EVS;
start = true;
}
else {
if ( NUMS == mode ) { mem = ops(mem, std::stod(cur)); start = true; }
op = c[0];
mode = OPS;
}
if ( 0 == std::modf(mem, &useless) )
std::cout << mem << '\n';
else {
std::string ret = std::to_string( round( mem * 1000.0 ) / 1000.0 );
if ( auto it = std::find( std::begin(ret), std::end(ret), '.'); std::end(ret) != it ) {
ret.erase(it + 4, std::end(ret));
if ( size_t end = ret.find_last_not_of('0'); std::string::npos != end )
ret = ret.substr(0, end + 1);
std::cout << ret << '\n';
}
}
}
}