山东科技大学2020年5月7日作业题解

山东科技大学2020年5月7日作业题解

题目一: 汽车、客车、货车

Description

定义Vehicle类,包括:

  1. 一个int类型属性num,表示汽车的轮子数量。
  2. 构造函数、析构函数,输出如样例所示的信息。
    定义Bus类,是Vehicle类的子类,包括:
  3. 一个int类型属性guest,表示客车可乘坐的人数。
  4. 构造函数、析构函数,输出如样例所示的信息。
    定义Truck类,是Vehicle类的子类,包括:
  5. 一个int类型属性weight,表示货车可载荷的货物重量。
  6. 构造函数、析构函数,输出如样例所示的信息。

Input

输入有多行,每行一个测试用例,每行包括1个正整数(车的轮子数量)、1个字符b或者t(b、t分别表示客车、货车)、1个正整数(可乘坐的人数或可承载的货物重量)。

Output

见样例。

Sample Input

4 b 40
8 t 15

Sample Output

Vehicle has 4 wheels is created.
Bus which can carry 40 persons is created.
Bus which can carry 40 persons is erased.
Vehicle has 4 wheels is erased.
Vehicle has 8 wheels is created.
Truck which can carry 15 tons goods is created.
Truck which can carry 15 tons goods is created.
Vehicle has 8 wheels is erased.

扫描二维码关注公众号,回复: 11227271 查看本文章

题目给定代码

