forked from taniadovzhenko/CppPracticum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratio1.h
More file actions
99 lines (69 loc) · 1.91 KB
/
ratio1.h
File metadata and controls
99 lines (69 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _RATIO1_H_
#define _RATIO1_H_
//#pragma once
#include <iostream>
#include <cmath>
#include <exception>
using namespace std;
class ZeroDenominator : public exception{
public:
const char* what(){
return "Denominator should be non zero!";
}
};
//struct
class Rat{
private:
//public:
long long int nominator;
long long unsigned denominator;
//
public:
Rat(){nominator=1; denominator=1;}
Rat(long long int x, long long unsigned y): nominator(x),denominator(y){
if(denominator==0) { // denominator <> 0 !!! exception
throw ZeroDenominator();
}
reduce();
}
//Rat(int x, unsigned y){ nominator=x;denominator =y;}
int input(){
cout<<"Input n,m:";
cin>>nominator>>denominator;
if(denominator==0) { // denominator <> 0 !!! exception
throw ZeroDenominator();
}
}
void output(){
cout<<nominator<<"/"<<denominator<<endl;
}
void setNom(long long int nominator){
this->nominator = nominator; //
}
int setDenom(long long unsigned denominator){
if(denominator==0) { // denominator <> 0 !!! exception
throw ZeroDenominator();
}
this->denominator = denominator; //
return 0;
}
Rat add(const Rat & other); // addition z = x.add(y); // x+y
Rat mult(const Rat & other);
// subtraction, division
Rat division(const Rat &other);
Rat subtraction(const Rat &other);
void reduce();
bool less(const Rat& r);
bool operator>(const Rat & r) {
return (nominator*r.denominator > denominator*r.nominator);
}
bool greaterReal(double eps){
return (double)nominator/denominator>eps;
}
double value12(){
return sqrt(12.0*(double)nominator/denominator);
}
friend int saveToFile(const char* fname, const Rat & r);
friend int readFromFile(const char* fname, Rat & r);
};
#endif //_RATIO1_H_