-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecution.h
112 lines (92 loc) · 2 KB
/
Execution.h
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
#ifndef TOPCODER_EXECUTION_H
#define TOPCODER_EXECUTION_H
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
class Execution
{
public:
long long analyze(vector <string> code);
private:
long long eval(string s);
long long getNum(string &s);
};
long long Execution::analyze(vector<string> code)
{
long long count = 0;
string s = "";
for (auto line : code)
{
s += line;
}
count = eval(s);
return count;
}
inline long long Execution::getNum(string & s)
{
long long digit = 0;
bool done = false;
while (!done)
{
if (!isdigit(s[0]))
{
if (digit != 0)
{
done = true;
}
s.erase(0, 1);
}
else
{
if (0 == digit && s[0] == '0')
{
s.erase(0, 1);
return 0;
}
digit *= 10;
digit += s[0] - '0';
s.erase(0, 1);
}
}
return digit;
}
inline long long Execution::eval(string s)
{
long long count = 0;
while (!s.empty())
{
if (s.substr(0, 3).compare("for") == 0)
{
long long digit = getNum(s);
bool found = false;
long long end = s.find_first_of('{');
long long braces = 0;
while (!found)
{
if (s[end] == '{')
++braces;
else if (s[end] == '}')
--braces;
if (0 == braces)
found = true;
++end;
}
long long result = eval(s.substr(1, end - 1));
result *= digit;
count += result;
s.erase(0, end);
}
else if (s.substr(0, 5).compare("BASIC") == 0)
{
++count;
s.erase(0, 5);
}
else
{
s.erase(0, 1);
}
}
return count;
}
#endif TOPCODER_EXECUTION_H