-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
124 lines (121 loc) · 3.67 KB
/
main.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
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
#include <iostream>
#include <fstream>
#include <vector>
#include "LN.hpp"
using namespace std;
int printError(int r, const char *str) {
cout << str << endl;
return r;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
return printError(1, "Incorrect input arguments.\n"
"This program expect launching with 2 arguments: *name* input output\n"
"Where:\n"
" input - name of input file\n"
" output - name of output file");
}
vector<LN> st = vector<LN>();
ifstream input = ifstream(argv[1]);
if (!input.is_open() || !input.good()) {
return printError(1, "Error occurred while opening input file.");
}
//work with Long Numbers
try {
string in;
unsigned long size = 0;
while (input >> in) {
switch (in.at(0)) {
case '+':
st[size - 2] += st[size - 1];
size--;
st.pop_back();
break;
case '*':
st[size - 2] *= st[size - 1];
size--;
st.pop_back();
break;
case '/':
st[size - 2] /= st[size - 1];
size--;
st.pop_back();
break;
case '%':
st[size - 2] %= st[size - 1];
size--;
st.pop_back();
break;
case '~':
st[size - 1] = ~st[size - 1];
break;
case '_':
st[size - 1].changeSign();
break;
case '<':
if (in.length() == 2) {
st[size - 2] = st[size - 2] <= st[size - 1];
} else {
st[size - 2] = st[size - 2] < st[size - 1];
}
st.pop_back();
size--;
break;
case '>':
if (in.length() == 2) {
st[size - 2] = st[size - 2] >= st[size - 1];
} else {
st[size - 2] = st[size - 2] > st[size - 1];
}
st.pop_back();
size--;
break;
case '=':
st[size - 2] = st[size - 2] == st[size - 1];
st.pop_back();
size--;
break;
case '!':
st[size - 2] = st[size - 2] != st[size - 1];
st.pop_back();
size--;
break;
case '-':
if (in.length() == 1) {
st[size - 2] -= st[size - 1];
size--;
st.pop_back();
break;
}
default:
st.emplace_back(LN(in));
size++;
break;
}
}
} catch (exception &exception) {
cout << "Error: " << exception.what() << endl;
input.close();
if (input.bad()) {
return printError(2, "Error occurred while closing input file.");
}
return 2;
}
input.close();
if (input.bad()) {
return printError(2, "Error occurred while closing input file.");
}
//stack dump
ofstream output = ofstream(argv[2]);
if (!output.is_open() || !output.good()) {
return printError(1, "Error occurred while opening output file.");
}
for (const LN &a : st) {
output << (string) a << endl;
}
output.close();
if (output.bad()) {
return printError(2, "Error occurred while closing output file.");
}
return 0;
}