Monday, May 16, 2011

Calendar

/*
Created By Rana Hamid
http://uvacode.blogspot.com/
+8801717623876
http://facebook.com/ranahamid07/
*/


#include<stdio.h>
#include <stdlib.h>
int getyear();
int getleapyear(int year);
int getdaycode(int year);
void printcalendar(FILE *fp,int year,int daycode,int leapyear);

int main()
{
    int year,leapyear,daycode;
    FILE *fp;
    fp=fopen("calendar.txt","w");
    year=getyear();
    leapyear=getleapyear(year);
    daycode=getdaycode(year);
    printcalendar(fp,year,daycode,leapyear);
    system("PAUSE");
}
//1
int getyear()
{
    int year;
    printf("Enter a year\n");
    scanf("%d",&year);
    return year;
}
//2
int getleapyear(int year)
{
if(year%4==0 && year%100!=0 || year%400==0)
return 1;
else
return 0;   
}
//3
int getdaycode(int year)
{
    int x1,x2,x3;
    int daycode;
    x1=(year-1)/4;
    x2=(year-1)/100;
    x3=(year-1)/400;
    daycode=(year+x1-x2+x3)%7;
    printf("%d",daycode);
}
//4
void printcalendar(FILE *fp,int year,int daycode,int leapyear)
{
       int daysinmonth,day,month;
       fprintf(fp,"              %d",year);
       for(month=1;month<=12;month++)
       {
       switch(month)
                    {
                    case 1:
                         fprintf(fp,"\n\n\t\tJanuary\n");
                         daysinmonth=31;
                         break;
                      case 2:
                         fprintf(fp,"\n\t\tFebrurary\n");
                         daysinmonth=leapyear?29:28;
                         break;  
                       case 3:
                         fprintf(fp,"\n\n\t\tMarch\n");
                         daysinmonth=31;
                         break; 
                           case 4:
                         fprintf(fp,"\n\n\t\tApril\n");
                         daysinmonth=30;
                         break; 
                           case 5:
                         fprintf(fp,"\n\n\t\tMay\n");
                         daysinmonth=31;
                         break; 
                           case 6:
                         fprintf(fp,"\n\n\t\tJune\n");
                         daysinmonth=30;
                         break; 
                           case 7:
                         fprintf(fp,"\n\n\t\tJuly\n");
                         daysinmonth=31;
                         break; 
                           case 8:
                         fprintf(fp,"\n\n\t\tAugust\n");
                         daysinmonth=31;
                         break; 
                           case 9:
                         fprintf(fp,"\n\n\t\tSeptembar\n");
                         daysinmonth=30;
                         break; 
                           case 10:
                         fprintf(fp,"\n\n\t\tOctobar\n");
                         daysinmonth=31;
                         break; 
                           case 11:
                         fprintf(fp,"\n\n\t\tNovembar\n");
                         daysinmonth=30;
                         break; 
                           case 12:
                         fprintf(fp,"\n\n\t\tDecembar\n");
                         daysinmonth=31;
                         break;              
                    }
      fprintf(fp,"\n\nSUN  MON  TUE  WED  THU  FRI  SAT\n");
      // fprintf(fp,"\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
       for(day=1;day<=1+daycode*5;day++)
           fprintf(fp," ");
       for(day=1;day<=daysinmonth;day++)
              {
              fprintf(fp,"%2d",day);
              if(  (day+daycode)%7>0)
              fprintf(fp,"   ");
              else
              fprintf(fp,"\n ");
              }
       daycode=(daycode+daysinmonth)%7;
       } 
}