icarnegie网站测试题(exercise 1)答案

date.cpp如下:

**********************************************************************

#include <iostream>
#include "date.h"

date::date () : day(0), month(0), year(0) {}

date::date (int day, int month, int year) :
        day(day), month(month), year(year) {}

int date::compareTo (date another_date) {

    if (year != another_date.year)
        return year - another_date.year;

    if (month != another_date.month)
        return month - another_date.month;

    if (day != another_date.day)
        return day - another_date.day;

    return 0;
}

ostream &operator<< (ostream &stream, date d) {

    stream << d.month << '/' << d.day << '/' << d.year;
    return stream;
}
***********************************************************************

homework.cpp如下:

***********************************************************************

#include <iostream>
#include <string>
#include "homework.h"

homework::homework () : name(""), assigned_date(), due_date() {}

homework::homework (string name, date assigned_date, date due_date) :
        name(name), assigned_date(assigned_date), due_date(due_date) {}

int homework::compareTo (homework another_homework) {
    return (this->due_date.compareTo(another_homework.due_date));
}

ostream &operator<< (ostream &stream, homework h) {
    stream << h.name << ": " << h.assigned_date << '-' << h.due_date;
    return stream;
}
***********************************************************************

homeworklist.cpp如下:

***********************************************************************

#include "homeworklist.h"

homeworklist::homeworklist() : current_size(0) {}

bool homeworklist::add (homework h) {
    if (current_size == LIST_MAX)
        return false;

    list[current_size++] = h;
    return true;
}

homeworklist homeworklist::dueafter (date d) {

    homeworklist new_list;
    homework reference_date ("reference assignment", d, d);

    for (int index = 0; index < current_size; index++) {
        if (list[index].compareTo (reference_date) > 0) {
            new_list.add (list[index]);
        }
    }

    return new_list;
}

homeworklist homeworklist::duebefore (date d) {

    homeworklist new_list;
    homework reference_date ("reference assignment", d, d);

    for (int index = 0; index < current_size; index++) {
        if (list[index].compareTo (reference_date) < 0) {
            new_list.add (list[index]);
        }
    }

    return new_list;
}

homeworklist homeworklist::dueon (date d) {

    homeworklist new_list;

    homework reference_date ("reference assignment", d, d);

    for (int index = 0; index < current_size; index++) {
        if (list[index].compareTo (reference_date) == 0) {
            new_list.add (list[index]);
        }
    }

    return new_list;
}

ostream &operator<< (ostream &stream, homeworklist hl) {
    for (int index = 0; index < hl.current_size; index++) {
        stream << hl.list[index] << endl;
    }

    return stream;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值