山东科技大学2020年5月11日实验题解

山东科技大学2020年5月11日实验题解

**题目一:**给我一台计算机吧!

Description

CPU主频和主存容量是衡量一台计算机性能主要指标。请定义:

  1. CPU类:只有一个int类型的数据成员以表示其主频,并请根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

  2. Memory类:只有一个int类型的数据成员以表示其容量,并请根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

  3. Computer类:

(1)三个数据成员分别为CPU的对象、Memory的对象和一个字符串(表示该计算机属于谁的)。

(2)根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

(3)void show()方法,用于按照样例输出格式输出该计算机的信息。

Input

输入有2行。每行包括2个整数和1个字符串,分别表示CPU的主频、内存容量和计算机主人姓名。

Output

见样例。

Sample Input

2 1000 Zhang
4 2000 Li

Sample Output

This is Zhang’ computer with CPU = 2GHz, memory = 1000MB.
This is Li’ computer with CPU = 4GHz, memory = 2000MB.

题目给定代码

int main()
{
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.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 CPU{
	public:
		int my_CPU;
	public:
		CPU(int number){
			my_CPU = number;
		}
};
class Memory{
	public:
		int my_Memory;
	public:
		Memory(int number){
			my_Memory = number;
		}
};
class Computer{
	private:
		Memory my_computer_Memory;
		CPU my_computer_CPU;
		string name;
	public:
		Computer(CPU c, Memory m, string s) : my_computer_CPU(c), my_computer_Memory(m), name(s){
			
		}
		void show(){
			cout << "This is " << name << "\' computer with CPU = " << my_computer_CPU.my_CPU << "GHz, memory = " << my_computer_Memory.my_Memory << "MB.\n";
		}
};
int main()
{
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}

**题目二:**闹钟

Description

定义Time类,包括

  1. hour、minute、second三个int类型的属性,分别是时间的时、分、秒。
  2. 构造函数、析构函数,并在其中输出相应的信息,格式见样例。
  3. 其他必要的函数,使程序能正确运行。
    定义Alarm类,包括:
  4. 1个Time类的对象和1个字符串属性,分别是闹钟的时间和闹钟的名字。
  5. 构造函数、析构函数,并在其中输出相应的信息,格式见样例。
  6. int remainSeconds(Time& now)方法,用于计算当前时间now距离闹钟事件还有多少秒。假定now一定小于闹钟设定时间。
  7. getThing()方法,用于返回闹钟的名字。

Input

第1行是一个不含空白符的字符串,是闹钟的名字。
第2行和第3行是2个时间,分别是闹钟的时间,和当前时间。

Output

见样例。

Sample Input

GetUp

10 10 10
9 9 9

Sample Output

Time 10:10:10 is created.
Alarm GetUp is created.
Time 9:9:9 is created.
Alarm GetUp will start after 3661 seconds.
Time 9:9:9 is erased.
Alarm GetUp is erased.
Time 10:10:10 is erased.

题目给定代码

int main()
{
    int h, m, s;
    string th;
    cin>>th;
    cin>>h>>m>>s;
    Alarm alarm(h,m,s,th);
    cin>>h>>m>>s;
    Time now(h,m,s);
    cout<<"Alarm "<<alarm.getThing()<<" will start after "<<alarm.remainSeconds(now)<<" seconds."<<endl;
    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 Time{
	private:
		int hour, minute, second;
	public:
		Time(int a, int b, int c) : hour(a), minute(b), second(c){
			cout << "Time " << hour << ":" << minute << ":" << second << " is created.\n";
		}
		~Time(){
			cout << "Time " << hour << ":" << minute << ":" << second << " is erased.\n";
		}
		int time_now(){
			return second + 60 * minute + 3600 * hour;
		}
};
class Alarm{
	private:
		Time alarm_time;
		string alarm_name;
	public:
		Alarm(int a, int b, int c, string s) : alarm_time(a, b, c){
			alarm_name = s;
			cout << "Alarm " << alarm_name << " is created.\n";
		}
		~Alarm(){
			cout << "Alarm " << alarm_name << " is erased.\n";
		}
		string getThing(){
			return alarm_name;
		}
		int remainSeconds(Time &now){
			return alarm_time.time_now() - now.time_now();
		}
};
int main()
{
    int h, m, s;
    string th;
    cin>>th;
    cin>>h>>m>>s;
    Alarm alarm(h,m,s,th);
    cin>>h>>m>>s;
    Time now(h,m,s);
    cout<<"Alarm "<<alarm.getThing()<<" will start after "<<alarm.remainSeconds(now)<<" seconds."<<endl;
    return 0;
}

**题目三:**一个人的生日

Description

定义Date类,表示由年、月、日构成的日期,定义其构造函数和析构函数,产生如样例所示的输出。
定义Person类,包括一个Date类的对象成员表示其生日,一个string的数据成员表示其名字。定义其构造函数和析构函数,产生如样例所示的输出。

Input

第一行是一个合法的日期;第二行是一个无空白符的字符串和一个合法的日期。

Output

见样例。

Sample Input

1997 10 1
tom 1997 11 12

Sample Output

Date 1997-10-1 is created.
Date 1997-11-12 is created.
Person tom was born at 1997-11-12 is created.
Person tom was born at 1997-11-12 is erased.
Date 1997-11-12 is erased.
Date 1997-10-1 is erased.

题目给定代码

int main()
{
    string name;
    int year, month, day;
    cin>>year>>month>>day;
    Date date(year, month, day);
    cin>>name>>year>>month>>day;
    Person person(year, month, day, name);
    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{
	private:
		int year, mounth, day;
	public:
		Date(int y, int m, int d){
			year = y, mounth = m, day = d;
			cout << "Date " << year << "-" << mounth << "-" << day << " is created.\n";
		}
		~Date(){
			cout << "Date " << year << "-" << mounth << "-" << day << " is erased.\n";
		}
		void get_time(){
			cout << year << "-" << mounth << "-" << day;
		}
};
class Person{
	private:
		Date my_date;
		string name;
	public:
		Person(int a, int b, int c, string s) : my_date(a, b, c), name(s){
			cout << "Person " << name << " was born at ";
			my_date.get_time();
			cout << " is created.\n";
		}
		~Person(){
			cout << "Person " << name << " was born at ";
			my_date.get_time();
			cout << " is erased.\n";
		}
};
int main()
{
    string name;
    int year, month, day;
    cin>>year>>month>>day;
    Date date(year, month, day);
    cin>>name>>year>>month>>day;
    Person person(year, month, day, name);
    return 0;
}

**题目四:**类型可变的类

Description

定义一个Data类,至少包含1个int类型的数据成员、1个double类型的数据成员,定义其构造函数和析构函数,使得程序执行时,能产生样例所示的结果。

Input

输入2行,第1行是1个int类型的数据,第2行是1个double类型的数据。

Output

见样例。

Sample Input

120
3.14

Sample Output

A default object is created.
An integer object 120 is created.
A double object 3.14 is created.
The double object 3.14 is erased.
The integer object 120 is erased.
The default object is erased.

题目给定代码

int main()
{
    Data d1;
    int i;
    cin>>i;
    Data d2(i);
    double d;
    cin>>d;
    Data d3(d);
    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 Data{
	private:
		int a;
		double b;
		int flag;
	public:
		Data(){
			cout << "A default object is created.\n";
			flag = 0;
		}
		~Data(){
			if(flag == 0){
				cout << "The default object is erased.\n";
			}
			else if(flag == 1){
				cout << "The integer object " << a << " is erased.\n";
			}
			else{
				cout << "The double object " << b << " is erased.\n";
			}
		}
		Data(int i){
			flag = 1;
			a = i;
			cout << "An integer object " << a << " is created.\n";
		}
		Data(double i){
			flag = 2;
			b = i;
			cout << "A double object " << b << " is created.\n";
		}
};
int main()
{
    Data d1;
    int i;
    cin>>i;
    Data d2(i);
    double d;
    cin>>d;
    Data d3(d);
    return 0;
}

猜你喜欢

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