Article From:https://www.cnblogs.com/zhibifenli/p/9127322.html

6-1:chip

chip.h

 1 class chip0 {
 2 public:
 3     chip0(int x, int y) :m(x), n(y) {};                            //Constructor
 4     int plu() { return m + n; };
 5     int const Getm() { return m; };                                //Return the private function value
 6     int const Getn() { return n; };
 7 private:
 8     int m;
 9     int n;
10 };
11 
12 class chipA :public chip0 {                                        //Implementation of subtraction chip
13 public:
14     chipA(int x, int y) :chip0(x, y) {};
15     int min() { return Getm() - Getn(); };
16 };
17 
18 class chipB :public chip0 {                                        //Realization of multiplicative chip
19 public:
20     chipB(int x, int y) :chip0(x, y) {};
21     int mul() { return Getm() * Getn(); };
22 };
23 
24 class chipC :public chip0 {                                        //Implementation of division chip
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);                //Initialization chip
    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:vehicleclass

vehicle.h

 1 #include<iostream>
 2 using namespace std;
 3 class vehicle {                                             //Class definition 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; };//Derived construct virtual base class16 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) {};//Constructor definition30 };

mian.cpp

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

6-3:iFraction(Standardizing the construction of processing functions…)

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);//Comparison function
 8     int gongyinshu(int a, int b);//Seek public factor
 9     void transform();//Conversion function
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     };//Heavy load +
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     };//Heavy load -
24     Fraction operator*(Fraction &f0) {
25         Fraction f;
26         f.top *= f0.top;
27         f.bottom *= f0.bottom;
28         return f;
29     };//Heavy load *
30     Fraction operator/(Fraction &f0) {
31         Fraction f;
32         f.top *= f0.bottom;
33         f.bottom *= f0.top;
34         return f;
35     };//Heavy load /
36 private:
37     int top;            //molecule
38     int bottom;            //Denominator
39 };

iFraction.h

 1 #include"Fraction.h"
 2 class iFraction :public Fraction {
 3 public:
 4     iFraction();                          //New constructor
 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;                               //Integral part with fractions
11 };        

Fraction.cpp

 1 #include <iostream>
 2 #include "Fraction.h"
 3 using namespace std;
 4 Fraction::Fraction() :top(0), bottom(1) {}                            //Initialization
 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() {                                              //Output fraction
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++;                                                        //Determining the number of molecular denominator
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));                            //The maximum common factor
24     top /= g; bottom /= g;                                            //Simplification
25     if (bottom <= 0) {                                              //Standard fraction
26         bottom = -bottom;
27         top = -top;
28     }
29     cout << top << endl;                                            //Output molecule
30     do {
31         f++;
32         cout << "-";                                                //Output transverse line length based on number of digits
33     } while (f < e);
34     cout << endl << bottom << endl;                                    //Output denominator
35 }
36 
37 void Fraction::compare(Fraction &f1) {
38     double real0 = double(top) / double(bottom);                    //Forced conversion to double type, to decimal comparison
39     double real1 = double(f1.top) / double(f1.bottom);
40     if (real0 > real1)
41         cout << "The first number is greater than second";
42     else if (real0 == real1)
43         cout << "Equal";
44     else
45         cout << "The first number is less than second";
46 }
47 
48 int Fraction::gongyinshu(int a, int b) {                                //The maximum common factor
49     int t;                                                            //Suppose a> B; if a can not be divisible by B, assign B to a.
50     if (a < b)                                                        //The remainder is assigned to B, repeating a%b until a can be divisible by B. The value returned to B at this time is the maximum common divisor.
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;                                                            //Return to the greatest common factor
63 }
64 
65 void Fraction::transform() {                                        //Conversion to decimal
66     double real0 = double(top) / double(bottom);
67     cout << "The conversion is to the decimal system." << real0;
68 }
69 
70 int Fraction::gettop() { return top; }                    //Access to top
71 int Fraction::getbottom() { return bottom; }          //Access to 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++;                                                        //Determining the number of molecular denominator
18         a /= 10;
19     } while (a > 0);
20     do {
21         d++;
22         b /= 10;
23     } while (b > 0);
24     do {                                                            //Output space
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;                                            //Output molecule
35     cout << side;                                                    //Output integer
36     do {
37         f++;
38         cout << "-";                                                //Output transverse line length based on number of digits
39     } while (f < e);
40     cout << endl;
41     c = 0;
42     do {                                                            //Output space
43         c++;
44         cout << " ";
45     } while (c < h);
46     cout << getbottom() << endl;                                    //Output denominator
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();                        //Output band fraction
10     b = b + c;                        //Use heavy load operator to calculate
11     cout << "Computing 2/5+2/3=" << endl;
12     b.show();
13     return 0;
14 }

There is a problem: because the Fraction class is copied by experiment 4, it is not optimized, written in a mess, heavy load + * / no.Cpp.

6-4:RPGGames (in construction…)

Link of this Article: Experiment 6

Leave a Reply

Your email address will not be published. Required fields are marked *