回 帖 发 新 帖 刷新版面

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

[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个回复)

沙发

怎么每人给点建议啊?

板凳

大家看了给点建议啊!

3 楼

ch=getchar();

4 楼

fflush(stdin);不只这一处错!老大,确定没问题了再拿出来炫耀吧!

5 楼

#include"iostream.h"//给你我做的看看啊
class Firn{
    int year;
    int mounth;
    int day;

    int howday;
public:
    Firn(){howday=0;}
    int Isleap(int);
    int manyday();
    int Iswhich();
    void show();
    void menu();
    void set(int ,int);
    void set_one(int ,int,int);
};
void Firn::set(int y,int m){
    
    year=y;
    mounth=m;
    day=1;
}
void Firn::set_one(int y,int m,int d){
    year=y;
    mounth=m;
    day=d;
}
int Firn::Isleap(int year_1)
{    
    if(year_1%4==0&&year_1%100!=0)
        return 1;
    else
        if(year_1%400==0)
            return 1;
        return 0;
}
int Firn::manyday()
{    
    int t=0;
    switch(mounth)
    {
        case 1:
            t=0;howday=31;
            break;
        case 2:
            t=31;howday=28;
            break;
        case 3:
            t=31*2-3;howday=31;
            break;
        case 4:
            t=31*3-3;howday=30;
            break;        
        case 5:
            t=31*4-4;howday=31;
            break;
        case 6:
            t=31*5-4;howday=30;
            break;
        case 7:
            t=31*6-5;howday=31;
            break;
        case 8:
            t=31*7-5;howday=31;
            break;
        case 9:
            t=31*8-5;howday=30;
            break;
        case 10:
            t=31*9-6;howday=31;
            break;
        case 11:
            t=31*10-6;howday=30;
            break;
        case 12:
            t=31*11-7;howday=31;
            break;
    }
    t+=day;
    return t;
}
int Firn:: Iswhich()
{    
    int i,j,t=0;
    j=manyday()%7;
    for(i=2;i<=year;i++)
        if(Isleap(i))
        t++;
    j=j+year-1+t;
    j=j%7;
    return j;
}
void Firn::show()
{    
    cout<<"  日  一  二  三  四  五  六\n";
    int k=Iswhich();
    if(Isleap(year))
        howday++;
    int j=k;
    k=1;
    for(int i=0;i<j;i++)
        cout<<"    ";
    while(k<howday+1)
    {        
        
        cout.width(4);
        cout<<k;
        j++;k++;
        if(j%7==0)
            cout<<endl;
    
    }
    cout<<endl;
}
void Firn::menu(){
    
    int q;
    do{
        cout<<"                      * * * * * * * * * * * *\n";
        cout<<"                      *1  算你的生日是星期几*\n";
        cout<<"                      *2  万年历            *\n";
        cout<<"                      *0  离开              *\n";              
        cout<<"                      * * * * * * * * * * * *\n";
        cin>>q;
        switch(q){
        case 1:
            cout<<"请输入年 月 日\n";
            int x, y,z;
            cin>>x>>y>>z;
            set_one(x,y,z);
            if(Iswhich()){
                cout<<"\n                 * * * * * * * * *\n";
                cout<<"                 *你的生日是星期"<<Iswhich()<<"*\n";
                cout<<"                 * * * * * * * * *\n";
            }
            else{
                cout<<"\n                 * * * * * * * * *\n";
                cout<<"                 *你的生日是星期7*\n";
                cout<<"                 * * * * * * * * *\n";
            }
            break;
        case 2:
            cout<<"请输入年 月 \n";
            cin>>x>>y;
            set(x,y);
            show();
            break;
        }
    }
    while(q);
}
void main()
{    
    Firn D;
    D.menu();
}

6 楼

别吵了, 弄的老子性起, 整个Windows版的给你们瞧瞧

7 楼

楼主好不容易写出来的,摆出来晒晒也是人之常情!

8 楼

你运行了吗?
反正效率不会太高!!!!!!!!!!!!!![color=008000][/color]

9 楼

万年历好像少了四十二节气之类的。

10 楼

9楼的,是24节气。
还有,万年历至少还应该包含农历吧?否则我查哪天是春节都查不到。

我来回复

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