int main()
{
    int w, g;
    char t;
    Vehicle *veh;
    while (cin>>w>>t>>g)
    {
        if (t == 'b')
        {
            veh = new Bus(w, g);
        }
        else
        {
            veh = new Truck(w, g);
        }
        delete veh;
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Vehicle{
	public:
		int number;
	public:
		Vehicle(int n){
			number = n;
			cout << "Vehicle has " << number << " wheels is created.\n";
		}
		~Vehicle(){
			cout << "Vehicle has " << number << " wheels is erased.\n";
		}
};
class Bus : public Vehicle{
	private:
		int guest;
	public:
		Bus(int a, int b) : Vehicle(a), guest(b){
			cout << "Bus which can carry " << guest << " persons is created.\n";
			cout << "Bus which can carry " << guest << " persons is erased.\n";	
		}
		~Bus(){
			
		}
		
};
class Truck : public Vehicle{
	private:
		int weight;
	public:
		Truck(int g, int w) : Vehicle(g), weight(w){
			cout << "Truck which can carry " << weight << " tons goods is created.\n";
			cout << "Truck which can carry " << weight << " tons goods is created.\n";
		}
		~Truck(){
			
		}
};
int main()
{
    int w, g;
    char t;
    Vehicle *veh;
    while (cin>>w>>t>>g)
    {
        if (t == 'b')
        {
            veh = new Bus(w, g);
        }
        else
        {
            veh = new Truck(w, g);
        }
        delete veh;
    }
    return 0;
}

**题目二:**老师与学生

Description

定义Person类,
1.有一个int类型属性age和1个char类型属性sex,分别为年龄和姓名。
2.构造函数和析构函数,输出如样例所示的信息。
定义Student类,是Person类的子类:
1.有一个int类型属性,是学生所在的班级号。
2.构造函数与析构函数,输出如样例所示的信息。
定义Teacher类,是Person类的子类:
1.有一个int类型属性,表示老师的工资。
2.构造函数与析构函数,输出如样例所示的信息。

Input

输入有多行,每行是一个测试用例。每行的第1个是一个正整数,是年龄;第2个是一个字符,表示性别;第3个是一个字符t或s,表示老师或学生;第4个是一个正整数,表示班号(对于学生)或工资(对于老师)。

Output

见样例。

Sample Input

18 f s 1
35 m t 8001

Sample Output

Person age = 18, sex = f is created.
Student of class 1 is created.
Student of class 1 is erased.
Person age = 18, sex = f is erased.
Person age = 35, sex = m is created.
Teacher with salary 8001 is created.
Teacher with salary 8001 is erased.
Person age = 35, sex = m is erased.

题目给定代码

int main()
{
    Person *p;
    int age, par;
    char sex, t;
    while (cin>>age>>sex>>t>>par)
    {
        if (t == 's')
        {
            p = new Student(age, sex, par);
        }
        else
        {
            p = new Teacher(age, sex, par);
        }
        delete p;
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Person{
	public:
		int age;
		char sex;
	public:
		Person(int a, char s){
			age = a, sex = s;
			cout << "Person age = " << age << ", sex = " << sex << " is created.\n";
		}
		~Person(){
			cout << "Person age = " << age << ", sex = " << sex << " is erased.\n";
		}
};
class Student : public Person{
	private:
		int class_number;
	public:
		Student(int age, char sex, int par) : Person(age, sex), class_number(par){
			cout << "Student of class " << class_number << " is created.\n";
			cout << "Student of class " << class_number << " is erased.\n";
		}
		~Student(){
			
		}
};
class Teacher : public Person{
	private:
		int money;
	public:
		Teacher(int age, char sex, int par) : Person(age, sex), money(par){
			cout << "Teacher with salary " << money << " is created.\n";
			cout << "Teacher with salary " << money << " is erased.\n";
		}
		~Teacher(){
			
		}
};
int main()
{
    Person *p;
    int age, par;
    char sex, t;
    while (cin>>age>>sex>>t>>par)
    {
        if (t == 's')
        {
            p = new Student(age, sex, par);
        }
        else
        {
            p = new Teacher(age, sex, par);
        }
        delete p;
    }
}

**题目三:**一帮学生

Description

学生Student类是Person类的子类,而且每个人都有生日,生日是Date类的对象。所以,需要定义如下类:

  1. Date类:拥有年、月、日三个int类型的属性。

  2. Person类:有一个Date类型对象的属性(表示生日)、string类型属性(表示名字),以及一个int类型的静态属性numOfPersons(对象个数)。

  3. Student类:是Person类的子类,并拥有一个int类型属性(表明学生学号),一个int类型的静态属性numOfStudents(对象个数)。
    定义上述类的构造、析构函数,并根据样例输出格式输出相应的信息。

Input

第一行整数N>0表示之后有N行输入。

之后的N行,每行包括4个整数、1个字符串,分别表示年、月、日、学号和姓名。

Output

见样例~

Sample Input

3
2010 3 4 1 Tom
2010 3 5 2 Jack
2010 3 6 3 Mary

Sample Output

Date 2010-3-4 is created.
The 1th person Tom whose birthday is 2010-3-4 is created.
The 1th student Tom whose birthday is 2010-3-4 and id is 1 is created.
Date 2010-3-5 is created.
The 2th person Jack whose birthday is 2010-3-5 is created.
The 2th student Jack whose birthday is 2010-3-5 and id is 2 is created.
Date 2010-3-6 is created.
The 3th person Mary whose birthday is 2010-3-6 is created.
The 3th student Mary whose birthday is 2010-3-6 and id is 3 is created.
Student Tom whose birthday is 2010-3-4 and id is 1 is erased.
Person Tom whose birthday is 2010-3-4 is erased.
Date 2010-3-4 is erased.
Student Jack whose birthday is 2010-3-5 and id is 2 is erased.
Person Jack whose birthday is 2010-3-5 is erased.
Date 2010-3-5 is erased.
Student Mary whose birthday is 2010-3-6 and id is 3 is erased.
Person Mary whose birthday is 2010-3-6 is erased.
Date 2010-3-6 is erased.

题目给定代码

int main()
{
    int year, month, day, id, i;
    string name;
    int num;
    cin>>num;
    Student **students = new Student*[num];
    for (i = 0; i < num; i++)
    {
        cin>>year>>month>>day>>id>>name;
        students[i] = new Student(year, month, day, name, id);
    }
    for (i = 0;i <num; i++)
        delete students[i];
    delete[] students;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Date{
	public:
	    int year_(){
			return year;
		}
	    int month_(){
			return month;
		}
	    int day_(){
			return day;
		}
	public:
	    Date(int y, int m, int d) : year(y), month(m), day(d){
			cout << "Date " << year_() << "-" << month_() << "-" << day_() << " is created." << endl;
		}
	    ~Date(){
			cout << "Date " << year_() << "-" << month_() << "-" << day_() << " is erased." << endl;
		}
	private:
	    int year,month,day;
};
class Person{
	public:
	    Person(int y, int m, int d, string n) : birth(y, m, d), name(n){
	        ++ numOfPerson;
	        cout << "The " << numOfPerson << "th person " << name << " whose birthday is " << birth.year_() << "-" << birth.month_() << "-" << birth.day_() << " is created." << endl;
		}
	    ~Person(){
			cout << "Person " << name << " whose birthday is " << birth.year_() << "-" << birth.month_() << "-" << birth.day_() << " is erased." << endl;
		}
	public:
	    string name_(){
			return name;
		}
	    Date &birth_(){
			return birth;
		}
	private:
	    string name;
	    Date birth;
	    static int numOfPerson;
};
int Person :: numOfPerson = 0;
class Student : public Person{
	public:
	    Student(int y, int m, int d, string n, int id) : Person(y, m, d, n){
	        stu = id;
	    ++numOfStudent;
	    cout << "The " << numOfStudent << "th student " << name_() << " whose birthday is " << birth_().year_() << "-" << birth_().month_() << "-" << birth_().day_() << " and id is " << stu << " is created." << endl;
	    }
	    ~Student(){
	    cout << "Student " << name_() << " whose birthday is " << birth_().year_() << "-" << birth_().month_() << "-" << birth_().day_() << " and id is " << stu << " is erased." << endl;
	    }
	private:
	    int stu;
	    static int numOfStudent;
};
int Student :: numOfStudent = 0;
int main()
{
    int year, month, day, id, i;
    string name;
    int num;
    cin>>num;
    Student **students = new Student*[num];
    for (i = 0; i < num; i++)
    {
        cin>>year>>month>>day>>id>>name;
        students[i] = new Student(year, month, day, name, id);
    }
    for (i = 0;i <num; i++)
        delete students[i];
    delete[] students;
    return 0;
}

**题目四:**家教课程

Description

老师都是穷人,所以需要经常去外面搞点兼职啥的。可是除了上课,啥也不会啊。所以就只好做家教了。现在请利用面向对象的思想设计这样一个系统。具有如下类:

  1. Person类:有一个string类型的属性,表明对象的名字。是Student和Teacher的父类。

  2. Student类:是Person类的子类,拥有一个int类型的属性,表明对象的序号。

  3. Teacher类:是Person类的子类,拥有一个string类型的属性,表明对象的职称。

  4. Course类:是一个组合类,有1个Teacher类的对象、1个Student类型的对象,以及一个string类型的属性(表明对象的名称)组成。

请定义上述类的构造函数和析构函数,并在函数中输出相应的字符串。具体格式请参照样例输出。

Input

输入5行,前4个是4个字符串,分别是老师的名字、学生的名字、老师的职称、课程的名字。最后一行是一个整数,表示学生的序号。

Output

见样例~

Sample Input

Tom
Jack
Prof
C++
10

Sample Output

Person Tom is created.
Person Jack is created.
Person Tom is created.
Teacher Tom with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Person Jack is created.
Teacher Jack with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Course C++ is created.
Course C++ is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Jack with position Prof is erased.
Person Jack is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Tom with position Prof is erased.
Person Tom is erased.
Person Jack is erased.
Person Tom is erased.

题目给定代码

int main()
{
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Person{
	public:
		string name;
	public:
		Person(string n){
			name = n;
			cout << "Person " << name << " is created.\n";
		}
		~Person(){
			cout << "Person " << name << " is erased.\n";
		}
};
class Student : public Person{
	public:
		int id;
		string name;
	public:
		Student(string s, int i) : Person(s), id(i){
			name = s;
			cout << "Student " << s << " with id " << id << " is created.\n";
		}
		~Student(){
			cout << "Student " << name << " with id " << id << " is erased.\n";
		}
};
class Teacher : public Person{
	public:
		string job;
	public:
		Teacher(string name, string job_job) : Person(name){
			job = job_job;
			cout << "Teacher " << name << " with position " << job << " is created.\n";
		}
		~Teacher(){
			cout << "Teacher " << name << " with position " << job << " is erased.\n";
		}
};
class Course{
	public:
		Teacher T;
		Student S;
		string name;
	public:
		Course(string s1, string s2, string s3, string s4, int i) : T(s2, s3), S(s2, i){
			cout << "Course " << s4 << " is created.\n";
			name = s4;
		}
		~Course(){
			cout << "Course " << name << " is erased.\n";
		}
};
int main()
{
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

**题目五:**Person类与Student类的关系

Description

当然,一个student首先是一个person。所以,Student类是Person类的派生类。请定义Person类,包括:

  1. 数据成员string name和int age,分别表示姓名和年龄。

  2. 构造函数和析构函数,它们有相应的输出,见样例。

  3. void show()函数:按照样例输出该对象的name和age属性值。

定义Student类,是Person类的子类:

  1. 数据成员int grade,表示学生所在年级。

  2. 构造函数和析构函数,它们有相应的输出,见样例。

  3. void show()函数:按照样例输出该对象的grade属性值。

Input

只有1行,分为三部分:一个不含空白符的字符串以及两个整数。

Output

见样例。

Sample Input

Tom 19 3

Sample Output

A person Tom whose age is 19 is created.
A student whose grade is 3 is created.
Name is Tom and age is 19.
Grade is 3.
A student whose grade is 3 is erased.
A person Tom whose age is 19 is erased.

题目给定代码

int main()
{
    string n;
    int a, g;
    cin>>n>>a>>g;
    Student student(n, a, g);
    student.Person::show();
    student.show();
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Person{
	public:
		string name;
		int age;
	public:
		Person(string n, int a){
			name = n, age = a;
			cout << "A person " << name << " whose age is " << age << " is created.\n";
		}
		~Person(){
			cout << "A person " << name << " whose age is " << age << " is erased.\n";
		}
		void show(){
			cout << "Name is " << name << " and age is " << age << ".\n";
		}
};
class Student : public Person{
	private:
		int grade;
	public:
		Student(string n, int a, int g) : Person(n, a), grade(g){
			cout << "A student whose grade is " << grade << " is created.\n";
		}
		~Student(){
			cout << "A student whose grade is " << grade << " is erased.\n";
		}
		void show(){
			cout << "Grade is " << grade << ".\n";
		}
};
int main()
{
    string n;
    int a, g;
    cin>>n>>a>>g;
    Student student(n, a, g);
    student.Person::show();
    student.show();
    return 0;
}

**题目六:**我们来做个Student类吧!

Description

请定义一个Student类,有4个属性:

1.char *name:姓名。

2.int numOfScores:课程数量

3.int *scores:所有课程的成绩。

4.int id:学生的编号。

只有3个方法:

  1. 构造函数

2.析构函数

3.void Student::showStudent()方法:用于输出学生的信息。

请根据样例输出,写出该类的实现。

Input

输入分为多行。

第一行包含3个正整数M,N和P:其中M表明之后输入的测试用例数量;N表示每个人姓名的最大长度;P表示学生学习的课程的数量。

之后有M行,包含一个学生姓名(没有任何空白符)、P门课程的成绩。

Output

见样例。

注意:所有的输出两两之间用一个空格隔开,且每行输出的首尾都没有空格。

Sample Input

3 10 5
Tom 60 61 72 56 89
Jack 99 100 98 89 77
Mary 88 88 88 88 88

Sample Output

A student whose name is “Tom” and id is 1 is created!
This student is “Tom” whose id is 1.
This student’s scores are: 60 61 72 56 89
A student whose name is “Tom” and id is 1 is erased!
A student whose name is “Jack” and id is 2 is created!
This student is “Jack” whose id is 2.
This student’s scores are: 99 100 98 89 77
A student whose name is “Jack” and id is 2 is erased!
A student whose name is “Mary” and id is 3 is created!
This student is “Mary” whose id is 3.
This student’s scores are: 88 88 88 88 88
A student whose name is “Mary” and id is 3 is erased!

题目给定代码

int main()
{
    int cases;
    char *str;
    int maxLenOfString, numOfCourses;
    int *scores;
 
    cin>>cases>>maxLenOfString>>numOfCourses;
    str = new char[maxLenOfString + 1];
    scores = new int[numOfCourses];
    for (int i = 0; i < cases; i++)
    {
        cin>>str;
        for (int j = 0; j < numOfCourses; j++)
            cin>>scores[j];
        Student stu(str,scores,numOfCourses);
        stu.showStudent();
    }
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

class Student{
	private:
		char *name;
		int numOfScores;
		int *scores;
		static int id;
	public:
		Student(char *n, int *c, int i) : name(n), scores(c), numOfScores(i){
			++ id;
			cout << "A student whose name is \"" << name << "\" and id is " << id << " is created!\n";
		}
		~Student(){
			cout << "A student whose name is \"" << name << "\" and id is " << id << " is erased!\n";
		}
		void showStudent(){
			cout << "This student is \"" << name << "\" whose id is " << id << ".\n";
			cout << "This student's scores are:";
			for(int i = 0; i < numOfScores; ++ i){
				cout << " " << scores[i];
			}
			cout << "\n";
		}
};
int Student :: id = 0;
int main()
{
    int cases;
    char *str;
    int maxLenOfString, numOfCourses;
    int *scores;

    cin>>cases>>maxLenOfString>>numOfCourses;
    str = new char[maxLenOfString + 1];
    scores = new int[numOfCourses];
    for (int i = 0; i < cases; i++)
    {
        cin>>str;
        for (int j = 0; j < numOfCourses; j++)
            cin>>scores[j];
        Student stu(str,scores,numOfCourses);
        stu.showStudent();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1351937368/article/details/105981372