YXTS122 2016-01-17 00:52 采纳率: 100%
浏览 1607
已采纳

这万年历的代码错哪了?

 #include<iostream>
using namespace std;
#include<iomanip>
int daysofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int Isleap_year(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
        return 1;
    else
        return 0;
}
int get_days(int y,int m,int d)
{
    int i,count=d;  //公年元年1月1日是星期1
    for(i=1;i<y;i++)
    {
        if(Isleap_year(i))
            count++; 
    }
    count+=(365*(y-1));
    if(Isleap_year(y))
        daysofmonth[2]=29;
    for(i=1;i<m;i++)
        count+=daysofmonth[i];
    return count;
}
void printf_year()
{
    system("cls");//清屏 
    int sum,year,month;
    int a[10],b[10];
    cout<<"你想知道哪一年的年历呢?请输入:";
    cin>>year;
    cout<<"======================= The calender of year "<<year<<"========================"<<endl;
    int i=1;
    int j;
    for(i=1;i<=12/3;i++)//每一行显示三个月的月历
    {
        cout<<"----------------------------------------------------------------------------"<<endl;
        for(int h=1;h<=3;h++)
        {
            int m=3*(i-1)+h;
            cout<<"        "<<m<<"月          ";//月份名
        }
        cout<<endl<<"-----------------------------------------------------------------------------"<<endl;
        for( j=0;j<3;j++)
            cout<<"日"<<" 一"<<" 二"<<" 三"<<" 四"<<" 五"<<" 六 "<<"  ";
        cout<<endl;

        for( j=0;j<3;j++)
        {
            month=3*(i-1)+j+1;
            sum=get_days(year,month,1);
            sum=sum%7;
            for(int m=0;m<sum;m++)
                cout<<"   ";
            if(Isleap_year(year))
                daysofmonth[2]=29;
            b[j]=daysofmonth[month];
            for(int k=1;k<8-sum;)
            {
                cout<<setw(2)<<setfill(' ')<<k<<" ";
                k++;
                a[j]=k;
            }
            cout<<"  ";
        }
        cout<<endl;
        for(int w=1;w<=5;w++)
        {
            for(int j=0;j<3;j++)
            {
               int m;
               for(m=1;m<=7;m++,a[j]++)
               {
                   if(a[j]>b[j])
                       break;
                   cout<<setw(2)<<setfill(' ')<<a[j]<<" ";
               }
               for(int n=1;a[j]>b[j],n<=8-m+1;n++)
                   cout<<"  ";
               if(a[j]>b[j]&&m!=8)
                   cout<<"  ";
            }
            cout<<endl;
        }
    }
    cout<<"================================================="<<endl;
}
void main()
{
    printf_year();
    system("pause");
}

图片说明

  • 写回答

4条回答 默认 最新

  • ysuwood 2016-01-17 01:57
    关注

    修改:

                for(int w=1;w<=5;w++)
            {
                for(int j=0;j<3;j++)
                {
                    int m;
                    for(m=1;m<=7;m++,a[j]++)
                    {
                        if(a[j]>b[j]) cout<<"   ";
                        else cout<<setw(2)<<setfill(' ')<<a[j]<<" ";
                    }
                    cout<<"  ";
                }
                cout<<endl;
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • threenewbee 2016-01-17 01:35
    关注

    程序本身没大问题,就是输出的时候空格计算不对

    评论
  • threenewbee 2016-01-17 01:42
    关注

    仔细看了下,问题不少,参考下这个
    http://blog.sina.com.cn/s/blog_7745925a0100w2t1.html

    评论
  • 把分全给哥 2016-01-17 04:28
    关注
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 B660主板安装xp系统老是出现错误怎么办
  • ¥15 定义过的变量报错未定义
  • ¥15 如何在EXCEL某单元格中查找是否存在另一列中的内容。
  • ¥50 非对称加密(相关搜索:密码学)
  • ¥15 关于非编程寻迹小车的分析
  • ¥15 java导出EXCEL这里已经执行成功了,但是流浪器没有下载
  • ¥15 帮我把代码改的能正常运行就行
  • ¥50 有限元修正、模型修正、最小二乘法,详细步骤
  • ¥15 用plc编写梯形程序
  • ¥15 关于#物联网#的问题:新大陆AIOT中,按照手册教程进行添加,设置完成后网关一直不上线,显示Never这是网络服务的信息