From a577cee5a0b44aadadd2f0932a794310bb775a83 Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 4 Sep 2024 16:26:22 +0200 Subject: more exercise --- 5p22.cpp | 31 +++++++++++++++++++++++++++++++ 5p23.cpp | 17 +++++++++++++++++ 5p24.cpp | 21 +++++++++++++++++++++ 5p25.cpp | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 5p22.cpp create mode 100644 5p23.cpp create mode 100644 5p24.cpp create mode 100644 5p25.cpp diff --git a/5p22.cpp b/5p22.cpp new file mode 100644 index 0000000..9bc9a0f --- /dev/null +++ b/5p22.cpp @@ -0,0 +1,31 @@ +#include + +/* + * + * 5.22 + * + * + */ + +int get_size() { + + return 2; +} + +int main () { + + begin: + int sz = get_size(); + if (sz <= 0) { + goto begin; + } + + for(int sz1 = get_size() ; sz1 <= 0 ; sz1 = get_size()) {} + + int sz2 = get_size(); + while(sz2 <= 0) { + sz2 = get_size(); + } + + return 0; +} diff --git a/5p23.cpp b/5p23.cpp new file mode 100644 index 0000000..861cce0 --- /dev/null +++ b/5p23.cpp @@ -0,0 +1,17 @@ +#include + +/* + * + * 5.23 + * + * + */ + +int main () { + + int a = 0; + int b = 0; + std::cin >> a >> b; + std::cout << a / b << std::endl; + return 0; +} diff --git a/5p24.cpp b/5p24.cpp new file mode 100644 index 0000000..d8a7bc9 --- /dev/null +++ b/5p24.cpp @@ -0,0 +1,21 @@ +#include + +/* + * + * 5.24 + * + * + */ + +int main () { + + int a = 0; + int b = 0; + std::cin >> a >> b; + if(b == 0) { + throw std::runtime_error("You cannot divide by 0!"); + } + + std::cout << a / b << std::endl; + return 0; +} diff --git a/5p25.cpp b/5p25.cpp new file mode 100644 index 0000000..6a3fbf6 --- /dev/null +++ b/5p25.cpp @@ -0,0 +1,37 @@ +#include +#include + +/* + * + * 5.23 + * + * + */ + +int main () { + + int a = 0; + int b = 0; + bool d = true; + while(d) { + d = false; + try { + std::cin >> a >> b; + if(b == 0) { + throw std::runtime_error("divide by 0!"); + } + + std::cout << a / b << std::endl; + } catch(std::runtime_error &err) { + std::cout << err.what() << "\nTry again? Y/N " << std::endl; + char c = 0; + std::cin >> c; + if(c == 'Y' || c == 'y') { + d = true; + } + } + + } + + return 0; +} -- cgit v1.2.3