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

#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;
}

 

#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();
}
/*
周杰伦
青花瓷
菊花台
双节棍
东风破
珊瑚海
稻香
*/

 

#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 @ 2019-04-03 14:10  LightAc  阅读(338)  评论(0编辑  收藏  举报
返回顶端