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 #922

Closed
wants to merge 73 commits into from
Closed

Lab2 #922

Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
93ae4a0
Create Lab1CPP
tolstosup Sep 11, 2024
f54d5e7
Update CMakeLists.txt
tolstosup Sep 11, 2024
bbced65
Delete Lab1CPP
tolstosup Sep 11, 2024
10d0458
Create 1
tolstosup Sep 11, 2024
e08eb2b
Delete Lab1CPP/1
tolstosup Sep 11, 2024
be9b3b3
Create CMakeList.txt
tolstosup Sep 11, 2024
4ca63f4
Create input.txt
tolstosup Sep 11, 2024
17b6188
Add files via upload
tolstosup Sep 11, 2024
ee95690
Update CMakeLists.txt
tolstosup Sep 11, 2024
0f95d0a
Update array.cpp
tolstosup Sep 11, 2024
06d4f59
Update array.h
tolstosup Sep 11, 2024
78f8113
Create Михайлов Роман 3091
tolstosup Sep 11, 2024
5250111
Update CMakeList.txt
tolstosup Sep 11, 2024
a25e470
Update CMakeList.txt
tolstosup Sep 11, 2024
496f8ce
Rename CMakeList.txt to CMakeLists.txt
tolstosup Sep 11, 2024
0283fa5
Update CMakeLists.txt
tolstosup Sep 11, 2024
eb3fc8a
Add files via upload
tolstosup Sep 11, 2024
b68f54c
Add files via upload
tolstosup Sep 11, 2024
ccd9dca
Add files via upload
tolstosup Sep 11, 2024
b2ac8dc
Update array.cpp
tolstosup Sep 11, 2024
a74eb68
Update array.h
tolstosup Sep 11, 2024
14c22ad
Update lab1.cpp
tolstosup Sep 12, 2024
78c1f63
Update input.txt
tolstosup Sep 12, 2024
cfdb35d
Update array.cpp
tolstosup Sep 12, 2024
5edd363
Update array.h
tolstosup Sep 12, 2024
2df5416
Update array.cpp
tolstosup Sep 12, 2024
98cbf4f
Add files via upload
tolstosup Sep 18, 2024
6f15302
Create 1
tolstosup Sep 18, 2024
1e02640
Add files via upload
tolstosup Sep 18, 2024
e5d8c45
Add files via upload
tolstosup Sep 18, 2024
70cb6b9
Update CMakeLists.txt
tolstosup Sep 18, 2024
09ed8ef
Create lab2.cpp
tolstosup Sep 18, 2024
0264cad
Delete Lab2CPP/1
tolstosup Sep 18, 2024
6adbe17
Update lab2.cpp
tolstosup Sep 18, 2024
b7dac41
Rename CMakeLists .txt to CMakeLists.txt
tolstosup Sep 18, 2024
535e839
Update list.cpp
tolstosup Sep 18, 2024
cdab269
Update list.cpp
tolstosup Sep 18, 2024
26b451e
Update list.cpp
tolstosup Sep 18, 2024
bd28b9a
Update stack.h
tolstosup Sep 18, 2024
d526cb2
Update stack.cpp
tolstosup Sep 18, 2024
e346e9c
Update CMakeLists.txt
tolstosup Sep 18, 2024
fb6564c
Update list.cpp
tolstosup Sep 18, 2024
27c441c
Update stack.cpp
tolstosup Sep 18, 2024
e287c51
Delete Lab1CPP directory
tolstosup Sep 23, 2024
8f7e77a
Update lab2.cpp
tolstosup Sep 23, 2024
0d677f9
Add files via upload
tolstosup Sep 23, 2024
4daacc0
Update stack.cpp
tolstosup Sep 23, 2024
8ad0d1a
Update list.cpp
tolstosup Sep 23, 2024
ad26d88
Update stack.cpp
tolstosup Sep 23, 2024
6f287e7
Update list.cpp
tolstosup Sep 23, 2024
b39aaa8
Update CMakeLists.txt
tolstosup Sep 24, 2024
058efc7
Update lab2.cpp
tolstosup Sep 24, 2024
6730125
Update stack.cpp
tolstosup Sep 24, 2024
8e4a696
Update lab2.cpp
tolstosup Sep 25, 2024
69bb0b8
Update input.txt
tolstosup Sep 25, 2024
0eb1662
Update list.cpp
tolstosup Sep 25, 2024
0e78e12
Update stack.h
tolstosup Sep 25, 2024
39cead9
Update lab2.cpp
tolstosup Sep 25, 2024
0264738
Update lab2.cpp
tolstosup Sep 25, 2024
969b0b1
Update stack.cpp
tolstosup Sep 25, 2024
0a10a0f
Update list.cpp
tolstosup Sep 26, 2024
f52b3cf
Update array.cpp
tolstosup Sep 26, 2024
a77c95e
Update list.cpp
tolstosup Sep 27, 2024
5bf832e
Update list.cpp
tolstosup Sep 27, 2024
0f6f2a2
Update lab2.cpp
tolstosup Sep 27, 2024
3acf5d9
Update CMakeLists.txt
tolstosup Sep 27, 2024
8a43462
Delete Lab2CPP/input.txt
tolstosup Sep 27, 2024
2f2fea7
Update lab2.cpp
tolstosup Sep 27, 2024
6dacaf4
Update CMakeLists.txt
tolstosup Sep 27, 2024
c18867c
Update CMakeLists.txt
tolstosup Sep 27, 2024
dbcf9c8
Update lab2.cpp
tolstosup Sep 27, 2024
2a773a7
Update list.cpp
tolstosup Sep 30, 2024
831dfe2
Update list.cpp
tolstosup Oct 1, 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
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

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

