面向对象的程序设计第四次作业

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <bits/stdc++.h>
 
using namespace std;
 
class Date
{
public:
    Date(int year = 1990, int month = 1, int day = 1) {
        setDate(year, month, day);
    }
 
 
    void setDate(int _year, int _month, int _day) {
        year = _year;
        month = _month;
        day = _day;
    }
 
    void setYear(int _year) {year = _year;}
 
    int getYear() {return year;}
 
    void setMonth(int _month) {month = _month;}
 
    int getMonth() {return month;}
 
    void setDay(int _day) {day = _day;}
 
    int getDay() {return day;}
 
    void setSeparator(char _separator) {separator = _separator;}
 
    void printFullYear() {
        if (month < 10)
            cout << year << separator << 0 << month << separator;
        else
            cout << year << separator << month << separator;
        if (day < 10)
            cout << 0 << day;
        else
            cout << day;
    }
 
    void printStandardYear() {
        int _year = year % 100;
        if (month < 10)
            cout << _year << separator << 0 << month << separator;
        else
            cout << _year << separator << month << separator;
        if (day < 10)
            cout << 0 << day;
        else
            cout << day;
    }
 
 
    int fullYearsTo(Date &_date) {
        int sum = year - _date.year;
        if (month < _date.month) {
            sum--;
        }
        else if (month == _date.month && day < _date.day)
            sum--;   
        return sum;
    }
 
    int daysTo(Date &_date) {
        int sum = 0;
        for (int i = _date.year + 1; i < year; ++i) {
            if (isLeapyear(i))
                sum++;
            sum += 365;
        }
 
        sum += _date.getLeftDaysYear();
        sum += getDayOfYear();
        return sum;
    }
 
    int getDayOfYear() {
        int sum = 0;
        for (int i = 1; i < month; ++i) {
            sum += DAYS_PER_MONTH[i - 1];
        }
        if (isLeapyear(year) && month > 2)
            sum++;
        sum += day;
        return sum;
    }
 
