Skip to content

Commit

Permalink
lab2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-anastasiia committed Sep 23, 2024
1 parent 569ea9f commit 2bf7d1a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 67 deletions.
5 changes: 0 additions & 5 deletions Lab1C/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions Lab1C/input1.txt

This file was deleted.

40 changes: 0 additions & 40 deletions Lab1C/lab1.c

This file was deleted.

2 changes: 1 addition & 1 deletion Lab2CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ add_executable(Lab2CPP lab2.cpp)
target_include_directories(Lab2CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab2CPP LibraryCPP)

add_test(NAME TestLab2CPP COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/inputt.txt)
add_test(NAME TestLab2CPP COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
File renamed without changes.
47 changes: 33 additions & 14 deletions Lab2CPP/lab2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,35 @@ int evaluate_example(const std::string& example) {
if (stack_empty(values)) throw std::runtime_error("Invalid expression");
int val1 = stack_get(values);
stack_pop(values);
char op = stack_get(operators);
if (stack_empty(operators)) {
throw std::runtime_error("Invalid expression: not enough operators");
}
char op = (char)stack_get(operators);
stack_pop(operators);
stack_push(values, apply_operator(val1, val2, op));
}

if (!stack_empty(operators)) {
if (stack_empty(operators)) {
throw std::runtime_error("Invalid expression: not enough operators");
}
stack_pop(operators);
}

}

else if (example[i] == '+' || example[i] == '-' || example[i] == '*' || example[i] == '/') {
while (!stack_empty(operators) && precedence(stack_get(operators)) >= precedence(example[i])) {
while (!stack_empty(operators) && precedence((char)stack_get(operators)) >= precedence(example[i])) {
if (stack_empty(values)) throw std::runtime_error("Invalid expression");
int val2 = stack_get(values);
stack_pop(values);
if (stack_empty(values)) throw std::runtime_error("Invalid expression");
int val1 = stack_get(values);
stack_pop(values);
char op = stack_get(operators);
if (stack_empty(operators)) {
throw std::runtime_error("Invalid expression: not enough operators");
}
char op = (char)stack_get(operators);
stack_pop(operators);
stack_push(values, apply_operator(val1, val2, op));
}
Expand All @@ -84,34 +94,43 @@ int evaluate_example(const std::string& example) {


while (!stack_empty(operators)) {
if (stack_empty(values)) throw std::runtime_error("Invalid expression");
if (stack_empty(values)) {
throw std::runtime_error("Invalid expression: not enough values");
}
int val2 = stack_get(values);
stack_pop(values);
if (stack_empty(values)) throw std::runtime_error("Invalid expression");

if (stack_empty(values)) {
throw std::runtime_error("Invalid expression: not enough values");
}
int val1 = stack_get(values);
stack_pop(values);
char op = stack_get(operators);

if (stack_empty(operators)) {
throw std::runtime_error("Invalid expression: not enough operators");
}
char op = (char)stack_get(operators);
stack_pop(operators);

stack_push(values, apply_operator(val1, val2, op));
}

int result = stack_get(values);


stack_delete(values);
stack_delete(operators);

return result;
return result;
}

int main() {
std::ifstream inputFile("inputt.txt");
std::ifstream inputFile("input.txt");
std::string example;

if (inputFile.is_open()) {
std::getline(inputFile, example);
inputFile.close();

std::getline(inputFile, example);

}
inputFile.close();
int result = evaluate_example(example);
std::cout << "result: " << result << std::endl;

Expand Down
4 changes: 2 additions & 2 deletions LibraryCPP/Tests/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main()
}

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
vector_set(vector, i, i);
vector_set(vector, i, static_cast<Data>(i));

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
Expand Down Expand Up @@ -61,7 +61,7 @@ int main()
for (int i = 1 ; i <= 10000000 ; ++i)
{
vector_resize(vector, i);
vector_set(vector, i - 1, i);
vector_set(vector, static_cast<size_t>(i) - 1, i);
}

long long sum = 0;
Expand Down
4 changes: 3 additions & 1 deletion LibraryCPP/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ void stack_push(Stack* stack, Data data) {

Data stack_get(const Stack* stack) {
if (stack_empty(stack)) {
throw std::out_of_range("Stack is empty");
return {};

}
return stack->vector->data[stack->vector->size - 1];

}

void stack_pop(Stack* stack) {
Expand Down

0 comments on commit 2bf7d1a

Please sign in to comment.