Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ReallyWeirdCat authored May 14, 2023
1 parent cfc0edb commit 3077d15
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ПР14/1/divisor.cpp
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;
}
13 changes: 13 additions & 0 deletions ПР14/1/divisor.h
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();
};
11 changes: 11 additions & 0 deletions ПР14/1/main.cpp
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;
}
20 changes: 20 additions & 0 deletions ПР14/2/main.cpp
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();
}
55 changes: 55 additions & 0 deletions ПР14/2/nicearray.cpp
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;
}
15 changes: 15 additions & 0 deletions ПР14/2/nicearray.h
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();
};

0 comments on commit 3077d15

Please sign in to comment.