CSDN博客

img bohut

c语言写的万年历

发表于2004/10/26 10:07:00  2190人阅读

分类: WTL/ATL/STL

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define X " Sun Mon Tue Wed Thu Fri Sat"
#define P printf
#define B break
#define FP fprintf

void print(int n)
{
 int i;
 for(i=0;i<n;i++) printf(" ");
}
int day(int year)
{
 long a,b;
 if(year<=2000)
 {
  a=2000-year;b=6-(a+a/4-a/100+a/400)%7; return b;
 }
 else
 {
  a=year-2000; b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6; return b%7;
 }
}
void main()
{
 int i,j,k,m,n,f1,f2,year,d;
 int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 char fname[12];  FILE *fw;
 system("cls");
 P(" Input the year:");
 scanf("%d",&year);
 P(" Input the file name:");
 scanf("%s",fname); P("/n");
 fw=fopen(fname,"w+");
 if(fw==NULL)
 {
  P(" can not open file."); return;
 }
 P(" The calendar of the year %d./n",year);
 FP(fw," The calendar of the year %d./n",year);
 d=day(year);
 if(year%4==0&&year%100||year%400==0) a[2]++;
 for(i=1;i<=12;i+=2)
 {
  m=0; n=0; f1=0; f2=0;
  switch(i)
  {
  case 1:P(" Januray 1   "); FP(fw," Januray 1   "); B;
  case 3:P(" March 3     "); FP(fw," March 3     "); B;
  case 5:P(" May 5       "); FP(fw," May 5       "); B;
  case 7:P(" July 7     ");  FP(fw," July 7      "); B;
  case 9:P(" September 9 "); FP(fw," September 9 "); B;
  case 11:P(" Nevember 11 "); FP(fw," Nevember 11 "); B;
  }
  print(21);
  for(k=0;k<21;k++) fprintf(fw," ");
  switch(i+1)
  {
  case 2:P(" February 2  "); FP(fw," February 2  ");B;
  case 4:P(" April 4    "); FP(fw," April 4     ");B;
  case 6:P(" June 6      "); FP(fw," June 6       ");B;
  case 8:P(" August 8   "); FP(fw," August 8   "); B;
  case 10:P(" October 10  "); FP(fw," October 10  ");B;
  case 12:P(" December 12"); FP(fw," December 12");B;
  }
  P("/n");
  fprintf(fw,"/n");
  P(X); fprintf(fw,X); print(6);
  fprintf(fw,"      "); P(X); fprintf(fw,X);
  fprintf(fw,"/n"); P("/n");
  for(j=0;j<6;j++)
  {
   if(j==0)
   {
    print(d*4); for(k=0;k<d*4;k++) fprintf(fw," ");
    for(k=0;k<7-d;k++)
    {
     P("%4d",++m); fprintf(fw,"%4d",m);
    }
    print(6); fprintf(fw,"      ");
    d+=a[i]%7; d%=7;
    print(d*4); for(k=0;k<d*4;k++) fprintf(fw," ");
    for(k=0;k<7-d;k++)
    {
     P("%4d",++n); fprintf(fw,"%4d",n);
    }
    P("/n"); fprintf(fw,"/n");
   }
   else
   {
    for(k=0;k<7;k++)
    {
     if(m<a[i])
     {
      P("%4d",++m); fprintf(fw,"%4d",m);}
     else
     {
      print(4);   fprintf(fw,"    ");}
     if(m==a[i]) f1=1;
    }
    print(6); fprintf(fw,"      ");
    for(k=0;k<7;k++)
    {
     if(n<a[i+1])
     {
      P("%4d",++n);fprintf(fw,"%4d",n);}
     else
     {
      print(4);  fprintf(fw,"    ");}
     if(n==a[i+1]) f2=1;
    }
    P("/n"); fprintf(fw,"/n");
    if(f1&&f2) B;
   }
  }
  d+=a[i+1]%7; d%=7; P(" ");  fprintf(fw," ");
  for(k=0;k<27;k++) {P("="); fprintf(fw,"=");}
  print(6);fprintf(fw,"      ");    P(" ");  fprintf(fw," ");
  for(k=0;k<27;k++) {P("=");fprintf(fw,"=");}
  P("/n"); fprintf(fw,"/n");
  if(i==5)
  {
   getch();
   system("cls");
  }
 }
 fclose(fw);
 getch();
}

阅读全文
0 0

相关文章推荐

img
取 消
img