关闭

一个日历小程序.

806人阅读 评论(0) 收藏 举报


#include<stdio.h>
#define Sunday 0
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6
void GiveInstructions(void);/*输出介绍*/
int GetYearFromUser(void);/*用户输入年份*/
void PrintCalendar(int year);
void PrintCalendarMonth(int month,int year);
void IndentFirstLine(int weekday);
int MonthDays(int month,int year);
int FirstDayOfMonth(int month,int year);/*月的第一天*/
char* MonthName(int month);/*月份的名称*/
int IsLeapYear(int year);
int IsLeapYear(int year)
 {if(((year%4==0)&&(year%100!=0))||(year%400==0))
    return(1);
    else return(0);
 }


/*main program*/
void main()
{
    int year;
    GiveInstructions();
    year=GetYearFromUser();
    PrintCalendar(year);
    getch();
    }

void GiveInstructions(void)
{
printf("This program displays a calender for a null/n");
printf("year,the year must not before 1900/n");
 }
 int GetYearFromUser(void)
 {int year;
    while(1){
        printf("which year?");
        scanf("%d",&year);
        if(year>=1900) return(year);
        printf("the  year must be at least 1900/n");
        }
   }
void PrintCalendar(int year)
{
int month;
    for(month=1;month<=12;month++){
            PrintCalendarMonth(month,year);
            printf("/n");
            }
   }

void PrintCalendarMonth(int month,int year)
{
int weekday,ndays,day;
printf("     %s,   %d/n/n",MonthName(month),year);
printf(" Su Mo Tu We Th Fr Sa /n");
ndays=MonthDays(month,year);
weekday=FirstDayOfMonth(month,year);
IndentFirstLine(weekday);
    for(day=1;day<=ndays;day++){
        printf("%3d",day);
        if(weekday==Saturday)printf("/n");
        weekday=(weekday+1)%7;
        }
       if(weekday!=Sunday)printf("/n");
    }
void IndentFirstLine(int weekday)
{int i;
    for(i=0;i<weekday;i++){
    printf("   ");
    }
 }
int MonthDays(int month,int year)
{switch(month){
    case 2:
        if(IsLeapYear(year)) return(29);
        return(28);

    case 4:case 6: case 9: case 11:
        return(30);
    default: return(31);
}
 }
int FirstDayOfMonth(int month,int year)
{int weekday,i;
    weekday=Monday;
        for(i=1900;i<year;i++)
        {weekday=(weekday+365)%7;
       if(IsLeapYear(i))weekday=(weekday+1)%7;
           }
      for(i=1;i<month;i++){
        weekday=(weekday+MonthDays(i,year))%7;
        }
        return(weekday);
        }
char* MonthName(int month)
{switch(month){
    case 1:return("Januray");
    case 2:return("Februay");
    case 3:return("Marth");
    case 4:return("April");
    case 5:return("May");
    case 6:return("June");
    case 7:return("July");
    case 8:return("Augest");
    case 9:return("September");
    case 10:return("October");
    case 11:return("November");
    case 12:return("December");
    default:return("illegal month");
    }
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20435次
    • 积分:342
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:5篇
    • 译文:0篇
    • 评论:6条
    最新评论