April 27th, 1999, 05:09 PM
I need help in writing routines for the following program:
The "Days Between" Problem
**
** This program, when completed, will prompt the user for two calendar dates
** and compute the number of days between them. The approach described
** here works for any dates after March 1, 1900.
**
** We'll be storing a date in the class Date, as three integers. The
** month is a number between 1 (January) and 12 (December). The day
** is a number in the range [1..31] and the year should be in
** the range [1900..whatever]. This program needs to verify that the
** user typed in valid dates, otherwise the calculations won't be correct.
**
** Fortunately, there's a handy formula for solving this problem (as long as
** both dates are March 1, 1900 or later). For each of the two dates, we
** need to compute a number we'll call N, according to this equation (note:
** this equation assumes all the numbers are integers, don't use floats!):
**
** For a given year, month, and day:
**
** 1461 x F 153 x G
** N = ---------- + --------- + day
** 4 5
**
** The value of F is:
** year - 1 if the month is 1 or 2
** year otherwise
**
**
** The value of G is:
** month + 13 if the month is 1 or 2
** month + 1 otherwise
**
** If N1 is the value of N for the earlier date, and N2 is the
** value of N for the later date, N2 - N1 is the number of days
** between.
**
** (Note: when typing in the test dates given above, take care
** to type them in exactly as written above. Specifically, if
** you're supposed to type in "8/1/1942" but type in "08/01/1942",
** the program will not read the date correctly and will generate
** strange results.)
**/
/**
** Don't modify anything between here and the next ** comment
**/
#include <iostream.h>
#include <stdlib.h>
/*
* The Date class definition
*/
class Date {
public:
/* Attributes */
long int month, day, year;
/* Methods */
long int computeF(void);
long int computeG(void);
long int computeN(void);
char validate(void);
};
/*
* This is the main routine, don't modify anything here.
*/
void main()
{
Date a,b;
char slash;
long int n;
cout << "Please enter the earlier date (month/day/year): ";
cin >> a.month >> slash >> a.day >> slash >> a.year;
cout << "Please enter the later date (month/day/year): ";
cin >> b.month >> slash >> b.day >> slash >> b.year;
if(a.validate() && b.validate()) {
n = b.computeN() - a.computeN();
cout << endl;
cout << "There are " << n << " days between ";
cout << a.month << "/" << a.day << "/" << a.year << " and ";
cout << b.month << "/" << b.day << "/" << b.year << endl;
}
else {
if(!a.validate()) {
cout << "Sorry, the earlier date is before 3/1/1900"
<< endl;
}
if(!b.validate()) {
cout << "Sorry, the later date is before 3/1/1900"
<< endl;
}
}
}
/**
** The parts you need to modify are below
**/
/*
* When this method is called on a Date, it should return the value
* of F as described above.
*/
long int Date::computeF(void)
The "Days Between" Problem
**
** This program, when completed, will prompt the user for two calendar dates
** and compute the number of days between them. The approach described
** here works for any dates after March 1, 1900.
**
** We'll be storing a date in the class Date, as three integers. The
** month is a number between 1 (January) and 12 (December). The day
** is a number in the range [1..31] and the year should be in
** the range [1900..whatever]. This program needs to verify that the
** user typed in valid dates, otherwise the calculations won't be correct.
**
** Fortunately, there's a handy formula for solving this problem (as long as
** both dates are March 1, 1900 or later). For each of the two dates, we
** need to compute a number we'll call N, according to this equation (note:
** this equation assumes all the numbers are integers, don't use floats!):
**
** For a given year, month, and day:
**
** 1461 x F 153 x G
** N = ---------- + --------- + day
** 4 5
**
** The value of F is:
** year - 1 if the month is 1 or 2
** year otherwise
**
**
** The value of G is:
** month + 13 if the month is 1 or 2
** month + 1 otherwise
**
** If N1 is the value of N for the earlier date, and N2 is the
** value of N for the later date, N2 - N1 is the number of days
** between.
**
** (Note: when typing in the test dates given above, take care
** to type them in exactly as written above. Specifically, if
** you're supposed to type in "8/1/1942" but type in "08/01/1942",
** the program will not read the date correctly and will generate
** strange results.)
**/
/**
** Don't modify anything between here and the next ** comment
**/
#include <iostream.h>
#include <stdlib.h>
/*
* The Date class definition
*/
class Date {
public:
/* Attributes */
long int month, day, year;
/* Methods */
long int computeF(void);
long int computeG(void);
long int computeN(void);
char validate(void);
};
/*
* This is the main routine, don't modify anything here.
*/
void main()
{
Date a,b;
char slash;
long int n;
cout << "Please enter the earlier date (month/day/year): ";
cin >> a.month >> slash >> a.day >> slash >> a.year;
cout << "Please enter the later date (month/day/year): ";
cin >> b.month >> slash >> b.day >> slash >> b.year;
if(a.validate() && b.validate()) {
n = b.computeN() - a.computeN();
cout << endl;
cout << "There are " << n << " days between ";
cout << a.month << "/" << a.day << "/" << a.year << " and ";
cout << b.month << "/" << b.day << "/" << b.year << endl;
}
else {
if(!a.validate()) {
cout << "Sorry, the earlier date is before 3/1/1900"
<< endl;
}
if(!b.validate()) {
cout << "Sorry, the later date is before 3/1/1900"
<< endl;
}
}
}
/**
** The parts you need to modify are below
**/
/*
* When this method is called on a Date, it should return the value
* of F as described above.
*/
long int Date::computeF(void)