DLUT C++上机作业(实验六)

注意,博客所有代码在VS上均能编译通过,codeblocks等编译器可能因为某些变量名无法识别而无法编译。(我的VS上不能用end做变量名就很迷呀)
/************************************************/

(2)有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。

#include
#include
using namespace std;
class vehicle{
public:
    virtual void show(){
        cout << "vehicle" << endl;
    }
    virtual ~vehicle(){}
};
class car :public vehicle{
public:
    void show();
};
void car::show(){
    cout << "car " << endl;
}
class truck :public vehicle{
public:
    void show();
};
void truck::show(){
    cout << "truck" << endl;
}
class boat :public vehicle{
public:
    void show();
};
void boat :: show(){
    cout << "boat" << endl;
}
int main(){
    vehicle v;
    car c;
    truck t;
    boat b;
    vehicle *p[4];
    p[0] = &v;
    p[1] = &c;
    p[2] = &t;
    p[3] = &b;
    for (int i = 0; i < 4; i++){
        p[i]->show();
    }
}
/******************/
/*(3)实现下图中的Shape层次结构。
每个TwoDimensionalShape类应包括成员函数getArea,以计算二维图形的面积
。每个ThreeDimensionalShape类包含成员函数getArea和getVolume,
分别计算三维图形的表面积和体积。编写一个程序
,使用层次结构中每个具体类的对象的Shape向量指针
。程序要打印出向量元素所指的对象。
同样,再将所有形状存入向量的循环中,
要能判断每个图形到底属于TwoDimensionalShape还是属于ThreeDimenionalShape。
如果某个图形是TwoDimensionalShape就显示其面积,
如果某个图形是ThreeDimenionalShape,则显示其面积和体积。*/
#include
#include
#define pi acos(-1.0)
using namespace std;
class shape{
public:
    virtual double getarea() = 0;

    virtual void show(){};
    virtual void showname(){};
};
class twoshape:public shape{
public:
    void show(){
        cout << getarea() << endl;
    }
};
class threeshape:public shape{
public:
    virtual double getvolum() = 0;
    void show(){
        cout << getarea() << endl << getvolum() << endl;
    }
};
class square :public twoshape{
private:
    double size;
public:
    square(double sizes) :size(sizes){}
    double getarea(){
        return size*size;
    }
    void showname(){ cout << "square" << endl; }
};
class triangle :public twoshape{
private:
    double a, b, c;
public:
    triangle(double d, double e, double f) :a(d), b(e), c(f){}
    double getarea(){
        double s = (a + b + c) / 2;
        return s*(s - a)*(s - b)*(s - c);
    }
    void showname(){
        cout << "truangle" << endl;
    }
};

class circle :public twoshape{
private:
    double r;
public:
    circle(double rr) :r(rr){}
    double getarea(){
        return pi*r*r;
    }
    void showname(){
        cout << "circle" << endl;
    }
};
class sphere :public threeshape{
private:
    double r;
public:
    sphere(double rr) :r(rr){}
    double getarea(){
        return pi * 4 * r*r;
    }
    double getvolum(){
        return pi * 4 / 3 * r*r*r;
    }
    void showname(){
        cout << "sphere" << endl;
    }
};
class cube :public threeshape{
private:
    double a, b, c;
public:
    cube(double d, double e, double f) :a(d), b(e), c(f){}
    double getarea(){
        return 2 * (a*b + b*c + c*a);
    }
    double getvolum(){
        return a*b*c;
    }
    void showname(){
        cout << "cube" << endl;
    }
};
int main(){
    shape*arr[5];
    square squ(4);
    triangle tri(3, 4, 5);
    circle ci(4);
    sphere sp(4);
    cube cub(3, 4, 5);
    arr[0] = □
    arr[1] = &tri;
    arr[2] = &ci;
    arr[3] = &sp;
    arr[4] = &cub;
    for (int i = 0; i < 5; i++){
        arr[i]->showname();
        arr[i]->show();
    }
}

(4)
编写一个程序,先设计一个链表List类,再从链表类派生出一个集合类Set类,再集合类中添加一个记录元素个数的数据项。要求可以实现对集合的插入、删除、查找和显示。

#include
#include
using namespace std;
class node{
public:
    node*next;
    node*front;
    int x;
    friend class list;
};
class list{
private:
    node *head;
    node*tail;
    node*temp;
public:
    void add(int);
    list();
    ~list();
    void show();

};
class set :public list{
private:
    node *head, *tail, *temp;
    int len;
public:
    void add(int);
    void remove(int);
    node*find(int);
    void show();
    set::set();
    set::~set();
    int getnum();
};
int set::getnum(){
    return len;
}
void set::show(){
    node *t = head;
    while (t){
        cout << t->x << " ";
        t = t->next;
    }
    cout << endl;
}
set::set(){
    head = tail = temp = NULL;
    len = 0;
}
set::~set(){

    while (head){
        node*t = head;
        t = t->next;
        delete head;
        head = t;
    }

}
void set::add(int x){
    temp = new node;
    temp->x = x;
    temp->front = NULL;
    temp->next = NULL;
    if (!head){
        head = tail = temp;
        len = 1;
    }
    else{
        node *t = head;
        len++;
        while (t){
            if (x > tail->x){
                tail->next = temp;
                temp->front = tail;
                tail = temp;
                break;
            }
            if (t != head)
            {
                if (x < t->x){
                    if (x == t->front->x){
                        len--;
                        break;
                    }
                    temp->next = t;
                    t->front->next = temp;
                    t->front = temp;
                    break;
                }
            }
            else{
                if (x < t->x){
                    temp->next = t;
                    t->front = temp;
                    head = temp;
                    break;
                }
            }
            t = t->next;
        }
    }
}
list::list(){
    head = tail = temp = NULL;
}
list::~list(){
    while (head){
        node*t = head;
        t = t->next;
        delete head;
        head = t;
    }
}
void list::add(int x){
    temp = new node;
    temp->x = x;
    temp->next = NULL;
    if (tail == NULL){
        head = tail = temp;
    }
    else{
        tail->next = temp;
        tail = temp;
    }
}
void list::show(){
    node *t = head;
    while (t){
        cout << t->x << " ";
        t = t->next;
    }
    cout << endl;
}
void set::remove(int x){
    node*t = head;
    bool flag = false;
    while (t){
        if (t->x == x){
            flag = true;
            len--;
            if (t == head){
                t = head->next;
                delete head;
                head = t;
                break;
            }
            else{
                t->front->next = t->next;
                delete t;
                break;
            }


        }
        t = t->next;
    }
    if (!flag)cout << "mistake" << endl;
}
node* set::find(int x){
    node*t = head;
    bool flag = false;
    while (t){
        if (t->x == x){
            flag = true;
            return t;
        }
        t = t->next;
    }
    if (!flag)cout << "mistake" << endl;
    return NULL;
}
int main(){
    list lis;
    lis.add(1);
    lis.add(2);
    lis.add(3);
    lis.show();
    set se;
    for (int i = 0; i < 5; i++){
        se.add(i);
    }
    se.add(3);
    se.show();
    cout << se.getnum() << endl;
    se.remove(3);
    se.show();
    node *t = se.find(2);
    cout << t->x << endl;
    cout << se.getnum() << endl;
}

你可能感兴趣的:(作业)