-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
91 lines (82 loc) · 1.6 KB
/
main.cpp
File metadata and controls
91 lines (82 loc) · 1.6 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
#include <iostream>
#include <string>
#include <vector>
#include "getch.h"
using namespace std;
bool canUse(char c)
{
if ( (c > 46 && c < 58) || c == 32 || c == '\n' || c == 42 || c == 43 || c == 45 || c == 47 || c == 61) return true;
return false;
}
int eval(int num1,int num2,char op)
{
if (op == 42)
return num1 * num2;
if (op == 43)
return num1 + num2;
if (op == 45)
return num1 - num2;
if (op == 47)
return num1 / num2;
return 0;
}
int main()
{
string s;
vector <int> numbers;
vector <char> operations;
string tempNum = "";
while (true)
{
char c = getch();
if (c == 27) break;
if (c == '\n')
{
cout << c;
continue;
}
if (canUse(c))
{
if (c == 61)
{
cout << "= ";
for (char c2 : s)
{
if (c2 == 32) continue;
if (c2 > 47 && c2 < 58)
{
tempNum += c2;
continue;
}
if (tempNum == "")
{
operations.pop_back();
operations.push_back(c2);
} else
{
numbers.push_back(stoi(tempNum));
operations.push_back(c2);
tempNum = "";
}
}
numbers.push_back(stoi(tempNum));
int out = numbers[0];
for (int i = 1; i < numbers.size(); i ++)
{
out = eval(out, numbers[i], operations[i - 1]);
}
cout << out;
s = "";
tempNum = "";
operations.clear();
numbers.clear();
}
else
{
s += c;
cout << c;
}
}
}
return 0;
}