-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lab2 #897
lab2 #897
Changes from all commits
9d11189
784dc53
5d39b76
8defa41
96360b4
31ce04c
2a2414e
567c9a5
d003d89
e1acadd
01f79ac
569ea9f
2bf7d1a
f498273
e5a35df
8960175
48fd759
c09a4b4
ab6765b
88ef3b0
06b409e
d0d6df5
bb18f02
5674941
e3fd2ff
703784b
a108d2f
dc0c5b0
1da6b75
e6ef871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
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}/input.txt) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1+2*3-1 | ||
(2+2)*3 | ||
4+1- | ||
2*3+(4+4 | ||
3/3*(2+5))+1 | ||
4*2+1) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include <iostream> | ||
Check notice on line 1 in Lab2CPP/lab2.cpp GitHub Actions / cpp-linterRun clang-format on Lab2CPP/lab2.cpp
|
||
#include <fstream> | ||
#include <stdexcept> | ||
#include "stack.h" | ||
#include "vector.h" | ||
#include <string> | ||
|
||
int precedence(char op) { | ||
if (op == '+' || op == '-') return 1; | ||
if (op == '*' || op == '/') return 2; | ||
return 0; | ||
} | ||
|
||
int apply_operator(int a, int b, char op) { | ||
switch (op) { | ||
case '+': return a + b; | ||
case '-': return a - b; | ||
case '*': return a * b; | ||
case '/': | ||
if (b == 0) throw std::runtime_error("Division by zero"); | ||
return a / b; | ||
default: | ||
throw std::invalid_argument("Invalid operator"); | ||
} | ||
} | ||
|
||
int evaluate_example(const std::string& example) { | ||
Stack* values = stack_create(); | ||
Stack* operators = stack_create(); | ||
|
||
for (size_t i = 0; i < example.length(); i++) { | ||
if (example[i] == ' ') continue; | ||
|
||
if (isdigit(example[i])) { | ||
int value = 0; | ||
while (i < example.length() && isdigit(example[i])) { | ||
value = value * 10 + (example[i] - '0'); | ||
i++; | ||
} | ||
i--; | ||
stack_push(values, value); | ||
} | ||
else if (example[i] == '(') { | ||
stack_push(operators, example[i]); | ||
} | ||
else if (example[i] == ')') { | ||
while ( stack_get(operators) != '(') { | ||
|
||
int val2 = stack_get(values); | ||
stack_pop(values); | ||
|
||
|
||
int val1 = stack_get(values); | ||
stack_pop(values); | ||
|
||
|
||
char op = (char)stack_get(operators); | ||
stack_pop(operators); | ||
|
||
stack_push(values, apply_operator(val1, val2, op)); | ||
} | ||
if (!stack_empty(operators)) { | ||
stack_pop(operators); | ||
} | ||
else { | ||
throw std::runtime_error("Mismatched parentheses: no matching '(' found"); | ||
} | ||
} | ||
else if (example[i] == '+' || example[i] == '-' || example[i] == '*' || example[i] == '/') { | ||
while (!stack_empty(operators) && precedence((char)stack_get(operators)) >= precedence(example[i])) { | ||
|
||
int val2 = stack_get(values); | ||
stack_pop(values); | ||
|
||
int val1 = stack_get(values); | ||
stack_pop(values); | ||
|
||
|
||
char op = (char)stack_get(operators); | ||
stack_pop(operators); | ||
|
||
stack_push(values, apply_operator(val1, val2, op)); | ||
} | ||
stack_push(operators, example[i]); | ||
} | ||
} | ||
|
||
while (!stack_empty(operators)) { | ||
|
||
int val2 = stack_get(values); | ||
stack_pop(values); | ||
|
||
|
||
int val1 = stack_get(values); | ||
stack_pop(values); | ||
|
||
|
||
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; | ||
|
||
} | ||
|
||
int main() { | ||
std::ifstream inputFile("input.txt"); | ||
std::string example; | ||
|
||
while (std::getline(inputFile, example)) { | ||
try { | ||
int result = evaluate_example(example); | ||
std::cout << "Result: " << result << std::endl; | ||
} | ||
catch (const std::exception& e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) | ||
add_library(LibraryCPP STATIC stack.cpp vector.cpp) | ||
|
||
add_subdirectory(Tests) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,58 @@ | ||
#include "array.h" | ||
Check notice on line 1 in LibraryCPP/array.cpp GitHub Actions / cpp-linterRun clang-format on LibraryCPP/array.cpp
|
||
#include <stdexcept> | ||
|
||
struct Array | ||
{ | ||
size_t size; | ||
Data* data; | ||
}; | ||
|
||
// create array | ||
Array *array_create(size_t size) | ||
{ | ||
return new Array; | ||
if (size == 0) { | ||
throw std::invalid_argument("Size must be greater than 0"); | ||
} | ||
|
||
Array* arr = new Array; | ||
arr->size = size; | ||
arr->data = new Data[size]; | ||
return arr; | ||
|
||
} | ||
|
||
// delete array, free memory | ||
void array_delete(Array *arr) | ||
{ | ||
delete arr; | ||
if (arr) { | ||
delete[] arr->data; | ||
delete arr; | ||
} | ||
|
||
} | ||
|
||
// returns specified array element | ||
Data array_get(const Array *arr, size_t index) | ||
{ | ||
return (Data)0; | ||
if (index >= arr->size) { | ||
throw std::out_of_range("Index out of range"); | ||
} | ||
return arr->data[index]; | ||
|
||
} | ||
|
||
// sets the specified array element to the value | ||
void array_set(Array *arr, size_t index, Data value) | ||
{ | ||
if (index >= arr->size) { | ||
throw std::out_of_range("Index out of range"); | ||
} | ||
arr->data[index] = value; | ||
} | ||
|
||
// returns array size | ||
size_t array_size(const Array *arr) | ||
{ | ||
return 0; | ||
return arr->size; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужная проверка на пустоту тоже пропала вместе с ненужной.