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

实验六

时间:2018-06-05 21:17:57      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:src   run   停止   image   自行车   include   img   max   ==   

第一题 芯片
1
#include<iostream> 2 using namespace std; 3 class basic 4 { 5 public: 6 basic(int a,int b):m(a),n(b){cout<<m+n<<endl;} 7 ~basic(){} 8 private: 9 int m,n; 10 }; 11 class A:public basic 12 { 13 public: 14 A(int a,int b):basic(a,b),m(a),n(b){} 15 ~A(){} 16 void jian() 17 { 18 cout<<m-n<<endl; 19 } 20 private: 21 int m,n; 22 }; 23 class B:public basic 24 { 25 public: 26 B(int a,int b):basic(a,b),m(a),n(b){} 27 ~B(){} 28 void cheng() 29 { 30 cout<<m*n<<endl; 31 } 32 private: 33 int m,n; 34 }; 35 class C:public basic 36 { 37 public: 38 C(int a,int b):basic(a,b),m(a),n(b){} 39 ~C(){} 40 double chu() 41 {double u,i,p; 42 u=m; 43 i=n; 44 p=u/i; 45 cout<<p<<endl; //我觉得,答案保留小数,更好一些,毕竟是除法。 46 } 47 private: 48 int m,n; 49 }; 50 int main() 51 { 52 int i,j; 53 cin>>i>>j; 54 cout<<"芯片A"<<endl; 55 A a(i,j); 56 a.jian(); 57 cout<<"芯片B"<<endl; 58 B b(i,j); 59 b.cheng(); 60 cout<<"芯片C"<<endl; 61 C c(i,j); 62 c.chu(); 63 64 return 0; 65 }

技术分享图片

 第二题 车

#include<iostream>
using namespace std;
class vehicle{
    public:
        vehicle(int m,int w):maxspeed(m),weight(w){};
        void run(){
            cout<<"run"<<endl;
        };
        void stop(){
            cout<<"stop"<<endl;
        };
        ~vehicle(){};
    protected: int maxspeed,weight;
};
class bicycle:virtual public vehicle{
    public:
        bicycle(int m,int w,int h):vehicle(m,w),height(h){};
        void run()
        {
        cout<<"自行车已启动"<<endl<<"Height:"<<height<<endl;
        };
        void stop()
        {
        cout<<"自行车已停止"<<endl; 
        
        };
        ~bicycle(){};
        protected:
            int height;
};
class motorcar:virtual public vehicle{
    public:
        motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){};
        void run()
        {
        cout<<"汽车已启动"<<endl<<"Seatnum:"<<seatnum<<endl;
        };
        void stop()
        {
        cout<<"汽车已停止"<<endl; 
        
        };
        ~motorcar(){};
        protected:
            int seatnum;
};
class motorcycle:public bicycle,public motorcar{
     public:
        motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){};
        void run()
        {
            cout<<"摩托车已启动"<<endl;
            cout<<"Maxspeed:"<<maxspeed<<endl;
            cout<<"Height:"<<height<<endl;
            cout<<"Seatnum:"<<seatnum<<endl;
         }
         void stop()
        {
        cout<<"摩托车已停止"<<endl; 
        
        };
     ~motorcycle(){};   
};
int main()
{
vehicle a(120,50);
a.vehicle::run();
a.vehicle::stop();
bicycle b(50,50,2);
b.bicycle::run();
b.bicycle::stop();
motorcar c(120,80,2);
c.motorcar::run();
c.motorcar::stop();
motorcycle d(100,80,2,2);
d.motorcycle::run();
d.motorcycle::stop();
 return 0;  
}

技术分享图片

第三题 Fraction

fraction.h

 1 class Fraction
 2 {
 3 public:
 4     Fraction(int x, int y);
 5     Fraction(int x);
 6     Fraction();
 7     void show();
 8     int gcd(int a, int b);
 9     void simplify();
10     Fraction operator+(Fraction &b);
11     Fraction operator-(Fraction &b);
12     Fraction operator*(Fraction &b);
13     Fraction operator/(Fraction &b);
14 protected:
15     int top;
16     int bottom;
17 };

ifraction.h

#pragma once
#include"Fraction.h"
class iFraction :public Fraction
{
private:
    int left;
public:
    iFraction(int x = 0, int y = 1, int z = 0) :Fraction(x, y), left(z) {}
    void print();
    friend iFraction convertF(iFraction &d);
};

fraction.cpp

//fraction.cpp
#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction()
{
    top = 0;
    bottom = 1;
}
Fraction::Fraction(int x, int y)
{
    top = x;
    bottom = y;
}
Fraction::Fraction(int x)
{
    top = x;
    bottom = 1;
}
int Fraction::gcd(int a, int b)
{
    if (a%b == 0)
        return b;
    else
        gcd(b, a%b);
}
void Fraction::simplify()
{
    if (bottom<0)
    {
        top = -top;
        bottom = -bottom;
    }
    int g = gcd(abs(top), abs(bottom));
    top = top / g;
    bottom = bottom / g;
}
void Fraction::show()
{
    Fraction temp;
    temp.top = top;
    temp.bottom = bottom;
    temp.simplify();
    if (temp.bottom == 1)
        cout << temp.top << endl;
    else
        cout << temp.top << "/" << temp.bottom << endl;
}
Fraction Fraction::operator+(Fraction &b)
{
    Fraction temp;
    temp.top = top * b.bottom + b.top*bottom;
    temp.bottom = bottom * b.bottom;
    return temp;
}
Fraction Fraction::operator-(Fraction &b)
{
    Fraction temp;
    temp.top = top * b.bottom - b.top*bottom;
    temp.bottom = bottom * b.bottom;
    return temp;
}
Fraction Fraction::operator*(Fraction &b)
{
    Fraction temp;
    temp.top = top * b.top;
    temp.bottom = bottom * b.bottom;
    return temp;
}
Fraction Fraction::operator/(Fraction &b)
{
    Fraction temp;
    temp.top = top * b.bottom;
    temp.bottom = bottom * b.top;
    return temp;
}

ifraction.cpp

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

main.cpp

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

 

实验六

标签:src   run   停止   image   自行车   include   img   max   ==   

原文地址:https://www.cnblogs.com/wuyijie/p/9141332.html

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