zoukankan      html  css  js  c++  java
  • c++简单程序设计-6

    //chip.h
    #pragma once
    class base{
    public:
        base(double M=0, double N=0) :m(M), n(N) {};
        double show();
        double getM() const { return m; };
        double getN() const { return n; };
    private:
        double m;
        double n;
    };
    
    class chipA :public base {
    public:
        chipA(double M, double N) :base(M, N) {};
        double showA();
    };
    
    class chipB :public base {
    public:
        chipB(double M, double N) :base(M, N) {};
        double showB();
    };
    
    class chipC :public base {
    public:
        chipC(double M, double N) :base(M, N) {};
        double showC();
    };
    //chip.cpp
    #include "chip.h"
    #include<iostream>
    using namespace std;
    
    double base::show() {
        return m + n ;
    }
    
    double chipA::showA() {
        cout << "chipA:" << endl;
        cout << "m+n:" << base::show() << endl;
        cout << "m-n:";
        return getM() - getN();
    }
    
    double chipB::showB() {
        cout << "chipB:" << endl;
        cout << "m=n:" << base::show() << endl;
        cout << "m*n:";
        return getM() * getN();
    }
    
    double chipC::showC() {
        cout << "chipC:" << endl;
        cout << "m+n:" << base::show() << endl;
        cout << "m/n:";
        return getM() / getN();
    }
    //main.cpp
    #include "chip.h"
    #include<iostream>
    using namespace std;
    
    int main() {
        chipA A(1, 2);
        chipB B(1, 2);
        chipC C(1, 2);
        cout << "m=1  n=2" << endl;
        cout << A.showA() << endl;
        cout << B.showB() << endl;
        cout << C.showC() << endl;
    }

    //vehicle
    #pragma once
    #include<iostream>
    using namespace std;
    
    class vehicle {            
    public:                                                         //外部接口
        vehicle(int a, int b) :maxspeed(a), weight(b) {             
            cout << "maxspeed=" << maxspeed << endl << "weight=" << weight << endl;
        };
        void run() {
            cout << "run" << endl;
        }
        void stop() {
            cout << "stop" << endl;
        }
        ~vehicle() {                                                //析构函数
            cout << "Destructing vehicle" << endl;
        }
    private:
        int maxspeed;
        int weight;
    };
    //定义派生类自行车
    class bicycle :virtual public vehicle {        
    public:                                                         //新增外部接口
        bicycle(int a, int b, int c) :vehicle(a, b), height(c) {
            cout << "height=" << height << endl;
        }
        ~bicycle() {                                                 //析构函数
            cout << "Destructing bicycle" << endl;
        }
    private:
        int height;
    };
    //定义派生类汽车
    class motorcar :virtual public vehicle {    
    public:                                                          //新增外部接口
        motorcar(int a, int b, int d) :vehicle(a, b), seatnum(d) {
            cout << "seatnum=" << seatnum << endl;
        }
        ~motorcar() {                                                //析构函数
            cout << "Destructing motorcar" << endl;
        }
    private:
        int seatnum;
    };
    //定义派生类摩托车
    class motocycle :public bicycle, public motorcar {
    public:                                                          //新增外部接口
        motocycle(int a, int b, int c, int d) :vehicle(a, b), bicycle(a, b, c), motorcar(a, b, d) {};
        ~motocycle() {                                               //析构函数
            cout << "Destructing motocycle" << endl;
        }
    };
    //main.cpp
    #include<iostream>
    #include"vehicle.h"
    using namespace std;
    int main() {
        motocycle M(1, 2, 3, 4);  //定义motocycle类对象M
        M.run();
        M.stop();
        return 0;
    }

     

    //Fraction.h
    #pragma once
    class Fraction {
    public:
        Fraction();            //构造函数
        Fraction(int t, int b);//函数重载
        Fraction(int t);       //函数重载
        void show();           //输出
        Fraction operator+ (const Fraction &f0) const {    //重载+
            Fraction f;
            f.top = top * f0.bottom + f0.top*bottom;
            f.bottom = bottom * f0.bottom;
            return f;
        };
    
        Fraction operator- (const Fraction &f0) const {    //重载-
            Fraction f;
            f.top = top * f0.bottom - f0.top*bottom;
            f.bottom = bottom * f0.bottom;
            return f;
        };
    
        Fraction operator* (const Fraction &f0) const {     //重载*
            Fraction f;
            f.top = top * f0.top;
            f.bottom = bottom * f0.bottom;
            return f;
        };
    
        Fraction operator/ (const Fraction &f0) const {     //重载/
            Fraction f;
            f.top = top * f0.bottom;
            f.bottom = bottom * f0.top;
            return f;
        };
    
        int gett() { return top; }
        int getb() { return bottom; }
    private:
        int top;      //分子
        int bottom;   //分母
    };
    //Fraction.cpp
    #include "Fraction.h"
    #include <iostream>
    #include<stdlib.h>
    using namespace std;
    int measure(int x, int y)   //构造函数,找出分子与分母的最大公约数
    {
        int z = y;
        while (x%y != 0)
        {
            z = x % y;
            x = y;
            y = z;
        }
        return z;
    }
    
    Fraction::Fraction() :top(0), bottom(1) {                     //不提供初始值的函数实现
    }
    
    Fraction::Fraction(int t, int b) : top(t), bottom(b) {        //提供两个初始值的函数实现
    }
    
    Fraction::Fraction(int t) : top(t), bottom(1) {               //提供一个初始值的函数实现
    }
    
    void Fraction::show() {                                       //输出
        int t,b,z;
        t = top;
        b = bottom;
        while (b!=0) {
            if (t == 0) {
                cout << "0" << endl;
                break;
            }
            z = measure(t, b);
            t /= z;
            b /= z;
    
            if (b == 1) {
                cout << t << endl;
                break;
            }
            if (b == -1) {
                cout << -t << endl;
                break;
            }
            if (t > 0 && b> 0) {
                cout << t << "/" << b << endl;
                break;
            }
            if (t < 0 && b < 0) {
                cout << abs(t) << "/" << abs(b) << endl;
                break;
            }
            if (t > 0 && b< 0) {
                cout << -abs(t) << "/" << abs(b) << endl;
                break;
            }
            if (t < 0 && b> 0) {
                cout << -abs(t) << "/" << abs(b) << endl;
                break;
            }
        }
    
    }
    //iFraction.h
    #pragma once
    #include<iostream>
    #include"Fraction.h"
    using namespace std;
    
    class iFraction :public Fraction {
    public:
        iFraction() :Fraction() {};
        iFraction(int t, int b) :Fraction(t, b) {};
        iFraction(int t) :Fraction(t) {};
        void convertF();
    };
    //iFraction.cpp
    #include "iFraction.h"
    #include <iostream>
    using namespace std;
    int measure(int x, int y); //构造函数,找出分子与分母的最大公约数
    
    void iFraction::convertF() {              //输出
        int t, b, z, A;
        t = gett();
        b = getb();
        while (b != 0) {
            A = t / b;
            if (A != 0) {
                cout << A;
                t %= b;
                z = measure(t, b);
                t /= z;
                b /= z;
                if (t == 0) {
                    cout << endl;
                    break;
                }
                else {
                    cout << "(" << abs(t) << "/" << abs(b) << ")" << endl;
                    break;
                }
            }
                if (A == 0) {
                    Fraction::show();
                    break;
                }
            
        }
    }
    //main.cpp
    #include <iostream>
    #include "Fraction.h"
    #include "iFraction.h"
    using namespace std;
    
    int main() {
        Fraction f1;              //不提供初始值
        Fraction f2(-2, 3);        //提供两个初始值
        Fraction f3(3);           //提供一个初始值
        Fraction f4,f5,f6,f7;
        f4 = f2 + f3;
        f5 = f2 - f3;
        f6 = f2 * f3;
        f7 = f2 / f3;
        f1.show();
        f2.show();
        f3.show();
        f4.show();
        f5.show();
        f6.show();
        f7.show();
        iFraction F1(6, 3);
        iFraction F2(2, 4);
        iFraction F3(10, 6);
        iFraction F4(-10, 6);
        F1.convertF();
        F2.convertF();
        F3.convertF();
        F4.convertF();
        return 0;
    }

     实验总结与体会:

    第三题在Fraction.cpp里写重载的时候出错了,然后就写在了Fraction.h里

    感觉第三题还可以再优化一下,有些部分的代码使用了两次

  • 相关阅读:
    JavaScript高级-----8.函数进阶(2)
    JavaScript高级-----7.函数进阶(1)
    2014-10-18 来美半个月
    修手机记
    圆梦美利坚之三:租房记
    圆梦美利坚之二:买机票记
    Hadoop 停止Job
    IIS应用程序池数目
    HTML5 microdata
    Java sql helper[转]
  • 原文地址:https://www.cnblogs.com/tensheep/p/9138362.html
Copyright © 2011-2022 走看看