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

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open

Lab2 #922

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 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
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)
9 changes: 9 additions & 0 deletions Lab2CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_executable(Lab2CPP lab2.cpp)
target_include_directories(Lab2CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab2CPP LibraryCPP)

add_test(NAME Test1 COMMAND Lab2CPP "1+2*3")
add_test(NAME Test2 COMMAND Lab2CPP "(1+2)*3")

set_tests_properties(Test1 PROPERTIES PASS_REGULAR_EXPRESSION "7")
set_tests_properties(Test2 PROPERTIES PASS_REGULAR_EXPRESSION "9")
124 changes: 124 additions & 0 deletions Lab2CPP/lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#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, 7, 8, 9, 10, 11, 12, 13, 14, 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, 57, 59, 60, 61, 62, 63, 65, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 96, 97, 98, 100, 101, 105, 106, 107, 108, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122)
#include <string>
#include <fstream>
#include "list.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]

'list.h' file not found
#include "stack.h"

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 32 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:32: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 86 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:86: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(int argc, char** argv)
{
std::string data = "";
char* ch = argv[1];
while (*ch != '\0')
{
data += *(ch);
ch++;
}
std::cout << calculate(data);
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
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
97 changes: 85 additions & 12 deletions LibraryCPP/list.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,134 @@
#include <cstddef>

Check notice on line 1 in LibraryCPP/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/list.cpp

File LibraryCPP/list.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 7, 8, 11, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 38, 39, 40, 41, 45, 46, 47, 48, 52, 53, 54, 55, 59, 60, 61, 62, 66, 67, 68, 69, 70, 72, 73, 74, 75, 76, 77, 78, 80, 85, 86, 87, 88, 89, 91, 92, 93, 95, 96, 97, 99, 100, 104, 105, 106, 107, 108, 109, 110, 111, 112, 114, 115, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131)
#include "list.h"

struct ListItem

Check warning on line 4 in LibraryCPP/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.cpp:4:8 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: value
{
Data value;
ListItem* next = nullptr;
ListItem* prev = nullptr;
};

struct List
{
ListItem* node = nullptr;
};

List *list_create()
{
return new List;
List* newList = new List;
return newList;
}

void list_delete(List *list)
void list_delete(List* list)
{
// TODO: free items
delete list;
if (list != nullptr)
{
ListItem* current = list->node;
while (current != nullptr)
{
ListItem* next = current->next;
delete current;
current = next;
}
delete list;
}
}


ListItem *list_first(List *list)
{
return NULL;
if (list != nullptr)
return list->node;
return nullptr;
}

Data list_item_data(const ListItem *item)
{
if (item != nullptr)
return item->value;
return (Data)0;
}

ListItem *list_item_next(ListItem *item)
{
return NULL;
if(item != nullptr)
return item->next;
return nullptr;
}

ListItem *list_item_prev(ListItem *item)
{
return NULL;
if (item != nullptr && item->prev != nullptr)
return item->prev;
return nullptr;
}

ListItem *list_insert(List *list, Data data)
ListItem* list_insert(List* list, Data data)
{
return NULL;
ListItem* newEl = new ListItem;
newEl->value = data;
newEl->prev = nullptr;

if (list->node != nullptr)
{
newEl->next = list->node;
list->node->prev = newEl;
}
else
newEl->next = nullptr;

list->node = newEl;
Dovgalyuk marked this conversation as resolved.
Show resolved Hide resolved

return newEl;
}

ListItem *list_insert_after(List *list, ListItem *item, Data data)
ListItem* list_insert_after(List* list, ListItem* item, Data data)
{
return NULL;
if (list != nullptr && item != nullptr)
{
ListItem* newEl = new ListItem;

newEl->prev = item;
newEl ->next = item->next;
newEl->value = data;

Copy link
Owner

Choose a reason for hiding this comment

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

item->next->prev не меняется

if (item->next != nullptr)
item->next->prev = newEl;
item->next = newEl;

return newEl;
}
return nullptr;
}

ListItem *list_erase_first(List *list)
{
if (list != nullptr && list->node != nullptr)
{
ListItem* temp = list->node->next;
if(temp != nullptr)
temp->prev = nullptr;
delete list->node;
list->node = temp;

return list->node;
}
return NULL;
}

ListItem *list_erase_next(List *list, ListItem *item)
{
return NULL;
if (list != nullptr && item != nullptr && item->next != nullptr)
{
if(item->next->next != nullptr)
item->next->next->prev = item;
ListItem* temp = item->next->next;
if (temp != nullptr)
temp->prev = item;
delete item->next;
item->next = temp;
return temp;
}

return nullptr;
}
Loading
Loading