-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
cmake-build-debug/CMakeFiles/cmake.check_cache | ||
cmake-build-debug/CMakeFiles/clion-log.txt | ||
cmake-build-debug/CMakeFiles/clion-environment.txt | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdCXX/CMakeCXXCompilerId.cpp | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdCXX/a.exe | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdC/CMakeCCompilerId.c | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdC/a.exe | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeSystem.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeRCCompiler.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeDetermineCompilerABI_CXX.bin | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeDetermineCompilerABI_C.bin | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeCXXCompiler.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeCCompiler.cmake | ||
cmake-build-debug/CMakeCache.txt | ||
cmake-build-debug/cmake_install.cmake | ||
cmake-build-debug/build.ninja | ||
cmake-build-debug/.ninja_log | ||
cmake-build-debug/.ninja_deps | ||
cmake-build-debug/.cmake/api/v1/reply/toolchains-v1-879ae172463b8b264ea5.json | ||
cmake-build-debug/.cmake/api/v1/reply/target-projekt_1_cpp-Debug-60ecf23dce0a1db6a52b.json | ||
cmake-build-debug/.cmake/api/v1/reply/index-2022-11-07T18-57-00-0646.json | ||
cmake-build-debug/.cmake/api/v1/reply/directory-.-Debug-d0094a50bb2071803777.json | ||
cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-17f88577573314ab56d4.json | ||
cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-5e6afa09f66051f08fee.json | ||
cmake-build-debug/.cmake/api/v1/reply/cache-v2-67bdd4019d2b4a441b5f.json | ||
cmake-build-debug/.cmake/api/v1/query/toolchains-v1 | ||
cmake-build-debug/.cmake/api/v1/query/codemodel-v2 | ||
cmake-build-debug/.cmake/api/v1/query/cmakeFiles-v1 | ||
cmake-build-debug/.cmake/api/v1/query/cache-v2 | ||
cmake-build-debug/CMakeFiles/CMakeOutput.log | ||
cmake-build-debug/CMakeFiles/projekt_1_cpp.dir/main.cpp.obj | ||
cmake-build-debug/CMakeFiles/rules.ninja | ||
cmake-build-debug/CMakeFiles/TargetDirectories.txt | ||
cmake-build-debug/projekt_1_cpp.exe | ||
cmake-build-debug/Testing/Temporary/LastTest.log | ||
CMakeLists.txt | ||
.idea/vcs.xml | ||
.idea/projekt-1-cpp.iml | ||
.idea/modules.xml | ||
.idea/misc.xml | ||
.idea/inspectionProfiles/Project_Default.xml | ||
.idea/.name | ||
.idea/workspace.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# projekt-1-cpp | ||
Pierwszy projekt z Algorytmów i struktur danych zaimplementowany w c++ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <vector> | ||
|
||
// Przykładowe macierze 2-wymiarowe. | ||
// Jedna liczba się powtarza : 5. | ||
std::vector<std::vector<int>> example1 = { | ||
{2, 4, 3, 8, 5}, | ||
{4, 7, 1, 3, 5}, | ||
{3, 5, 2, 1, 5}, | ||
{4, 5, 0, 2, 5} | ||
}; | ||
|
||
// Dwie lub więcej liczb się powtarza: 2, 3. | ||
std::vector<std::vector<int>> example2 = { | ||
{3, 2, 3, 3, 9}, | ||
{4, 2, 1, 3, 6}, | ||
{3, 2, 2, 1, 3}, | ||
{4, 2, 0, 2, 3} | ||
}; | ||
|
||
// Jedna liczba się powtarza: 3. Inny rozmiar tablicy. | ||
std::vector<std::vector<int>> example3 = { | ||
{1, 2, 3}, | ||
{1, 5, 3}, | ||
{7, 1, 3}, | ||
{1, 3, 13}, | ||
{3, 15, 1}, | ||
{17, 1, 3}, | ||
{3, 3, 22} | ||
}; | ||
|
||
// Żadna liczba się nie powtarza. | ||
std::vector<std::vector<int>> example4 = { | ||
{7, 2, 8, 2, 9}, | ||
{4, 2, 1, 3, 6}, | ||
{3, 0, 5, 1, 3}, | ||
{4, 5, 0, 2, 3} | ||
}; | ||
|
||
// W tablicy jest tylko jeden wiersz => wszystkie liczby się powtarzają. | ||
std::vector<std::vector<int>> example5 = {{7, 2, 8, 9}}; | ||
|
||
// Pusta tablica => żadna liczba się nie powtarza. | ||
std::vector<std::vector<int>> example6 = {{}}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
#include <chrono> | ||
|
||
#include "examples.h" | ||
#include "tests.cpp" | ||
|
||
/* | ||
* Sprawdź, które elementy tablicy dwuwymiarowej występują w każdym wierszu tej tablicy. | ||
*/ | ||
|
||
// Prosta funkcja wyszukiwania liniowego zwracająca prawda/fałsz. | ||
bool linearSearch(int key, const std::vector<int> &vector){ | ||
for (const auto &item : vector){ | ||
if (item == key) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
// Funkcja zwracająca tablicę kluczy z mapy. | ||
std::vector<int> transformMapKeysToVector(const std::map<int, bool> &map) { | ||
std::vector<int> vector; | ||
for (auto const& [key, value] : map) { | ||
if (value) { | ||
vector.push_back(key); | ||
} | ||
} | ||
return vector; | ||
} | ||
|
||
// Funkcja wypisująca elementy tablicy. | ||
void printVector(const std::vector<int> &vector) { | ||
std::cout << "Powtarzajace sie liczby: "; | ||
for (const auto &item : vector) { | ||
std::cout << item << " "; | ||
} | ||
std:: cout << std::endl << std::endl; | ||
} | ||
|
||
// Funkcja zwracająca tablicę ze wspólnymi elementami wszystkich wierszy. | ||
std::vector<int> getCommonElements(std::vector<std::vector<int>> vector) { // Jako argument przyjmujemy tablicę dwuwymiarową. | ||
auto start = std::chrono::high_resolution_clock::now(); // Timestamp do obliczenia czasu wykonywania funkcji. | ||
std::map<int, bool> appearanceToElement; // Deklaracja mapy przechowującej klucze[elementy tablicy] i wartości[prawda/fałsz]. | ||
std::vector<int> commonElements; // Deklaracja tablicy, która będzie zwracana. | ||
for(const auto &item : vector[0]) { // Pętla for iterująca po każdym elemencie w pierwszej tablicy. | ||
for(unsigned int i = 0; i <= (int)vector.size() - 1; i++) { // Pętla for iterująca po każdej tablicy. | ||
if (linearSearch(item, vector[i])) { // Jeśli element występuje w i-tej tablicy. | ||
appearanceToElement[item] = true; // Ustaw jego wartość w mapie na prawdę. | ||
} else { // Jeśli element nie występuje w i-tej tablicy. | ||
appearanceToElement[item] = false; // Ustaw jego wartość w mapie na fałsz. | ||
break; // Przerwij pętlę. | ||
} | ||
} | ||
} | ||
commonElements = transformMapKeysToVector(appearanceToElement); // Zamiana kluczy[elementów wspólnych wszystkich tablic] mapy na tablicę do zwrócenia. | ||
|
||
auto stop = std::chrono::high_resolution_clock::now(); // Timestamp do obliczenia czasu wykonywania funkcji. | ||
auto duration = duration_cast<std::chrono::microseconds>(stop - start); // Obliczenie czasu wykonywania funkcji w ms. | ||
std::cout << "Czas wykonania: " << duration.count() << "us" << std::endl; | ||
|
||
return commonElements; // Zwrócenie wyniku. | ||
} | ||
|
||
int main() { | ||
printVector(getCommonElements(example1)); | ||
printVector(getCommonElements(example2)); | ||
printVector(getCommonElements(example3)); | ||
printVector(getCommonElements(example4)); | ||
printVector(getCommonElements(example5)); | ||
printVector(getCommonElements(example6)); | ||
printVector(getCommonElements(getRandom2DVector(300, 120, 100))); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
1. inicjuj tab[0...n][0...m] | ||
2. inicjuj wynik[] | ||
3. inicjuj mapa[klucz][wartość] | ||
4. dla i <- 1 do n powtarzaj | ||
5. dla j <- 0 do długość(tab[i][m]) powtarzaj | ||
6. jeżeli tab[i][j] znajduje się w tab[0] to | ||
mapa[tab[0][i]][1] | ||
7. w przeciwnym razie | ||
mapa[tab[0][i]][0] | ||
8. do wynik[] dodaj każdy mapa[key][1] | ||
9. zwróć wynik[] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <vector> | ||
|
||
// Funkcja generująca losową macierz 2-wymiarową z podanymi w argumentach właściwościami [zakres liczb, liczba wierszy, liczba kolumn]. | ||
std::vector<std::vector<int>> getRandom2DVector(int range, int rows, int columns) { | ||
std::vector<std::vector<int>> random2DVector; | ||
srand((unsigned) time(NULL)); | ||
for (unsigned int i = 0; i < rows; i++) { | ||
std::vector<int> temp; | ||
for (unsigned int j = 0; j < columns; j++) { | ||
int random_number = 1 + rand() % range; | ||
temp.push_back(random_number); | ||
} | ||
random2DVector.push_back(temp); | ||
} | ||
return random2DVector; | ||
} | ||
|
||
// Funkcja wypisująca elementy tablicy 2-wymiarowej. | ||
void print2DVector(const std::vector<std::vector<int>> &vector) { | ||
for (const auto &row : vector) { | ||
for (const auto &item: row) { | ||
std::cout << item << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
} | ||
|
||
// Funkcja wypisująca klucze i wartości mapy. | ||
void printMap(const std::map<int, bool> &map) { | ||
for (auto const& [key, value] : map) { | ||
std::cout << " " << key << ": " << value << "\n"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
.idea/.name | ||
.idea/inspectionProfiles/Project_Default.xml | ||
.idea/misc.xml | ||
.idea/modules.xml | ||
.idea/projekt-2-cpp.iml | ||
.idea/vcs.xml | ||
cmake-build-debug/.cmake/api/v1/query/cache-v2 | ||
cmake-build-debug/.cmake/api/v1/query/cmakeFiles-v1 | ||
cmake-build-debug/.cmake/api/v1/query/codemodel-v2 | ||
cmake-build-debug/.cmake/api/v1/query/toolchains-v1 | ||
cmake-build-debug/.cmake/api/v1/reply/cache-v2-6e2976c4c578059e76fc.json | ||
cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-bda97756539227417564.json | ||
cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-ee51fbf8950d3e1e6ce4.json | ||
cmake-build-debug/.cmake/api/v1/reply/directory-.-Debug-d0094a50bb2071803777.json | ||
cmake-build-debug/.cmake/api/v1/reply/index-2022-12-08T20-52-34-0447.json | ||
cmake-build-debug/.cmake/api/v1/reply/target-projekt_2_cpp-Debug-acfce5e6db68d8646496.json | ||
cmake-build-debug/.cmake/api/v1/reply/toolchains-v1-879ae172463b8b264ea5.json | ||
cmake-build-debug/.ninja_deps | ||
cmake-build-debug/.ninja_log | ||
cmake-build-debug/build.ninja | ||
cmake-build-debug/cmake_install.cmake | ||
cmake-build-debug/CMakeCache.txt | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeCCompiler.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeCXXCompiler.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeDetermineCompilerABI_C.bin | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeDetermineCompilerABI_CXX.bin | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeRCCompiler.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CMakeSystem.cmake | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdC/a.exe | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdC/CMakeCCompilerId.c | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdCXX/a.exe | ||
cmake-build-debug/CMakeFiles/3.23.2/CompilerIdCXX/CMakeCXXCompilerId.cpp | ||
cmake-build-debug/CMakeFiles/clion-environment.txt | ||
cmake-build-debug/CMakeFiles/clion-log.txt | ||
cmake-build-debug/CMakeFiles/cmake.check_cache | ||
cmake-build-debug/CMakeFiles/CMakeOutput.log | ||
cmake-build-debug/CMakeFiles/projekt_2_cpp.dir/main.cpp.obj | ||
cmake-build-debug/CMakeFiles/rules.ninja | ||
cmake-build-debug/CMakeFiles/TargetDirectories.txt | ||
cmake-build-debug/projekt_2_cpp.exe | ||
cmake-build-debug/Testing/Temporary/LastTest.log | ||
CMakeLists.txt | ||
cmake-build-debug/output200000insertionSort.txt | ||
cmake-build-debug/output200000mergeSort.txt |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# projekt-2-cpp | ||
Drugi projekt z Algorytmów i struktur danych zaimplementowany w c++ |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
void printArray(int arr[], int size) { | ||
for (int i = 0; i < size; i++) | ||
std::cout << arr[i] << " "; | ||
} | ||
|
||
int getArraySize(string fileName) { | ||
int size = 0; | ||
ifstream File; | ||
File.open(fileName); | ||
if(File.is_open()) { | ||
File >> size; | ||
} | ||
File.close(); | ||
return size; | ||
} | ||
|
||
void getArrayFromFile(int array[], string fileName) { | ||
int arraySize = 0; | ||
int counter = 1; | ||
ifstream File; | ||
File.open(fileName); | ||
File.clear(); | ||
if(File.is_open()) { | ||
File >> arraySize; | ||
while(counter < arraySize + 1) { | ||
cout << counter << endl; | ||
File >> array[counter - 1]; | ||
counter++; | ||
} | ||
} | ||
File.close(); | ||
} | ||
|
||
void writeArrayToFile(int array[], int arraySize, auto duration, string sortType, string fileName) { | ||
if(sortType == "mergeSort") { | ||
sortType = "scalanie"; | ||
} | ||
if(sortType == "insertionSort") { | ||
sortType = "wstawianie"; | ||
} | ||
ofstream File; | ||
File.open(fileName); | ||
File.clear(); | ||
if(File.is_open()) { | ||
File << "Sortowanie przez "<< sortType <<" dla tablicy o " << arraySize << " elementow - czas: " << duration << "ms" << endl; | ||
File << arraySize << endl; | ||
for(int i = 0; i < arraySize; i++) { | ||
File << array[i] << " "; | ||
} | ||
} | ||
File.close(); | ||
} | ||
|
||
void printVector(const std::vector<string> &vector) { | ||
for (const auto &item : vector) { | ||
std::cout << item << " "; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Funkcja sortuje tablicę poprzez wstawianie elementów na odpowiednie indeksy. | ||
void insertionSort(int array[], int size) { | ||
// Za iterator bierzemy step = 1, ponieważ zakładamy, że pierwszy element jest posortowany. | ||
for (int i = 1; i < size; i++) { | ||
int currElement = array[i]; // Do zmiennej currElement przypisujemy liczbę, którą chcemy wstawić w odpowiednie miejsce. | ||
int j = i - 1; | ||
|
||
// Dopóki po lewej stronie są większe liczby | ||
while (currElement > array[j] && j >= 0) { | ||
array[j + 1] = array[j]; // Przesuwamy je o jeden indeks w prawo | ||
--j; | ||
} | ||
|
||
array[j + 1] = currElement; // Wstawiamy liczbę na odpowiednie miejsce. | ||
} | ||
} |