-
Calendar program
Hello!!
I'm trying to built a program and I'd like your advice! :)
So, I'm trying to make a program that does the following :
1) the user inserts a year
2)the user enters the first day of the year(1/1/year) (eg. Monday, Friday...)
3)The user enters the number of the days that the compurer will print (eg. 100 days)
4) The computer prints the (100) first days if the year.
I also, want to check if the year is leap (365 days or 366 days).
I have made the program, however I would like your opinion. Is this the correct solution? Is there any other (better) solution??
-------------------------
My code :
-------------------------
#include <iostream.h>
#include <stdlib.h>
int myear;
int i = 1;
int week=1;
int hd;
unsigned short int proti_mera;
bool disekto(unsigned short int year)
{
if(year % 100 != 0)
return (year % 4 == 0);
else
return (year % 400 == 0);
}
void menu()
{
cout<<"Insert the year : \n";
cin>>myear;
cout<<endl;
cout<<"Which is the first day of the year ?\n";
cout<<"1. Monday\n";
cout<<"2. Tuesday\n";
cout<<"3. Wednesday\n";
cout<<"4. Thursday\n";
cout<<"5. Friday\n";
cout<<"6. Saturday\n";
cout<<"7. Sunday\n";
cout<<endl;
do
{
cout<<"Press (1..7) : ";
cin>>proti_mera;
if (!((proti_mera > 0) && (proti_mera < 8)))
continue;
}
while (!((proti_mera > 0) && (proti_mera < 8)));
cout<<endl;
cout<<"How many days would you like me to print ?\n";
do
{
if (disekto(myear))
{
cout<<"Press (1..366) : ";
cin>>hd;
if (!((hd > 0) && (hd <= 366)))
continue;
else
break;
}
if (!disekto(myear))
{
cout<<"Press (1..365) : ";
cin>>hd;
if (!((hd > 0) && (hd <= 365)))
continue;
else
break;
}
}
while (1);
cout<<endl;
}
void display_week()
{
cout.width(15);
cout<<week++<<" Week\n";
cout<<endl;
}
void pause_page()
{
if (i % 7 == 0)
{
cout<<endl;
system("PAUSE");
cout<<endl;
display_week();
}
}
int main()
{
char mdays[7][10] = {"Monday","Tuesday","Wednesday","Thursday","Friday ","Saturday","Sunday"};
char mmonths[12][12] = {"January","February","March","April","May","June" ,"July","August","September","October","November", "December"};
int disdays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
int udisdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int dday = 1;
int mmonth=0;
menu();
int mday = proti_mera-1;
display_week();
for(i;i<=hd;i++)
{
cout<<i<<" ";
cout.width(5);
cout<<mdays[mday++]<<" "<<dday++<<" "<<mmonths[mmonth]<<" "<<myear<<endl;
if (disekto(myear))
{
if(dday == disdays[mmonth]+1)
{
dday=1;
mmonth++;
}
}
if (!disekto(myear))
{
if(dday == udisdays[mmonth]+1)
{
dday=1;
mmonth++;
}
}
if (mday >= 7)
mday=0;
pause_page();
}
cout<<endl;
return 0;
}
----------------------------------------------
Thank you in advance!! :)
Apostolis
-
here's my solution
here's my solution..
this one also saves it to a text file if you want..
PHP Code:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
int leapyear(int year)
{
if ( (year%4==0) && (year%400==0) )
{
return 1;
}
if ( (year%4==0) && (year%100!=0) )
{
return 1;
}
return 0;
}
void getweekday(int month, int day, int year, int *week)
{
int centurycode, monthcode[13], C, D;
monthcode[0]=1;
monthcode[1]=1;
monthcode[2]=4;
monthcode[3]=4;
monthcode[4]=0;
monthcode[5]=2;
monthcode[6]=5;
monthcode[7]=0;
monthcode[8]=3;
monthcode[9]=6;
monthcode[10]=1;
monthcode[11]=4;
monthcode[12]=6;
C=year/100;
D=year%100;
if (C==17)
{
centurycode=4;
}
if (C==18)
{
centurycode=2;
}
if (C==19)
{
centurycode=0;
}
if (C==20)
{
centurycode=6;
}
*week = (centurycode + (D/4 + D)%7 +monthcode[month] + day)%7;
}
void drawcalendar(int days, int firstday)
{
int i, daythen;
char space = ' ';
if (firstday == 0)
{
firstday = 7;
}
daythen=firstday;
printf("SUN MON TUE WED THU FRI SAT\n");
for (i=1; i<firstday; i++)
{
printf("%3c ", space);
}
for (i=1; i<=days; i++)
{
printf("%3d ", i);
daythen++;
if (daythen>7 && i != days)
{
daythen=1;
printf("\n");
}
}
printf("\n\n\n");
}
void writecalendar(int days, int firstday, FILE *fp)
{
int i, daythen;
char space = ' ';
if (firstday == 0)
{
firstday = 7;
}
daythen=firstday;
fprintf(fp, "SUN MON TUE WED THU FRI SAT\n");
for (i=1; i<firstday; i++)
{
fprintf(fp, "%3c ", space);
}
for (i=1; i<=days; i++)
{
fprintf(fp, "%3d ", i);
daythen++;
if (daythen>7 && i != days)
{
daythen=1;
fprintf(fp, "\n");
}
}
fprintf(fp, "\n\n\n");
}
void getdays(int yeartype, int month, int *days)
{
if (yeartype == 0 && month == 2)
{
*days = 28;
}
if (yeartype == 1 && month == 2)
{
*days = 29;
}
if ( (month==4) || (month==6) || (month==9) || (month==11) )
{
*days = 30;
}
if ( (month==1) || (month==3) || (month==5) || (month == 7) || (month==8) || (month==10) || (month==12) )
{
*days = 31;
}
}
void writefile(int month, int year, int days, int firstday, int yeartype)
{
FILE *fp;
fp = fopen("CALENDAR.TXT", "w");
for (month =1; month <=12; month++)
{
getweekday(month, 1, year, &firstday);
getdays(yeartype, month, &days);
if (month == 1)
{
fprintf(fp, "January %d\n\n", year);
}
else if (month ==2)
{
fprintf(fp, "February %d\n\n", year);
}
else if (month ==3)
{
fprintf(fp, "March %d\n\n", year);
}
else if (month ==4)
{
fprintf(fp, "April %d\n\n", year);
}
else if (month ==5)
{
fprintf(fp, "May %d\n\n", year);
}
else if (month ==6)
{
fprintf(fp, "June %d\n\n", year);
}
else if (month ==7)
{
fprintf(fp, "July %d\n\n", year);
}
else if (month ==8)
{
fprintf(fp, "August %d\n\n", year);
}
else if (month ==9)
{
fprintf(fp, "September %d\n\n", year);
}
else if (month ==10)
{
fprintf(fp, "October %d\n\n", year);
}
else if (month ==11)
{
fprintf(fp, "November %d\n\n", year);
}
else if (month ==12)
{
fprintf(fp, "December %d\n\n", year);
}
writecalendar(days, firstday, fp);
}
fprintf(fp, "Program written by Maharshi Akilla\n");
fprintf(fp, "http://mahurshi.tripod.com");
fclose(fp);
}
main()
{
int month, year, days, firstday, yeartype;
char answer;
cout << "Calendar" << endl << "Generates the calendar for any year between 1700 and 2099" << endl << endl;
do
{
cout << "Enter the year: ";
cin >> year;
} while (year<1700 || year>2099);
clrscr();
yeartype = leapyear(year);
for (month =1; month <=12; month++)
{
getweekday(month, 1, year, &firstday);
getdays(yeartype, month, &days);
if (month == 1)
{
cout << "January " << year << endl << endl;
}
else if (month ==2)
{
cout << "February " << year << endl << endl;
}
else if (month ==3)
{
cout << "March " << year << endl << endl;
}
else if (month ==4)
{
cout << "April " << year << endl << endl;
}
else if (month ==5)
{
cout << "May " << year << endl << endl;
}
else if (month ==6)
{
cout << "June " << year << endl << endl;
}
else if (month ==7)
{
cout << "July " << year << endl << endl;
}
else if (month ==8)
{
cout << "August " << year << endl << endl;
}
else if (month ==9)
{
cout << "September " << year << endl << endl;
}
else if (month ==10)
{
cout << "October " << year << endl << endl;
}
else if (month ==11)
{
cout << "November " << year << endl << endl;
}
else if (month ==12)
{
cout << "December " << year << endl << endl;
}
drawcalendar(days, firstday);
}
do
{
cout << "Do you want to write to a file? (Y/N)" ;
cin >> answer;
} while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');
if (answer == 'y' || answer == 'Y')
{
writefile(month, year, days, firstday, yeartype);
cout << endl << "A file 'CALENDAR.TXT' has been created which contains the calendar" << endl;
}
cout << endl << "Program written by Maharshi Akilla" << endl;
cout << "http://mahurshi.tripod.com" << endl;
while (!kbhit())
{
}
}
The above code has a mixture of couts and printfs,
(i was too lazy to use printfs all the time) you might
want to edit that. It doesn't cause any harm anyway.
You can modify this to fit your requirements.
GOOD LUCK!
-
Thank you my friend!! :)
Your code was very helpfull for me!
-
you're welcome