add_subdirectory(Lab1C)
add_subdirectory(Lab2CPP)
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)
2 changes: 2 additions & 0 deletions Lab2CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(1+2)*3
1+2*3
125 changes: 125 additions & 0 deletions Lab2CPP/lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#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, 6, 7, 8, 9, 10, 11, 12, 13, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 64, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 95, 96, 97, 99, 100, 104, 105, 106, 107, 112, 113, 114, 116, 117, 118, 120, 121, 123)
#include <string>
#include <fstream>
#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

unsigned int priority(const char symbol)
{
if (symbol == '*' || symbol == '/')
return 3;
if (symbol == '+' || symbol == '-')
return 2;
if (symbol == '(')
return 1;

return 0;
}

std::string RPN(std::string data)
{
Stack* stack = stack_create();
std::string res = "";
for (size_t i = 0; i < data.size(); i++)
{
if (isdigit(data[i]))
{
res += data[i];
}
else
{
if (data[i] == '(')
{

Check warning on line 31 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:31:4 [bugprone-branch-clone]

repeated branch in conditional chain
stack_push(stack, data[i]);
}
else if (data[i] == ')')
{
while (!stack_empty(stack) && stack_get(stack) != '(')
{
res += stack_get(stack);
stack_pop(stack);
}
if (!stack_empty(stack) && stack_get(stack) == '(')
{
stack_pop(stack);
}
}
else
{
while (!stack_empty(stack) && priority(stack_get(stack)) >= priority(data[i]))
{
res += stack_get(stack);
stack_pop(stack);
}
stack_push(stack, data[i]);
}
}
}

while (!stack_empty(stack))
{
res += stack_get(stack);
stack_pop(stack);
}

stack_delete(stack);
return res;
}

Data solve(std::string data)
{
Stack* stack = stack_create();
for (size_t i = 0; i < data.size(); i++)
{
if (isdigit(data[i]))
stack_push(stack, data[i] - '0');
else
{
Data val1 = stack_get(stack);
stack_pop(stack);
Data val2 = stack_get(stack);
stack_pop(stack);
Data res = 0;

switch (data[i])
{
case '+': res = val1 + val2;

Check warning on line 85 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:85:4 [bugprone-branch-clone]

switch has 4 consecutive identical branches
break;
case '-': res = val1 - val2;
break;
case '*': res = val1 * val2;
break;
case '/': res = val1 / val2;
break;
}

stack_push(stack, res);
}
}

Data res = stack_get(stack);
stack_delete(stack);
return res;
}

