Skip to content

Commit 419d53b

Browse files
committed
Calculator
1 parent 57eb1f1 commit 419d53b

1 file changed

Lines changed: 46 additions & 44 deletions

File tree

src/main/java/algorithms/sprint2/Calculator.java

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
/*
1111
Принцип:
1212
- Читаем токены ОПН слева направо.
13-
- Число -> push в стек.
14-
- Операция -> pop b, pop a, считаем a op b, push результат.
13+
- Если считанный символ - Число -> push в стек.
14+
- Если считанный символ - Математическая Операция (+-*\) -> pop b, pop a, считаем a op b, push результат.
1515
- Ответ: верх стека.
1616
1717
Корректность:
@@ -21,8 +21,10 @@
2121
2222
Сложность:
2323
- Время O(m), m — число токенов.
24-
- Память O(s), s — максимальный размер стека (в худшем O(m)).
24+
- Память - в худшем O(m)
2525
*/
26+
27+
2628
public class Calculator {
2729

2830
private static int eval(FastIn in) throws IOException {
@@ -64,6 +66,47 @@ private static int eval(FastIn in) throws IOException {
6466
return st.peek();
6567
}
6668

69+
private static void run() throws Exception {
70+
FastIn in = new FastIn(System.in);
71+
FastOut out = new FastOut(System.out);
72+
73+
int ans = eval(in);
74+
75+
out.writeInt(ans);
76+
out.writeByte('\n');
77+
out.flush();
78+
}
79+
80+
// -------------------- LOCAL TESTS --------------------
81+
private static void test() throws Exception {
82+
assertEq(9, evalFromString("2 1 + 3 *"));
83+
assertEq(38, evalFromString("7 2 + 4 * 2 +"));
84+
assertEq(-1, evalFromString("-1 3 /"));
85+
assertEq(-2, evalFromString("-4 3 /"));
86+
assertEq(2, evalFromString("10 2 4 * -"));
87+
assertEq(5, evalFromString("5"));
88+
System.out.println("Test OK");
89+
}
90+
91+
private static int evalFromString(String s) throws Exception {
92+
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
93+
return eval(new FastIn(is));
94+
}
95+
96+
private static void assertEq(int exp, int act) {
97+
if (exp != act) {
98+
throw new AssertionError("Expected=" + exp + ", actual=" + act);
99+
}
100+
}
101+
102+
public static void main(String[] args) throws Exception {
103+
if (System.getProperty("os.name").startsWith("Windows")) {
104+
test();
105+
} else {
106+
run();
107+
}
108+
}
109+
67110
// -------------------- FAST INPUT --------------------
68111
static final class FastIn {
69112
private final InputStream in;
@@ -159,45 +202,4 @@ void flush() throws IOException {
159202
p = 0;
160203
}
161204
}
162-
163-
private static void run() throws Exception {
164-
FastIn in = new FastIn(System.in);
165-
FastOut out = new FastOut(System.out);
166-
167-
int ans = eval(in);
168-
169-
out.writeInt(ans);
170-
out.writeByte('\n');
171-
out.flush();
172-
}
173-
174-
// -------------------- LOCAL TESTS --------------------
175-
private static void test() throws Exception {
176-
assertEq(9, evalFromString("2 1 + 3 *"));
177-
assertEq(38, evalFromString("7 2 + 4 * 2 +"));
178-
assertEq(-1, evalFromString("-1 3 /"));
179-
assertEq(-2, evalFromString("-4 3 /"));
180-
assertEq(2, evalFromString("10 2 4 * -"));
181-
assertEq(5, evalFromString("5"));
182-
System.out.println("Test OK");
183-
}
184-
185-
private static int evalFromString(String s) throws Exception {
186-
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
187-
return eval(new FastIn(is));
188-
}
189-
190-
private static void assertEq(int exp, int act) {
191-
if (exp != act) {
192-
throw new AssertionError("Expected=" + exp + ", actual=" + act);
193-
}
194-
}
195-
196-
public static void main(String[] args) throws Exception {
197-
if (System.getProperty("os.name").startsWith("Windows")) {
198-
test();
199-
} else {
200-
run();
201-
}
202-
}
203205
}

0 commit comments

Comments
 (0)