当先锋百科网

首页 1 2 3 4 5 6 7

Homework List

Prerequisites, Goals, and Outcomes

Prerequisites: Students should have mastered the following prerequisite skills.

·         Control Structures - For-loops and if-else statements

·         Arrays - Access and modification of array elements

·         Console I/O - Definition of operator << for class output

·         Class Specification - Definition of class member functions given a full class declaration

·         Preprocessor - Inclusion of library and header files

Goals: This assignment is designed to help bridge the gap between Java and C++. It emphasizes the use of C++ header files, basic C++ syntax, and stream-based I/O.

Outcomes: Students successfully completing this assignment would master the following outcomes.

·         Understand basic C++ syntax

·         Use basic C++ control structures

·         Use arrays for sequential access

·         Create classes that properly separate definition and declaration into header and implementation files

·         Use the preprocessor to perform file inclusion

Background

Homework assignments are part of any typical college or university course. This assessment asks you to construct a simple software tool for maintaining a list of homework assignments.

Description

The program produced in this exercise maintains a list of homework assignments. Using this program, a user can add homework assignments to the program maintained list. The user can then view all assignments or assignments due on, before, or after a specific date. The program interacts with the user through a menu that displays these options. The following screen shot depicts the program during execution.

 

A typical use of the application is as follows. The user enters the menu item corresponding to the task they wish to perform. Depending on the task, the user may have to enter additional input. For example, to add a homework assignment, the user first enters the menu item number corresponding to the "add" operation. This is the number 1. Once the user presses the Enter key, the program prompts the user for the name of the assessment. Once this input is received, the program prompts the user for the date assigned and then the date due. This completes the addition of the homework. Each time an operation completes, the program displays another instance of the menu. This loop continues until the user chooses to quit.

This program includes separate classes for modeling a date, an individual assignment, and a collection of assignments.

Class date

Class date models a calendar date. A date, in this context, includes the day, month, and year. Class date contains the following members.

·  date ();

Default constructor that initializes day, month, and year all to zero

·  date (int day, int month, int year);

Three parameter constructor that initializes the day, month, and year to more meaningful values

·  int compareTo (date another_date);

Function that compares two dates

Class date also dictates how its object instances appear when they are sent to an output stream. The insertion operator outputs a date using the "mm/dd/yyyy" format. This method declaration appears below.

friend ostream& operator<< (ostream <stream, date d);

Class homework

Class homework models a homework assignment. For each homework assignment, this program maintains the assignment name, the date assigned, and the date due. Class homework contains the following members.

·  homework ();

Default constructor that initializes name to the empty string and the assigned and due dates to their default values

·  homework (string name, date assigned_date, date due_date);

Three parameter constructor that initializes the assignment name, assigned date, and due date

·  int compareTo (homework another_homework);

Function that compares two homework objects

Similar to class date, class homework also dictates how its object instances are displayed when they are sent to an output stream. The insertion operator outputs a homework object using the following format.

name: assigned date - due date

This method declaration appears below.

friend ostream &operator<< (ostream &stream, homework h);

Class homeworklist

Class homeworklist models a list of homework assignments. Class homeworklist maintains this list of assignments internally using an array. This class limits the number of assignments that it can store to 50.

Class homeworklist contains the following members.

·  homeworklist ();

Default constructor that initializes the size of the list to zero

·  bool add (homework h);

This method adds an assignment. It returns true if the add operation succeeds and false if the maximum number of assignments is already stored.

·  homeworklist dueafter (date d);

Returns a new homeworklist containing only those homeworks due after, but not on, the specified date

·  homeworklist duebefore (date d);

Returns a new homeworklist containing only those homeworks due before, but not on, the specified date

·  homeworklist dueon (date d);

Returns a new homeworklist containing only those homeworks due exactly on the specified date, but not before or after

Class homeworklist also defines how instances of this class appear when they are sent to an output stream. The following method outputs each homework object stored in the private array on a separate line.

friend ostream &operator<< (ostream &stream, homeworklist hl);

Files

