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

lab1 #842

Closed
wants to merge 15 commits into from
Closed

lab1 #842

Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

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

# add_subdirectory(Lab1C)
add_subdirectory(Lab1CPPClass)

add_subdirectory(Lab1C)
8 changes: 4 additions & 4 deletions Lab1C/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_executable(Lab1C lab1.c)
target_include_directories(Lab1C PUBLIC ../LibraryC)
target_link_libraries(Lab1C LibraryC)
# add_executable(Lab1C lab1.c)
# target_include_directories(Lab1C PUBLIC ../LibraryC)
# target_link_libraries(Lab1C LibraryC)

add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
# add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
5 changes: 5 additions & 0 deletions Lab1CPPClass/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(Lab1CPPClass lab1.cpp)
target_include_directories(Lab1CPPClass PUBLIC ../LibraryCPPClass)
target_link_libraries(Lab1CPPClass LibraryCPPClass)

add_test(NAME TestLab1CPPClass COMMAND Lab1CPPClass ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/output.txt ${CMAKE_CURRENT_SOURCE_DIR}/randomInput.txt)
4 changes: 4 additions & 0 deletions Lab1CPPClass/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
9
5 4 3 2 5 4 3 2 5
7
1 3 2 3 4 3 5
168 changes: 168 additions & 0 deletions Lab1CPPClass/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include <iostream>

Check notice on line 1 in Lab1CPPClass/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1CPPClass/lab1.cpp

File Lab1CPPClass/lab1.cpp does not conform to Custom style guidelines. (lines 1, 8, 9, 10, 11, 13, 15, 16, 17, 18, 19, 20, 22, 25, 26, 27, 29, 31, 32, 33, 34, 36, 39, 40, 41, 42, 43, 44, 45, 46, 47, 51, 52, 53, 54, 56, 57, 58, 59, 60, 62, 63, 65, 66, 67, 68, 70, 71, 73, 74, 75, 76, 77, 78, 81, 85, 86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 101, 102, 103, 104, 105, 106, 110, 111, 112, 113, 114, 115, 116, 117, 121, 122, 123, 124, 126, 127, 128, 129, 130, 131, 132, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 156, 157, 158, 160, 161, 162, 164)
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <bits/stdc++.h>
#include "array.h"

Check failure on line 6 in Lab1CPPClass/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab1CPPClass/lab1.cpp:6:10 [clang-diagnostic-error]

'array.h' file not found

Array* array_create_and_read(std::ifstream& input)
{
size_t n;
input >> n;

Array* arr = new Array(n);

for (size_t i = 0; i < n; ++i)
{
int x;
input >> x;
arr->set(i, x);
}
return arr;
}


Array* array_create_and_random_input(std::ifstream& input, int left, int right) {
size_t n;
input >> n;

Array* arr = new Array(n);

for (size_t i = 0; i < n; ++i) {
int random_num = left + rand() % (right - left + 1);
arr->set(i, random_num);
}
return arr;
}


void task1(const Array& arr)
{
Array marks(4);
for(size_t i = 0; i < arr.size(); i++) {
marks.set(arr.get(i)-2, marks.get(arr.get(i)-2) + 1); // marks[arr[i] - 2]++;
}
for(size_t i = 0; i < 4; i++) {
std::cout << "Mark " << i+2 << " : " << marks.get(i) << '\t';
}
std::cout << '\n';
}

void task2(const Array& arr)
{
int min_value = INT_MAX;
int max_value = INT_MIN;

for (size_t i = 0; i < arr.size(); ++i) {
int current = arr.get(i);
if (current < min_value) min_value = current;
if (current > max_value) max_value = current;
}

size_t frequency_size = max_value - min_value + 1;
Array frequency(frequency_size);

for (size_t i = 0; i < arr.size(); ++i) {
int index = arr.get(i) - min_value;
frequency.set(index, frequency.get(index) + 1);
}

int max_frequency = 0;
int most_frequent_number = min_value;

for (size_t i = 0; i < frequency_size; ++i) {
if (frequency.get(i) > max_frequency) {
max_frequency = frequency.get(i);
most_frequent_number = i + min_value;
}
}

std::cout << most_frequent_number << '\n';
}



//be work for any nums, not only for the segment [-100, 100]
void task2_v2(const Array& arr) {
std::unordered_map<int, int> mp;

for(size_t i = 0; i < arr.size(); i++) {
int num = arr.get(i);
if(mp.find(num) == mp.end()) {
mp[num] = 0;
} else {
mp[num]++;
}
}

int max_frequency = 0;
int most_frequent_number = INT_MIN;

for(auto it : mp) {
if(it.second > max_frequency) {
max_frequency = it.second;
most_frequent_number = it.first;
}
}
std::cout << most_frequent_number << '\n';
}

bool check_output(std::ifstream& output, const Array& arr) {
for(size_t i = 0; i < arr.size(); i++) {
int num;
output >> num;
if(num != arr.get(i)) {
return false;
}
}
return true;
}

