Compare commits
19 commits
Include-li
...
main
Author | SHA1 | Date | |
---|---|---|---|
4a29e375f5 | |||
6a3935cac2 | |||
6ecbab2b64 | |||
69b4f6ecf8 | |||
c50ef8bfe3 | |||
921ca197bf | |||
3f5945ca8c | |||
29ee27dbab | |||
5911856dc6 | |||
7f0548cd69 | |||
79644bad85 | |||
3964df5916 | |||
ebe10eb632 | |||
15d6472161 | |||
0b7f487152 | |||
95bbaa9aab | |||
943f09563e | |||
ee652cb6ee | |||
cda6ed407a |
11 changed files with 150 additions and 36 deletions
|
@ -4,7 +4,7 @@
|
||||||
"name": "Debug: C/C++",
|
"name": "Debug: C/C++",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe",
|
"program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
|
@ -30,7 +30,7 @@
|
||||||
"name": "Release: C/C++",
|
"name": "Release: C/C++",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe",
|
"program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
|
|
|
@ -6,12 +6,13 @@
|
||||||
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
|
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
|
||||||
"args": [
|
"args": [
|
||||||
"-fdiagnostics-color=always",
|
"-fdiagnostics-color=always",
|
||||||
|
"-I${workspaceFolder}\\include",
|
||||||
"-ggdb",
|
"-ggdb",
|
||||||
"-std=c++20",
|
"-std=c++20",
|
||||||
"-pedantic-errors",
|
"-pedantic-errors",
|
||||||
"${fileDirname}/**.cpp",
|
"${workspaceFolder}/src/**.cpp",
|
||||||
"-o",
|
"-o",
|
||||||
"${fileDirname}\\build\\${fileBasenameNoExtension}.exe"
|
"${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe"
|
||||||
],
|
],
|
||||||
"options": {
|
"options": {
|
||||||
"cwd": "${fileDirname}"
|
"cwd": "${fileDirname}"
|
||||||
|
@ -26,13 +27,14 @@
|
||||||
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
|
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
|
||||||
"args": [
|
"args": [
|
||||||
"-fdiagnostics-color=always",
|
"-fdiagnostics-color=always",
|
||||||
|
"-I${workspaceFolder}\\include",
|
||||||
"-O2",
|
"-O2",
|
||||||
"-DNDEBUG",
|
"-DNDEBUG",
|
||||||
"-std=c++20",
|
"-std=c++20",
|
||||||
"-pedantic-errors",
|
"-pedantic-errors",
|
||||||
"${fileDirname}/**.cpp",
|
"${workspaceFolder}/src/**.cpp",
|
||||||
"-o",
|
"-o",
|
||||||
"${fileDirname}\\build\\${fileBasenameNoExtension}.exe"
|
"${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe"
|
||||||
],
|
],
|
||||||
"options": {
|
"options": {
|
||||||
"cwd": "${fileDirname}"
|
"cwd": "${fileDirname}"
|
||||||
|
|
0
include/.gitkeep
Normal file
0
include/.gitkeep
Normal file
14
io.cpp
14
io.cpp
|
@ -1,14 +0,0 @@
|
||||||
#include "io.h"
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int readNumber()
|
|
||||||
{
|
|
||||||
std::cout << "Enter a number to add: ";
|
|
||||||
int x{};
|
|
||||||
std::cin >> x;
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
void writeAnswer(int x)
|
|
||||||
{
|
|
||||||
std::cout << "The answer is: " << x << "\n";
|
|
||||||
}
|
|
4
io.h
4
io.h
|
@ -1,4 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
int readNumber();
|
|
||||||
void writeAnswer(int x);
|
|
12
main.cpp
12
main.cpp
|
@ -1,12 +0,0 @@
|
||||||
#include <iostream> // for std::cout
|
|
||||||
#include "io.h"
|
|
||||||
|
|
||||||
// Definition of function main()
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
|
|
||||||
int a{readNumber()};
|
|
||||||
int b{readNumber()};
|
|
||||||
writeAnswer(a + b);
|
|
||||||
return 0;
|
|
||||||
}
|
|
11
src/algo.cpp
Normal file
11
src/algo.cpp
Normal 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
55
src/algo.h
Normal 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";
|
||||||
|
};
|
54
src/io.cpp
Normal file
54
src/io.cpp
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
#include "io.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
void add_two_numbers()
|
||||||
|
{
|
||||||
|
int a{read_number()};
|
||||||
|
int b{read_number()};
|
||||||
|
write_answer(a + b);
|
||||||
|
}
|
||||||
|
int read_number()
|
||||||
|
{
|
||||||
|
std::cout << "Enter a number to add: ";
|
||||||
|
int x{};
|
||||||
|
std::cin >> x;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
void write_answer(int x)
|
||||||
|
{
|
||||||
|
std::cout << "The answer is: " << x << "\n";
|
||||||
|
}
|
||||||
|
void convert_char_to_ascii()
|
||||||
|
{
|
||||||
|
std::cout << "Enter a single character: ";
|
||||||
|
char c{};
|
||||||
|
std::cin >> c;
|
||||||
|
std::cout << "You entered '" << c << "', which hass ASCII code " << static_cast<int>(c) << ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_type_size_bytes()
|
||||||
|
{
|
||||||
|
std::cout << std::left; // left justify output
|
||||||
|
std::cout << std::setw(16) << "bool:" << sizeof(bool) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "char:" << sizeof(char) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "short:" << sizeof(short) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "int:" << sizeof(int) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "long:" << sizeof(long) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "long long:" << sizeof(long long) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "float:" << sizeof(float) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
|
||||||
|
std::cout << std::setw(16) << "long double:" << sizeof(long double) << " bytes\n\n";
|
||||||
|
}
|
||||||
|
void string_demo()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
cout << "Enter your full name: ";
|
||||||
|
string name{};
|
||||||
|
getline(cin >> ws, name);
|
||||||
|
cout << "Enter your age: ";
|
||||||
|
int age{};
|
||||||
|
cin >> age;
|
||||||
|
int letters{static_cast<int>(name.length())};
|
||||||
|
cout << "Your age + length of name is: " << (letters + age) << "\n";
|
||||||
|
}
|
8
src/io.h
Normal file
8
src/io.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void add_two_numbers();
|
||||||
|
int read_number();
|
||||||
|
void write_answer(int x);
|
||||||
|
void convert_char_to_ascii();
|
||||||
|
void print_type_size_bytes();
|
||||||
|
void string_demo();
|
14
src/main.cpp
Normal file
14
src/main.cpp
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#include <iostream> // for std::cout
|
||||||
|
#include "io.h"
|
||||||
|
#include "algo.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// string_demo();
|
||||||
|
// add_two_numbers();
|
||||||
|
// convert_char_to_ascii();
|
||||||
|
print_type_size_bytes();
|
||||||
|
// bubble_sort_print_demo();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue