-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
124 lines (104 loc) · 2.21 KB
/
main.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* main.cpp
*
* Created on: 23.02.2017 г.
* Author: trifon
*/
#include <iostream>
#include <cmath>
// using namespace std;
#include "rational.h"
#include "rational.h"
#include "rational_point.h"
/*
* !!!
class Rational {
int numer, denom;
// ...
};
*/
long fact(int n) {
return n == 0 ? 1 : n * fact(n - 1);
}
Rational myexp(int k, int n) {
Rational sum;
for(int i = 0; i <= n; i++)
sum = add(sum,Rational(pow(k, i),fact(i)));
return sum;
}
const int endl = 10;
void testRational() {
Rational r;
// Rational s = { 1, 2 };
Rational s(1, 2);
// Rational s = Rational(1, 2);
// !!! cout << s.numer << '/' << s.denom << endl;
// std::cout << s->numer << '/' << s->denom << std::endl;
std::cout << s.getNumerator() << '/' << s.getDenominator() << std::endl;
s.print();
std::cout << s.toDouble() << std::endl;
s.read();
s.print();
std::cout << s.toDouble() << std::endl;
add(s, Rational(1, 2)).print();
subtract(s, Rational(1, 2)).print();
multiply(s, Rational(1, 2)).print();
divide(s, Rational(1, 2)).print();
divide(s, r).print();
Rational(5, 0).print();
// !!! s.denom = 0;
Rational er = myexp(1, 10);
er.print();
std::cout << er.toDouble() << std::endl;
// !!! er.numer = 5;
// Не е препоръчително
// !!! class A { int a; public: int getA() const; } objectA;
Rational* ps = &s;
// !!! *ps.print();
// !!! *(ps.print());
(*ps).print();
ps->print();
s.toDouble();
ps->toDouble();
// !!! s.reduce();
// !!! ps->reduce();
int endl = 5;
std::cout << s.getNumerator() << endl;
std::cout << s.getNumerator() << ::endl;
std::cout << s.getNumerator() << std::endl;
}
Rational addOne(Rational r) {
return add(r, Rational(1));
}
void testCopy() {
Rational r(2, 3);
r.print();
// Rational p(r);
// Rational p = Rational(r);
Rational p = r;
p.print();
addOne(p).print();
}
void testRationalPoint() {
RationalPoint rp;
rp.print();
RationalPoint rp2(7, Rational(5, 3));
rp2.print();
RationalPoint rp3 = rp2;
rp3.print();
}
void testArrays() {
Rational *ar = new Rational[5];
for(int i = 0; i < 5; i++) {
ar[i].print();
std::cout << std::endl;
}
delete ar;
}
int main() {
// testRational();
// testCopy();
// testRationalPoint();
testArrays();
return 0;
}