int main(int argc, char **argv)
{
//For random
std::srand(std::time(nullptr));

// With my tests
std::ifstream input(argv[1]);
std::ifstream output(argv[2]);
if (!input.is_open()) {
std::cerr << "Failed to open file: " << argv[1] << "\n";
return 1;
}

Array* arr = array_create_and_read(input);
task1(*arr);
if(check_output(output, *arr)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Здесь пусть остаётся, а в другие разы основная программа должна только выводить результат, а не проверять его.
Для проверки надо использовать вспомогательную программу, которая сравнит вывод основной и эталонный файл.

return 1;
}
delete arr;

arr = array_create_and_read(input);
task2(*arr);
if(check_output(output, *arr)) {
return 1;
}
delete arr;
input.close();

// With random
std::ifstream randomInput(argv[3]);
if (!randomInput.is_open()) {
std::cerr << "Failed to open file: " << argv[3] << "\n";
return 1;
}

arr = array_create_and_random_input(randomInput, 2, 5);
task1(*arr);
delete arr;

arr = array_create_and_random_input(randomInput, -100, 100);
task2(*arr);
delete arr;

randomInput.close();


return 0;
}
2 changes: 2 additions & 0 deletions Lab1CPPClass/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2 2 2 3
3
2 changes: 2 additions & 0 deletions Lab1CPPClass/randomInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
10
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
8 changes: 4 additions & 4 deletions LibraryCPPClass/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# add_executable(TestArrayCPPClass array.cpp)
# target_include_directories(TestArrayCPPClass PUBLIC ..)
# target_link_libraries(TestArrayCPPClass LibraryCPPClass)
# add_test(TestArrayCPPClass TestArrayCPPClass)
add_executable(TestArrayCPPClass array.cpp)
target_include_directories(TestArrayCPPClass PUBLIC ..)
target_link_libraries(TestArrayCPPClass LibraryCPPClass)
add_test(TestArrayCPPClass TestArrayCPPClass)

# add_executable(TestListCPPClass list.cpp)
# target_include_directories(TestListCPPClass PUBLIC ..)
Expand Down
22 changes: 22 additions & 0 deletions LibraryCPPClass/Tests/array.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

File LibraryCPPClass/Tests/array.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 14, 15, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49)
#include "array.h"

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

'array.h' file not found

int main()
{
Expand All @@ -26,5 +26,27 @@
}
}

try
{
arr->set(20, 5); // error index
std::cout << "set() set() does not handle invalid index\n";
return 1;
}
catch (const std::out_of_range& e)
{
std::cout << "Caught expected out_of_range exception for set() with invalid index\n";
}

try
{
arr->get(20); // error index
std::cout << "set() set() does not handle invalid index\n";
return 1;
}
catch (const std::out_of_range& e)
{
std::cout << "Caught expected out_of_range exception for get() with invalid index\n";
}

delete arr;
}
35 changes: 31 additions & 4 deletions LibraryCPPClass/array.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
#include "array.h"

Check notice on line 1 in LibraryCPPClass/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPPClass/array.cpp

File LibraryCPPClass/array.cpp does not conform to Custom style guidelines. (lines 3, 4, 5, 6, 10, 11, 12, 13, 17, 18, 19, 20, 21, 23, 25, 26, 28, 29, 30, 35, 36, 37, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 56, 57, 58)

Array::Array(size_t size)
Array::Array(size_t size) : m_size(size), m_data(new Data[size]())
{
for(size_t i = 0; i < m_size; i++) {
m_data[i] = 0;
}
}

Array::Array(const Array &a)
Array::Array(const Array &a) : m_size(a.m_size), m_data(new Data[a.m_size])
{
for (size_t i = 0; i < m_size; ++i) {
m_data[i] = a.m_data[i];
}
}

Array &Array::operator=(const Array &a)
{
if (this == &a) {
return *this;
}

delete[] m_data;

m_size = a.m_size;
m_data = new Data[m_size];

for (size_t i = 0; i < m_size; ++i) {
m_data[i] = a.m_data[i];
}

return *this;
}

Array::~Array()
{
delete[] m_data;
}

Data Array::get(size_t index) const
{
return Data(0);
if (index >= m_size) {
throw std::out_of_range("Index out of range\n");
}
return m_data[index];
}

void Array::set(size_t index, Data value)
{
if (index >= m_size) {
throw std::out_of_range("Index out of range");
}
m_data[index] = value;
}

size_t Array::size() const
{
return 0;
return m_size;
}
4 changes: 4 additions & 0 deletions LibraryCPPClass/array.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef ARRAY_H

Check notice on line 1 in LibraryCPPClass/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPPClass/array.h

File LibraryCPPClass/array.h does not conform to Custom style guidelines. (lines 4, 10, 12, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 34, 35, 36, 37)
#define ARRAY_H

#include <cstddef>

Check failure on line 4 in LibraryCPPClass/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

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

'cstddef' file not found
#include <stdexcept>
#include <iostream>

typedef int Data;

class Array

Check warning on line 10 in LibraryCPPClass/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPPClass/array.h:10:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'Array' is non-const and globally accessible, consider making it const
{
public:
// create array
Expand All @@ -31,6 +33,8 @@

private:
// private data should be here
size_t m_size; //size of array
Data* m_data; // pointer
};

#endif
1 change: 1 addition & 0 deletions my_name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Романов Дмитрий Александрович 3092
Loading