Question: Implement a simple abstract data type which represents a date. The hash-tag for this exercise is #cab202StructPractice2. This exercise will let you practice with struct,

Implement a simple abstract data type which represents a date. The hash-tag for this exercise is #cab202StructPractice2.

This exercise will let you practice with struct, array and pointer variables. You will complete the type definition for a struct type called date_t, and then implement the following functions which perform meaningful operations on objects of this type.

date_read Read three integers into a date_t object referenced by a pointer.

date_write Display the value of a date_t object.

date_valid Determine whether a date_t object represents a valid date in the standard calendar.

date_compare Determine the ordering relationship (i.e. one value is before, the same, or after another) between two date_t objects.

date_match Search an array of date_t objects to find the first date having a specified relationship to a query date.

Instructions are included as in-line comments in the test driver below.

#include #include #include

// (Task 1) Declare a new struct type called date_t. This structure // could be used to store a calendar date in an application which requires // timekeeping on a daily basis. // // Members: // year - an int which records the year. // // month - an int which records the month, with valid values ranging // from 1 to 12. // // day - an int which records the day in the month, ranging from 1 to // 31, subject to the rules set out in the documentation comment of the // date_valid function.

typedef struct date_t { // (Task 1.1) Declare the fields of the struct in the order listed above. } date_t;

// (Task 2) Define a function called date_read which uses scanf to get the // data for a date_t. Fields are to be entered as three separate int values // in the format "%d-%d-%d". // // Parameters: // date_ptr - the address of a date_t which must be populated by the // function call. // // Returns: // The function must return a boolean value indicating the status of the // I/O operation. The status is true if and only if three integer values // have been successfully parsed and saved in date_ptr. // // Do not try to perform other data validation in this function.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { // (Task 2.1) Insert logic to read three integer values from standard input // and save them in the appropriate fields of date_ptr. Use scanf, but do // NOT use printf or any other output function. }

// (Task 3) Define a function called date_write which uses printf to // display the value of a date_t structure. // // Parameters: // date - a date_t structure that will be displayed. // // Returns: // Nothing.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { // (Task 3.1) Print the horizontal and vertical position of SCREEN-POS_VAR // with format string "%d-%d-%d". Do NOT insert a linefeed. }

// (Task 4) Define a function called date_compare which compares two // date_t values. Your implementation may assume that these values are // valid dates. // // Parameters: // date1 - a date_t structure. // date2 - a date_t structure. // // Returns: // An int which is: // -1 if the date represented by date1 is before that represented by // date2; // 0 if the two values represent the same date; // +1 otherwise.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE } // (Task 5) Implement the date_valid function which determines if the // supplied date is valid: // * Year must be greater than or equal to 1. // * Month must between 1 and 12 inclusive. // * Day must be at least 1, with upper limits given below: // 30 days: September, April June, and November. // 31 days: January, March, May, July, August, October, December. // 28 or 29: February (usually 28, but 29 in a leap year). // // A year is a leap year if it is divisible by 400, or if it is // divisible by 4 but not divisible by 100. // // Parameters: // date - a date_t value. // // Returns: // Returns true if and only if the supplied date is valid according to // the definition listed above.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE }

// (Task 6) Define a function called date_match which compares a query date to // the elements of an array of date_t objects. The function returns the // address of the first object in the list which satisfies a designated criterion. // // Parameters: // query - a date_t structure. // dates - an array of date_t structures. // num_dates - an int which tells the function how many elements there // are in the array. // criterion - an int (guaranteed to be -1, 0, or 1) which defines the // matching criterion. // // Returns: // A pointer to a date_t object. // If num_dates is equal to or less than 0: this value will be NULL. // If the query is not valid: this value will be NULL. // If there is no valid element x in the array which // date_compare(x,query) == criterion // then this value will be NULL. // Otherwise: the return value will be the address of the first valid // date_t x in the array for which // date_compare(x,query) == criterion.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE }

#define MAX_ITEMS (100)

int main(void) { date_t query; printf("Input query date using format %s, with year first and day last: ", "%d-%d-%d"); date_read(&query);

date_t ref_dates[MAX_ITEMS] = { {0,0,0} }; int num_items;

// Get number of ref_dates. printf("Please enter number of items (up to %d) that will be processed: ", MAX_ITEMS); scanf("%d", &num_items);

// if number of ref_dates exceeds array size, restrict it to that value. if (num_items > MAX_ITEMS) { num_items = MAX_ITEMS; }

for (int i = 0; i < num_items; i++) { printf("Please enter item %d of %d using format %s, with year first and day last: ", (i + 1), num_items, "%d-%d-%d"); date_read(&ref_dates[i]); }

for (int i = 0; i < num_items; i++) { date_write(ref_dates[i]);

if (!date_valid(ref_dates[i])) { printf(" is not valid. "); continue; }

int cmp = date_compare(ref_dates[i], query); if (cmp < -1 || cmp > 1) { printf("Error!!! date_compare is broken. "); exit(1); } char * labels[] = { "less than", "equal to", "greater than" }; printf(" is %s ", labels[cmp + 1]); date_write(query); printf(" "); }

const int criterion = -1; date_t * cmp = date_match(query, ref_dates, num_items, criterion);

if (cmp) { printf("The first valid date matching the search criterion is "); date_write(*cmp); } else { printf("There is no valid date matching the search criterion. "); }

return 0; }

'HELP'

1. Prompt the user to enter a date_t, and read it in with date_read.

2. Prompt the user to enter the number of reference dates, and read it in with scanf. If it exceeds the dimensionality of the array, clip it to that value.

3. Successively prompt for and read a reference date. Each reference date is appended to a list stored in an array.

4. Display the reference dates, including their distance to the query. Distances are computed using the city block distance.

5. Identify the first valid reference date which has a designated relationship (before, equal, or after) to the query, and display the details.

When the test driver is executed with a typical mixture of valid and invalid input sequences, the results are as seen in Figure 1. Note that the characters used to separate fields for input and output in your question may be different from those shown in the illustration. The required format is set out in the relevant documentation comments in the test driver.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!