题目一
【问题描述】
看程序写结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include<iostream>
using namespace std;
class shape
{
public:
void Draw() { cout << "Base::Draw()\n"; }
void Erase() { cout << "Base::Erase()\n\n"; }
shape() { Draw(); }
~shape() { Erase(); }
};
class Polygon :public shape
{
public:
Polygon() { Draw(); }
void Draw() { cout << "Polygon::Draw()\n"; }
void Erase() { cout << "Polygon::Erase()\n"; }
~Polygon() { Erase(); }
};
class Rectangle :public Polygon
{
public:
Rectangle() { Draw(); }
void Draw() { cout << "Rectangle::Draw()\n"; }
void Erase() { cout << "Rectangle::Erase()\n"; }
~Rectangle() { Erase(); }
};
class Square :public Rectangle
{
public:
Square() { Draw(); }
void Draw() { cout << "Square::Draw()\n"; }
void Erase() { cout << "Square::Erase()\n"; }
~Square() { Erase(); }
};
int main()
{
Square t;
cout << "----------------\n";
}
|
【输出】
1 2 3 4 5 6 7 8 9 10 11
| Base::Draw() Polygon::Draw() Rectangle::Draw() Square::Draw() ---------------- Square::Erase() Rectangle::Erase() Polygon::Erase() Base::Erase()
|
题目二
【问题描述】
队列具有先进先出的特点,所有新来的元素都放在队列尾部,出队列的元素从队列头部出去。下面是队列的示意图:
栈具有后进先出的特点。所有入栈的元素都放在栈顶,出栈时栈顶元素先出。下面是栈的示意图:
这两种结构具有很多相似的地方:都存放了一系列的元素,元素的操作都在两头进行,元素个数都是动态可变的。我们可以设计一个基类,完成它们共同的功能,然后分别派生出队列类和栈类。本实验要求以上一个实验实现的链表类作为基类并设计它的两个派生类。
要求:
(1) 在上一个实验实现的链表类的基础上派生队列类和栈类,要求队列类可以进行元素入队列和出队列操作,栈类可以进行入栈和出栈操作。
(2) 在队列类中实现一个输出队列内容的函数printQueue,在栈类中实现一个输出栈中内容的函数printStack,
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| #include <iostream> #include <string> using namespace std; class Person { public: Person(){ } Person(int ID,string name); void showMessage(); int getId(); private: int mId; string mName; }; Person::Person(int ID,string name) { this->mId=ID; this->mName=name; } int Person::getId() { return mId;} void Person::showMessage() { cout << this->mId << " " << this->mName << endl; } class Node { public: Node(); Node(Person val); friend class LinkList; void showMessage(); protected: Person data; Node *next; }; class LinkList { public: LinkList(); void addNodeAtTail(Person val); void print(); void deleteNode(int id); int getHead (Person &val); void addNodeAtHead(const Person &val); protected: Node *head; }; int LinkList::getHead (Person &val) { int n; if(head==NULL) { cout<<"error"<<endl; return 0; } else {n=(head->data).getId(); val=head->data; Node *temp=head; head=head->next; delete temp; } return 1; } void LinkList::deleteNode(int id) { Node* p=NULL, * q=NULL; if (head!= NULL) { p = head; while (p != NULL && p->data.getId() != id) { q = p; p = p->next; } if (p != NULL) { if (p == head) { head = head->next; delete p; } else { q->next = p->next; delete p; } } } } void LinkList::addNodeAtHead(const Person& val) { Node* pNew = new Node(val); pNew->next = head; head = pNew; }
void LinkList::addNodeAtTail(Person val) { Node* p, * q; p = new Node(val); if (head == NULL) head = p; else { q = head; while (q->next != NULL) q = q->next; q->next = p; } } void LinkList::print() { Node* ptmp = head; while (ptmp != NULL) { ptmp->showMessage(); ptmp = ptmp->next; } }
LinkList::LinkList() { this->head = NULL; }
class Queue:public LinkList { public: void enterQueue(const Person &val); int outQueue(Person &val); void printQueue(); }; void Queue::enterQueue(const Person& val) { addNodeAtTail(val); } int Queue::outQueue(Person& val) { return getHead(val); } void Queue::printQueue() { cout<<"queue member:"<<endl; print(); } Node::Node() { next = NULL; } Node::Node(Person val) { data = val; next = NULL; } void Node::showMessage() { data.showMessage(); }
class Stack :public LinkList { public: void push(const Person& val); int pop(Person &val); void printStack(); };
void Stack::push(const Person &val) {addNodeAtHead(val);} int Stack::pop(Person &val) {return getHead(val);} void Stack::printStack() {cout<<"stack member:"<<endl; print(); } int main() { string name; Queue *q1=new Queue; Stack *s1=new Stack; cout << "请输入学生姓名:" << endl; Person val; for (int i = 2018003; i >= 2018001; i--) { cin >> name; val=Person(i,name); s1->push(val); } for (int i = 2018004; i <= 2018006; i++) { cin >> name; val=Person(i,name); q1->enterQueue(val); } cout <<"学生信息为:"<< endl; q1->printQueue(); s1->printStack(); cout<<"outqueue:"<<endl; for(int i=0;i<2;i++) {q1->outQueue(val); val.showMessage(); } cout<<"pop:"<<endl; for(int i=0;i<2;i++) { s1->pop(val); val.showMessage(); } cout<<"after deletes:"<<endl; q1->printQueue(); s1->printStack(); delete q1; delete s1; return 0; }
|
代码可能略有不足,请大佬多多指教