Sunday, July 26, 2009

C++ program, write a program in which the user enters any date and the computer displays the day?

i cant find the logic of this program could anyone plz tell me that,,,, that when user enters a date it can be in past years, present year or future years, the program has to tell me the correct day....... the program has to be in C++ not .Net

C++ program, write a program in which the user enters any date and the computer displays the day?
#include%26lt;iostream.h%26gt;


#define isleapyear(year) ((!(year % 4) %26amp;%26amp; (year % 100)) || (!(year % 400) %26amp;%26amp; (year % 1000)))





class Date


{


private:


int month, day, year;





public:


Date(int month,int day,int year):month(month),day(day),year(year){}...


int isdatevalid();


int weekday();





};





//


// return 1 if date is valid, 0 otherwise.


//


int Date::isdatevalid()


{


if (day %26lt;= 0) return 0 ;


switch( month )


{


case 1 :


case 3 :


case 5 :


case 7 :


case 8 :


case 10 :


case 12 : if (day %26gt; 31) return 0 ; else return 1 ;


case 4 :


case 6 :


case 9 :


case 11 : if (day %26gt; 30) return 0 ; else return 1 ;


case 2 :


if ( day %26gt; 29 ) return 0 ;


if ( day %26lt; 29 ) return 1 ;


if (isleapyear(year)) return 1 ; // leap year


else return 0 ;


}


return 0 ;


}





//


// given month, day, year, returns day of week, eg. Monday = 0 etc.


// tested for 1901 to 2099 (seems to work from 1800 on too)


//


int Date::weekday()


{


int ix, tx, vx;





switch (month) {


case 2 :


case 6 : vx = 0; break;


case 8 : vx = 4; break;


case 10 : vx = 8; break;


case 9 :


case 12 : vx = 12; break;


case 3 :


case 11 : vx = 16; break;


case 1 :


case 5 : vx = 20; break;


case 4 :


case 7 : vx = 24; break;


}


if (year %26gt; 1900) // 1900 was not a leap year


year -= 1900;


ix = ((year - 21) % 28) + vx + (month %26gt; 2); // take care of February


tx = (ix + (ix / 4)) % 7 + day; // take care of leap year


return (tx % 7);


}








int main()


{


char week[7][10] = {


"Monday","Tuesday","Wednesday","Thursday...


"Friday","Saturday","Sunday"


};


Date *d;


int month, day, year;


cout%26lt;%26lt;"Return the day of the week given the date."%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter date in the form mm/dd/yyyy : ";


scanf("%d/%d/%d",%26amp;month,%26amp;day,%26amp;year);


d = new Date(month,day,year);


if (d-%26gt;isdatevalid())


{


cout%26lt;%26lt;"The day of the week for this date is "%26lt;%26lt;week[d-%26gt;weekday()]%26lt;%26lt;endl;


}


else


printf("%d/%d/%d not a valid date!", month,day,year);





getchar(); // wait


getchar(); // 2nd wait needed


return 0;


}
Reply:easy, just check this


http://www.geocities.com/peacecrusader88...


No comments:

Post a Comment