c语言 日历问题

时间:2021-05-19 09:17:38
#include "stdio.h"
int isleapyear(int year)
{
if((year%4!=0||year%100==0) && (year%400!=0) )
return 0;
else
return 1;
}
int firstdayofyear(int year)
{
return (year+(year-1)/4+(year-1)/400-(year-1)/100)%7;
}
main()
{
char *monthname[13]={" ","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int numday_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int firstday_month[13];
int workarray[13];
int year;
int i,m,n;
printf("输入待处理的年份:");
scanf("%d",&year);

if(isleapyear(year))
numday_month[2]=29;

firstday_month[1]=firstdayofyear(year);
for(i=2;i<=12;i++)
firstday_month[i]=(firstday_month[i-1]+numday_month[i-1])%7;
for(i=1;i<=12;i++)
workarray[i]=-firstday_month[i];

printf("%34s"," ");
printf("year %d\n",year);
for(i=1;i<=12;i++)
{
printf("\n");
printf("%41s",monthname[i]);
printf("%22s"," ");
printf("星期日 星期一 星期二 星期三 星期四 星期五 星期六");
for(m=1;m<=6;m++)
{
printf("%18s"," ");
for(m=1;m<=7;m++)
{
workarray[i]++;
if(workarray[i]>0&&workarray[i]<=numday_month[i])
printf("%6d",workarray[i]);
else
printf("%6d"," ");
}
printf("\n");
}
}
}

怎么输出的日历这么乱啊?
那位高手来改下啊!谢谢!

7 个解决方案

#1


刚好手里有一个,但不是我写的,出处也不记得了直接粘贴给你...


int i,j,m,n,k;
   int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
   float y,m1,k1;
   printf("欢迎你使用万年历,请输入你要查询的年数:");
   while(scanf("%f",&y))
   {n=(int)((y-1)/3324);
   m1=(int)((y-1)/100);
   m=(int)(m1*3/4+0.85);
   k1=(int)(y/4*5-0.15)-m-n;
   k=(int)((k1/7-(int)(k1/7))*7+0.5);
   printf("  公元%4.0f年日历\n",y);
   for(i=1;i<=12;i++)
   {
      n=a[i-1];
   if(i==2&&y==(int)(y/4)*4)
   {
      n=29;
   if((y==((int)(y/100)*100&&y!=(int)(y/400)*400)*400)||y==(int)(y/3324)*3324)
    n=28;
   }
   printf("          %d   月\n\n\n",i);
   printf("----------------------------\n");
   printf("  日  一  二  三  四  五  六 \n");
   for(j=1;j<=k;j++)
    printf("    ");
   for(j=1;j<=n;j++)
   {
      printf("%4d",j);
   k++;
   if(k>6)
   {
      printf("\n");
   k=0;
   }
   }
   if(k1!=0||i==2)
    printf("\n");
   printf("----------------------------\n\n\n");
   }
  }

#2


哪位高手来指点一下啊

#3


用绝对时间来算很简单 

#4



#include "stdio.h"
int isleapyear(int year)
{
if((year%4!=0||year%100==0) && (year%400!=0) )
return 0;
else
return 1;
}
int firstdayofyear(int year)
{
return (year+(year-1)/4+(year-1)/400-(year-1)/100)%7;
}
void main()
{
char *monthname[13]={" ","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int numday_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int firstday_month[13];
int workarray[13];
int year;
int i,m,n;
printf("输入待处理的年份:");
scanf("%d",&year);

if(isleapyear(year))
numday_month[2]=29;

firstday_month[1]=firstdayofyear(year);
for(i=2;i<=12;i++)
firstday_month[i]=(firstday_month[i-1]+numday_month[i-1])%7;
for(i=1;i<=12;i++)
workarray[i]=-firstday_month[i];

// printf("%34s"," ");
printf("year %d\n",year);
for(i=1;i<=12;i++)
{
printf("\n");
printf("%s\n\n",monthname[i]);
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
for(m=1;m<=6;m++)
{
// printf("%18s\t"," ");
for(n=1;n<=7;n++)
{
workarray[i]++;
if(workarray[i]>0&&workarray[i]<=numday_month[i])
printf("%d\t",workarray[i]);
else
printf("%\t");
}
printf("\n");
}
}
}

楼主的思想基本上没什么错误,只是个人觉得在对齐方面用\t更好一点,对你的程序空格的地方做了小小的修改

#5


该回复于2011-05-04 09:07:29被版主删除

#6



#include <stdio.h>
#include <time.h>
#define N 12
#define M 10

int isleap(int y)
{
    return( (y%4==0&&y%100!=0) || y%400==0 );
}

int per_year_day_week(int y)
{
    return( (y+y/4-y/100+y/400)%7 );
}

int per_month(int y,int m)
{
    int month_every_day[][N]={{31,28,31,30,31,30,31,31,30,31,30,31},
                            {31,29,31,30,31,30,31,31,30,31,30,31}};
    return month_every_day[isleap(y)][m-1];
}

char * eglish_month(int i)
{
    char *str[N] = {"January","February","March","Aril","May","June",
            "July","August","September","October","November","December"};
    return str[i];
}

void main()
{
int i,day,year,weekday;
char t[20];
_strdate(t);
printf("当前日期:%s\n",t);
_strtime(t);
printf("当前时间:%s\n",t);
printf("Please input a year:\n");
    scanf("%d",&year);
    printf("\n\t\t%d年日历如下:\n",year);
    weekday = per_year_day_week(year);
    for (i=1;i<=12;i++)
    {
        char * strmonth = eglish_month(i-1);
        printf("\n%s-%d",strmonth,year);
        printf("\n---------------------------------------------------\n");
        printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
        for (day=0;day<weekday;day++)
        {
            printf("\t");
        }
        for (day=1;day<=per_month(year,i);day++)
        {
            printf("%d\t",day);
            weekday = (weekday+1)%7;
            if(weekday==0) printf("\n");
        }
        printf("\n---------------------------------------------------\n");
    }
}

#7


震惊了我~

#1


刚好手里有一个,但不是我写的,出处也不记得了直接粘贴给你...


int i,j,m,n,k;
   int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
   float y,m1,k1;
   printf("欢迎你使用万年历,请输入你要查询的年数:");
   while(scanf("%f",&y))
   {n=(int)((y-1)/3324);
   m1=(int)((y-1)/100);
   m=(int)(m1*3/4+0.85);
   k1=(int)(y/4*5-0.15)-m-n;
   k=(int)((k1/7-(int)(k1/7))*7+0.5);
   printf("  公元%4.0f年日历\n",y);
   for(i=1;i<=12;i++)
   {
      n=a[i-1];
   if(i==2&&y==(int)(y/4)*4)
   {
      n=29;
   if((y==((int)(y/100)*100&&y!=(int)(y/400)*400)*400)||y==(int)(y/3324)*3324)
    n=28;
   }
   printf("          %d   月\n\n\n",i);
   printf("----------------------------\n");
   printf("  日  一  二  三  四  五  六 \n");
   for(j=1;j<=k;j++)
    printf("    ");
   for(j=1;j<=n;j++)
   {
      printf("%4d",j);
   k++;
   if(k>6)
   {
      printf("\n");
   k=0;
   }
   }
   if(k1!=0||i==2)
    printf("\n");
   printf("----------------------------\n\n\n");
   }
  }

#2


哪位高手来指点一下啊

#3


用绝对时间来算很简单 

#4



#include "stdio.h"
int isleapyear(int year)
{
if((year%4!=0||year%100==0) && (year%400!=0) )
return 0;
else
return 1;
}
int firstdayofyear(int year)
{
return (year+(year-1)/4+(year-1)/400-(year-1)/100)%7;
}
void main()
{
char *monthname[13]={" ","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int numday_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int firstday_month[13];
int workarray[13];
int year;
int i,m,n;
printf("输入待处理的年份:");
scanf("%d",&year);

if(isleapyear(year))
numday_month[2]=29;

firstday_month[1]=firstdayofyear(year);
for(i=2;i<=12;i++)
firstday_month[i]=(firstday_month[i-1]+numday_month[i-1])%7;
for(i=1;i<=12;i++)
workarray[i]=-firstday_month[i];

// printf("%34s"," ");
printf("year %d\n",year);
for(i=1;i<=12;i++)
{
printf("\n");
printf("%s\n\n",monthname[i]);
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
for(m=1;m<=6;m++)
{
// printf("%18s\t"," ");
for(n=1;n<=7;n++)
{
workarray[i]++;
if(workarray[i]>0&&workarray[i]<=numday_month[i])
printf("%d\t",workarray[i]);
else
printf("%\t");
}
printf("\n");
}
}
}

楼主的思想基本上没什么错误,只是个人觉得在对齐方面用\t更好一点,对你的程序空格的地方做了小小的修改

#5


该回复于2011-05-04 09:07:29被版主删除

#6



#include <stdio.h>
#include <time.h>
#define N 12
#define M 10

int isleap(int y)
{
    return( (y%4==0&&y%100!=0) || y%400==0 );
}

int per_year_day_week(int y)
{
    return( (y+y/4-y/100+y/400)%7 );
}

int per_month(int y,int m)
{
    int month_every_day[][N]={{31,28,31,30,31,30,31,31,30,31,30,31},
                            {31,29,31,30,31,30,31,31,30,31,30,31}};
    return month_every_day[isleap(y)][m-1];
}

char * eglish_month(int i)
{
    char *str[N] = {"January","February","March","Aril","May","June",
            "July","August","September","October","November","December"};
    return str[i];
}

void main()
{
int i,day,year,weekday;
char t[20];
_strdate(t);
printf("当前日期:%s\n",t);
_strtime(t);
printf("当前时间:%s\n",t);
printf("Please input a year:\n");
    scanf("%d",&year);
    printf("\n\t\t%d年日历如下:\n",year);
    weekday = per_year_day_week(year);
    for (i=1;i<=12;i++)
    {
        char * strmonth = eglish_month(i-1);
        printf("\n%s-%d",strmonth,year);
        printf("\n---------------------------------------------------\n");
        printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
        for (day=0;day<weekday;day++)
        {
            printf("\t");
        }
        for (day=1;day<=per_month(year,i);day++)
        {
            printf("%d\t",day);
            weekday = (weekday+1)%7;
            if(weekday==0) printf("\n");
        }
        printf("\n---------------------------------------------------\n");
    }
}

#7


震惊了我~