C/C++ 编程代写
当前位置:以往案例 > >C++语言 code Help案例 :Simple
2017-11-02





Purpose: This project has several goals:

1. This is a relatively simple project, meant to get you back into programming after the semester break.

2. Make sure you can edit/compile/run C++ code in whatever IDE you will be using this semester (preferably CLion).

3. Gain experience in using and manipulating objects in C++.

4. Gain experience in writing test code that will thoroughly test a C++ class.

5. Make sure you can correctly submit code for grading via Brightspace.

You will be provided a C++ class that represents some type of object and you will write a program that exercises that class as fully as possible. Note: your task is only to test the supplied class; not to implement the class.

project: This project deals with a calendar which stores a collection of reminders. The reminders will be storedin sorted (chronological) order. The Calendar class is able to contain up to MAX_REM (currently defined to be 50) reminders. The Calendar.h file describes all the functions provided by the class.

You will be provided with the source code that supports the reminders and object code (pre-compiled code) that supports the calendar. Your task will be to test the provided Calendar class as fully as possible to ensure it works as expected. This is known as test-driven development : we write code to test that a class works as expected, and afterward we write the class such that it passes all tests (the only difference is that I am giving you working code so that you can see if your tests are correct or not). Note: your task is to only test the Calendar class, not to implement the Calendar class. All the functions that you are to test are fully described in the Calendar.h file.

Functional Specifications: You will be supplied seven files:

• Date.h: declaration of the Date class (do not change)

• Date.cpp: definition of the Date class (do not change)

• Reminder.h: declaration of the Reminder class (do not change)

• Reminder.cpp: definition of the Reminder class (do not change)

• Calendar.h: declaration of the Calendar class (do not change)

• Calendar.cpp.obj or Calendar.cpp.o: compiled object code of the Calendar class which implements all the classfunctions [it was produced from a properly working Calendar.cpp file]

• CalTest.cpp: initial test program. Your job is to add code to the CalTest.cpp file to fully test/exercise theCalendar class.

The Calendar.h file contains the declaration of a set of functions to manipulate a calendar, which is implemented as a collection of Reminder objects. A predetermined maximum size of the collection is defined by the constant identifier MAX_REM. You will also be supplied a compiled object file, Calendar.cpp.obj or Calendar.cpp.o, which implements all the functions [it was created from a properly working Calendar.cpp file]. You will also be provided with an initial test program: CalTest.cpp. Your job is to add code to the CalTest.cpp file to fully test/exercise the Calendar class. Note: your task is to only test the Calendar class, not to implement the Calendar class. You are not to test the Date or Reminder classes – you can assume they work as given (if you think you have found a bug in either the Date or Reminder classes, please report them to the instructor or post them on Piazza).

Here are the methods/features of the Calendar class that you are to test (see the Calendar.h file for descriptions that are more complete):



Calendar class


Methods

Function

Calendar()

Default constructor – creates an empty Calendar

getNumRem()

Return the total number of Reminders in the Calendar

addRem(const Reminder &r)

Add a reminder to the Calendar

getRem(size_t index)

Returns the reminder at the specified index

displayRem()

Return a string of all reminders



displayRem(size_t index)

Return a string of the reminder at a particular index

displayRem(const string&

Return a string of all reminders whose message matches the

str)

provided string

displayRem(const Date& d)

Return a string of all reminders for a given date

displayRem(const Date& d1,

Displays reminders in a range of two given dates

const Date& d2)

findRem(const Date& d)

Find first reminder for the given date and return its index

findRem(const string& str)

Find first reminder with the given message and return its index

deleteRem()

Deletes all reminders earlier than today’s date

deleteRem(size_t index)

Deletes reminder object at a provided index position

deleteRem(const string&

Delete all reminders whose message matches a given string

str)

deleteRem(const Date& d)

Deletes all reminders on a particular date

deleteRem(const Date& d1,

Deletes all reminders between a range of two given dates

const Date& d2)


Implementation details: Here are a few notes that might be helpful:


1. You are to download a zip file which is provided with this specification – be sure to download the correct one for your platform. The zip file contains a CLion project that includes starter code for this project. You must unzip/extract the files before you work on them. Once you unzip the file, you can open the project by starting CLion, then specify that you want to “Open Project”, and then navigate to the folder you just extracted. When you open the project, let CLion do its initialization work and load symbols (there are more details later in this document). The code as provided compiles cleanly (no errors or warnings) and executes successfully – though it only performs minimal testing in its distributed form. Again, you must unzip/extract the files before you work on them.


2. The test code that you write should be fully automated, as demonstrated in the small segment of provided test code. That means your test code should not depend upon reading any input from a user nor should it require the user to visually inspect any output. Relying on the user to enter all special cases to be tested is prone to omissions. Plus anyone else who uses such test code would not know what data to enter so that the class is fully tested.

3. You will likely be performing some operations with C++ strings. C++ string operations are described here. You will only need a few of the available operations in this project, such as length, at (or use [ ]), project, equality operator or compare method, substr, and maybe others.