Following is a list of files needed to complete this assessment.

  • handout-files.zip contains all of the following necessary files:
    • solution.exe - This is a sample solution.
    • date.h - This file contains the class date declaration. Do not modify this file.
    • homework.h - This file contains the class homework declaration. Do not modify this file.
    • homeworklist.h - This file contains the class homeworklist declaration. Do not modify this file.
    • main.cpp - This file contains the interactive menu implementation. Function main exists in this file. Do not modify this file.

Tasks

To complete this assessment, you will finish the definitions of the three classes date, homework, and homeworklist. Your implementation of the methods for each class should be placed in implementation files named date.cpp, homework.cpp, and homeworklist.cpp, respectively.

To begin, verify the files needed for this assessment.

  1. Extract the archive to retrieve the files needed to complete this assessment.
  2. Run the sample executable by issuing the following command at the command prompt.

$ ./solution.exe

  1. Interact with the sample executable to become familiar with the expected program behavior.

Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often.

  1. First, complete the constructors for class date. The default constructor should initialize all private data members to zero. The other constructor should initialize the private data members to the corresponding parameter of the constructor. Remember to use initializer lists.
  2. Next, complete the class date member function compareTo. This function compares two date objects. It returns zero if the objects are equal, a negative value if the invoking object is less than the date given in the parameter, and a positive value if the invoking object is greater than the date given in the parameter.
  3. Then, complete the definition of the insertion operator. This function should output a date object in the form "mm/dd/yyyy". This completes the implementation of class date.
  4. For class homework, begin by implementing the constructors. These implementations resemble what you did in class date.
  5. Next, implement the member function compareTo. To compare two homework objects, you need only compare their due dates.
  6. Then, complete the definition of the insertion operator for objects of type homework. This function should output a homework object in the form "<name>: <assigned date> - <due date>". This completes the implementation of class homework.
  7. Begin the implementation of class homeworklist by completing the default constructor. This method only needs to initialize the private data member current_size to zero.
  8. Next, complete the implementation of the member function add. Do not allow the method to store more than the maximum number of homeworks.
  9. Then, complete methods duebefore, dueon, and dueafter. Each of these methods returns a homeworklist object containing a subset of homeworks. These subsets contain all homeworks due before, on, or after a specified date. The single parameter for each of these functions specifies the date.
  10. Finally, finish the implementation of the insertion operator defined for class homeworklist. This method should iterate through the array of homework objects, outputting each on a separate line.

Submission

Submit only the following.

  1. date.cpp
  2. homework.cpp
  3. homeworklist.cpp

附:handput_files.zip文件中的文件:

date.h:

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

#ifndef _DATE_H
#define _DATE_H

#include <iostream>

using namespace std;

class date;

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

class date {

private:
    int day;
    int month;
    int year;

public:
    // The default constructor
    date ();

    // Initializes the date with meaningful data
    date (int day, int month, int year);

    // This follows the Java semantics -- we'll look at overloading operators
    // a little bit later.
    int compareTo (date another_date);

    // This is the standard insertion operator. It should produce "mm/dd/yyyy"
    friend ostream &operator<< (ostream &stream, date d);
};

#endif // Nothing belongs below here

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

homework.h

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

#ifndef _HOMEWORK_H
#define _HOMEWORK_H

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

class homework;

ostream &operator<< (ostream &stream, homework h);

class homework {

private:
    string name; // Important note: Spaces within a name are not supported
    date assigned_date;
    date due_date;

public:
    homework ();
    homework (string name, date assigned_date, date due_date);

    // This should follow the Java syntax -- we'll discuss overloading
    // oeprators soon enough. The comparison should be based ont he due
    // date.
    int compareTo (homework another_homework);

    // The standard insertion operator.
    // It should output as follows:
    //      HW#1: 1/1/03 - 2/23/03
    friend ostream &operator<< (ostream &stream, homework h);

};

#endif // Nothing belongs below here

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

homeworklist.h

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

#ifndef _HOMEWORKLIST_H
#define _HOMEWORKLIST_H

#include "homework.h"

const int LIST_MAX = 50; // Maximum of 50 assignments; change if desired

