实验6

6-1:芯片

chip.h

 1 class chip0 {
 2 public:
 3     chip0(int x, int y) :m(x), n(y) {};                            //构造函数
 4     int plu() { return m + n; };
 5     int const Getm() { return m; };                                //返回私有函数值
 6     int const Getn() { return n; };
 7 private:
 8     int m;
 9     int n;
10 };
11 
12 class chipA :public chip0 {                                        //减法芯片实现
13 public:
14     chipA(int x, int y) :chip0(x, y) {};
15     int min() { return Getm() - Getn(); };
16 };
17 
18 class chipB :public chip0 {                                        //乘法芯片实现
19 public:
20     chipB(int x, int y) :chip0(x, y) {};
21     int mul() { return Getm() * Getn(); };
22 };
23 
24 class chipC :public chip0 {                                        //除法芯片实现
25 public:
26     chipC(int x, int y) :chip0(x, y) {};
27     int div() { return Getm() / Getn(); };
28 };

main.cpp

#include<iostream>
#include"chip.h"
using namespace std;
int main()
{
    chipA A(6, 2);                //初始化芯片
    chipB B(6, 2);
    chipC C(6, 2);
    cout << "m=6 n=2" << endl << "m+n=" << A.plu() << endl << "m-n=" << A.min() << endl << "m*n=" << B.mul() << endl << "m/n=" << C.div() << endl;
    return 0;
}

6-2:vehicle类

vehicle.h

 1 #include<iostream>
 2 using namespace std;
 3 class vehicle {                                             //类定义
 4 public:
 5     vehicle(int x, int y) :maxspeed(x), weight(y) { cout << "maxseppd=  " << maxspeed << endl<< "weight=  " << weight << endl; };
 6     void run() { cout << "run" << endl; };
 7     void stop() { cout << "stop" << endl; };
 8 private:
 9     int maxspeed;
10     int weight;
11 };
12 
13 class bicycle :virtual public vehicle {
14 public:
15     bicycle(int x, int y, int z) :vehicle(x, y), height(z) { cout << "height=  " << height << endl; };//派生构造虚基类
16 private:
17     int height;
18 };
19 
20 class motocar :virtual public vehicle {
21 public:
22     motocar(int x, int y, int z) :vehicle(x, y), seatnum(z) { cout << "seatnum=  " << seatnum << endl; };
23 private:
24     int seatnum;
25 };
26 
27 class motocycle :public bicycle, public motocar {
28 public:
29     motocycle(int a, int b, int c, int d) :vehicle(a, b), bicycle(a, b, c), motocar(a, b, d) {};//构造函数定义
30 };

mian.cpp

 1 #include<iostream>
 2 #include"vehicle"
 3 using namespace std;
 4 int main()
 5 {
 6     motocycle M(1, 2, 3, 4);       //初始化一个motocycle
 7     M.run();
 8     M.stop();
 9     return 0;
10 }

6-3:iFraction(规范化处理函数施工中……)

Fraction.h

 1 class Fraction {
 2 public:
 3     Fraction();
 4     Fraction(int t, int b);
 5     Fraction(int t);
 6     void show();
 7     void compare(Fraction &f1);//比较函数
 8     int gongyinshu(int a, int b);//求公因数
 9     void transform();//转换函数
10     int gettop();
11     int getbottom();
12     Fraction operator+(Fraction &f0) {
13         Fraction f;
14         f.top = top *f0.bottom + f0.top*bottom;
15         f.bottom = bottom*f0.bottom;
16         return f;
17     };//重载+
18     Fraction operator-(Fraction &f0) {
19         Fraction f;
20         f.top = top *f0.bottom - f0.top*bottom;
21         f.bottom = bottom*f0.bottom;
22         return f;
23     };//重载-
24     Fraction operator*(Fraction &f0) {
25         Fraction f;
26         f.top *= f0.top;
27         f.bottom *= f0.bottom;
28         return f;
29     };//重载*
30     Fraction operator/(Fraction &f0) {
31         Fraction f;
32         f.top *= f0.bottom;
33         f.bottom *= f0.top;
34         return f;
35     };//重载/
36 private:
37     int top;            //分子
38     int bottom;            //分母
39 };

iFraction.h

扫描二维码关注公众号,回复: 1421638 查看本文章
 1 #include"Fraction.h"
 2 class iFraction :public Fraction {
 3 public:
 4     iFraction();                          //新构造函数
 5     iFraction(int x);
 6     iFraction(int x, int y);
 7     iFraction(int x, int y, int z);
 8     void ishow();
 9 private:
10     int side;                               //带分数的整数部分    
11 };        

