幫我把月曆改成年曆!!

可以幫我改成輸入西元幾年和一月一日是星期幾就跑出一到十二月的年曆來,要會算出閏年唷!!

#include <stdio.h>

#include <stdlib.h>

void PrCal(int , int); //function prototype

int CheckLeap(int year);

int main(){

int first, month, year;

int m[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

printf("Enter the year and month: ");

scanf("%d %d", &year, &month);

printf("What date is the first day? ");

scanf("%d", &first);

m[1] += CheckLeap(year);

PrCal(first, m[month-1]);

system("pause");

return 0;

}

void PrCal(int first, int days){

int i;

printf("Sun Mon Tue Wed Thu Fri Sat\n");

printf("===========================\n");

for(i=1;i<=first;i++) printf(" ");

for(i=1;i<=days;i++){

printf("%3d ", i);

if((i+first)%7==0) printf("\n");

}

printf("\n===========================\n");

}

int CheckLeap(int year){

if(year%400==0 || (year%4==0 && year%100!=0) )

return 1;

else

return 0;

}

1 Answer

Rating
  • dd
    Lv 6
    1 decade ago
    Best Answer

    #include <stdio.h>#include <stdlib.h>void PrCal(int *, int); /*印年曆*/int CheckLeap(int); /*判別閏年,是傳回1,否傳回0*/int main(){   int date, year, i;   int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};   printf("Enter the year: ");   scanf("%d", &year);   month[1] += CheckLeap(year);    /*計算該年一月一日的星期*/   year--;   date = 36 + year + year/4 - year/100 + year/400;   date %= 7;   /*印出年曆*/   PrCal(month, date);   system("pause");   return 0;}void PrCal(int *m, int d){   int i, j, k;   /*k為每月一日的星期,先設為一月一日的星期*/   k = d;   for(i=0;i<12;i++) {      printf(" Sun Mon Tue Wed Thu Fri Sat\n");      printf("============================\n");      for(j=1;j<=k;j++) printf("    ");      for(j=1;j<=m[i];j++){         printf("%4d", j);         if((j+k)%7==0) printf("\n");         }      printf("\n============================\n");      /*該月過後,k改設為下個月一日的星期*/      k = (k + m[i]) % 7;   }}int CheckLeap(int y){   if(y%400==0||(y%4==0&&y%100!=0)) return 1;   else return 0;}

Still have questions? Get your answers by asking now.