利用C++预处理器,我们可以让头文件只在这个类还没有被声明过的情况下才声明它。作为一种固定模式,这里使用常量名通常与相应的文件名保持一致(换成大写),把句号替换为下划线。例如rational.h文件对应RATIONAL_H
1 Rational.h文件
2
3 //Ration.h
4 //Create by 亦我飞也
5
6 //这个头文件用来声明有理数类(Rational class)
7 //类里面对四则运算进行重载,以实现分数运算
8
9 //如果RATIONAL_H未被定义,则定义rational.h,执行定义类程序
10 //如果RATIONAL_H已经被定义,程序则直接跳到endif(即定义类程序只会执行一次)
11 #ifndef RATIONAL_H//如果没有定义rational.h
12
13 #define RATIONAL_H//定义rational.h
14
15 #include <iostream>
16
17 class Rational//定义基类
18 {
19 public:
20 Rational(int num,int denom); //构造器 num = 分子,denom = 分母
21
22 Rational operator+(Rational rhs);// rhs == right hand side(右手边参数)
23 Rational operator-(Rational rhs);
24 Rational operator*(Rational rhs);
25 Rational operator/(Rational rhs);
26
27 private:
28 void normalize();//负责对分数的简化处理
29
30 int numerator; //分子
31 int denominator; //分母
32
33 friend std::ostream& operator<<(std::ostream& os,Rational f);//传递给它的是哪一个流,它返回的就是那个流的一个引用
34 };
35
36 #endif//如果已经定义rational.h,则结束
37 Rational.cpp文件
38
39 #include <iostream>
40 #include <string>
41 #include <math.h>
42 #include "Rational.h"//系统级别用单尖括号,自定义级别用双引号
43
44 using namespace std;
45
46 Rational::Rational(int num,int denom)//构造函数实现
47 {
48 numerator = num;
49 denominator = denom;
50
51 normalize();
52 }
53 //normalize()对分数进行简化操作包括:
54 //1.只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2==-1/2
55 //2.利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5
56
57 void Rational::normalize()
58 {
59 //确保分母为正
60 if( denominator < 0)
61 {
62 numerator = -numerator;
63 denominator = -denominator;
64 }
65 //欧几里德算法
66 int a = abs(numerator);
67 int b = abs(denominator);
68
69 //求出最大公约数
70 while(b>0)
71 {
72 int t = a % b;//取余
73 a = b;
74 b = t;
75 }
76
77 //分子、分母分别除以最大公约数得到最简化分数
78 numerator /= a;
79 denominator /= a;
80 }
81 //a c a*d c*b a*d + c*d
82 //- + - = --- + --- = ----------
83 //b d b*d b*d b*d
84 Rational Rational::operator+(Rational rhs)//分数的加运算
85 {
86 int a = numerator;
87 int b = denominator;
88 int c = rhs.numerator;
89 int d = rhs.denominator;
90
91 int e = a*b + c*d;
92 int f = b*d;
93
94 return Rational(e,f);
95 }
96 //a c a -c
97 //- - - = - + --
98 //b d b d
99 Rational Rational::operator-(Rational rhs)//分数的减运算
100 {
101 rhs.numerator = -rhs.numerator; //被减数分子取负数
102 return operator+(rhs);
103 }
104 //a c a*c
105 //- * - = ---
106 //b d b*d
107 Rational Rational::operator*(Rational rhs)//分数的乘运算
108 {
109 int a = numerator;
110 int b = denominator;
111 int c = rhs.numerator;
112 int d = rhs.denominator;
113
114 int e = a*c;
115 int f = b*d;
116
117 return Rational(e,f);
118 }
119 //a c a d
120 //- / - = - * -
121 //b d b c
122 Rational Rational::operator/(Rational rhs)//分数的除运算
123 {
124 //rhs的分子分母互换
125 int t = rhs.numerator;
126 rhs.numerator = rhs.denominator;
127 rhs.denominator = t;
128
129 return operator*(rhs);
130 }
131
132 ostream& operator<<(ostream& os,Rational f)//并不属于Rational类,是一个独立的函数
133 {
134 if(f.numerator % f.denominator == 0)
135 os << f.numerator / f.denominator;
136 else
137 os << f.numerator << "/" << f.denominator;//打印分数
138 return os;
139 }
140
141 void call_love_fishc()//输出爱心
142 {
143 int i,j;
144 int n = 10;
145
146 for(i=1-(n>>1);i<=n;i++)//>>右移,右移一位除以2,左移一位乘以2
147 {
148 cout << "Hello World!";
149 if(i>=0)
150 {
151 for(j=0;j<i;j++)
152 cout << " ";
153 for(j=1;j<=2*(n-i)+1;j++)
154 cout << " *";
155 cout << endl;
156 }
157 else
158 {
159 for(j=i;j<0;j++)
160 cout << " ";
161 for(j=1;j<=n+2*i+1;j++)
162 cout << " *";
163 for(j=1;j<=-1-2*i;j++)
164 cout << " ";
165 for(j=1;j<=n+2*i+1;j++)
166 cout << " *";
167 cout << endl;
168 }
169 }
170
171 }
172
173 main.cpp文件
174
175 #include <iostream>
176 #include "includes\\fishc.h"//此处表示fish.h在includes目录中 .\\表示在当前目录 ..\\表示在上一级目录
177 #include "Rational.h"//系统级别用单尖括号,自定义级别用双引号
178 #include <string>
179 #include <math.h>
180
181 using namespace std;
182 int main()
183 {
184 call_love_fishc();//输出爱心
185
186 Rational f1(2,16);//定义f1对象,且传入(2,16)参数
187 Rational f2(7,8);
188
189 //测试有理数加法运算
190 cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n"; // 左移操作符<<已经被重载了
191
192 //测试有理数减法运算
193 cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n"; //<< f1中由于f1属于Rational类型,将自动转到打印分数形式输出
194
195 //测试有理数乘法运算
196 cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";//<< "+"中由于<<后面参数是字符串,所以使用系统默认的打印
197
198 //测试有理数除法运算
199 cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";
200
201 return 0;
202 }