From 3077d1581413a9ae93a8a1fe1169d89318467147 Mon Sep 17 00:00:00 2001 From: ReallyWeirdCat <94784134+ReallyWeirdCat@users.noreply.github.com> Date: Sun, 14 May 2023 13:24:58 +0300 Subject: [PATCH] Add files via upload --- "\320\237\320\24014/1/divisor.cpp" | 24 ++++++++++++ "\320\237\320\24014/1/divisor.h" | 13 +++++++ "\320\237\320\24014/1/main.cpp" | 11 ++++++ "\320\237\320\24014/2/main.cpp" | 20 ++++++++++ "\320\237\320\24014/2/nicearray.cpp" | 55 ++++++++++++++++++++++++++++ "\320\237\320\24014/2/nicearray.h" | 15 ++++++++ 6 files changed, 138 insertions(+) create mode 100644 "\320\237\320\24014/1/divisor.cpp" create mode 100644 "\320\237\320\24014/1/divisor.h" create mode 100644 "\320\237\320\24014/1/main.cpp" create mode 100644 "\320\237\320\24014/2/main.cpp" create mode 100644 "\320\237\320\24014/2/nicearray.cpp" create mode 100644 "\320\237\320\24014/2/nicearray.h" diff --git "a/\320\237\320\24014/1/divisor.cpp" "b/\320\237\320\24014/1/divisor.cpp" new file mode 100644 index 0000000..f839cb1 --- /dev/null +++ "b/\320\237\320\24014/1/divisor.cpp" @@ -0,0 +1,24 @@ +#include "divisor.h" + +Divisor::Divisor(int X, int Y) +{ + x = X; y = Y; + try { + if (y == 0) throw std::runtime_error("Impossible to divide by zero."); + res = x / y; + } + catch (std::runtime_error& e) + { + res = 0; + std::cout << e.what() << std::endl; + } +} + +Divisor::~Divisor() +{ +} + +double Divisor::result() +{ + return res; +} diff --git "a/\320\237\320\24014/1/divisor.h" "b/\320\237\320\24014/1/divisor.h" new file mode 100644 index 0000000..dc681b0 --- /dev/null +++ "b/\320\237\320\24014/1/divisor.h" @@ -0,0 +1,13 @@ +#pragma once +#include +#include + +class Divisor +{ + double x, y; + double res; +public: + Divisor(int X, int Y); + ~Divisor(); + double result(); +}; \ No newline at end of file diff --git "a/\320\237\320\24014/1/main.cpp" "b/\320\237\320\24014/1/main.cpp" new file mode 100644 index 0000000..88ba381 --- /dev/null +++ "b/\320\237\320\24014/1/main.cpp" @@ -0,0 +1,11 @@ +#include +#include "divisor.h" + +int main() +{ + double x, y, z; + std::cin >> x >> y; + + z = Divisor(x, y).result(); + std::cout << "Result: " << z << std::endl; +} \ No newline at end of file diff --git "a/\320\237\320\24014/2/main.cpp" "b/\320\237\320\24014/2/main.cpp" new file mode 100644 index 0000000..924a62f --- /dev/null +++ "b/\320\237\320\24014/2/main.cpp" @@ -0,0 +1,20 @@ +#include +#include "nicearray.h" + +int main() +{ + std::cout << "array size: "; + int SIZE; + std::cin >> SIZE; + nicearray arr(SIZE); + + std::cout << "We offer you to add 3 elements to the array." << std::endl; + for (int i = 0; i < 3; i++) + { + std::cout << "[format: index number]: "; + int idx, num; + std::cin >> idx >> num; + if (!arr.add(num, idx)) i--; + } + arr.show(); +} \ No newline at end of file diff --git "a/\320\237\320\24014/2/nicearray.cpp" "b/\320\237\320\24014/2/nicearray.cpp" new file mode 100644 index 0000000..ec21647 --- /dev/null +++ "b/\320\237\320\24014/2/nicearray.cpp" @@ -0,0 +1,55 @@ +#include "nicearray.h" + +nicearray::nicearray(int Size) +{ + size = Size; + array = new int[size](); +} + +nicearray::~nicearray() +{ + +} + +bool nicearray::add(int num, int idx) +{ + try { + if (idx < 0 || size <= idx) + { + throw std::range_error("Could not insert at idx: out of range"); + } + array[idx] = num; + return true; + } + catch (std::range_error& e) + { + std::cout << e.what() << std::endl; + return false; + } +} + +int nicearray::get(int idx) +{ + try { + if (idx < 0 || size <= idx) + { + throw std::range_error("Could not read at idx: out of range"); + } + return array[idx]; + } + catch (std::range_error& e) + { + std::cout << e.what() << std::endl; + return 0; + } +} + +void nicearray::show() +{ + std::cout << "["; + for (int i = 0; i < size; i++) + { + std::cout << array[i] << ", "; + } + std::cout << "]" << std::endl; +} diff --git "a/\320\237\320\24014/2/nicearray.h" "b/\320\237\320\24014/2/nicearray.h" new file mode 100644 index 0000000..79515ad --- /dev/null +++ "b/\320\237\320\24014/2/nicearray.h" @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +class nicearray +{ + int* array; + int size; +public: + nicearray(int Size); + ~nicearray(); + bool add(int num, int idx); + int get(int idx); + void show(); +}; \ No newline at end of file