std::string calculate(std::string expr)
{
expr = RPN(expr);
expr = std::to_string(solve(expr));

return expr;
}

int main()
{
std::ifstream in("input.txt");

std::string temp = "";
in >> temp;
std::cout << temp << " = " << calculate(temp);

in >> temp;
Copy link
Owner

Choose a reason for hiding this comment

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

Лучше добавить два теста.

Copy link
Owner

Choose a reason for hiding this comment

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

Не исправлено.

std::cout << temp << " = " << calculate(temp);

in.close();
return 0;
}
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 list.cpp stack.cpp)

add_subdirectory(Tests)
24 changes: 12 additions & 12 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# 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 ..)
# target_link_libraries(TestListCPP LibraryCPP)
# add_test(TestListCPP TestListCPP)
add_executable(TestListCPP list.cpp)
target_include_directories(TestListCPP PUBLIC ..)
target_link_libraries(TestListCPP LibraryCPP)
add_test(TestListCPP TestListCPP)

# add_executable(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
# target_link_libraries(TestQueueCPP LibraryCPP)
# 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 ..)
Expand Down
57 changes: 48 additions & 9 deletions LibraryCPP/array.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
#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 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 43, 44, 45, 46, 47, 52, 53, 54, 55, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70)

struct Array
{
Data* array;
size_t size;
};

// create array
Array *array_create(size_t size)
Array* array_create(size_t size)
{
return new Array;
if (size != 0)
{
Array* array = new Array;
array->size = size;
array->array = new Data[size];
return array;
}
else
return nullptr;
}

// delete array, free memory
void array_delete(Array *arr)
void array_delete(Array* arr)
{
delete arr;
if (arr->array != nullptr)
{
delete[] arr->array;
delete arr;
}
}

// returns specified array element
Data array_get(const Array *arr, size_t index)
Data array_get(const Array* arr, size_t index)
{
return (Data)0;
if (index < arr->size)
return arr->array[index];
else
return 0;
}

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value)
void array_set(Array* arr, size_t index, Data value)
{
if (index < arr->size)
{
arr->array[index] = value;
}
}

// returns array size
size_t array_size(const Array *arr)
size_t array_size(const Array* arr)
{
return 0;
if (arr->array != nullptr)
return arr->size;
return 0;
}

// fill array from file
Array* fill_array(std::ifstream &in)
{
size_t size;
Data value;
in >> size;
Array* array = array_create(size);
for (size_t i = 0; i < size; i++)
{
in >> value;
array_set(array, i, value);
}

return array;
}
15 changes: 10 additions & 5 deletions LibraryCPP/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef ARRAY_H

Check notice on line 1 in LibraryCPP/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.h

File LibraryCPP/array.h does not conform to Custom style guidelines. (lines 15, 18, 21, 24, 27, 30)
#define ARRAY_H

#include <cstddef>

Check failure on line 4 in LibraryCPP/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.h:4:10 [clang-diagnostic-error]

'cstddef' file not found
#include <cstdlib>
#include <fstream>

// Non-resizeable array
// Stores integer values inside
Expand All @@ -10,18 +12,21 @@
struct Array;

// create array
Array *array_create(size_t size);
Array* array_create(size_t size);

// delete array, free memory
void array_delete(Array *arr);
void array_delete(Array* arr);

// returns specified array element
Data array_get(const Array *arr, size_t index);
Data array_get(const Array* arr, size_t index);

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value);
void array_set(Array* arr, size_t index, Data value);

// returns array size
size_t array_size(const Array *arr);
size_t array_size(const Array* arr);

// fill array from file
Array* fill_array(std::ifstream &in);

#endif
Loading
Loading