class homeworklist;

ostream &operator<< (ostream &stream, homeworklist hl);

class homeworklist {

private:
    homework list[LIST_MAX]; // Array of individual assignments
    int current_size; // total currently within array

public:
    homeworklist(); // default constructor
    bool add (homework h); // adds a new assignment to this collection

    // Returns a new homeworklist containing only those homeworks due
    // after, but not on, the specified date
    homeworklist dueafter (date d);

    // Returns a new homeworklist containing only those homeworks due
    // before, but not on, the specified date
    homeworklist duebefore (date d);

    // Returns a new homeworklist containing only those homeworks due
    // exactly on the specified date, but not before or after
    homeworklist dueon (date d);

    // The standard insertion operator. It should just list
    // one homework per line
    friend ostream &operator<< (ostream &stream, homeworklist hl);
};

#endif // Nothing belongs below here

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

main.cpp

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

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>

#include "date.h"
#include "homework.h"
#include "homeworklist.h"

using namespace std;

// Constants
const int EMPTY = 0;
const int ADD = 1;
const int LIST = 2;
const int DUE_ON = 3;
const int DUE_BEFORE = 4;
const int DUE_AFTER = 5;
const int QUIT = 6;


// Function declarations
date string_to_date (string);
void display_menu (void);
int read_menu_selection (void);
void display_due(homeworklist, int);
date read_date(string);


// Function definitions
int main (int argc, char *argv[]) {

    string hw_name;
    date hw_asg_date;
    date hw_due_date;
    homework new_asg;

    homeworklist hl;

    int choice = EMPTY;
    while (choice != QUIT) {

        display_menu();
        choice = read_menu_selection();

        switch (choice) {

        case ADD:

            // read in the name, date assigned, and due date
            cout << endl << "Homework name: ";
            cin >> hw_name;

            hw_asg_date = read_date("Date assigned: ");
            hw_due_date = read_date("Date due: ");

            // create and store the homework object
            new_asg = homework(hw_name, hw_asg_date, hw_due_date);
            hl.add(new_asg);
            break;

        case LIST:
            cout << hl << endl;
            break;

        case DUE_AFTER:   // falls through
        case DUE_BEFORE:  // falls through
        case DUE_ON:
            // Due after, before, and on are all
            // handled by this case. Since there
            display_due(hl, choice);
            break;

        case QUIT:
            break;

        default:
            choice = EMPTY;
            break;

        } // end switch (choice)

    } // end main work loop (while)

    return EXIT_SUCCESS;
}

void display_menu(void) {

    cout << "/n1. Add/n";
    cout << "2. List all assignments/n";
    cout << "3. List all assignments /"due on/" a particular date/n";
    cout << "4. List all assignments /"due before/" a particular date/n";
    cout << "5. List all assignments /"due after/" a particular date/n";
    cout << "6. Quit/n";
}

int read_menu_selection(void) {

    int choice = 0;
    cout << "Enter selection:  ";
    cin >> choice;
    cout << endl;

    return choice;
}

void display_due(homeworklist hl, int choice) {

    date d = read_date("Enter a date: ");

    if (DUE_AFTER == choice) {
        cout << endl << hl.dueafter (d);

    } else if (DUE_ON == choice) {
        cout << endl << hl.dueon (d);

    } else if (DUE_BEFORE == choice) {
        cout << endl << hl.duebefore (d);

    }

}

date read_date(string prompt) {

    // display prompt, read input, convert to date

    cout << prompt;

    string date_whole;
    cin >> date_whole;

    return string_to_date(date_whole);
}

date string_to_date (string date_string) {

    // convert from a string to a date

    int day, month, year;
    int posn;

    posn = date_string.find_first_of ("/", 0);
    month = atoi(date_string.substr(0, posn).c_str());

    day = atoi(date_string.substr(posn + 1,
                                  date_string.find_last_of("/", 0)).c_str());

    posn = date_string.find_first_of("/", posn + 1) + 1;
    year = atoi(date_string.substr(posn, date_string.length()).c_str());

    return date (day, month, year);
}

// eof