|
 |
|
 |
02-21-2005, 10:55 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Feb 2005
Posts: 5
|
anyone knows the code for this c++ question?
The Julian calendar consist of a solar year of 12 months and of 365 days withan extra day every four years.
In the Gregorian calendar a year is a leap year if either
1)it is divisible by 4 but not 100 or
2)it is divisible by 100
Using the gregorian calendar, in 1752, (wednesday) september 2 1752 is such that is followed immediately by september 14 1752.
Most computers use the Julian calendar until september 1752, with leap year every four years , then use the Gregorian calendar after that.
1/1/1 falls on a saturday.
Need to construct an ADT for calendar with class "Date" which follow the specifications above.
Need to have the various construtors and functions below:
Date()//initialise to 1/1/1
Date(DD,MM,YYYY)//initialise date to DD/MM/YYYY
next()//To set the date such that it advance the day by 1 day.
printD()//print out day of week(eg.monday, tuesday ,......)
printT()//print out day in the form DD/MM/YYYY.
printW()//print out day in the form such as 12 March 1782.
diffD(D)//print out the absolute number of days between the current date and the and D.
The calendar only work for date from 1/1/1 to 31/12/9999.
Thank you very much! 
|
|
|
02-21-2005, 11:13 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
|
We're not here to solve your assignments, we will how ever provide you with help, if you get stuck and show us what code you've got so far..
Anyway, a google search on the subject: "a leap year test written in c"
Will provide you with enough info to get started, just pick the first item on the list.
|
|
|
02-21-2005, 11:35 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Feb 2005
Posts: 5
|
Here is my code.
Here is my code, but its very long and i'm not sure how to shorten it.
Code:
//Set current variable.
void Date::Date(int DD, int MM, int YYYY){
date=DD;
month=MM;
year=YYYY;
}
//Next date for the current variable.
void Date::next(){
if(date<28)
date+=1;
else{
switch(month){
//Compare the month.
case '1':if(date==31){
date=1;
month=2;
}
else
date+=1;
break;
case '2':if(year<=1752){ //For years following Julian calendar.
if(year%4==0){ //Leap year.
if(date==29){
date=1;
month=3;
}
else
date+=1;
}
else{
date=1;
month=3;
}
}
if(year>1752 && year<=9999){ //For year following the Gregorian calendar.
if((year%4==0 && year%1000!=0) || year%400==0){ //Leap year.
if(date==29){
date=1;
month=3;
}
else
date+=1;
}
else{
date=1;
month=3;
}
}
break;
case '3':if(date==31){
date=1;
month=4;
}
else
date+=1;
break;
case '4':if(date==30){
date=1;
month=5;
}
else
date+=1;
break;
case '5':if(date==31){
date=1;
month=6;
}
else
date+=1;
break;
case '6':if(date==30){
date=1;
month=7;
}
else
date+=1;
break;
case '7':if(date==31){
date=1;
month=8;
}
else
date+=1;
break;
case '8':if(date==31){
date=1;
month=9;
}
else
date+=1;
break;
case '9':if(year==1752 && date==2){
//Special case in september 1972.
date=14;
}
else{
if(date==30){
date=1;
month=10;
}
else
date+=1;
}
break;
case '10':if(date==31){
date=1;
month=11;
}
else
date+=1;
break;
case '11':if(date==30){
date=1;
month=2;
}
else
date+=1;
break;
case '12':if(date==31){
date=1;
month=1;
if(year<9999)
year+=1;
}
else
date+=1;
break;
}//End of switch loop.
}//End of else.
}
//Print out variable day of week.
void printD(){
int tempDate, tempMonth, tempYear, k, numOfDays=0;//Local declarations.
tempDate=date;
tempMonth=month;
tempYear=year;
date=1;
month=1;
year=1;
while(tempDate!=date && tempMonth!=month && tempYear!=year){
next();
numOfDays++;
}//End of while loop.
k=numOfDays%7;
switch(k){
case '0':cout << "Saturday";
case '1':cout << "Sunday";
case '2':cout << "Monday";
case '3':cout << "Tuesday";
case '4':cout << "Wednesday";
case '5':cout << "Thursday";
case '6':cout << "Friday";
}//End of switch.
date=tempDate;
month=tempMonth;
year=tempYear;
if(date==1 && month==1)
cout << " (New Year's Day)";//If that day is a new year.
if(date=14 && month==2)
cout << " (Valentine's Day)";//If that day is a valentine's day.
cout << endl;
}//End of printD function.
//Print out variable as DD/MM/YYYY.
void printT(){
cout << date << "/" << month << "/" << year << endl;
}//End of printT function.
//Print out variable in the form of 28 March 2004;
void printW(){
cout << date;
//Compare month.
switch(month){
case '1':cout << " January ";
case '2':cout << " FebRuary ";
case '3':cout << " March ";
case '4':cout << " April ";
case '5':cout << " May ";
case '6':cout << " June ";
case '7':cout << " July ";
case '8':cout << " August ";
case '9':cout << " September ";
case '10':cout << " October ";
case '11':cout << " November ";
case '12':cout << " December ";
}//End of switch.
cout << year << endl;
}//End of printW function.
//Print out the absolute number of days between current date and a particular date, D.
void diff(int theDate, int theMonth, int theYear){
int tempDate, tempMonth, tempYear, k,difference, numOfDays=0;//Local declarations.
tempDate=date;
tempMonth=month;
tempYear=year;
if(theDate>=date && theMonth>=month && theYear>=year){
// If current date is earlier than or same as D.
while(date!=theDate && month!=theMonth && year!=theYear){
next();
numOfDays++;
}//End of while loop.
if(numOfDays>=1)
difference=numOfDays-1;
else
difference=numOfDays;
cout << difference << endl;
date=tempDate;
month=tempMonth;
year=tempYear;
}
else{ //If d is earlier than current date.
date=theDate;
month=theMonth;
year=theYear;
while(date!=tempDate && month!=tempMonth && year!=tempYear){
next();
numOfDays++;
}
if(numOfDays>=1)
difference=numOfDays-1;
else
difference=numOfDays;
cout << difference << endl;
date=tempDate;
month=tempMonth;
year=tempYear;
}
}//End of diff function.
Last edited by redhead; 02-22-2005 at 12:52 AM.
|
|
|
02-22-2005, 05:40 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
You can easely optimize the leap year function:
Code:
bool is_leap_year(int year)
{
if( year % 4 ) return false;
if( year < ReformYear ) return true;
if( year % 100 ) return true;
if( year % 400 ) return false;
return true;
}
But what exactly is it that you need us for? What is the concrete question or request?
__________________
|
|
|
02-22-2005, 06:01 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Feb 2005
Posts: 5
|
I don't really get what you meant. Do you meant why am i asking the question here or....?
|
|
|
02-22-2005, 06:12 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
My question is: how can we help you exactly?
__________________
|
|
|
02-22-2005, 06:19 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Feb 2005
Posts: 5
|
I think there are logic errors in the code that i posted out because the output that i got using the code did not give me the correct outputs. Thank you for your help!
|
|
|
02-22-2005, 07:59 AM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
First things first. Learn to design a decent class first. The math behind all this is hard enough. So let's setup a decent class and then see if you can manage it from here. If not, then feel free to come back.
Date.h
Code:
#ifndef DATE_H
#define DATE_H
#include <ctime>
class Date
{
public:
enum month
{jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
enum week_day
{ mon = 1, tue, wed, thu, fri, sat, sun };
static const int REFORM_YEAR;
static const month REFORM_MONTH;
static const unsigned REFORM_DAY_NUMBER;
public: //Ctors et al. only.
Date();
Date(month, int, int);
public: //Accessors only.
int get_year() const;
int get_day() const;
month get_month() const;
public:
static int days_in_month(month, int);
static bool is_leap(int);
protected:
void set_date(month, int, int);
unsigned long calc_day_number() const;
private:
month month_;
int day_;
int year_;
unsigned long daynumber_;
};
/***************** inlines ***************/
inline int Date::get_year() const
{
return year_;
}
//-----------------------------------------
inline int Date::get_day() const
{
return day_;
}
//-----------------------------------------
inline Date::month Date::get_month() const
{
return month_;
}
//-----------------------------------------
#endif //DATE_H
Date.cpp
Code:
#include "Date.h"
const int Date::REFORM_YEAR = 1582;
const unsigned Date::REFORM_DAY_NUMBER = 577737L;
const Date::month Date::REFORM_MONTH = Date::oct;
//-----------------------------------------------------
Date::Date()
{
set_date(jan, 1, 1);
}
//-----------------------------------------------------
Date::Date(month m, int d, int y)
{
set_date(m, d, y);
}
//-----------------------------------------------------
void Date::set_date(month m, int d, int y)
{
month_ = m;
day_ = d;
year_ = y;
daynumber_ = calc_day_number();
}
//----------------------------------------------------
bool Date::is_leap(int yr)
{
if( yr % 4 ) return false;
if( yr < REFORM_YEAR ) return true;
if( yr % 100 ) return true;
if( yr % 400 ) return false;
return true;
}
//-----------------------------------------------------
int Date::days_in_month(month mth, int yr)
{
static const int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if( yr == REFORM_YEAR && mth == REFORM_MONTH )
{
return 21;
}
return days[mth] + (feb == mth && is_leap(yr));
}
//-----------------------------------------------------
unsigned long Date::calc_day_number() const
{
long days;
long year = (long)year_ - 1L;
days = year * 365L + year / 4L - year / 100L + year / 400L;
//Years before 1582 were all leap if it is divisible by 4.
if( year > REFORM_YEAR )
{
days += 12;
}
else
{
days += year / 100L;
days -= year / 400L;
}
for( int i = jan; i < month_; ++i )
{
days += days_in_month((month)i, year_);
}
days += day_;
//Adjust for the 10 missing days (Oct 4 - Oct 15, 1582).
if( days > REFORM_DAY_NUMBER )
{
days -= 10L;
}
return days;
}
main.cpp
Code:
#include "Date.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Date dt;
cout << dt.get_day() << " " << dt.get_month() << " " << dt.get_year() << endl;
cin.get();
return 0;
}
The calc_day_number() method is your key to success if you want to implement the next_day() function.
Here are the global steps:
1) Implement an overloaded operator+().
2) Implement a method that calculates the total amount of days added with the number of days you want. Make it calculate the year, month and day.
For example if day_number is day_number+1, then check if the resulting day_number > refordaynumber. If it is, then add 10 days to the resulting day_number! That is because there are 10 days missing because pope Gregor XXLLLCXXX (or something) reformed it.
3) The year_ is then obviously: (int)daynumber_ / 365. Duh.
4) The day_ is INITIALLY: (int)daynumber_ % 365L. Convince your self!
5) Adjust the initial day IF resulting years_ < 1700.
6) And/Or adjust the initial day_, taking leap years into account.
Etcetera etcetera. I needed to study for this too. And now I want a break. See how far you can get. Be welcome anytime. But beware, I do not like lazy folks.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:07 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|