题目如下:
要求显示出单月的日历,用户说明这个月的天数和本月的起始日是星期几,即可输出该月的日历。
就是这么一道简单的程序,我憋了两天总算弄出来了,结果虽然达到了要求,但距离我预想的还有一段差距,先把程序
代码放在上面,希望大家多提修改意见。
/***********************************************************
* Name: date.c
* Purpose: Print the month day
* Author: zimo
* Date:01/25/2010
* *********************************************************/
#include<stdio.h>
int main(void)
{
int week, month, temp;
printf("Enter number of days in month:");
scanf(" %d", &month);
printf("Enter starting day of the week (1=sun,7=sat):");
scanf(" %d", &week);
if(month==28||month==29||month==30||month==31)
{
switch(week)
{
case 0:
printf("Please enter the correct weekdaty! /n");
break;
case 1:
for(temp = 1; temp <= month; temp++)
{
if(temp == 7 || temp == 14 ||
temp == 21 ||temp == 28)
printf("/n");
else
printf(" %3d",temp);
}
printf("/n");
break;
case 2:
printf(" ");
for(temp = 1; temp <= month; temp++)
{
if(temp == 6 || temp == 13 ||
temp == 20 || temp ==27)
printf("%3d /n",temp);
else
printf("%3d", temp);
}
printf("/n");
break;
case 3:
printf(" ");
for(temp = 1; temp <= month; temp++)
{
if(temp == 5 || temp == 12 ||
temp == 19 || temp == 26)
printf("%3d /n",temp);
else
printf("%3d",temp);
}
printf("/n");
break;
case 4:
printf(" ");
for(temp = 1; temp <= month; temp++)
{
if(temp == 4 || temp ==11 ||
temp ==18 || temp ==25)
printf("%3d /n",temp);
else
printf("%3d",temp);
}
printf("/n");
break;
case 5:
printf(" ");
for(temp = 1; temp <= month; temp++)
{
if(temp == 3 || temp == 10 ||
temp == 17 || temp ==24)
printf("%3d /n",temp);
else
printf("%3d",temp);
}
printf("/n");
break;
case 6:
printf(" ");
for(temp =1; temp <= month; temp++)
{
if(temp == 2 || temp == 9 ||
temp ==16 || temp ==23)
printf("%3d /n",temp);
else
printf("%3d", temp);
}
printf("/n");
break;
case 7:
printf(" ");
for(temp = 1; temp <= month; temp++)
{
if(temp == 1 || temp == 8 ||
temp == 15 || temp == 22)
printf("%3d /n",temp);
else
printf("%3d",temp);
}
printf("/n");
break;
default:
printf("Illegal enter!/n");
}
}
else
printf("ERROR!!!/n");
return 0;
}