Compare commits
11 commits
chapter4qu
...
main
Author | SHA1 | Date | |
---|---|---|---|
4a29e375f5 | |||
6a3935cac2 | |||
6ecbab2b64 | |||
69b4f6ecf8 | |||
c50ef8bfe3 | |||
921ca197bf | |||
3f5945ca8c | |||
29ee27dbab | |||
5911856dc6 | |||
7f0548cd69 | |||
79644bad85 |
5 changed files with 89 additions and 71 deletions
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";
|
||||||
|
};
|
16
src/io.cpp
16
src/io.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
void add_two_numbers()
|
void add_two_numbers()
|
||||||
{
|
{
|
||||||
int a{read_number()};
|
int a{read_number()};
|
||||||
|
@ -25,6 +26,7 @@ void convert_char_to_ascii()
|
||||||
std::cin >> c;
|
std::cin >> c;
|
||||||
std::cout << "You entered '" << c << "', which hass ASCII code " << static_cast<int>(c) << ".";
|
std::cout << "You entered '" << c << "', which hass ASCII code " << static_cast<int>(c) << ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_type_size_bytes()
|
void print_type_size_bytes()
|
||||||
{
|
{
|
||||||
std::cout << std::left; // left justify output
|
std::cout << std::left; // left justify output
|
||||||
|
@ -36,5 +38,17 @@ void print_type_size_bytes()
|
||||||
std::cout << std::setw(16) << "long long:" << sizeof(long 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) << "float:" << sizeof(float) << " bytes\n";
|
||||||
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
|
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
|
||||||
std::cout << std::setw(16) << "long double:" << sizeof(long 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";
|
||||||
}
|
}
|
1
src/io.h
1
src/io.h
|
@ -5,3 +5,4 @@ int read_number();
|
||||||
void write_answer(int x);
|
void write_answer(int x);
|
||||||
void convert_char_to_ascii();
|
void convert_char_to_ascii();
|
||||||
void print_type_size_bytes();
|
void print_type_size_bytes();
|
||||||
|
void string_demo();
|
73
src/main.cpp
73
src/main.cpp
|
@ -1,77 +1,14 @@
|
||||||
#include <iostream> // for std::cout
|
#include <iostream> // for std::cout
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include <cmath>
|
#include "algo.h"
|
||||||
|
|
||||||
// Definition of function main()
|
|
||||||
double get_user_double()
|
|
||||||
{
|
|
||||||
std::cout << "Enter a double value: ";
|
|
||||||
double x{};
|
|
||||||
std::cin >> x;
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
char get_user_operator()
|
|
||||||
{
|
|
||||||
std::cout << "Enter +, -, * or /: ";
|
|
||||||
char x{};
|
|
||||||
std::cin >> x;
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
void print_result(double a, double b, char c)
|
|
||||||
{
|
|
||||||
double result{};
|
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case '+':
|
|
||||||
result = a + b;
|
|
||||||
break;
|
|
||||||
case '-':
|
|
||||||
result = a - b;
|
|
||||||
break;
|
|
||||||
case '*':
|
|
||||||
result = a * b;
|
|
||||||
break;
|
|
||||||
case '/':
|
|
||||||
result = a / b;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::cout << a << ' ' << c << ' ' << b << " is " << result;
|
|
||||||
}
|
|
||||||
void task1()
|
|
||||||
{
|
|
||||||
double a{get_user_double()};
|
|
||||||
double b{get_user_double()};
|
|
||||||
char c{get_user_operator()};
|
|
||||||
print_result(a, b, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
long get_user_tower_height()
|
|
||||||
{
|
|
||||||
std::cout << "Enter the height of the tower in meters: ";
|
|
||||||
long x{};
|
|
||||||
std::cin >> x;
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
void task2()
|
|
||||||
{
|
|
||||||
long tower_height{get_user_tower_height()};
|
|
||||||
for (size_t i = 0; i < 6; i++)
|
|
||||||
{
|
|
||||||
double result{static_cast<double>(tower_height) - 9.8 * pow(static_cast<double>(i), 2) / 2};
|
|
||||||
if (result <= 0.0)
|
|
||||||
std::cout << "At " << i << " seconds, the ball is on the ground\n";
|
|
||||||
else
|
|
||||||
std::cout << "At " << i << " seconds, the ball is at height: " << result << " meters\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
// string_demo();
|
||||||
// add_two_numbers();
|
// add_two_numbers();
|
||||||
// convert_char_to_ascii();
|
// convert_char_to_ascii();
|
||||||
// print_type_size_bytes();
|
print_type_size_bytes();
|
||||||
// task1();
|
// bubble_sort_print_demo();
|
||||||
task2();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue