26. Use the while loop to print out the calendar

/*

*/
#include "stdio.h"
void main()
{
int y,m,week,sumdays=0,maxdays;
int n;
int i;

n=0;
printf("Please enter the number of years:");
yy: n=scanf("%d",&y);
if(n!=1||y<1900)
{
printf("The year input is incorrect, please retype:");
fflush(stdin);
goto yy;
}

n =0;
printf("Please enter the month number:");
mm:n=scanf("%d",&m);
if(n!=1||m<1||m>12)
{
printf("Month The input is incorrect, please retype: ");
fflush(stdin);
goto mm;
}

//The following loop solves the total number of days in a complete year
i=1900;
while(i<=y-1)
{
if(i %4==0&&i%100!=0||i%400==0)
sumdays+=366;
else
sumdays+= 365;
i++;
}
printf("%d",sumdays);

//The following loop solves the total number of days in a full month
i=1;
while(i<=m-1)
{
switch(i)
{
case 1:case 3 :case 5:case 7:case 8:case 10: case 12:
maxdays=31;break;
case 4:case 6:case 9:case 11:
maxdays=30;break;
case 2:
if(y%4== 0&&y%100!=0||y%400==0)
maxdays=29;
else
maxdays=28;
break;
}
sumdays+=maxdays;
i++;
}

//The next sentence puts the 1st of the month of the year we entered Add to the total number of days
sumdays+=1;

//Find out the day of the week on the 1st of the month in the input year
week=sumdays%7;

printf("\n\t\t%d The monthly calendar of year %d is as follows: \n\n",y,m);
printf("Day\tOne\tTwo\tThree\tFour\tFive\tSix\n");

//Output the same number of spaces as the week number
i=1;
while(i<=week)
{
printf("\t");
i++;
}

//First calculate the maximum number of days in the month you entered
switch(m)
{
case 1:case 3:case 5: case 7:case 8:case 10: case 12:
maxdays=31;break;
case 4:case 6:case 9:case 11:
maxdays=30;break;
case 2:
if(y%4==0&&y%100! =0||y%400==0)
maxdays=29;
else
maxdays=28;
break;
}

//Print out the maximum number of days from the 1st to the current month
i=1;
while(i<=maxdays)
{
if(i< 10)
printf("0%d\t",i);
else
printf("%d\t",i);
if((i+week)%7==0)
printf("\n");
i++ ;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811986&siteId=291194637