SDUST 第8次作业,第7次实验

作业:::::

/*
Problem A, B: 关于multiset的用法,在set文件里, 可自动排序,允许有重复元素;
Problem C, D, E :1, 求最大公约数的递归函数
int gcd(int m, int n)
{
    return m % n == 0 ? n : gcd(n, m % n );
}
对于分数特殊情况的分类和处理!!整理以及综合抽象总结。
Problem G:多态的运用
*/





//Problem F:传值传常引用与传引用之间又出问题了,传参和传常引用都可以,就是不能传引用。

#include <iostream>
#include<cstdlib>
using namespace std;

int gcd2(int m, int n)
{
 return n == 0 ? m : gcd2(n, m % n) ;
}

template <class T>
class Array
{
private:
    T *arr_;
    int len_;
public:
    Array(int n):len_(n)
    {
        arr_=new T[len_];
    }
    void input(int n)
    {
        for(int i=0;i<n;i++)
            cin >> arr_[i];
    }
    T operator[](int i)
    {
        return arr_[i];
    }
};

class Fract
{
private:
    int x_, y_, flag;
public:
    Fract(){}
    Fract(int x,int y)
    {
        judge(x, y);
    }
    void judge(int x, int y)
    {
        if(x == 0) {x_ = 0; flag = 0; y_ = 1;}
        else if((x > 0 && y < 0) || ( x < 0 && y > 0)) {flag = -1;setXY(abs(x), abs(y)); }
        else { flag = 1; setXY(abs(x), abs(y)); }
    }
    void setXY(int x, int y)
    {
        int m = gcd2(x, y);
        x_ = x / m;
        y_ = y / m;
    }
    void show()
    {
        if(flag == 0) cout << "0" << endl;
        else if(flag == 1)
        {
            if(y_ == 1) cout << x_ << endl;
            else cout << x_ << "/" << y_ << endl;
        }
        else
        {
            if(y_ == 1) cout << "-" << x_;
            else cout << "-" << x_ <<"/"<< y_ << endl;
        }
    }
    Fract operator+= (Fract & b)
    {
        int x, y;
        x = x_ * b.y_ *flag + b.flag * b.x_ * y_;
        y = y_ * b.y_;
        judge(x, y);
        setXY(abs(x), abs(y));
    }
    friend istream &operator >> (istream &is,Fract &a)
    {
        int x, y;
        cin >> x >> y;
        a.judge(x, y);
        a.setXY(abs(x), abs(y));
        return is;
    }
};

int main()
{
    int  n;
    cin >> n;
    Array<double> db(1000);
    db.input(n);
    double dbsum(0.0);
    for(int i = 0; i < n; i++)
        dbsum += db[i];
    cout << dbsum << endl;

    cin >> n;
    Array<Fract> fr(1000);
    fr.input(n);
    Fract frsum(0, 1);
    for(int i = 0; i < n; i++)
        frsum += fr[i];
    frsum.show();
}

实验:::::
关于多态,继承和虚函数的一些问题,虚析构函数的应用,
附发现的new的小秘密:
new 出来的东西如果不手动dlete掉,就会自动流失,找不到了。

#include<iostream>
#include<string>
using namespace std;

class Vechicle
{
public:
    int speed_;
    static int num1;
    static int num2;
    Vechicle(int sp): speed_(sp) { }
    static int numOfVechicles()
    {
        num1++;
    }
    static int getNumOfVechicles()
    {
        return num1;
    }
    virtual void show() = 0;
    virtual ~Vechicle()
    {
        cout << "A vechicle is deleted." << endl;
    }
};
int Vechicle:: num1 = 0;

class Bike:public Vechicle
{
public:
    Bike(int a):Vechicle(a){numOfVechicles();}
    void show()
    {
        cout << "A bike's speed is " << speed_ << "km/h." << endl;
    }
    ~Bike()
    {
        cout << "A bike is deleted." << endl;
    }
};
class Car: public Vechicle
{
public:
    Car(int s): Vechicle(s){numOfVechicles();}
    ~Car()
    {
        cout << "A Car is deleted." <<endl;
    }
    void show()
    {
        cout << "A Car's speed is " << speed_ << "km/h." << endl;
    }
};
class MotoBike: public Vechicle
{
public:
    MotoBike(int s): Vechicle(s){  numOfVechicles();}
    ~MotoBike()
    {
        cout << "A MotoBike is deleted." << endl;
    }
    void show()
    {
        cout << "A MotoBike's speed is " << speed_ << "km/h." << endl;
    }
};
class Person
{
public:
    string name_;
    Person(string name): name_(name){}
    void drive(Vechicle &v)
    {
        cout << name_ << " drives. ";
        v.show();
    }
};


int main()
{
    int cases, n;
    char c;
    string name;
    Vechicle* vechicle;
    cout<<"In beginning, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>name>>c>>n;
        Person person(name);
        switch (c)
        {
        case 'B':
            vechicle = new Bike(n);
            break;
        case 'M':
            vechicle = new MotoBike(n);
            break;
        case 'C':
            vechicle = new Car(n);
            break;
        }
        person.drive(*vechicle);
        delete vechicle;
    }
    cout<<"At the end, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值