回 帖 发 新 帖 刷新版面

主题:[讨论]我刚做的万年历,大家给点建议!

[em4]
#include"stdio.h"
#include"conio.h"
int caculate(int);
void print(int,int,int,long);
int leap(int);
int le[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};              /*存储闰年月份信息的全局数组*/
int ping[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};            /*存储平年信息的全局数组*/
main()
{       int year,month;
        int i,j;
        long sum=0;
        int flag;
        char ch;
        int u=1;
        int p=1;
        printf("if you want to continue print y/Y\n");
        ch=getch();
        while(ch=='Y'||ch=='y')                                   /*利用while()循环实现程序的循环执行*/
        {       flush();                                          /*由用户给出结束标志*/
                p=1;
                u=1;
                while(p)
                {       printf("input the year(year>0):");
                        scanf("%d",&year);
                        if(year>0)
                        p=0;
                        else
                        printf("the input is wrong!\n");
                }
                while(u)
                {       printf("input the month(1--12):");
                        scanf("%d",&month);
                        if(month>=1&&month<=12)
                        u=0;
                        else
                        printf("the input is wrong!\n");
                }
                for(i=1;i<year;i++)
                {       flag=leap(i);                                   /*判断某年是否是闰年,确定加减基数*/
                        sum=sum+caculate(flag);                         /*求和改年之前的天数*/

                }                                                       /*判断给定年份是否是闰年*/
                flag=leap(year);
                printf("the total days before this year is:%ld\n",sum); /*输出改年之前的所有天数*/
                print(year,flag,month,sum);                             /*输出改年改月信息*/
                printf("if you want to continue print y/Y else print n/N\n");
                ch=getch();
         }
         exit(0);

}
int leap(int y)                                                            /*判断是否是闰年函数*/
{       int f;
        if((y%4==0&&y%100!=0)||(y%400==0))
        {       f=1;
                return(f);
        }
        else
        {       f=0;
                return(f);
        }                                                                   /*计算该年之前的天数*/
}
int caculate(int f)
{       int i,j,ds;

           if(f==1)
           {       for(j=1;j<13;j++)
                   ds=ds+le[j];
           }
           else
           {       for(j=1;j<13;j++)
                   ds=ds+ping[j];
           }

        return(ds);
}
void print(int y,int f,int m,long s)                                           /*完成输出月历功能*/
{       int i,j;
        int day=0;
        int volum;
        printf("the cal of the %d month of %d year's is shown:\n",m,y);
        printf("**********************************************\n");
        printf("  Sun  Mon  Tue  Wed  Tir  Fri  Sat\n");                       /*打印首行格式*/
        if(f==1)
        {
                for(i=1;i<m;i++)
                {       day=day+le[i];                                         /*计算闰年给定月份之前的天数*/

                }
                day=day+s;                                                    /*加上原来的计算结果:该年前的天数*/
                volum=(6+day)%7;                                              /*利用以上的总天数来计算改月的显示格式*/
                                                                              /*也就是说要计算该月中1号出现的位置*/
                                                                              /*具体的就是计算应该打印的空格数*/
        }
        else
        {       for(i=1;i<m;i++)                                               /*平年的处理和润年相似,不同之处仅是2月份的天数不同*/

                {       day=day+ping[i];
                }
                day=day+s;
                volum=(6+day)%7;

        }
        for(j=0;j<5*volum;j++)                                                  /*根据前面计算的空格数进行该月的月历输出*/
        printf(" ");
        if(f==0)
    {    for(i=1;i<=ping[m];i++)
        {       printf("%5d",  i);
            if((i+volum)%7==0)
            printf("\n");
        }
    }
    if(f==1)
    {    for(i=1;i<=le[m];i++)
        {    printf("%5d",  i);
            if((i+volum)%7==0)
            printf("\n");
        }
    }
        printf("\n");
}

回复列表 (共12个回复)

11 楼

哈哈,我的玩笑一点也不好笑.

12 楼

言之有理啊.

我来回复

您尚未登录,请登录后再回复。点此登录或注册