山东科技大学2020年6月15日实验题解

山东科技大学2020年6月15日实验题解

题目一: 时间之差

Description

定义一个类Time,包含小时、分钟、秒三个属性。定义其构造函数Time(int, int, int)分别初始化其小时、分钟、秒。重载减法运算符,用于求两个时间之间相差的秒数(非负整数)。

Input

输入有2行。每行表示1个时间,包括小时、分钟、秒三个值。输入都是合法的24小时制的时间。

Output

见样例。

Sample Input

12 10 10
10 20 20

Sample Output

Deference is 6590 seconds.

题目给定代码

int main()
{
    int a, b, c;
    cin>>a>>b>>c;
    Time t1(a, b, c);
    cin>>a>>b>>c;
    Time t2(a, b, c);
    cout<<"Deference is "<<(t2 - t1)<<" 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<typeinfo>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;

inline int my_abs(int number) {
    return number < 0 ? -number : number;
}

class Time {
private:
    int hour, minute, second;
    int total;
public:
    Time(int h, int m, int s) {
        hour = h, minute = m, second = s;
        total = hour * 60 * 60 + minute * 60 + second;
    }
    int operator - (Time& T) {
        return my_abs(total - T.total);
    }
};

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    Time t1(a, b, c);
    cin >> a >> b >> c;
    Time t2(a, b, c);
    cout << "Deference is " << (t2 - t1) << " seconds." << endl;
    return 0;
}

题目二: 今年多少岁

Description

定义类Date,用于表示日期,拥有:

  1. 3个int类型属性,分别表示年、月、日。
  2. 构造函数、析构函数。要输出一些样例所示的信息。
  3. void show方法,用于输出日期值。格式见样例。
    定义类Person,包括
  4. 一个Date类的对象用于表示生日,一个字符串表示其姓名(假定不含有空白符)。
  5. 构造函数和析构函数。要输出一些样例所示的信息。
  6. int getAge(Date& now)方法,求在当前日期now时的周岁年龄。日期now一定大于birthday。
  7. void show()方法,用于显示Person的信息,格式见样例。
  8. getName()方法,用于返回其名字。

Input

输入3行。第1行包括1个合法的日期的年、月、日三个数据。
第2行是一个不含空白符的字符串,是一个人的名字。
第3行是当前日期,包括年、月、日三个数据。

Output

见样例。

Sample Input

1999 2 2
Tom
2014 5 8

Sample Output

Date 1999-2-2 is created.
Person Tom is created.
Tom’s birthday is 1999-2-2.
Date 2014-5-8 is created.
Now, Tom is 15.
Date 2014-5-8 is erased.
Person Tom is erased.
Date 1999-2-2 is erased.

题目给定代码

int main()
{
    int y, m, d;
    string name;
    cin >> y >> m >> d >> name;
    Person person(y, m, d, name);
    person.show();
    cin >> y >> m >> d;
    Date now(y, m, d);
    cout << "Now, " << person.getName() << " is " << person.getAge(now) << "." << endl;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<typeinfo>
#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 show() {
        cout << year << "-" << mounth << "-" << day;
    }
    int get_year() {
        return year;
    }
    int get_mounth() {
        return mounth;
    }
    int get_day() {
        return day;
    }
};
class Person {
private:
    Date date;
    string name;
public:
    Person(int y, int m, int d, string s) : date(y, m, d) {
        name = s;
        cout << "Person " << name << " is created.\n";
    }
    ~Person() {
        cout << "Person " << name << " is erased.\n";
    }
    void show() {
        cout << name << "'s birthday is ";
        date.show();
        cout << ".\n";
    }
    string getName() {
        return name;
    }
    int getAge(Date &now) {
        return now.get_year() - date.get_year();
    }
};

int main()
{
    int y, m, d;
    string name;
    cin >> y >> m >> d >> name;
    Person person(y, m, d, name);
    person.show();
    cin >> y >> m >> d;
    Date now(y, m, d);
    cout << "Now, " << person.getName() << " is " << person.getAge(now) << "." << endl;
    return 0;
}

题目三: 不一样的奇偶性

Description

定义类Integer,包括:
1.一个int类型的属性data。
2. 构造函数。
3. bool judge()方法:求data的各位数字之和,如果这个和是偶数,则返回假,否则返回真。

Input

输入若干个正整数,每个占一行。

Output

每行输入对应一行输出,是对每行输入对应的对象调用judge()方法后输出的结果。

Sample Input

123
456
1
33

Sample Output

NO
YES
YES
NO

题目给定代码

int main()
{
    int i;
    while (cin >> i)
    {
        Integer INT(i);
        if (INT.judge())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

标程

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

class Integer {
private:
    int number;
public:
    Integer(int n) {
        number = n;
    }
    bool judge() {
        int count = 0;
        while (number) {
            count += number % 10, number /= 10;
        }
        if (count & 1) {
            return true;
        }
        else {
            return false;
        }
    }
};

int main()
{
    int i;
    while (cin >> i)
    {
        Integer INT(i);
        if (INT.judge())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

题目四: 大学的组织架构

Description

一个大学是由若干个学院、系组成的,每个学院、系有自己的名称和领导。定义Orgnization类,具有2个string属性,分别是一个组织的名称和其领导的名字;具有一个show方法,用于显示该组织的信息。

该类有2个子类:College、Department。其中College的show方法显示格式为:

Dean of $ is &

Department的show方法显示格式为:

Director of $ is &

上述格式中,$表示College或Department的名字,&是相应的领导的名字。

Input

输入多行。

第1行N表示一个大学的下属机构的个数。

之后有N组输入。每组输入有3行,第1行是0或1,0表示这是一个College,1表示这是一个Department。

第2行是College或Department的名字。

第3行是相应的领导名字。

Output

见样例。

Sample Input

4
0
College of Information
Tom Zhang
1
Department of Software
Jack Li
0
College of Math
Mary Wang
1
Department of Computer
Fu Qi

Sample Output

Dean of College of Information is Tom Zhang
Director of Department of Software is Jack Li
Dean of College of Math is Mary Wang
Director of Department of Computer is Fu Qi

题目给定代码

int main()
{
    vector<Orgnization*> university;
    vector<Orgnization*>::iterator itr;
    int n, i, t;
    string str1, str2;
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> t;
        cin.ignore();
        getline(cin, str1);
        getline(cin, str2);
        if (t == 0)
            university.push_back(new College(str1, str2));
        else
            university.push_back(new Department(str1, str2));
    }
    for (itr = university.begin(); itr != university.end(); itr++)
        (*itr)->show();
    return 0;
}

标程

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

class Orgnization {
protected:
    string name_1, name_2;
public:
    virtual void show() = 0;
};
class College : public Orgnization{
public:
    College(string n_1, string n_2) {
        name_1 = n_1, name_2 = n_2;
    }
    void show() {
        cout << "Dean of " << name_1 << " is " << name_2 << "\n";
    }
};
class Department : public Orgnization {
public:
    Department(string n_1, string n_2) {
        name_1 = n_1, name_2 = n_2;
    }
    void show() {
        cout << "Director of " << name_1 << " is " << name_2 << "\n";
    }
};

int main()
{
    vector<Orgnization*> university;
    vector<Orgnization*>::iterator itr;
    int n, i, t;
    string str1, str2;
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> t;
        cin.ignore();
        getline(cin, str1);
        getline(cin, str2);
        if (t == 0)
            university.push_back(new College(str1, str2));
        else
            university.push_back(new Department(str1, str2));
    }
    for (itr = university.begin(); itr != university.end(); itr++)
        (*itr)->show();
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值