update 05.03.24

This commit is contained in:
ZennDev1337 2024-03-05 09:58:45 +01:00
parent 6ecbab2b64
commit 6a3935cac2
5 changed files with 68 additions and 62 deletions

11
src/algo.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "algo.h"
void bubble_sort_print_demo()
{
int demo[]{11, 14, 3, 18, 8, 17, 43, 6, 5, 4};
double demo2[]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3, 12.3};
std::string demo3[]{"hallo", "velo", "kekw", "imagin"};
bubble_sort_print(sizeof(demo) / sizeof(demo[0]), demo);
bubble_sort_print(sizeof(demo2) / sizeof(demo2[0]), demo2);
bubble_sort_print(sizeof(demo3) / sizeof(demo3[0]), demo3);
}

55
src/algo.h Normal file
View file

@ -0,0 +1,55 @@
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
void bubble_sort_print_demo();
template <typename T>
struct TypeName
{
static const char *Get()
{
return typeid(T).name();
}
};
template <>
struct TypeName<std::string>
{
static const char *Get()
{
return "s";
}
};
template <class T>
void bubble_sort(int size, T *a)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
T temp{a[j]};
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
};
template <typename T>
void bubble_sort_print(int size, T *a)
{
std::cout << std::left; // left justify output
std::cout << "unsorted " << TypeName<T>::Get() << std::setw(10) << " Array: ";
for (int i = 0; i < size; i++)
std::cout << a[i] << ", ";
std::cout << "\n";
bubble_sort(size, a);
std::cout << "sorted " << TypeName<T>::Get() << std::setw(12) << " Array: ";
for (int i = 0; i < size; i++)
std::cout << a[i] << ", ";
std::cout << "\n\n";
};

View file

@ -1,6 +1,7 @@
#include "io.h"
#include <iostream>
#include <iomanip>
void add_two_numbers()
{
int a{read_number()};
@ -39,12 +40,3 @@ void print_type_size_bytes()
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
std::cout << std::setw(16) << "long double:" << sizeof(long double) << " bytes\n\n";
}
void bubble_sort_print_demo()
{
int demo[]{11, 14, 3, 18, 8, 17, 43, 6, 5, 4};
double demo2[]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3, 12.3};
std::string demo3[]{"hallo", "velo", "kekw", "imagin"};
bubble_sort_print(sizeof(demo) / sizeof(demo[0]), demo);
bubble_sort_print(sizeof(demo2) / sizeof(demo2[0]), demo2);
bubble_sort_print(sizeof(demo3) / sizeof(demo3[0]), demo3);
}

View file

@ -1,60 +1,7 @@
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
void add_two_numbers();
int read_number();
void write_answer(int x);
void convert_char_to_ascii();
void print_type_size_bytes();
void bubble_sort_print_demo();
template <typename T>
struct TypeName
{
static const char *Get()
{
return typeid(T).name();
}
};
template <>
struct TypeName<std::string>
{
static const char *Get()
{
return "s";
}
};
template <class T>
void bubble_sort(int size, T *a)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
T temp{a[j]};
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
};
template <typename T>
void bubble_sort_print(int size, T *a)
{
std::cout << std::left; // left justify output
std::cout << "unsorted " << TypeName<T>::Get() << std::setw(10) << " Array: ";
for (int i = 0; i < size; i++)
std::cout << a[i] << ", ";
std::cout << "\n";
bubble_sort(size, a);
std::cout << "sorted " << TypeName<T>::Get() << std::setw(12) << " Array: ";
for (int i = 0; i < size; i++)
std::cout << a[i] << ", ";
std::cout << "\n\n";
};

View file

@ -1,5 +1,6 @@
#include <iostream> // for std::cout
#include "io.h"
#include "algo.h"
int main()
{