万年历 字符不对齐问题
#include<stdio.h>int main()
{
int firstday(int year);
int sw,leap,i,j,k,wd,day;
int date[12][6][7]; //定义一个三维数组 分别对应 月、周、日
char title[] = "SUN MON TUE WED THU FRI SAT";
int day_tab[][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
}; //平年和闰年的月份天数
int year;
printf("please input the year:");
scanf("%d",&year);
sw = firstday(year); //计算这一年的第一天是星期几
leap = (year%4 ==0 && year%100 != 0) || year%400 ==0;
for( i=0; i<12; i++ )
for( j=0; j<6; j++)
for( k=0; k<7; k++)
date[i][j][k] = 0; //将所有的日期都归零
for( i=0; i<12; i++)
for( wd=0,day=1; day<=day_tab[leap][i]; day++)
{
date[i][wd][sw] = day;
sw = ++sw%7;
if(sw == 0)
wd++;
}
printf("\n==================The Calendar of Year %d=======================\n ",year);
for( i=0; i<6; i++)
{
for(wd=0,k=0;k<7;k++) //将同一行的天数相加
wd = wd + date[i][5][k] + date[i+6][5][k];
wd=wd>0? 6:5;
printf("%2d %s %2d %s \n",i+1,title,i+7,title);
for(j=0;j<wd;j++)
{
printf(" ");
for( k=0; k<7; k++)
if(date[i][j][k])
printf("%4d",date[i][j][k]);
else printf(" ");
printf(" ");
for( k=0; k<7; k++)
if(date[i+6][j][k])
printf("%4d",date[i+6][j][k]);
else printf(" ");
printf(" \n");
}
}
puts("==================================================================");
return 0;
}
/*计算该年份第一天是星期几*/
int firstday(int year)
{
int leap(int ye);
int i;
long int n;
n = year*365;
for(i = 1; i < year; i++)
if(leap(i))
n += 1;
return (n%7);
}
int leap(int ye)
{
if( (ye%4 ==0 && ye%100 != 0) || ye%400 ==0 )
return 1;
else
return 0;
}
程序结果字符总是不对齐 哪边出错了呢