4. The length() method of the string class returns a value of type size_t (an unsigned integer type). We also use variables and parameters of type size_t whenever we expect the value to be non-negative (e.g., parameters that represent an array index are declared to be size_t variables). The compiler may give you conversion warning messages if you attempt to assign/compare these values to a value of type int. Your  submissions are expected to have clean compiles (no errors or warnings). To eliminate this particular warning, you can cast the size_t values to an int, or cast the int values to type size_t if you know the integer values are not negative. Note: all grading will be done with CLion and the clang compiler, which may produce different warning & error messages than other compilers – if you are developing with something other than CLion/clang, it is your responsibility to ensure you code compiles cleanly with CLion/clang.

5. The Calendar.h specifies that some methods throw an exception if someone attempts to perform an operation that is not allowed (e.g., adding a reminder to a full calendar). When an exception is thrown it will cause your program to stop executing if the exception is not handled. You will want to test the class to make sure exceptions are thrown appropriately, so you need to know how to handle the exceptions. To handle exceptions you use what is known as try-catch blocks, which can catch the thrown exceptions. The distributed CalTest.cpp file contains a try-catch block that you can duplicate to test the exception throwing capabilities of the Calendar class. Your test program must use try-catch blocks whenever it expects an exception to be thrown – this will allow your program to continue executing. You should write a new try-catch block for each exception you are testing. More information on C++ exceptions can be found on pages 228-237 in our text.

6. Note that if you do not understand how a method of the class is supposed to act from its description/specification, then you can simply try it out – you were supplied a working class as a part of the project.

7. To be perfectly clear, your job in this project is to fully test the Calendar class to the best of your ability. Your job is not to write the Calendar class – that code has been provided to you in the form of a compiled object file.



Background: A calendar is simply a collection of reminders. A reminder is simply a dated string (i.e., the text of the


reminder and an associated date). Reminders are created/supported through two classes: the Date class and the Reminder class. Again, you may assume the Date & Reminder classes are bug-free. The purpose of this project is to only test the Calendar class.


In this program, a date is recognized in the MM/DD/YYYY format (Note: We have a four -digit year, which indicates that “18” would indicate the year 18, and not 2018, and so on. Also, it is assumed that the year cannot be more than four digits long). A reminder is a string message associated with a date. Since the reminder is essentially a date with an added feature (the associated message), the Reminder class is derived from the Date class by using inheritance.


The properties of the Date and Reminder classes (which are already provided to you) are given below (note: a Date object contains a month, day, and year, and is a very simplified class and so it does not handle leap year issues):


Date class



Methods

Function

Date()

Default constructor – creates a date object having today’s date

Date(int m, int d, int y)

Overloaded constructor that creates a Date object with the values of

month, day and year corresponding to the passed values of m, d, and y

Date(const string& dateStr)

Overloaded constructor that parses a string in mm/dd/yyyy form and

creates a Date object

void setDate(const Date &d)

Sets the date of current Date object to the date of the passed Date object

void setDate(int m, int d, int y)

Sets the date of the current Date object with the values of month, day

and year corresponding to the passed values of m, d, and y

void setDate(const string&

Sets the date of the current Date object by parsing a string in

dateStr)

mm/dd/yyyy form

void incrementDay()

Changes the date to the next day

void decrementDay()

Changes the date to the previous day

void incrementMonth()

Changes the date to the next month

void decrementMonth()

Changes the date to the previous month

void incrementYear()

Changes the date to the next year

void decrementYear()

Changes the date to the previous year

int getMonth()

Returns the integer value of the month of the date

int getDay()

Returns the integer value of the day of the date

int getYear()

Returns the integer value of the year of the date

string toString()

Returns a formatted string (mm/dd/yyyy) of the date object

Date operator+= (int n)

Adds ‘n’ days to current date

Date operator-= (int n)

Subtracts ‘n’ days to current date

Date operator+(int n)

Adds ‘n’ days to current date and returns the value

Date operator-(int n)

Subtracts ‘n’ days from current date and returns the value

int operator-(const Date& d)

Finds the difference between two dates (overloaded operator)

Date operator++()

Pre- and Post-increment

Date operator–()

Pre- and Post-decrement

bool operator==(const Date& d)

Compares date to d. If the two dates are equal, returns true, else returns

false

bool operator!=(const Date& d)

Compares date to d. If the two dates are not equal, returns true, else

returns false

bool operator< (const Date& d)

Compares date to d. If date < d, returns true, else returns false

bool operator<=(const Date& d)

Compares date to d. If date <= d, returns true, else returns false

bool operator> (const Date& d)

Compares date to d. If date > d, returns true, else returns false

bool operator>=(const Date& d)

Compares date to d. If date >= d, returns true, else returns false





Note on using overloaded operators: In the above class description you see that several operators have been overloaded for the class; for ple “operator==” has been defined. You can use these like any other method of the class; for



ple if d1 & d2 were two Date objects, you could type: d1.operator==(d2) to test if the two dates were equal.


However, it is much easier to simply use them as the operators you know and love; for ple: d1==d2.


Reminder class

As the Reminder class is derived from the Date class, all the functions in the Date class can also be used with the Reminder class. There are a few added functionalities, as described below:

Methods

Function

Reminder()

Default constructor – creates a Reminder object having today’s date

and an empty string

Reminder(const Date& d,

Overloaded constructor that creates a Date object with d as the date

const string& str)

and str as the message

void setMsg (const string& msgStr)

Sets the message of the reminder to the string msgStr that was passed

in the argument

string getMsg()

Returns the message of the reminder in form of a string

Date getDate()

Returns the date of the reminder in form of a date object

string toString()


在线提交订单