• 实验6




    #include<iostream> using namespace std; class Gross { public: void orient(int a,int b); void plus(); private: int m,n; }; void Gross::orient(int a,int b) { m=a; n=b; } void Gross::plus() { cout<<"两数相加得:"<<m+n<<endl; } class A:public Gross /*芯片A由Gross派生*/ { public: void orienta(int a,int b); void min(); /*新成员*/ private: int m1,n2; }; void A::orienta(int a,int b) { orient(a,b); m1=a; n2=b; } void A::min() { cout<<"两数相减得:"<<m1-n2<<endl; } class B:public Gross /*同上*/ { public: void orientb(int a,int b); void mult(); private: int m2,n2; }; void B::orientb(int a,int b) { orient(a,b); m2=a; n2=b; } void B::mult() { cout<<"两数相乘得:"<<m2*n2<<endl; } class C:public Gross /*同上*/ { public: void orientc(int a,int b); void div(); private: int m3,n3; }; void C::orientc(int a,int b) { orient(a,b); m3=a; n3=b; } void C::div() { cout<<"两数相除得:"<<m3/n3<<endl; } int main() { A a;B b;C c; a.orienta(3,9); a.min(); a.plus(); b.orientb(3,9); b.mult(); b.plus(); c.orientc(3,9); c.plus(); c.div(); return 0; }

    2.

    #include<iostream>
    using namespace std;
    class vehicle {                                             /*车类定义*/
    public:
         vehicle(int x, int y) :maxspeed(x), weight(y) { cout << "maxseppd=" << maxspeed << endl<< "weight=  " << weight << endl; };
         void run() { cout << "go" << endl; };
         void stop() { cout << "stop" << endl; };
    private:
         int maxspeed;
         int weight;
    };
     
    class bicycle :virtual public vehicle {           /*自行车*/
    public:
        bicycle(int x, int y, int z) :vehicle(x, y), height(z) { cout << "height=" << height << endl; };//派生构造虚基类
    private:
        int height;
    };
     
    class motocar :virtual public vehicle {         /*汽车*/
    public:
        motocar(int x, int y, int z) :vehicle(x, y), seatnum(z) { cout <<"seatnum=" << seatnum << endl; };
    private:
        int seatnum;
    };
     
    class motocycle :public bicycle, public motocar {
    public:
        motocycle(int a, int b, int c, int d) :vehicle(a, b), bicycle(a, b, c), motocar(a, b, d) {};//构造函数定义
    };
    int main()
    {
        motocycle M(1, 2, 3, 4);       //初始化一个motocycle
        M.run();
        M.stop();
        return 0;
    }

     3.

    #pragma once
    #ifndef FRACTION_H
    #define FRACTION_H
    class fraction
    {
    public:
        fraction(int t0, int b0);
        fraction(int t0);
        fraction();
        ~fraction();
        /*运算符重载*/
        fraction operator+(const fraction &f1);
        fraction operator-(const fraction &f1);
        fraction operator*(const fraction &f1);
        fraction operator/(const fraction &f1);
        void compare(fraction &f1); //比较大小
        void input();
        void output();
    
    protected:
        int top;
        int bottom;
    };
    #endif // !FRACTION_H
    #pragma once
    #ifndef IFRACTION_H
    #define IFRACTION_H
    #include"Fraction.h"
    class ifraction :public fraction {
    public:
        ifraction(int t0, int b0);
        ifraction(int t0);
        ifraction();
        void show();
        friend void convertF(ifraction &f0);
    private:
        int extra = 0;/*用于保存将假分数变为真分数时产生的前缀数字*/
    };
    #endif // !IFRACTION_H
    #include "fraction.h"
    #include <iostream>
    using namespace std;
    /*构造函数*/
    fraction::fraction(int t0, int b0) :top(t0), bottom(b0)
    {}
    fraction::fraction(int t0) : top(t0),bottom(1)
    {}
    fraction::fraction():top(1),bottom(1)
    {}
    /*析构函数*/
    fraction::~fraction()
    {}
    /*重载运算符*/
    fraction fraction::operator+(const fraction &f1) {
        int newtop = top * f1.bottom + f1.top * bottom;
        int newbottom = bottom * f1.bottom;
        return fraction(newtop, newbottom);
    }
    fraction fraction::operator-(const fraction &f1) {
        int newtop = top * f1.bottom - f1.top * bottom;
        int newbottom = bottom * f1.bottom;
        return fraction(newtop, newbottom);
    }
    fraction fraction::operator*(const fraction &f1) {
        int newtop = top * f1.top;
        int newbottom = bottom * f1.bottom;
        return fraction(newtop, newbottom);
    }
    fraction fraction::operator/(const fraction &f1) {
        int newtop = top * f1.bottom;
        int newbottom = bottom * f1.top;
        return fraction(newtop, newbottom);
    }
    
    void fraction::compare(fraction &f1)        /*比较大小*/
    {
        if (top * f1.bottom > bottom * f1.top)
            cout << top << "/" << bottom << endl;
        else if (top * f1.bottom < bottom * f1.top)
            cout << f1.top << "/" << f1.bottom << endl;
        else if (top * f1.bottom == bottom * f1.top)
            cout << "一样大" << endl;
    }
    
    void fraction::input()
    {
        cin >> top >> bottom;
    }
    void fraction::output()
    {
        cout << top << "/" << bottom << endl;
    }
    #include"iFraction.h"
    #include<iostream>
    using namespace std;
    ifraction::ifraction(int t0, int b0):fraction(t0,b0){}
    ifraction::ifraction(int t0):fraction(t0,1){}
    ifraction::ifraction():fraction(1,1){}
    
    void ifraction::show() {
        if (bottom == 0) {
            cout << top << endl;
        }
        else if (top == 0) {
            cout << extra << endl;
        }
        else if (extra != 0) {
            cout << extra << "" << top << "/" << bottom << endl;
        }
        else {
            cout << top << "/" << bottom << endl;
        }
    }
    #include "stdafx.h"
    #include <iostream>
    #include "fraction.h"
    #include "iFraction.h"
    using namespace std;
    
    int max(int a, int b)
    {
        if (a < b)
            swap(a, b);
        int x;
        while (b != 0)
        {
            x = a % b;
            a = b;
            b = x;
        }
        return a;
    }
    void convertF(ifraction &f0) {
        //约分
        int max0 = max(f0.top, f0.bottom);
        f0.top /= max0;
        f0.bottom /= max0;
        //化为真分数
        if (f0.top == f0.bottom) {
            f0.top = 1;
            f0.bottom = 0;
        }
        else if (f0.top > f0.bottom) {
            f0.extra = f0.top / f0.bottom;
            f0.top %= f0.bottom;
        }
    }
    
    int main()
    {
        fraction a;
        fraction b(3, 4);
        fraction c(5);
        fraction result;
    
        cout << "函数输出测试" << endl;
        cout << "分数a为:";    a.output();
        cout << "分数b为:";    b.output();
        cout << "分数c为:";    c.output();
        
        cout << "加减乘除比较测试" << endl;
        cout << "a + b = ";     result = a + b;     result.output();
        cout << "a - b = ";     result = a - b;     result.output();
        cout << "b * c = ";     result = b * c;     result.output();
        cout << "b / c = ";     result = b / c;     result.output();
        cout << " a和b中较大的是:";    a.compare(b);
        cout << "c和c比较:";       c.compare(c);
    
        cout << "请输入分数a(分子和分母中间以空格分隔):";
        a.input();
        cout << "a的大小为:";       a.output();
        //ifraction测试
        ifraction temp1(155, 5), temp2(20, 8);
        cout << "转化前:";
        temp1.output();
        temp2.output();
        cout << "转化后:";
        convertF(temp1);
        convertF(temp2);
        temp1.show();
        temp2.show();
        return 0;
    }

  • 相关阅读:
    MySQL笔记(6)---锁
    MySQL笔记(5)---索引与算法
    MySQL笔记(4)---表
    MySQL笔记(3)---文件
    MySQL笔记(2)---InnoDB存储引擎
    MySQL笔记(1)---MySQL体系结构和存储引擎
    生成器,迭代器,装饰器
    文件操作、def函数、模块导入、json
    数据类型、字符串操作
    基本数据类型,条件判断
  • 原文地址:https://www.cnblogs.com/spring-winds/p/9153088.html
Copyright © 2020-2023  润新知