0
点赞
收藏
分享

微信扫一扫

【C++OJ拷贝构造】软件备份(拷贝构造函数)

Sky飞羽 2022-05-04 阅读 109
C++

【C++OJ拷贝构造】软件备份(拷贝构造函数)

题目描述

输入

输出

输入样例

输出样例

参考代码

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

class CDate
{
private:
    int year, month, day;

public:
    CDate() {} //无参构造函数
    CDate(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }
    void set(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }
    bool isLeapYear() { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; }
    int getYear() { return year; }
    int getMonth() { return month; }
    int getDay() { return day; }
    int getDayofYear() //计算日期从当年1月1日算起的天数
    {

        int i, sum = day;
        int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int b[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if (isLeapYear())
            for (i = 0; i < month; i++)
                sum += b[i];
        else
            for (i = 0; i < month; i++)
                sum += a[i];

        return sum;
    }
};

class soft
{
private:
    char *name;
    char type;  //类型(分别用O、T和B表示原版、试用版还是备份)
    CDate d1;   //效截至日期(用CDate类子对象表示)
    char store; //存储介质(分别用D、H和U表示光盘、磁盘和U盘)
public:
    soft(char *n, char t, char s, int y, int m, int d) //构造函数
    {
        name = new char[strlen(n) + 1];
        strcpy(name, n);
        type = t;
        store = s;
        d1.set(y, m, d);
    }
    soft(soft &s1) //拷贝构造函数
    {
        name = new char[strlen(s1.name) + 1]; //要new一个新空间存放指针,完成深拷贝
        strcpy(name, s1.name);
        type = 'B';  //软件类型改成“B”
        store = 'H'; //存储介质改为"H"
        d1 = s1.d1;  //其它不变
    }
    void print() //打印
    {
        cout << "name:" << name << endl;
        switch (type)
        {
        case 'O':
            cout << "type:original" << endl;
            break;
        case 'T':
            cout << "type:trial" << endl;
            break;
        case 'B':
            cout << "type:backup" << endl;
            break;
        }
        switch (store)
        {
        case 'D':
            cout << "media:optical disk" << endl;
            break;
        case 'H':
            cout << "media:hard disk" << endl;
            break;
        case 'U':
            cout << "media:USB disk" << endl;
            break;
        }
        if (d1.getDay() == 0 && d1.getMonth() == 0 && d1.getYear() == 0)
            cout << "this software has unlimited use" << endl;
        else if ((d1.getYear() >= 2015 && d1.getMonth() >= 4 && d1.getDay() >= 7) || (d1.getYear() >= 2015 && d1.getMonth() > 4))
        {
            cout << "this software is going to be expired in " << d1.getDayofYear() - 97 << " days" << endl; //一年的天数减去到4月7日的天数
        }
        else
        {
            cout << "this software has expired" << endl;
        }
    }

    ~soft() //析构函数,释放new的name空间
    {
        delete[] name;
    }
};

int main()
{
    int t;
    cin >> t;
    int y, d, m;
    char name[100], type, store;

    while (t--)
    {
        cin >> name >> type >> store >> y >> m >> d;
        soft s2(name, type, store, y, m, d);
        s2.print();
        cout << endl;
        soft s3(s2);
        s3.print();
        cout << endl;
    }
}
举报

相关推荐

0 条评论