    int getLeftDaysYear() {
        if (isLeapyear(year))
            return 366 - getDayOfYear();
        else
            return 365 - getDayOfYear();
    }
 
private:
    int year;
    int month;
    int day;
    char separator = '-';
    static const int DAYS_PER_MONTH[12];
    intcheckDay(int day);
    bool isLeapyear(int _year) {
        if ( (_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
            return 1;
        else
            return 0;
    }  
};
 
const int Date::DAYS_PER_MONTH[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
 
class Employee{
public:
    Employee(string str1, string str2, Date &date1, Date &date2) {
        firstName = str1;
        lastName = str2;
        birthDate = date1;
        hireDate = date2;
    }
 
    void print() {
        cout << lastName << ", "  << firstName <<" Hired: ";
        hireDate.printFullYear();
        cout << " Birthday: ";
        birthDate.printFullYear();
    }
 
    int getAge(Date &_date) {
        return _date.fullYearsTo(birthDate);
    }
 
    int getYearsWorked(Date &_date) {
        return _date.fullYearsTo(hireDate);
    }
    int getDaysWorked(Date &_date) {
        return _date.daysTo(hireDate);
    }
    //~Employee();
 
private:
    string firstName;
    string lastName;
    Date birthDate;
    Date hireDate;
};
 
 
 
 
int main () {
    Date birth(1969, 8, 11);
    Date hire(1998, 4, 1);
    Date today(2010, 4, 30);
    Employee manager("Bob", "Blue", birth, hire);
    manager.print();
    cout << endl;
    cout << manager.getAge(today) << endl;
    cout << manager.getDaysWorked(today) << endl;
 
    return 0;
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <biTs/stdc++.h>
 
using namespace std;
 
class CD
{
public:
    CD(string name, string songs1[]) {
        singer = name;
        for( int i = 0; i < 6; ++i) {
            songs[i] = songs1[i];
        }
    }
    string getSinger() {
        return singer;
    }
    string getSong(int index) {
        return songs[index];
    }
    void listSongs() {
        cout << "Singer:" << getSinger() << endl;
        for (int i = 0; i < 6; ++i) {
            cout << "  " << i + 1 << "." << getSong(i) << endl;
        }
    }
private:
    string singer;
    string songs[6];
};
 
 
class CDPlayer
{
public:
    CDPlayer() {
        cd =   NULL;
    }
    void showMenu() {
        for ( int i = 0; i < 34; ++i)
            cout << "*";
        cout << endl;
        cout << "*  1.播放CD                      *" << endl;
        cout << "*  2.插入CD                      *" << endl;
        cout << "*  3.弹出CD                      *" << endl;
        cout << "*  0.关机                        *" << endl;  
        for ( int i = 0; i < 34; ++i)
            cout << "*";
        cout << endl;
    }
 
    void insertCD(CD*cd1) {
        cd = cd1;
        cout << "插入了" << cd -> getSinger() << "的CD......" << endl;
 
    }
 
    CD *ejectCD() {
        if(cd != NULL) {
            cout << "弹出了" << cd -> getSinger() << "的CD......" << endl;
            cd = NULL;
        }
        return cd;
    }
 
    void play() {
        if (cd == NULL) {
            cout << "Please insert CD first" << endl;
        }
        else {
            cout << "正在播放" << cd -> getSinger() << "的CD......" << endl;
            cd -> listSongs();
        }
    }
private:
    CD *cd;
    bool cdIn;
     
};
 
int main () {
    string name;
    string songs[6];
    cout << "制造CD......" << endl;
    cout << "   Singer's Name:";
    cin >> name;
 
    for (int i = 0; i < 6; ++i) {
        cout << "   song" << (i + 1) << "#: ";
        cin >> songs[i];
    }
    CD cd(name, songs);
    cd.listSongs();
 
    CDPlayer player;
    player.showMenu();
 
    player.play();
 
 
    player.insertCD(&cd);
 
    player.play();
 
    player.ejectCD();
 
    // CD cd2(name2, songs2);
    // player.insertCD(&cd2);
    // player.play();
}<br>/*<br>周杰伦<br>青花瓷<br>菊花台<br>双节棍<br>东风破<br>珊瑚海<br>稻香<br>*/

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
 
 
 
 
class Commodity
{
public:
    Commodity(string name1 = "1", double price1 = 0, double num1 = 0) {//一定要初始化!!!!!!!!!!!!可以缺省形参值的
        name = name1;
        price = price1;
        num = num1;
    }
 
    void printInfo() {
        cout << "name" << "," << price << "," << num << endl;
    }
    string getName() {
        return name;
    }
 
    double getPrice() {
        return price;
    }
 
    double getNum() {
        return num;
    }
    // ~Commodity();
private:
    string name;
    double price;
    double num;   
};
 
 
class Cart
{
public:
 
    Cart() {
        i  = 0;
    }
 
    void addItem(Commodity goods) {
        iterms[i++] = goods;
    }
    void checkout() {
        double ans = 0;
        for (int j = 0; j < i; j++) {
            ans += iterms[j].getPrice() * iterms[j].getNum();
        }
        cout << "您需要支付" << ans << "元。" << endl;
    }
    void printInvoice() {
        for (int j = 0; j < i; j++) {
            cout << iterms[j].getName() << "," << iterms[j].getPrice() << "," << iterms[j].getNum() << endl;
        }
        i = 0;
    }
 
private:
    Commodity iterms[20];
    int i;
};
 
 
 
 
int main () {
    Commodity tShirt("Tshirt", 79, 2);
    Commodity suit("suit", 1099, 1);
    Commodity hat("hat", 129, 3);
    Commodity tv("tv set", 4899, 1);
    Commodity ac("air condition", 5280, 1);
 
    Cart myCart;
    myCart.addItem(tShirt);
    myCart.addItem(suit);
    myCart.addItem(hat);
    myCart.addItem(tv);
    myCart.addItem(ac);
 
    myCart.checkout();
    myCart.printInvoice();
 
    return 0;
}

 

posted @   LightAc  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 智能Agent如何改造传统工作流:从搜索到全能助手
返回顶端
点击右上角即可分享
微信分享提示