This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfc0edb
commit 3077d15
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <stdexcept> | ||
|
||
class Divisor | ||
{ | ||
double x, y; | ||
double res; | ||
public: | ||
Divisor(int X, int Y); | ||
~Divisor(); | ||
double result(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <iostream> | ||
#include "divisor.h" | ||
|
||
int main() | ||
{ | ||
double x, y, z; | ||
std::cin >> x >> y; | ||
|
||
z = Divisor(x, y).result(); | ||
std::cout << "Result: " << z << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
#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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <stdexcept> | ||
|
||
class nicearray | ||
{ | ||
int* array; | ||
int size; | ||
public: | ||
nicearray(int Size); | ||
~nicearray(); | ||
bool add(int num, int idx); | ||
int get(int idx); | ||
void show(); | ||
}; |