Fraction.cpp

 1 #include <iostream>
 2 #include "Fraction.h"
 3 using namespace std;
 4 Fraction::Fraction() :top(0), bottom(1) {}                            //初始化
 5 Fraction::Fraction(int t, int b) : top(t), bottom(b) {}
 6 Fraction::Fraction(int t) : top(t), bottom(1) {}
 7 
 8 
 9 void Fraction::show() {                                              //输出分数
10     int a, b, c, d, e, f, g;
11     a = top;
12     b = bottom;
13     c = 0; d = 0; f = 0;
14     do {
15         c++;                                                        //判断分子分母的位数
16         a /= 10;
17     } while (a > 0);
18     do {
19         d++;
20         b /= 10;
21     } while (b > 0);
22     e = (c > d ? c : d);
23     g = gongyinshu(abs(top), abs(bottom));                            //求最大公因数
24     top /= g; bottom /= g;                                            //化简
25     if (bottom <= 0) {                                              //规范分数
26         bottom = -bottom;
27         top = -top;
28     }
29     cout << top << endl;                                            //输出分子
30     do {
31         f++;
32         cout << "-";                                                //根据位数输出横线长度
33     } while (f < e);
34     cout << endl << bottom << endl;                                    //输出分母
35 }
36 
37 void Fraction::compare(Fraction &f1) {
38     double real0 = double(top) / double(bottom);                    //强制转换为double类型,换成小数比较
39     double real1 = double(f1.top) / double(f1.bottom);
40     if (real0 > real1)
41         cout << "第一个数大于第二个数";
42     else if (real0 == real1)
43         cout << "相等";
44     else
45         cout << "第一个数小于第二个数";
46 }
47 
48 int Fraction::gongyinshu(int a, int b) {                                //求最大公因数                                            
49     int t;                                                            //假设a>b,如果a不能被b整除,则将b赋值给a,
50     if (a < b)                                                        //余数赋值给b,重复执行a%b,直到a能够被b整除。此时返回b的值,则为最大公约数。
51     {
52         t = a;
53         a = b;
54         b = t;
55     }
56     while (b != 0)
57     {
58         t = a % b;
59         a = b;
60         b = t;
61     }
62     return a;                                                            //返回最大公因数
63 }
64 
65 void Fraction::transform() {                                        //转换为十进制
66     double real0 = double(top) / double(bottom);
67     cout << "转换为十进制为:" << real0;
68 }
69 
70 int Fraction::gettop() { return top; }                    //访问top
71 int Fraction::getbottom() { return bottom; }          //访问bottom

iFraction.cpp

 1 #include"iFraction.h"
 2 #include<iostream>
 3 using namespace std;
 4 
 5 iFraction::iFraction() :Fraction() {};
 6 iFraction::iFraction(int x) :Fraction(x) {};
 7 iFraction::iFraction(int x,int y) :Fraction(x,y) {};
 8 iFraction::iFraction(int x, int y,int z) :Fraction(x,y),side(z) {};
 9 
10 void iFraction::ishow() {
11     int a, b, c, d, e, f,g,h;
12     a = gettop();
13     b = getbottom();
14     g = side;
15     c = 0; d = 0; f = 0; h = 0;
16     do {
17         c++;                                                        //判断分子分母的位数
18         a /= 10;
19     } while (a > 0);
20     do {
21         d++;
22         b /= 10;
23     } while (b > 0);
24     do {                                                            //输出空格
25         h++;
26         g /= 10;
27     } while (g > 0);
28     e = (c > d ? c : d);
29     c = 0;
30     do {
31         c++;
32         cout << " ";
33     } while (c < h);
34     cout << gettop() << endl;                                            //输出分子
35     cout << side;                                                    //输出整数
36     do {
37         f++;
38         cout << "-";                                                //根据位数输出横线长度
39     } while (f < e);
40     cout << endl;
41     c = 0;
42     do {                                                            //输出空格
43         c++;
44         cout << " ";
45     } while (c < h);
46     cout << getbottom() << endl;                                    //输出分母
47 }

main.cpp

 1 #include<iostream>
 2 #include"iFraction.h"
 3 using namespace std;
 4 int main()
 5 {
 6     Fraction b(2,5);
 7     Fraction c(2,3);
 8     iFraction d(205, 3111, 11);
 9     d.ishow();                        //输出带分数
10     b = b + c;                        //使用重载运算符计算
11     cout << "计算2/5+2/3=" << endl;
12     b.show();
13     return 0;
14 }

6-4:RPG游戏(施工中……)

猜你喜欢

转载自www.cnblogs.com/zhibifenli/p/9127322.html