C++实验六

ABC

#include<iostream>
using namespace std;
class ABC
{
    public:
        ABC(int a,int b);
        void add();
        int getM(){
            return m;
        }
        int getN(){
            return n;
        } 
    private:
        int m,n;
} ;
ABC::ABC(int a,int b):m(a),n(b){}
void ABC::add()
{
    cout<<(getM()+getN())<<endl;
}

class A:public ABC
{
    public:
        A(int a,int b);
        void minus();
};
A::A(int a,int b):ABC(a,b){}
void A::minus()
{
    cout<<(getM()-getN())<<endl;
}

class B:public ABC
{
    public:
        B(int a,int b);
        void sub();
};
B::B(int a,int b):ABC(a,b){}
void B::sub()
{
    cout<<(getM()*getN())<<endl;
}

class C:public ABC
{
    public:
        C(int a,int b);
        void div();
};
C::C(int a,int b):ABC(a,b){};
void C::div()
{
    cout<<(getM()/getN())<<endl;
}

int main()
{
    int a,b;
    cin>>a>>b;
    ABC a0(a,b);
    cin>>a>>b;
    A a1(a,b);
    cin>>a>>b;
    B a2(a,b);
    cin>>a>>b;
    C a3(a,b);
    a0.add();
    a1.add();
    a1.minus();
    a2.add();
    a2.sub();
    a3.add();
    a3.div();
    return 0;
}

vehicle

#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle(double,double);
        void run();
        void stop();
        ~vehicle();
    private:
        double maxspeed;
        double weight;
};
vehicle:: vehicle(double a,double b):maxspeed(a),weight(b){}
vehicle::~vehicle()
{
    cout<<"Destructing vehicle"<<endl;
}
void vehicle::run()
{
    cout<<"run"<<endl;
}
void vehicle::stop()
{
    cout<<"stop"<<endl;
}

class bicycle:virtual public vehicle
{
    public:
        bicycle(double a,double b,double c):height(a),vehicle(b,c){}
        ~bicycle()
        {
            cout<<"Destructing bicycle"<<endl;
        }
    private:
        double height;
};

class motorcar:virtual public vehicle
{
    public:
        motorcar(int a,double b,double c):seatnum(a),vehicle(b,c){}
        ~motorcar()
        {
            cout<<"Destructing motorcar"<<endl;
        }
    private:
        int seatnum;
};

class motorcycle:public bicycle,public motorcar
{
    public:
        motorcycle(double a,double b,double c,int d):
        vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){}
};

int main() 
{
    double a,b,c;
    int d;
    cin>>a>>b>>c>>d;
    motorcycle A(a,b,c,d);
    A.run();
    A.stop();
    return 0;
} 

Fraction

#include<iostream>
#include"Fraction.h"
#include"iFraction.h"
using namespace std;
int main()
{   int q,w,e,r,t;
    Fraction f;
    f.show();
    Fraction f0(5);
    f0.show();
    cout<<"请分别输入您想进行计算的分数!"<<endl; 
    do
{
    cin>>q>>w;
    iFraction f1(q,w);
    f1.ishow();
    cin>>e>>r;
    iFraction f2(e,r);
    f2.ishow();
    cout<<"输入你想进行的运算方式,如+、-、*、/、b(表示比较大小):"<<endl; 
    char c;
    cin>>c;
    switch(c)
    {
        case '+':f1+f2;break;
        case '-':f1-f2;break;
        case '*':f1*f2;break;
        case '/':f1/f2;break;
        case 'b':f1.com(f2);break;
    }
    cout<<"如果你想进行下一轮运算,请输入一个非0数,如果不需要请摁0!"<<endl; 
    cin>>t;
}while(t!=0);
    cout<<"Thanks for using!"<<endl; 
    return 0;
}
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
class Fraction
{
    public:
        Fraction();
        Fraction(int t,int b);
        Fraction(int t);
        int SL(int,int);
        void show();
        void add(Fraction &f);
        void minus(Fraction &f);
        void mul(Fraction &f);
        void div(Fraction &f);
        void com(Fraction &f);
        void operator+(const Fraction &a);
        void operator-(const Fraction &a);
        void operator*(const Fraction &a);
        void operator/(const Fraction &a);
        int getx()
        {
            return top;
        }
        int gety()
        {
            return bot;
        }
    private:
        int top;
        int bot;
};
#endif
#include<iostream>
#include"Fraction.h"
using namespace std;
Fraction::Fraction():top(0),bot(1){}
Fraction::Fraction(int t,int b):top(t),bot(b){}
Fraction::Fraction(int t):top(t),bot(1){}
int Fraction::SL(int x,int y)
{
    int c;
    do
        {
        c=x%y;
        x=y;
        y=c;
        }while(c!=0);
        return x; 
}
void Fraction::show()
{
    int m;
    m=top/SL(top,bot);
    bot=bot/SL(top,bot);
    top=m;
    if(top<0)
    cout<<"-"<<top<<"/"<<bot<<endl;
    else if(top>0)
    cout<<"结果为:"<<top<<"/"<<bot<<endl;
    else
    cout<<"0"<<endl;
}
void Fraction::add(Fraction &f)
{
    Fraction result;
    result.top=top*f.bot+f.top*bot;
    result.bot=bot*f.bot;
    result.show();
}
void Fraction::minus(Fraction &f)
{
    Fraction result;
    result.top=top*f.bot-f.top*bot;
    result.bot=bot*f.bot;
    result.show();
}
void Fraction::mul(Fraction &f)
{
    Fraction result;
    result.top=top*f.top;
    result.bot=bot*f.bot;
    result.show();
}
void Fraction::div(Fraction &f)
{
    Fraction result;
    result.top=top*f.bot;
    result.bot=bot*f.top;
    result.show();
}
void Fraction::com(Fraction &f)
{
    double m,n,a;
    m=top/bot;
    n=f.top/f.bot;
    a=m-n;
    if(a>0)
    cout<<"m大于n"<<endl;
    else if(a=0)
    cout<<"m等于n"<<endl;
    else
    cout<<"m小于n"<<endl;
}
void Fraction::operator+(const Fraction &f)
{
    Fraction result;
    result.top=top*f.bot+f.top*bot;
    result.bot=bot*f.bot;
    result.show();
}
void Fraction::operator-(const Fraction &f)
{
    Fraction result;
    result.top=top*f.bot-f.top*bot;
    result.bot=bot*f.bot;
    result.show();
} 
void Fraction::operator*(const Fraction &f)
{
    Fraction result;
    result.top=top*f.top;
    result.bot=bot*f.bot;
    result.show();
}
void Fraction::operator/(const Fraction &f)
{
    Fraction result;
    result.top=top*f.bot;
    result.bot=bot*f.top;
    result.show();
}
#ifndef IFRACTION_H_INCLUDED
#define IFRACTION_H_INCLUDED
#include"Fraction.h"
class iFraction:public Fraction
{
    public:
        iFraction(int,int);
        void ishow();
        friend class Fraction;
};
#endif
#include<iostream>
#include"iFraction.h"
using namespace std;
iFraction::iFraction(int a,int b):Fraction(a,b){}
void iFraction::ishow()
{
    int m,n;
    if(getx()>gety())
    {
        m=getx()/gety();
        n=getx()%gety();
        cout<<" "<<n<<endl<<m<<'-'<<endl<<" "<<gety()<<endl;
    }
    else
    cout<<getx()<<'/'<<gety()<<endl;
}

猜你喜欢

转载自www.cnblogs.com/shenleideblog/p/9108069.html