Skip to content
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

Closed
wants to merge 30 commits into from
Closed

lab2 #897

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9d11189
Рыбкина Анастасия Алексеевна Группа 3091
ms-anastasiia Sep 9, 2024
784dc53
Merge pull request #1 from ms-anastasiia/ms-anastasiia-patch-1
ms-anastasiia Sep 9, 2024
5d39b76
lab1CPP
ms-anastasiia Sep 11, 2024
8defa41
lab1--build
ms-anastasiia Sep 12, 2024
96360b4
\\
ms-anastasiia Sep 13, 2024
31ce04c
update
ms-anastasiia Sep 13, 2024
2a2414e
update
ms-anastasiia Sep 16, 2024
567c9a5
stack and vector
ms-anastasiia Sep 17, 2024
d003d89
stack and vector update
ms-anastasiia Sep 18, 2024
e1acadd
lab2 update
ms-anastasiia Sep 18, 2024
01f79ac
lab2 update
ms-anastasiia Sep 19, 2024
569ea9f
lab2 update
ms-anastasiia Sep 20, 2024
2bf7d1a
lab2 update
ms-anastasiia Sep 23, 2024
f498273
lab2 update
ms-anastasiia Sep 24, 2024
e5a35df
lab2 update
ms-anastasiia Sep 25, 2024
8960175
lab2 update
ms-anastasiia Sep 26, 2024
48fd759
lab2 update
ms-anastasiia Sep 27, 2024
c09a4b4
lab2 update
ms-anastasiia Oct 1, 2024
ab6765b
lab2 update
ms-anastasiia Oct 1, 2024
88ef3b0
lab2 update
ms-anastasiia Oct 1, 2024
06b409e
lab2 update
ms-anastasiia Oct 1, 2024
d0d6df5
lab2 update
ms-anastasiia Oct 2, 2024
bb18f02
lab2 update
ms-anastasiia Oct 2, 2024
5674941
lab2 update
ms-anastasiia Oct 3, 2024
e3fd2ff
lab2 update
ms-anastasiia Oct 6, 2024
703784b
lab2 update
ms-anastasiia Oct 7, 2024
a108d2f
lab2 update
ms-anastasiia Oct 7, 2024
dc0c5b0
lab2 update
ms-anastasiia Oct 9, 2024
1da6b75
lab2 update
ms-anastasiia Oct 10, 2024
e6ef871
lab2 update
ms-anastasiia Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

add_subdirectory(LibraryC)
#add_subdirectory(LibraryC)
add_subdirectory(LibraryCPP)
add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)
#add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
add_subdirectory(Lab2CPP)
5 changes: 0 additions & 5 deletions Lab1C/CMakeLists.txt

This file was deleted.

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

This file was deleted.

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

This file was deleted.

5 changes: 5 additions & 0 deletions Lab2CPP/CMakeLists.txt
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)
7 changes: 7 additions & 0 deletions Lab2CPP/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)
)
127 changes: 127 additions & 0 deletions Lab2CPP/lab2.cpp
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

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab2CPP/lab2.cpp

File Lab2CPP/lab2.cpp does not conform to Custom style guidelines. (lines 1, 8, 9, 10, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 27, 28, 29, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 53, 54, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 72, 73, 75, 76, 79, 80, 82, 83, 84, 85, 86, 88, 90, 91, 94, 95, 98, 99, 101, 102, 105, 106, 107, 108, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 123, 124)
#include <fstream>
#include <stdexcept>
#include "stack.h"

Check failure on line 4 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:4:10 [clang-diagnostic-error]

'stack.h' file not found
#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] == '(') {

Check warning on line 43 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:43:37 [bugprone-branch-clone]

repeated branch in conditional chain
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужная проверка на пустоту тоже пропала вместе с ненужной.



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;
}
8 changes: 4 additions & 4 deletions LibraryC/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_executable(TestArrayC array.cpp)
target_include_directories(TestArrayC PUBLIC ..)
target_link_libraries(TestArrayC LibraryC)
add_test(TestArrayC TestArrayC)
#add_executable(TestArrayC array.cpp)
#target_include_directories(TestArrayC PUBLIC ..)
#target_link_libraries(TestArrayC LibraryC)
#add_test(TestArrayC TestArrayC)

# add_executable(TestListC list.cpp)
# target_include_directories(TestListC PUBLIC ..)
Expand Down
2 changes: 1 addition & 1 deletion LibraryCPP/CMakeLists.txt
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)
26 changes: 13 additions & 13 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# add_executable(TestArrayCPP array.cpp)
# target_include_directories(TestArrayCPP PUBLIC ..)
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)
#add_executable(TestArrayCPP array.cpp)
#target_include_directories(TestArrayCPP PUBLIC ..)
#target_link_libraries(TestArrayCPP LibraryCPP)
#add_test(TestArrayCPP TestArrayCPP)

# add_executable(TestListCPP list.cpp)
# target_include_directories(TestListCPP PUBLIC ..)
Expand All @@ -14,13 +14,13 @@
# add_test(TestQueueCPP TestQueueCPP)
# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)

# add_executable(TestStackCPP stack.cpp)
# target_include_directories(TestStackCPP PUBLIC ..)
# target_link_libraries(TestStackCPP LibraryCPP)
# add_test(TestStackCPP TestStackCPP)
add_executable(TestStackCPP stack.cpp)
target_include_directories(TestStackCPP PUBLIC ..)
target_link_libraries(TestStackCPP LibraryCPP)
add_test(TestStackCPP TestStackCPP)

# add_executable(TestVectorCPP vector.cpp)
# target_include_directories(TestVectorCPP PUBLIC ..)
# target_link_libraries(TestVectorCPP LibraryCPP)
# add_test(TestVectorCPP TestVectorCPP)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
add_executable(TestVectorCPP vector.cpp)
target_include_directories(TestVectorCPP PUBLIC ..)
target_link_libraries(TestVectorCPP LibraryCPP)
add_test(TestVectorCPP TestVectorCPP)
set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
2 changes: 1 addition & 1 deletion LibraryCPP/Tests/array.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/array.cpp

File LibraryCPP/Tests/array.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26)
#include "array.h"

Check failure on line 2 in LibraryCPP/Tests/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/Tests/array.cpp:2:10 [clang-diagnostic-error]

'array.h' file not found

int main()
{
Array *arr = array_create(10);
Array* arr = array_create(10);

if (array_size(arr) != 10)
{
Expand Down
4 changes: 2 additions & 2 deletions LibraryCPP/Tests/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/vector.cpp

File LibraryCPP/Tests/vector.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 67, 68, 69, 71)
#include "vector.h"

Check failure on line 2 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/Tests/vector.cpp:2:10 [clang-diagnostic-error]

'vector.h' file not found

int main()
{
Expand All @@ -13,7 +13,7 @@
}

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 @@
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
32 changes: 28 additions & 4 deletions LibraryCPP/array.cpp
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

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.cpp

File LibraryCPP/array.cpp does not conform to Custom style guidelines. (lines 4, 5, 6, 7, 11, 12, 13, 14, 15, 17, 18, 19, 20, 25, 26, 27, 28, 29, 30, 35, 36, 37, 38, 39, 40, 45, 46, 47, 48, 49, 50, 54, 55, 56)
#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;

}
Loading
Loading