user-0 提问: 年历输出问题

回复:
user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: 引用 1 楼 自信男孩的回复:void function2(int y,int m,int d) {     FILE*fp;     int date[24][21];     int day[]={31,28,31,30,31,30,31,31,30,31,30,31};     const char *month[12]={ "Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};     const char *wday[7]={"Sun","Mon","Tuc","Wed","Tur","Fri","Sat"};     int w[12];     int i,j,k,year,n;     printf("\n\t请输入查询年份：");     scanf("%d",&year);     for(i=0;i<24;i++)         for(j=0;j<21;j++)             date[i][j]=0;     //if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)     if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))         day[1]=29;     else         day[1]=28;     w[0]=getWeekDay(year,1,1); //这个是求出这一年第一个月是周几。     for(i=1;i<12;i++)         w[i]=(w[i-1]+day[i-1])%7;     for(j=0;j<12;j++)     {         n=0;         for(i=1;i<=day[j];i++)         {             date[(j/3)*6+n][(j%3)*7+w[j]]=i ;             w[j]++;             if(w[j]==7)             {w[j]=0; n++; }         }     }     fp = fopen("cal.txt","w");     //if(fopen("cal.txt","w") == NULL)     if (!fp)     {         printf("文件打不开");         return;     }     fprintf(fp,"\n***********************************%d",year);     fprintf(fp,"\n***********************************");     for(i=0;i<4;i++)     {         for(j=0;j<3;j++)         {             fprintf(fp,"%4s",month[i*3+j]);             fprintf(fp,"%28s"," ");         }         fprintf(fp,"\n");         for(j=0;j<3;j++)         {             for(k=0;k<7;k++)                 fprintf(fp,"%4s",wday[k]);             fprintf(fp," ");         }         fprintf(fp,"\n");         for(j=0;j<6;j++)         {             for(k=0;k<21;k++)             {                 if(date[i*6+j][k]==0)                     fprintf(fp," ");                 if(k==6||k==13)                     fprintf(fp," ");             }         }     } }  代码逻辑有些问题，一个地方是判断闰年，一个地方时文件打开（文件打开一次即可，没必要打开两次）。 一张是我的程序图，一张是我的运行结果图，格式乱了。最后一张是要求的格式。能帮我看一下我的输出格式哪里错了嘛？

user: user-0
content: 引用 2 楼 zhao_miao的回复:我只会一次输出一个月的https://blog.csdn.net/zhao_miao/article/details/80566106不知道对你有没有用。 一个月的比较简单，我会。这个是三个月一行，用到了数组。我的算法是对的，但是输入之后，格式乱了。

user: user-0
content: 引用 1 楼 自信男孩的回复:void function2(int y,int m,int d) {     FILE*fp;     int date[24][21];     int day[]={31,28,31,30,31,30,31,31,30,31,30,31};     const char *month[12]={ "Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};     const char *wday[7]={"Sun","Mon","Tuc","Wed","Tur","Fri","Sat"};     int w[12];     int i,j,k,year,n;     printf("\n\t请输入查询年份：");     scanf("%d",&year);     for(i=0;i<24;i++)         for(j=0;j<21;j++)             date[i][j]=0;     //if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)     if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))         day[1]=29;     else         day[1]=28;     w[0]=getWeekDay(year,1,1); //这个是求出这一年第一个月是周几。     for(i=1;i<12;i++)         w[i]=(w[i-1]+day[i-1])%7;     for(j=0;j<12;j++)     {         n=0;         for(i=1;i<=day[j];i++)         {             date[(j/3)*6+n][(j%3)*7+w[j]]=i ;             w[j]++;             if(w[j]==7)             {w[j]=0; n++; }         }     }     fp = fopen("cal.txt","w");     //if(fopen("cal.txt","w") == NULL)     if (!fp)     {         printf("文件打不开");         return;     }     fprintf(fp,"\n***********************************%d",year);     fprintf(fp,"\n***********************************");     for(i=0;i<4;i++)     {         for(j=0;j<3;j++)         {             fprintf(fp,"%4s",month[i*3+j]);             fprintf(fp,"%28s"," ");         }         fprintf(fp,"\n");         for(j=0;j<3;j++)         {             for(k=0;k<7;k++)                 fprintf(fp,"%4s",wday[k]);             fprintf(fp," ");         }         fprintf(fp,"\n");         for(j=0;j<6;j++)         {             for(k=0;k<21;k++)             {                 if(date[i*6+j][k]==0)                     fprintf(fp," ");                 if(k==6||k==13)                     fprintf(fp," ");             }         }     } }  代码逻辑有些问题，一个地方是判断闰年，一个地方时文件打开（文件打开一次即可，没必要打开两次）。 判断闰年那个是另外一个函数判断的，现在我的是输出格式不对，我给你看一下我整理完之后的代码吧。  每个月的日期输出格式出错了，我本来写了一个语句把数组中等于0的变为空格，但是那个语句好像并没有运行。    //功能二  void function2(int y,int m,int d) {FILE*fp;  int date[24][21]; int day[]={31,28,31,30,31,30,31,31,30,31,30,31}; char *month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};  char *wday[7]={"Sun","Mon","Tuc","Wed","Tur","Fri","Sat"}; int w[12]; int i,j,k,year,n;    printf("\n\t请输入查询年份："); scanf("%d",&year);  for(i=0;i<24;i++)  for(j=0;j<21;j++)  date[i][j]=0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) day[1]=29; else day[1]=28; w[0]=getWeekDay(year,1,1); for(i=1;i<12;i++) w[i]=(w[i-1]+day[i-1])%7; for(j=0;j<12;j++) {   n=0;        for(i=1;i<=day[j];i++)           {                   date[(j/3)*6+n][(j%3)*7+w[j]]=i ;                    w[j]++;                   if(w[j]==7)                    {w[j]=0; n++; }                                  }  }    fp=fopen("cal.txt","w"); if((fopen("cal.txt","w")==NULL)) {printf("文件打不开"); }  fprintf(fp,"\n***********************************%d",year); fprintf(fp,"***********************************\n"); for(i=0;i<4;i++) {       for(j=0;j<3;j++)        { 	      fprintf(fp,"%4s",month[i*3+j]); 	      fprintf(fp,"%28s"," "); 	             }            fprintf(fp,"\n");            for(j=0;j<3;j++)             {             	for(k=0;k<7;k++)             	fprintf(fp,"%4s",wday[k]);             	fprintf(fp," ");                }                fprintf(fp,"\n");                for(j=0;j<6;j++)                {                	for(k=0;k<21;k++)                	{                  	   if(date[i*6+j][k]==0)                	   fprintf(fp," ");                	   if(k==6||k==13)                	   fprintf(fp," ");                    fprintf(fp, “%2d”,date[i*6+j][k]);                   }                                      }                                                    } fclose(fp);  again(y,m,d); }

user: user-2
content: 我只会一次输出一个月的https://blog.csdn.net/zhao_miao/article/details/80566106不知道对你有没有用。

user: user-3
content: void function2(int y,int m,int d) {     FILE*fp;     int date[24][21];     int day[]={31,28,31,30,31,30,31,31,30,31,30,31};     const char *month[12]={ "Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};     const char *wday[7]={"Sun","Mon","Tuc","Wed","Tur","Fri","Sat"};     int w[12];     int i,j,k,year,n;     printf("\n\t请输入查询年份：");     scanf("%d",&year);     for(i=0;i<24;i++)         for(j=0;j<21;j++)             date[i][j]=0;     //if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)     if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))         day[1]=29;     else         day[1]=28;     w[0]=getWeekDay(year,1,1); //这个是求出这一年第一个月是周几。     for(i=1;i<12;i++)         w[i]=(w[i-1]+day[i-1])%7;     for(j=0;j<12;j++)     {         n=0;         for(i=1;i<=day[j];i++)         {             date[(j/3)*6+n][(j%3)*7+w[j]]=i ;             w[j]++;             if(w[j]==7)             {w[j]=0; n++; }         }     }     fp = fopen("cal.txt","w");     //if(fopen("cal.txt","w") == NULL)     if (!fp)     {         printf("文件打不开");         return;     }     fprintf(fp,"\n***********************************%d",year);     fprintf(fp,"\n***********************************");     for(i=0;i<4;i++)     {         for(j=0;j<3;j++)         {             fprintf(fp,"%4s",month[i*3+j]);             fprintf(fp,"%28s"," ");         }         fprintf(fp,"\n");         for(j=0;j<3;j++)         {             for(k=0;k<7;k++)                 fprintf(fp,"%4s",wday[k]);             fprintf(fp," ");         }         fprintf(fp,"\n");         for(j=0;j<6;j++)         {             for(k=0;k<21;k++)             {                 if(date[i*6+j][k]==0)                     fprintf(fp," ");                 if(k==6||k==13)                     fprintf(fp," ");             }         }     } }  代码逻辑有些问题，一个地方是判断闰年，一个地方时文件打开（文件打开一次即可，没必要打开两次）。