首页 > 代码库 > 万年历

万年历

万年历系统

#include <stdio.h>
#include <stdlib.h>

char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

int IsLeapYear(int year)  /*find out the year is leap year or not*/
{
    if((year%4==0&&year%100!=0)||(year%400==0)) 
        return 1;
    else
        return 0;
    
}
int month_day(int year,int month)            
{
    int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(IsLeapYear(year)&&month==2)            
        return 29;
    else
        return(mon_day[month-1]);
    
}
int DaySearch(int year,int month,int day) 
{
    int c=0;
    float s;
    int m;
    for(m=1;m<month;m++)
        c=c+month_day(year,m);            
    c=c+day;
    s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
    return ((int)s%7);                    
}

int PrintAllYear(int year)                  
{
    int temp;
    int i,j;
    printf("\n\n%d Calander\n",year);
    for(i=1;i<=12;i++)
    {
        printf("\n\n%s(%d)\n",month_str[i-1],i);   
        printf("---------------------------------\n");
        printf("Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");
        temp=DaySearch(year,i,1);                   
        for(j=1;j<=month_day(year,i)+temp;j++)     
        {
            if(j-temp<=0)
                printf("     ");
            else
                printf("%d   ",j-temp);
            
            if(j%7==0)
                printf("\n");
        }
    }
    return 0;
}

int main()
{
    int option,da;
    char ch;
    int year,month,day;
    printf("\n欢迎来到万年历系统!\n");
    
    while(1)
    {
        printf("\n请选择你需要的服务:\n");
        printf("\n1 搜索该天是星期几");
        printf("\n2 判断该年是否是闰年");
        printf("\n3 打印该年的日历");
        printf("\n4 退出\n");
        scanf("%d",&option);
        
        switch(option)
        {
            case 1:
                while(1)
                {
                    printf("\n请输入你要搜索的日期?(XXXX,XX,XX):");
                    scanf("%d,%d,%d,c",&year,&month,&day);
                    da=DaySearch(year,month,day);
                    printf("\n%d-%d-%d is %s,是否继续?(Y/N)",year,month,day,week[da]);
                    fflush(stdin);           
                    scanf("%c",&ch);
                    if(ch=='N'||ch=='n')
                        break;
                }
                break;
            case 2:
                while(1)
                {
                    printf("\n请输入你要搜索的年份?(XXXX)");
                    scanf("%d",&year);
                    if(IsLeapYear(year))
                        printf("\n%d 是闰年,是否继续?(Y/N)",year);
                    else
                        printf("\n%d 不是闰年,是否继续?(Y/N)?",year);
                    fflush(stdin);  
                    scanf("%c",&ch);                                 
                    if(ch=='N'||ch=='n') 
                        break; 
                } 
                break; 
            case 3: 
                while(1) 
                { 
                    printf("\n请输入你想打印的年份(XXXX)"); 
                    scanf("%d",&year); 
                    PrintAllYear(year); 
                    printf("\n是否继续(Y/N)?"); 
                    fflush(stdin);  
                    scanf("%c",&ch); 
                    if(ch=='N'||ch=='n') 
                        break; 
                } 
                break; 
            case 4: 
                    return 0;
            default: 
                printf("\nError:Sorry,there is no this service now!\n"); 
                break; 
        } 
        
    } 
    return 0; 
}


万年历