码迷,mamicode.com
首页 > 其他好文 > 详细

实验6

时间:2018-06-06 21:54:11      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:sim   on()   math   截图   ted   clu   bic   span   img   

1

base.h

class base{
    public: void add();
            tog(int m0,int n0);
    private: int m;
             int n;
};

class A:public base{
    public: void min();
            A(int m0,int n0);
    private: int m;
             int n;
};

class B:public base{
    public: void mul();
            B(int m0,int n0);
    private: int m;
             int n;
};

class C:public base{
    public: void div();
            C(int m0,int n0);
    private: int m;
             int n;
};

base.cpp

#include"base.h"
#include<iostream>
using namespace std;
base::tog(int m0,int n0){
    m=m0;
    n=n0;
}
void base::add(){
    cout<<"m+n="<<m+n<<endl;
};

A::A(int m0,int n0){
    tog(m0,n0);
    m=m0;
    n=n0;
}
void A::min(){
    cout<<"m-n="<<m-n<<endl;
};

B::B(int m0,int n0){
    tog(m0,n0);
    m=m0;
    n=n0;
}
void B::mul(){
    cout<<"m*n="<<m*n<<endl;
};

C::C(int m0,int n0){
    tog(m0,n0);
    m=m0;
    n=n0;
}
void C::div(){
    cout<<"m/n="<<m/n<<endl;
};

main.cpp

#include <iostream>
#include"base.h"
using namespace std;
int main() {

    A A(3,2);
    A.add();
    A.min();
    B B(3,4);
    B.add();
    B.mul();
    C C(4,2);
    C.add();
    C.div();
    return 0;
}

截图

技术分享图片

2

vehicle.h

class vehicle{
    public: void stop();
            void run();
            vehicle(int max,int wei);
    private: int maxspeed;
             int weight;
};
 
class bicycle:virtual public vehicle{
    public: bicycle(int max,int wei,int hei);
    private:int height;
};

class motorcar:virtual public vehicle{
    public: motorcar(int max,int wei,int sea);
    private:int seatnum;
};

class motorcycle:public bicycle,public motorcar{
    public: motorcycle(int max,int wei,int hei,int sea);
};

vehicle.cpp

#include"vehicle.h"
#include<iostream>
using namespace std;
vehicle::vehicle(int max,int wei):maxspeed(max),weight(wei){
    cout<<"maxspeed"<<maxspeed<<endl;
    cout<<"weight"<<weight<<endl;
}
void vehicle::run(){
    cout<<"run"<<endl;
}
void vehicle::stop(){
    cout<<"stop"<<endl;
}
bicycle::bicycle(int max,int wei,int hei):vehicle(max,wei),height(hei){
    cout<<"height"<<height<<endl;
}
motorcar::motorcar(int max,int wei,int sea):vehicle(max,wei),seatnum(sea){
    cout<<"seatnum"<<seatnum<<endl;
}
motorcycle::motorcycle(int max, int wei, int hei, int sea):vehicle(max,wei),bicycle(max,wei,hei),motorcar(max,wei,sea){}

main.cpp

#include <iostream>
#include"vehicle.h"
using namespace std;
int main() {
    motorcycle M(100,80,10,5);
    M.run();
    M.stop();
    return 0;
}

截图

技术分享图片

3

Fraction.h

#include<iostream>
class Fraction{
    public:
        Fraction(int t,int b);
        Fraction(int t);
        Fraction();
        void show();
        int zc(int t,int b);
        void simplify();
        Fraction operator+(Fraction &b);
        Fraction operator-(Fraction &b);
        Fraction operator*(Fraction &b);
        Fraction operator/(Fraction &b);
    protected:
        int top;
        int bottom;
};

Fraction.cpp

#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction(int t,int b){
    top=t;
    bottom=b;
}
Fraction::Fraction(int t){
    top=t;
    bottom=1;
}
Fraction::Fraction(){
    top=0;
    bottom=1;
}
int Fraction::zc(int t,int b){
    if(t%b==0)
        return b;
    else
        zc(b,t%b);
}
void Fraction::simplify(){
    if(bottom<0){
        top=-top;
        bottom=-bottom;
}
    int z=zc(abs(top),abs(bottom));
    top=top/z;
    bottom=bottom/z;
}
void Fraction::show(){
    cout<<top<<"/"<<bottom<<endl;
}
Fraction Fraction::operator+(Fraction &b){return Fraction(top*b.bottom+b.top*bottom,bottom*b.bottom);}
Fraction Fraction::operator-(Fraction &b){return Fraction(top*b.bottom-b.top*bottom,bottom*b.bottom);}
Fraction Fraction::operator*(Fraction &b){return Fraction(top*b.top,bottom*b.bottom);}
Fraction Fraction::operator/(Fraction &b){return Fraction(top*b.bottom,bottom*b.top);}

iFraction.h

#include"Fraction.h"
class iFraction:public Fraction{
    private:
        int i;
    public:
        iFraction(int t=0,int b=1,int z=0):Fraction(t,b),i(z){}
        void print();
        friend iFraction convertF(iFraction &p); 
};

iFraction.cpp

#include"iFraction.h"
#include<iostream>
using namespace std;
void iFraction::print(){
    if(top==0)
        cout<<i<<endl;
    else if(i==0){
        cout<<top<<endl;
        cout<<"-"<<endl;
        cout<<bottom<<endl;
}
    else{
        cout<<" "<<top<<endl;
        cout<<i<<"-"<<endl;
        cout<<" "<<bottom<<endl;
}
}
iFraction convertF(iFraction &p){
    p.simplify(); 
    p.i+=p.top/p.bottom;
    p.top%=p.bottom;
    return p;
}

main.cpp

#include <iostream>
#include "iFraction.h"
using namespace std;
int main(){
    Fraction a(4,8);
    cout<<"a=";
    a.show();
    Fraction b(2);
    cout<<"b=";
    b.show();
    Fraction c;
    cout<<"c=";
    c.show();
    c=a+b;
    cout<<"a+b=";
    c.show();
    c=a-b;
    cout<<"a-b=";
    c.show();
    c=a*b;
    cout<<"a*b=";
    c.show();
    c=a/b;
    cout<<"a/b=";
    c.show();
    iFraction d(9,6,2);
    cout<<"d="<<endl;
    d.print();
    d=convertF(d);
    cout<<"convertF d="<<endl;
    d.print(); 
    return 0;
}

截图

技术分享图片

实验6

标签:sim   on()   math   截图   ted   clu   bic   span   img   

原文地址:https://www.cnblogs.com/zszssy/p/9146234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!