Skip to content

Commit e1acadd

Browse files
committed
lab2 update
1 parent d003d89 commit e1acadd

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ add_subdirectory(LibraryCPP)
1515
#add_subdirectory(LibraryCPPClass)
1616
#add_subdirectory(LibraryCPPTemplate)
1717

18-
add_subdirectory(Lab1CPP)
18+
add_subdirectory(Lab2CPP)

Lab2CPP/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(Lab2CPP lab2.cpp)
2+
target_include_directories(Lab2CPP PUBLIC ../LibraryCPP)
3+
target_link_libraries(Lab2CPP LibraryCPP)
4+
5+
add_test(NAME TestLab2CPP COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/inputt.txt)

Lab2CPP/input3.txt

Whitespace-only changes.

Lab2CPP/inputt.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1+2*3-1
2+
1*(2+3)

Lab2CPP/lab2.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <stdexcept>
4+
#include "stack.h"
5+
#include "vector.h"
6+
#include <string>
7+
8+
int precedence(char op) {
9+
if (op == '+' || op == '-') return 1;
10+
if (op == '*' || op == '/') return 2;
11+
return 0;
12+
}
13+
14+
int apply_operator(int a, int b, char op) {
15+
switch (op) {
16+
case '+': return a + b;
17+
case '-': return a - b;
18+
case '*': return a * b;
19+
case '/':
20+
if (b == 0) throw std::runtime_error("Division by zero");
21+
return a / b;
22+
}
23+
24+
}
25+
26+
int evaluate_example(const std::string& example) {
27+
Stack* values = stack_create();
28+
Stack* operators = stack_create();
29+
30+
for (size_t i = 0; i < example.length(); i++) {
31+
32+
if (example[i] == ' ') continue;
33+
34+
if (isdigit(example[i])) {
35+
int value = 0;
36+
while (i < example.length() && isdigit(example[i])) {
37+
value = value * 10 + (example[i] - '0');
38+
i++;
39+
}
40+
i--;
41+
stack_push(values, value);
42+
}
43+
44+
else if (example[i] == '(') {
45+
stack_push(operators, example[i]);
46+
}
47+
48+
else if (example[i] == ')') {
49+
while (!stack_empty(operators) && stack_get(operators) != '(') {
50+
int val2 = stack_get(values);
51+
stack_pop(values);
52+
int val1 = stack_get(values);
53+
stack_pop(values);
54+
char op = stack_get(operators);
55+
stack_pop(operators);
56+
stack_push(values, apply_operator(val1, val2, op));
57+
}
58+
59+
if (!stack_empty(operators)) {
60+
stack_pop(operators);
61+
}
62+
}
63+
64+
else if (example[i] == '+' || example[i] == '-' || example[i] == '*' || example[i] == '/') {
65+
while (!stack_empty(operators) && precedence(stack_get(operators)) >= precedence(example[i])) {
66+
int val2 = stack_get(values);
67+
stack_pop(values);
68+
int val1 = stack_get(values);
69+
stack_pop(values);
70+
char op = stack_get(operators);
71+
stack_pop(operators);
72+
stack_push(values, apply_operator(val1, val2, op));
73+
}
74+
stack_push(operators, example[i]);
75+
}
76+
}
77+
78+
79+
while (!stack_empty(operators)) {
80+
int val2 = stack_get(values);
81+
stack_pop(values);
82+
int val1 = stack_get(values);
83+
stack_pop(values);
84+
char op = stack_get(operators);
85+
stack_pop(operators);
86+
stack_push(values, apply_operator(val1, val2, op));
87+
}
88+
89+
int result = stack_get(values);
90+
91+
92+
stack_delete(values);
93+
stack_delete(operators);
94+
95+
return result;
96+
}
97+
98+
int main() {
99+
std::ifstream inputFile("inputt.txt");
100+
std::string example;
101+
102+
if (inputFile.is_open()) {
103+
std::getline(inputFile, example);
104+
inputFile.close();
105+
}
106+
int result = evaluate_example(example);
107+
std::cout << "result: " << result << std::endl;
108+
109+
110+
return 0;
111+
}

0 commit comments

Comments
 (0)