@wsa
0 / 0 / 0
Регистрация: 20.05.2013
Сообщений: 202
|
|
|
15.01.2014, 18:30. Просмотров 502. Ответов 2
Написать класс, для нахождения суммы обыкновенных дробей с выделенной целой частью. перегрузка суммы ввода и вывода. конструктор и деструктор. Помогите как его написать! очень срочно надо...
C++ (Qt) | 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
| #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
using namespace std;
class Rational
{
private:
int NOK(int a, int b);
int NOD(int a, int b);
int num, den ;
public:
Rational();//пустой список параметров
Rational(Rational &x); //конструктор
Rational(int n, int d);//дробная часть
~Rational();
void set_num(int n);
int get_num();
void set_den(int d);
int get_den();
void read();
void write();
Rational operator +(Rational &b);
};
Rational :: Rational()
{
num = 0;
den = 0;
}
Rational :: Rational(Rational &x)
{
num = x.get_num();
den = x.get_den();
}
Rational :: Rational(int n, int d)
{
num = n;
den = d;
}
Rational :: ~Rational()
{
}
void Rational :: set_num(int n)
{
num = n;
return;
}
int Rational :: get_num()
{
return num;
}
void Rational :: set_den(int d)
{
den = d;
return;
}
int Rational :: get_den()
{
return den;
}
int Rational :: NOK(int a, int b)
{
return (a*b)/NOD(a,b);
}
int Rational :: NOD(int a,int b)
{
while (a!=b)
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
return a;
}
// перегрузка операции
Rational Rational :: operator+ (Rational &b)
{
Rational s;
s.set_den(NOK(den, b.get_den()));
s.set_num ((num*s.get_den() / den) + (b.get_num() * s.get_den()/ b.get_den() ));
int i = NOD (s.get_num(), s.get_den());
s.set_num(s.get_num()/i);
s.set_den(s.get_den()/i);
return s;
}//operator +
void main()
{
setlocale(LC_ALL, "Russian");
Rational a(1,2);
Rational b(3,2);
Rational с=a+b;
с.write();
return;
} |
|
0
|