31121
备注:
|
749
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 16: | 行号 16: |
= 实验内容 = | |
行号 17: | 行号 18: |
= 在线判题系统使用 = | [https://czk.8866.org/oj/problems/] |
行号 20: | 行号 21: |
= 实验内容 = == 实验1 == 实验目的与要求: * 熟悉C++集成开发环境 * 回顾C语言语法 实验步骤: * 按照集成开发环境指南所示使用集成开发环境 * 按照本实验内容要求编写程序 实验内容: 1. 使用递归的方法编写求Fibonacci数列(1 1 2 3 5 8 ……)中某个数的程序。输入n,输出数列中第n个数的值。提交地址:[https://czk.8866.org/oj/problem/2/] 1. 用结构体、函数实现复数操作(复数的加减乘除)[https://czk.8866.org/oj/problem/3/] == 实验2 == 实验目的与要求: * 熟悉C++的输入输出 * C++函数的编写 实验步骤: * 按照集成开发环境指南所示使用集成开发环境 * 按照本实验内容要求编写程序 实验内容: 1. 使用标准C++输入输出实现在屏幕上用以下格式输出乘法表,要求排列整齐(不考虑屏幕宽度不够时造成的折行)。输入:乘法表的行数(1<=n<=99)输出:如下乘法表,总共n行{{{ 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 …… }}}[https://czk.8866.org/oj/problem/4/] 1. 输入各种不同的单词,输出每种单词的个数。输出按照单词的字典顺序排列。比如输入{{{ red blue blue red orange black blue }}}输出:{{{ black 1 blue 3 orange 1 red 2 }}}[https://czk.8866.org/oj/problem/5/] == 实验3 == 在 https://czk.8866.org/oj/volume/1/ 上从6到13题中选做2题 == 其它实验 == 1. Monty Hall游戏:该游戏来自电视节目Let's Make a Deal. 游戏中设有三扇门,其中一扇门后面是一辆汽车,另外两扇门后面各有一头山羊。玩家首先选择其中一扇门,然后节目主持人将另外两扇门中藏有山的那扇门打开,并给游戏参与者一个改选的机会。此时玩家可以维持原先的选择不变,也可以直接选择另一扇门,或者随机决定如何选择。最后玩家如果选中了那个藏有汽车的门,则获得胜利。请你写一个程序,计算游戏中玩家如何选择概率较高,不同的选择概率各是多少? 1. 将前面实验中的复数程序用C++类重新实现{{{#!cplusplus int main() { complex a(5, 6), b, sum, diff, product; b.input(); //读入复数 sum = a.add(b); //求和 diff = a.substract(b); //求差 product = a.multiply(b); //求乘积 sum.display(); diff.display(); product.display(); return 0; } }}} 1. 做一个存放整数的Stack栈类,包含如下成员函数:构造函数Stack(n):n指定栈中可以存放元素的最大个数;push(i):把元素i添加到栈尾部;pop():把尾部元素从栈中删除;top():取栈尾部的元素的值;size():取栈中间存放的元素个数;~Stack():析构函数(如果有必要的话);Stack(Stack & s ):拷贝构造函数(如果有必要)。使如下程序可以正常运行:{{{#!cplusplus int main() { Stack s1(5); s1.push(1); s1.push(2); s1.push(3); Stack s2(s1); while(s1.size() > 0) { cout << s1.top() << endl; s1.pop(); } s1.push(4); s1.push(5); while(s2.size() > 0) { cout << s2.top() << endl; s2.pop(); } } }}} 1. 改写前面的Stack类,使其可以统计Stack类对象的个数,GetStackNumber()函数返回对象的个数。 1. 改写前面的Stack类,使Stack类在整个程序里面只能够存在一个对象。 1. 用链表实现前面的stack类,实现相同的功能。 1. 写Date类,实现如下功能:Date(year, month, day):构造函数,构造代表year年month月day日的Date对象;DaysFrom19900101():从1990年1月1日以来的天数,日期在1990年1月1日以后,返回正数,以前的返回负数;Add(days):计算出days天之后的新日期。days为负为之前;Delta(Date another):计算两天之间的日期差;Output():输出日期值,格式为“二〇〇五年十一月十三日”;WeekDay():返回这个日期是星期几,0表示星期天,1表示星期一,6表示星期六;静态成员函数today()返回今天的日期;所有的成员函数可以定义成const都定义成const。 1. 实现Person, Student, GradStudent, Teacher类。类之间有正确的继承关系;每个类有对应的属性;每个类有构造函数,对属性初始化;每个类都没有默认构造函数;每个类都有一个print成员函数,输出属性的值。{{{#!cplusplus int main() { Person p( ... ); // 省略号处填上所需的初始化参数。 Student s( ... ); Teacher t( ... ); GradStudent g(...); p.print(); s.print(); t.print(); g.print(); } }}} 1. 用虚函数实现前面实验中的print函数{{{#!cplusplus int main() { Person *p[4]; p[0] = new Person(...); p[1] = new Student(...); p[2] = new Teacher( ... ); p[3] = new GradStudent(...); for( int i = 0; i < 4; i++) p[i]->print(); } }}} 1. 实现计算各种图形面积的程序。包括:Shape类:抽象基类,定义GetArea()和GetPerimeter()纯虚函数。Rectangle类:Shape的派生类,高、宽;Circle类:Shape的派生类,半径;每个类定义的构造函数,对成员进行初始化;在基类中定义虚函数GetArea, GetPerimeter,在派生类中override;每个类一个文件。{{{#!cplusplus int main() { Shape *s[2]; s[0] = new Rectangle(...); s[1] = new Circle(...); for ( int i = 0; i < 2; i++) cout<<s[i]->GetArea()<< s[i]->GetPerimeter() <<endl; } }}} 1. 复数程序用运算符重载重新实现,使以下main函数可以运行{{{#!cplusplus int main() { complex c1, c2, c3(-1.5, 1.5); cin >> c1 >> c2; complex c4 = c1 * c2; c4 *= c3; complex c5 = c1 + c2; c5 += c3; complex c6 = c4 - c5; cout << c4 << c5 << c6; cout << "c1 and c2 are" << ( c1 == c2 ? "equal" : "not equal") << endl; } }}} 1. 实现自己的string类,类的声明如下:{{{#!cplusplus class string{ char *str; int size; public: string(); string(const char *s); string(const string &s); string& operator=(const char *); string& operator=(const string&); ~string(); char &operator[](int i); char operator[](int i) const; bool operator==(const string &) const; string operator+(const string &) const; int length() const; operator char*(); friend ostream&operator<<(ostream&, const string&); friend istream&operator>>(istream&, string &); }; }}} 1. 使用多继承实现,使以下程序可以运行:{{{#!cplusplus int main() { Person *p[6]; p[0] = new Person(...); p[1] = new Student(...); p[2] = new Teacher( ... ); Assistant *a = new Assistant(...); Student *s = a; Teacher *t = a; p[3] = s; p[4] = t; p[5] = a; s->print(); t->print(); for( int i = 0; i < 6; i++) p[i]->print(); } }}} 1. 一个大学的教职员工(Person)很多,可以分为行政人员(Clerk),教师(Teacher),实验师(Laboratorian)。行政人员具有行政级别、所在行政部门、考勤状况等信息,教师有职称、从事专业、教授的课程等信息,实验师有实验师级别、主管实验室、承担的实验课程等信息。请用C++类来描述这些信息,并为每个类添加构造函数和display函数。 1. 给大学教职员工管理系统实现显示员工清单及计算工资的功能。工资的计算方法是:教师是职称对应的基本工资加上教授课时数对应的课时费;行政人员是行政级别对应基本工资,加上考勤奖,加上特定行政部门特别补贴;实验师是实验师级别基本工资,加上主管实验室的劳务费,加上承担实验课程的课时费。 = 实验参考答案 = 1. {{{#!cplusplus #include <stdio.h> int fibonacci(int n) { if(n==1 || n==2) return 1; else return fibonacci(n-1) + fibonacci(n-2); } int main() { int n; scanf("%d", &n); printf("%d", fibonacci(n)); } }}} 1. {{{#!cplusplus #include <stdio.h> struct complex { double imag; double real; }; struct complex add(struct complex a, struct complex b) { struct complex c; c.imag = a.imag + b.imag; c.real = a.real + b.real; return c; } struct complex substract(struct complex a, struct complex b) { struct complex c; c.imag = a.imag - b.imag; c.real = a.real - b.real; return c; } struct complex multiply(struct complex a, struct complex b) { struct complex c; c.imag = a.imag * b.real + a.real * b.imag; c.real = a.real * b.real - a.imag * b.imag; return c; } }}} 1. {{{#!cplusplus #include <iostream> #include <iomanip> using namespace std; int main() { int n; cin >> n; for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { cout << setw(5) << i*j; } cout << endl; } } }}} 1. {{{#!cplusplus #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int count; cin >> count; srand(time(NULL)); int nochange = 0; int change = 0; int random = 0; for(int i = 0; i < count; i++) { int car = rand() % 3; int choice = rand() % 3; if(car == choice) nochange ++; else change ++; if(rand()%2) random++; } cout << (double)nochange / count << endl; cout << (double)change / count << endl; cout << (double)random / count << endl; } }}} 1. {{{#!cplusplus #include <iostream> using namespace std; class complex { public: complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { } complex add(complex a) { complex c; c.real = real + a.real; c.imag = imag + a.imag; return c; } void substract(complex a) { imag -= a.imag; real -= a.real; } void multiply(complex a) { double r = real * a.real - imag * a.imag; double i = real * a.imag + imag * a.real; real = r; imag = i; } void print() const{ cout << real << '\t' << imag; } private: double imag; double real; }; int main() { double real, imag; cin >> real >> imag; complex a(real, imag); cin >> real >> imag; complex b(real, imag); complex sum = a.add(b); complex diff = a; diff.substract(b); complex product = a; product.multiply(b); sum.print(); diff.print(); product.print(); return 0; } }}} 1. {{{#!cplusplus #include <iostream> #include <stdexcept> using namespace std; class stack { public: stack(int capacity) : capacity_ (capacity), size_(0), data_ (new int[capacity]) { } void push(int i) { if(size_ < capacity_) data_[size_++] = i; } void pop() { if(size_ > 0) size_ --; } int top() const { if(size_ > 0) return data_[size_-1]; else throw std::out_of_range(""); } int size() const { return size_; } ~stack() { delete[] data_; } stack(stack& s) : size_(s.size_), capacity_(s.capacity_), data_(new int[s.capacity_]) { for(int i =0; i < size_; i++) data_[i] = s.data_[i]; } private: int *data_; int size_; int capacity_; }; }}} 1. {{{#!cplusplus #include <iostream> #include <stdexcept> using namespace std; class stack { public: stack(int capacity) : capacity_ (capacity), size_(0), data_(new int[capacity]) { stack_count_++; } void push(int i) { if(size_ < capacity_) data_[size_++] = i; } void pop() { if(size_ > 0) size_ --; } int top() const { if(size_ > 0) return data_[size_-1]; else throw std::out_of_range(""); } int size() const { return size_; } ~stack() { stack_count_--; delete[] data_; } stack(stack& s) : size_(s.size_), capacity_(s.capacity_), data_(new int[s.capacity_]) { stack_count_++; for(int i =0; i < size_; i++) data_[i] = s.data_[i]; } static int GetStackNumber() { return stack_count_; } private: static int stack_count_; int *data_; int size_; int capacity_; }; int stack::stack_count_; int main() { cout << stack::GetStackNumber()<< endl; stack s1(5); cout << s1.GetStackNumber()<< endl; s1.push(1); s1.push(2); s1.push(3); stack s2(s1); cout << s2.GetStackNumber()<< endl; while(s1.size() > 0) { cout << s1.top() << endl; s1.pop(); } while(s2.size() > 0) { cout << s2.top() << endl; s2.pop(); } } }}} 1. {{{#!cplusplus #include <iostream> #include <stdexcept> using namespace std; class stack { public: void push(int i) { if(size_ < capacity_) data_[size_++] = i; } void pop() { if(size_ > 0) size_ --; } int top() const { if(size_ > 0) return data_[size_-1]; else throw std::out_of_range(""); } int size() const { return size_; } ~stack() { delete[] data_; } static stack &create(int capacity) { static stack s(capacity); return s; } private: stack(stack& s); stack(int capacity) : capacity_ (capacity), size_(0), data_(new int[capacity]) { } int *data_; int size_; int capacity_; }; int main() { stack &s1 = stack::create(5); s1.push(1); s1.push(2); s1.push(3); stack &s2 = stack::create(6); while(s1.size() > 0) { cout << s1.top() << endl; s1.pop(); } while(s2.size() > 0) { cout << s2.top() << endl; s2.pop(); } } }}} 1. {{{#!cplusplus #include <iostream> #include <stdexcept> #include <stdlib.h> using namespace std; struct node{ int data; node *next; }; class stack{ private: node *head; public: stack(){ head=NULL; } void push(int i){ node *p=new node; p->next=head; p->data=i; head=p; } void pop(){ node *p=head; if(p!=NULL){ head=head->next; delete p; } } int size(){ int count=0; for(node *p=head;p!=NULL;p=p->next) count++; return count; } int top(){ return head->data; } stack(stack &s){ head=NULL; node *tail=NULL; for(node *q=s.head;q!=NULL;q=q->next){ node *p=new node; p->data=q->data; p->next=NULL; if(tail==NULL) tail=head=p; else{ tail->next=p; tail=p; } } } ~stack(){ while(head!=NULL) pop(); } }; }}} 1. Date.h: {{{#!cplusplus #ifndef DATE_H #define DATE_H class Date { public: bool SetDate( int, int, int ); // set the date int DaysFrom19900101() const; Date Add(int delta) const; int Delta(const Date &anotherDate) const; void Output() const; void increment(); void decrement(); bool leap_year() const; bool is_equal(const Date &anotherDate) const; int weekday() const; static Date today(); private: int month; int day; int year; bool is_valid() const; }; #endif }}} Date.cc: {{{#!cplusplus #include <iostream> using namespace std; #include "date.h" int nonleap_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int leap_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char *chinese[] = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; bool Date::leap_year() const { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } bool Date::is_valid() const { return year > 0 && month >=1 && month <=12 && day >=0 && ( leap_year() ? day <= leap_days[month-1] : day <= nonleap_days[month-1]); } bool Date::is_equal(const Date &a) const { return year == a.year && month == a.month && day == a.day; } void Date::increment() { day++; if(!is_valid()) { day = 1; month++; if(!is_valid()) { month = 1; year ++; } } } void Date::decrement() { day--; if(day == 0) { month--; if(month == 0) { year --; month = 12; } day = leap_year() ? leap_days[month-1] : nonleap_days[month-1]; } } bool Date::SetDate(int y, int m, int d) { year = y; month = m; day = d; return is_valid(); } int Date::DaysFrom19900101() const { Date d; d.SetDate(1900, 1, 1); return Delta(d); } Date Date::Add(int delta) const { Date d(*this); for(; delta > 0; delta--) d.increment(); for(; delta < 0; delta++) d.decrement(); return d; } int Date::Delta(const Date &anotherDate) const { int diff = 0; Date a (anotherDate); if( year > a.year || year == a.year && month > a.month || year == a.year && month == a.month && day > a.day) for(; !is_equal(a); diff++) a.increment(); else for(; !is_equal(a); diff--) a.decrement(); return diff; } void Date::Output() const { int dy = 1; for(int y = year; y >= 10; y /= 10) { dy *= 10; } for(int y = year; dy > 0; y%=dy, dy/=10) { cout << chinese[ (y - y % dy)/dy]; } cout << "年"; if(month >= 10) cout << "十"; if(month % 10 !=0) cout << chinese[month % 10]; cout << "月"; if(day >= 20) cout << chinese[day / 10]; if(day >= 10) cout << "十"; if(day % 10 != 0) cout << chinese[day % 10]; cout <<"日"; } int Date::weekday() const{ Date d(2005, 12, 25); int delta = Delta(d); if(delta >= 0) return delta % 7; else return ( 7 - (-delta % 7) ) % 7; } Date Date::today() { time_t t = time(NULL); tm *lt = localtime(&t); Date d; d.SetDate(lt->tm_year + 1900, lt->tm_mon+1, lt->tm_mday); return d; } }}} 1. {{{#!cplusplus #include <iostream> #include <string> using namespace std; #define MALE true #define FEMALE false struct Date{ int year, month, day; Date(int y, int m, int d): year(y), month(m), day(d) {} }; class Person{ public: Person(string name, Date birth, bool gender) : name_(name), birth_(birth), gender_(gender) { } virtual void print() const { cout << name_; cout << birth_.year << birth_.month << birth_.day; cout << (gender_?"男":"女"); } protected: string name_ ; Date birth_; bool gender_; }; class Student : public Person{ public: Student(string name, Date birth, bool gender, string id, string department) : Person(name, birth, gender), id_(id), department_(department) {} void print() const { Person::print(); cout << id_ << department_; } protected: string id_; string department_; }; class Teacher : public Person{ public: Teacher(string name, Date birth, bool gender, string title, double salary) : Person(name, birth, gender), title_(title), salary_(salary) {} void print() const { Person::print(); cout << title_ << salary_; } protected: string title_; double salary_; }; class GradStudent : public Student{ public: GradStudent(string name, Date birth, bool gender, string id, string department, Teacher *t) : Student(name, birth, gender, id, department) , tutor_(t) {} void print() const { Student::print(); tutor_->print(); } protected: Teacher *tutor_; }; int main() { Person *p = new Person( "jack", Date(1980, 8, 8), MALE ); Student *s = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); Teacher *t = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); GradStudent *g = new GradStudent("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", t); p->print(); cout << endl; s->print(); cout << endl; t->print(); cout << endl; g->print(); cout << endl; delete g; delete p; delete s; delete t; } }}} 1. {{{#!cplusplus #include <iostream> #include <string> using namespace std; #define MALE true #define FEMALE false struct Date{ int year, month, day; Date(int y, int m, int d): year(y), month(m), day(d) {} }; class Person{ public: Person(string name, Date birth, bool gender) : name_(name), birth_(birth), gender_(gender) { } virtual void print() const { cout << name_; cout << birth_.year << birth_.month << birth_.day; cout << (gender_?"男":"女"); } protected: string name_ ; Date birth_; bool gender_; }; class Student : public Person{ public: Student(string name, Date birth, bool gender, string id, string department) : Person(name, birth, gender), id_(id), department_(department) {} void print() const { Person::print(); cout << id_ << department_; } protected: string id_; string department_; }; class Teacher : public Person{ public: Teacher(string name, Date birth, bool gender, string title, double salary) : Person(name, birth, gender), title_(title), salary_(salary) {} void print() const { Person::print(); cout << title_ << salary_; } protected: string title_; double salary_; }; class GradStudent : public Student{ public: GradStudent(string name, Date birth, bool gender, string id, string department, Teacher *t) : Student(name, birth, gender, id, department) , tutor_(t) {} void print() const { Student::print(); tutor_->print(); } protected: Teacher *tutor_; }; int main() { Person *p[4]; p[0] = new Person( "jack", Date(1980, 8, 8), MALE ); p[1] = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); p[2] = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); p[3] = new GradStudent("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", (Teacher *)p[2]); for(int i = 0; i < 4; i++) { p[i] -> print(); cout << endl; } } }}} 1. shape.h: {{{#!cplusplus #ifndef SHAPE #define SHAPE class Shape{ public: virtual double GetArea()=0; virtual double GetPerimeter()=0; }; #endif }}} circle.h: {{{#!cplusplus #ifndef CIRCLE #define CIRCLE #define PI 3.14 class Circle : public Shape{ public: Circle(double radius_){ radius=radius_; } virtual double GetArea(){ return PI*radius*radius; } virtual double GetPerimeter(){ return 2*PI*radius; } protected: double radius; }; #endif }}} rectangle.h: {{{#!cplusplus #ifndef RECTANGLE #define RECTANGLE class Rectangle : public Shape{ public: Rectangle(double height_,double width_){ height=height_; width=width_; } virtual double GetArea(){ return height*width; } virtual double GetPerimeter(){ return 2*(height+width); } protected: double height,width; }; #endif }}} main.cc: {{{#!cplusplus #include<stdlib.h> #include<iostream> #include"Shape.h" #include"Rectangle.h" #include"Circle.h" using namespace std; int main() { Shape *s[2]; s[0] = new Rectangle(4,3); s[1] = new Circle(5); for ( int i = 0; i < 2; i++){ cout<<"Area:"<<s[i]->GetArea()<<endl; cout<<"Perimeter:"<<s[i]->GetPerimeter()<<endl; } } }}} 1. {{{#!cplusplus #include <iostream> using namespace std; class complex { public: complex(double r = 0.0,double i = 0.0) : real_(r), imag_(i) {} double &real(){ return real_; } double &imag(){ return imag_; } double real() const { return real_; } double imag() const { return imag_; } complex &operator+=(const complex &a) { real_ += a.real_; imag_ += a.imag_; return *this; } complex &operator-=(const complex &a) { real_ -= a.real_; imag_ -= a.imag_; return *this; } complex &operator*=(const complex &a) { double r = real_ * a.real_ - imag_ * a.imag_; double i = real_ * a.imag_ + imag_ * a.real_; real_ = r; imag_ = i; return *this; } private: double real_,imag_; }; bool operator==(const complex &a, const complex &b){ return a.real() == b.real() && a.imag() == b.imag(); } ostream &operator<<(ostream &os, const complex &c) { return os << "(" << c.real()<< showpos << c.imag() << "i)"; } istream &operator>>(istream &is, complex &c) { return is >> c.real() >> c.imag(); } complex operator+(complex a, const complex &b){ return a += b; } complex operator-(complex a, const complex &b) { return a -= b; } complex operator*(complex a, const complex &b) { return a *= b; } }}} 1. {{{#!cplusplus #include <iostream> class string{ char *str; int size; public: string() : size(1), str(new char[1]) { str[0] = '\0'; } string(const char *s) { size = strlen(s)+1; str = new char[size]; strcpy(str, s); } string(const string &s) { size = s.size; str = new char[size]; strcpy(str, s.str); } string& operator=(const char *s) { delete[] str; size = strlen(s); str = new char[size]; strcpy(str, s); } string& operator=(const string&s) { if(this!=&s) { delete[] str; size = s.size; str= new char[size]; strcpy(str, s.str); }} ~string() { delete [] str;} char &operator[](int i) { return str[i];} char operator[](int i) const { return str[i]; } bool operator==(const string &s) const { return strcmp(str, s.str) == 0; } string operator+(const string &s) const { char *t = new char[size+s.size-1]; strcpy(t, str); strcat(t, s.str); string r(t); delete[] t; return r; } int length() const { return size - 1; } friend std::ostream&operator<<(std::ostream&os, const string&s) { return os << s.str; } friend std::istream&operator>>(std::istream&is, string &s) { int capacity = 10; char *t = new char[capacity]; int i = 0; int c; while( !isspace(c = is.get())) { t[i++] = c; if(i == capacity) { char * t2 = new char [capacity * 2]; for(int j = 0; j < capacity; j++) t2[j] = t[j]; capacity *= 2; delete[] t; t = t2; } } t[i] = '\0'; s = t; delete[] t; return is; } }; }}} 1. {{{#!cplusplus #include <iostream> #include <string> using namespace std; #define MALE true #define FEMALE false struct Date{ int year, month, day; Date(int y, int m, int d): year(y), month(m), day(d) {} }; class Person{ public: Person(string name, Date birth, bool gender) : name_(name), birth_(birth), gender_(gender) { } virtual void print() const { cout << name_; cout << birth_.year << birth_.month << birth_.day; cout << (gender_?"男":"女"); } virtual ~Person(){} protected: string name_ ; Date birth_; bool gender_; }; class Student : virtual public Person{ public: Student(string name, Date birth, bool gender, string id, string department) : Person(name, birth, gender), id_(id), department_(department) {} void print() const { Person::print(); cout << id_ << department_; } protected: string id_; string department_; }; class Teacher : virtual public Person{ public: Teacher(string name, Date birth, bool gender, string title, double salary) : Person(name, birth, gender), title_(title), salary_(salary) {} void print() const { Person::print(); cout << title_ << salary_; } protected: string title_; double salary_; }; class Assistant : public Student, public Teacher{ public: Assistant(string name, Date birth, bool gender, string id, string department, string title, double salary) : Student(name, birth, gender, id, department) , Teacher(name, birth, gender, title, salary), Person(name, birth, gender) {} void print() const { Student::print(); Teacher::print(); } }; int main() { Person *p[6]; p[0] = new Person( "jack", Date(1980, 8, 8), MALE ); p[1] = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); p[2] = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); Assistant *a = new Assistant("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", "assistant", 500); Student *s = a; Teacher *t = a; p[3] = s; p[4] = t; p[5] = a; s->print(); t->print(); for( int i = 0; i < 6; i++) { p[i]->print(); cout << endl; } delete p[0]; delete p[1]; delete p[2]; delete a; } }}} |
实验基本要求
- 用标准C++完成程序
- 按照传统C语言风格进行命名和排版
- 在程序必要的地方进行注释
集成开发环境指导
- VC 2003 ( 集成 Microsoft C++ Compiler 7.1,Windows下最好,调试方便,价格高) ["VC++2003使用说明"]
- Dev-C++ ( 集成 MinGW编译器,小巧,免费,调试不好 ) ["Dev-C++使用说明"]
- Emacs + GCC + GDB( Linux平台下通用开发平台,多语言,免费,入门难 )
- Eclipse + CDT (配合GCC+GDB或者MinGW)( 跨平台,多语言编程环境,免费,速度慢 )["Eclipse开发C++程序使用说明"]
实验内容
[https://czk.8866.org/oj/problems/]
["在线判题系统使用说明"]