-
Notifications
You must be signed in to change notification settings - Fork 0
/
rational.cpp
102 lines (101 loc) · 2.25 KB
/
rational.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
//有理数实现
#include "rational.h"
#include "integer.h"
#include <iostream>
#include <string>
Rational::Rational(int a=0,int b=1):Object("Rational"),numerator(a),denominator(b)
{
}
Rational::~Rational()
{
}
int Rational::getValue1()
{
return numerator;
}
int Rational::getValue2()
{
return denominator;
}
void Rational::changeValue1(int a)
{
numerator = a;
}
void Rational::changeValue2(int a)
{
denominator = a;
}
void Rational::add(Object *b)
{
if(b->getType() == "Rational")
{
Rational *c = (Rational *)b;
//注意计算的先后顺序
this->numerator = (this->numerator)*(c->getValue2())+(this->denominator)*(c->getValue1());
this->denominator *= c->getValue2();
}
else if(b->getType() == "Integer")
{
Integer *c = (Integer *)b;
this->numerator += (c->getValue())*(this->denominator);
}
else
{
cerr << "Error calcution" << endl;
}
}
void Rational::sub(Object *b)
{
if(b->getType() == "Rational")
{
Rational *c = (Rational *)b;
//注意计算的先后顺序
this->numerator = (this->numerator)*(c->getValue2())-(this->denominator)*(c->getValue1());
this->denominator *= c->getValue2();
}
else if(b->getType() == "Integer")
{
Integer *c = (Integer *)b;
this->numerator -= (c->getValue())*(this->denominator);
}
else
{
cerr << "Error calcution" << endl;
}
}
void Rational::mul(Object *b)
{
if(b->getType() == "Rational")
{
Rational *c = (Rational *)b;
this->numerator *= c->getValue1();
this->denominator *= c->getValue2();
}
else if(b->getType() == "Integer")
{
Integer *c = (Integer *)b;
this->numerator *= c->getValue();
}
else
{
cerr << "Error calcution" << endl;
}
}
void Rational::div(Object *b)
{
if(b->getType() == "Rational")
{
Rational *c = (Rational *)b;
this->numerator *= c->getValue2();
this->denominator *= c->getValue1();
}
else if(b->getType() == "Integer")
{
Integer *c = (Integer *)b;
this->denominator *= c->getValue();
}
else
{
cerr << "Error calcution" << endl;
}
}