实验内容 1
chip
#include <iostream>
using namespace std;
class Cores
{
private:
int m,n;
public:
Cores(int ,int );
int getm(){return m;}
int getn(){return n;}
int addition();
};
Cores::Cores(int a,int b):m(a),n(b){}
int Cores::addition()
{
return m+n;
}
class CoreA:public Cores
{
public:
CoreA(int ,int );
int subtractoin();
};
CoreA::CoreA(int a,int b):Cores(a,b){}
int CoreA::subtractoin()
{
return getm()-getn();
}
class CoreB:public Cores
{
public:
CoreB(int ,int );
int multiplication();
};
CoreB::CoreB(int a,int b):Cores(a,b){}
int CoreB::multiplication()
{
return getm()*getn();
}
class CoreC:public Cores
{
public:
CoreC(int ,int );
int division();
};
CoreC::CoreC(int a,int b):Cores(a,b){}
int CoreC::division()
{
return getm()/getn();
}
int main()
{
Cores base(3,2);
CoreA A(5,2);
CoreB B(7,2);
CoreC C(9,2);
cout<<base.addition()<<endl;
cout<<A.addition()<<endl;
cout<<B.addition()<<endl;
cout<<C.addition()<<endl;
cout<<A.subtractoin()<<endl;
cout<<B.multiplication()<<endl;
cout<<C.division()<<endl;
return 0;
}
实验内容 2
vehicle
#include <iostream>
using namespace std;
class vehicle
{
public:
vehicle(int ,int );
~vehicle();
void run();
void stop();
private:
int maxspeed;
int weight;
};
vehicle::vehicle(int a,int b):maxspeed(a),weight(b)
{
cout<<"vehicle is being constructed... "<<endl;
cout<<"max speed set as "<<maxspeed<<endl;
cout<<"weight set as "<<weight<<endl;
}
vehicle::~vehicle(){}
void vehicle::run(){cout<<"Running..."<<endl;}
void vehicle::stop(){cout<<"Stop."<<endl;}
class bicycle:virtual public vehicle
{
public:
bicycle(int ,int ,int );
~bicycle();
private:
int height;
};
bicycle::bicycle(int a,int b,int c):vehicle(a,b),height(c)
{
cout<<"bicycle is being constructed... "<<endl;
cout<<"height set as "<<height<<endl;
}
bicycle::~bicycle(){}
class motorcar:virtual public vehicle
{
public:
motorcar(int ,int ,int );
~motorcar();
private:
int seatnum;
};
motorcar::motorcar(int a,int b,int c):vehicle(a,b),seatnum(c)
{
cout<<"motorcar is being constructed... "<<endl;
cout<<"seat number set as "<<seatnum<<endl;
}
motorcar::~motorcar(){}
class motorcycle:public bicycle,public motorcar
{
public:
motorcycle(int ,int ,int ,int );
motorcycle();
~motorcycle();
};
motorcycle::motorcycle(int a,int b,int c,int d):vehicle(a,b),bicycle(a,b,c),motorcar(a,b,d){}
motorcycle::motorcycle():vehicle(0,0),bicycle(0,0,0),motorcar(0,0,0){}
motorcycle::~motorcycle(){}
int main()
{
motorcycle Benz(120,100,1,2);
Benz.run();
Benz.stop();
return 0;
}
实验内容 3
iFraction
iFraction.h
#ifndef IFRACTION_H_INCLUDED
#define IFRACTION_H_INCLUDED
#include "Fraction.h"
class iFraction:public Fraction
{
private:
int integer;//记录带分数系数, 同时表示带分数的正负
unsigned int itop;//记录带分数分母
unsigned int ibottom;//记录带分数分子
public:
iFraction(int ,int );
iFraction(int ,unsigned int ,unsigned int );
void ishow();
};
#endif // IFRACTION_H_INCLUDED
iFraction.cpp
#include<iostream>
#include "iFraction.h"
using namespace std;
iFraction::iFraction(int b,int c):Fraction(b,c)
{
integer=gettop()/getbottom();
itop=abs(gettop()-integer*getbottom());
ibottom=getbottom();
}
iFraction::iFraction(int a,unsigned int b,unsigned int c):integer(a),itop(b),ibottom(c)
{
int pltop=a*ibottom+itop;
Fraction(pltop,ibottom);
}
int idigit(int p)
{
int i=0;
while(p!=0)
{
p/=10;
i++;
}
return i;
}
void icouof(int m,string toc)
{
for(int i=0;i<m;i++)
{
cout<<toc;
}
}
void iFraction::ishow()
{
int intd=idigit(abs(integer));
if(integer<0)intd++;
int topd=idigit(itop);
int botd=idigit(ibottom);
int m=(botd-topd)/2;
icouof(intd+m+1," ");
cout<<itop<<endl;
cout<<integer;
icouof(botd+1,"©¥");
cout<<endl;
icouof(intd+1," ");
cout<<ibottom<<endl;
}
main
#include <iostream>
#include"Fraction.h"
#include"iFraction.h"
using namespace std;
int main()
{
Fraction a(5,-2);
Fraction b(-1,-2);
Fraction f(1,2);
Fraction c;
Fraction g;
g=a*b;
g.show();
g=a/b;
g.show();
c=a-b;
compare(a,b);
compare(b,f);
c.show();
c.trshow();
double m;
m=c.tranf_d();
cout<<m<<endl;
m=a.tranf_d();
cout<<m<<endl;
m=b.tranf_d();
cout<<m<<endl;
m=f.tranf_d();
cout<<m<<endl;
m=g.tranf_d();
cout<<m<<endl;
Fraction p(1635,-135);
p.trshow();
iFraction ben(2,1,3);
ben.ishow();
iFraction hu(-2,1,321);
hu.ishow();
//下面用于手动输入,比较粗糙
/*
while(1)
{
cout<<"请输入两个整数以建立分数"<<endl;
int m,n;
Fraction c;
cin>>m>>n;
Fraction a(m,n);
cout<<"the fraction you set is "<<endl;
a.trshow();
cin>>m>>n;
Fraction b(m,n);
cout<<"the fraction you set is "<<endl;
b.trshow();
cout<<"a+b :"<<endl;c=a+b;c.trshow();
cout<<"a-b :"<<endl;c=a-b;c.trshow();
cout<<"a*b :"<<endl;c=a*b;c.trshow();
cout<<"a/b :"<<endl;c=a/b;c.trshow();
cout<<"请输入三个整数以建立带分数"<<endl;
int p;
cin>>m>>n>>p;
iFraction asd(m,n,p);
cout<<"the fraction you set is "<<endl;
asd.ishow();
}
*/
return 0;
}