用C语言实现万年历

要求双排输出 就是
左边是奇数月 右边是偶数月
一月 二月
三月 四月

~
阳历就可以= =

第1个回答  2010-12-08
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
long Year;
int date[12][6][7];
int day_tbl[ ][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}};//每月最后一天
char *str[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};

void nian(long year,FILE *fp)
{
int i,j,n=0,t;
char s[5][23];//把数字图形看作一个二维数组
void fyear(int t,int n,char s[5][23]);
for(i=0;i<5;i++){
for(j=0;j<23;j++){
if(((i==1)||(i==3))&&((j>=1&&j<=3)||(j>=7&&j<=9)||(j>=13&&j<=15)||(j>=19&&j<=21)))
s[i][j]=' ';
else if((j==5)||(j==11)||(j==17))
s[i][j]=' ';
else
s[i][j]='*';
}//判断‘*’的位置
}
do{
t=year%10;
year=year/10;
fyear(t,n,s);
n++;
}while(year!=0);
if(n<4){
for(i=0;i<5;i++){
for(j=0;j<24-6*n;j++)
s[i][j]=' ';
}
}
for(i=0;i<5;i++){
printf(" ");
fprintf(fp,"%s"," ");
for(j=0;j<23;j++){
printf("%c",s[i][j]);
fprintf(fp,"%c",s[i][j]);
}
printf("\n");
fprintf(fp,"\n");
}
}//用‘*’显示年份

void fyear(int t,int n,char s[5][23])
{
int i,j;
switch(t){
case 0:
for(j=19-6*n;j<22-6*n;j++)
s[2][j]=' ';
break;
case 1:
for(i=0;i<5;i++){
for(j=18-6*n;j<22-6*n;j++)
s[i][j]=' ';
}
break;
case 2:
s[1][18-6*n]=' ';
s[3][22-6*n]=' ';
break;
case 3:
s[1][18-6*n]=' ';
s[3][18-6*n]=' ';
break;
case 4:
for(j=19-6*n;j<22-6*n;j++){
s[0][j]=' ';
s[4][j]=' ';
}
s[3][18-6*n]=' ';
s[4][18-6*n]=' ';
break;
case 5:
s[1][22-6*n]=' ';
s[3][18-6*n]=' ';
break;
case 6:
s[1][22-6*n]=' ';
break;
case 7:
for(i=1;i<5;i++){
for(j=18-6*n;j<22-6*n;j++)
s[i][j]=' ';
}
break;
case 8:
break;
case 9:
s[3][18-6*n]=' ';
break;
}
}//判断图形数字里的空格
long int f(long year,int month)
{
if(month<3) return year-1;
else return year;
}

long int g(int month)
{
if(month<3) return month+13;
else return month+1;
}

long int n(int year,int month,int day)
{
return 1461L*f(year,month)/4+153L*g(month)/5+day;
}

int w(int year,int month,int day)
{
return(int)((n(year,month,day)%7-621049L%7+7)%7);
}//计算查询日期所处位置

void yueli(int m,FILE *fp)
{
struct tm *local;
long t;
long i,sw,wd,day,leap,k,j;
char title[]=" 日 一 二 三 四 五 六";
void rili(long year,FILE *fp);
time(&t);
local = localtime(&t);
local->tm_year = local->tm_year + 1900;
local->tm_mon ++;
Year=local->tm_year;
if(m==1){
printf("\t\t\t %s\n",str[local->tm_mon-1]);
fprintf(fp,"\t\t\t %s\n",str[local->tm_mon]);
printf("\t\t ___________________________\n");
fprintf(fp,"\t\t ___________________________\n");
printf("\t\t %s\n",title);
fprintf(fp,"\t\t %s\n",title);
sw=w(local->tm_year,local->tm_mon,1);
leap=local->tm_year%4==0&&local->tm_year%100||local->tm_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(wd=0,day=1;day<=day_tbl[leap][local->tm_mon-1];day++)
{
date[local->tm_mon][wd][sw]=day;
sw=++sw%7;//每星期七天,以0至6计数
if(sw==0) wd++;//日期表每七天一行,星期天开始新的一行
}
for(j=0;j<6;j++){
printf("\t\t ");
fprintf(fp,"\t\t ");
for(k=0;k<7;k++)
if(date[local->tm_mon][j][k]){
printf("%3d ",date[local->tm_mon][j][k]);
fprintf(fp,"%3d ",date[local->tm_mon][j][k]);
}
else{
printf(" ");
fprintf(fp,"%s"," ");
}
printf("\n");
fprintf(fp,"\n");
}
printf("\t\t 今天是: %ld年%ld月%ld日,",local->tm_year,local->tm_mon,local->tm_mday);
fprintf(fp,"\t\t 今天是: %ld年%ld月%ld日,",local->tm_year,local->tm_mon,local->tm_mday);
switch(local->tm_wday){
case 0:printf("星期日\n");fprintf(fp,"星期日\n");break;
case 1:printf("星期一\n");fprintf(fp,"星期一\n");break;
case 2:printf("星期二\n");fprintf(fp,"星期二\n");break;
case 3:printf("星期三\n");fprintf(fp,"星期三\n");break;
case 4:printf("星期四\n");fprintf(fp,"星期四\n");break;
case 5:printf("星期五\n");fprintf(fp,"星期五\n");break;
case 6:printf("星期六\n");fprintf(fp,"星期六\n");break;
default:printf("error\n");
}
printf(" \t\t 当前时间是: %ld时%ld分%ld秒\n",local->tm_hour,local->tm_min,local->tm_sec);
fprintf(fp," \t\t 当前时间是: %ld时%ld分%ld秒\n",local->tm_hour,local->tm_min,local->tm_sec);
}//显示当前系统时间
if(m==2)
rili(local->tm_year,fp);

}

void rili(long year,FILE *fp)
{ int sw,leap,i,j,k,wd,day;
char title[]=" 日 一 二 三 四 五 六";
sw=w(year,1,1);
leap=year%4==0&&year%100||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;//日期表置0
nian(year,fp);
for(i=0;i<12;i++)//一年十二个月
for(wd=0,day=1;day<=day_tbl[leap][i];day++)
{//将第i+1月的日期填入日期表
date[i][wd][sw]=day;
sw=++sw%7;//每星期七天,以0至6计数
if(sw==0) wd++;//日期表每七天一行,星期天开始新的一行
}
for(i=0;i<11;i+=2)
{//先测算第i+1月和第i+2月的最大星期数
// for(wd=0,k=0;k<7;k++)//日期表的第六行有日期,则wd!=0
// wd+=date[i][5][k]+date[i+2][5][k];
// wd=6;
printf("\t\t%s\t\t\t\t%s\n",str[i],str[i+1]);
fprintf(fp,"\t\t%s\t\t\t\t%s\n",str[i],str[i+1]);
printf(" ___________________________ ___________________________\n");
fprintf(fp," ___________________________ ___________________________\n");
printf(" %s %s \n",title,title);
fprintf(fp," %s %s \n",title,title);
for(j=0;j<6;j++)
{
printf(" ");//输出四个空白符
//左栏为第i+1月,右栏为第i+2月
fprintf(fp," ");
for(k=0;k<7;k++)
if(date[i][j][k]){
printf("%4d",date[i][j][k]);
fprintf(fp,"%4d",date[i][j][k]);
}
else{
printf(" ");
fprintf(fp," ");
}
printf(" ");//输出十个空白符
fprintf(fp," ");
for(k=0;k<7;k++)
if(date[i+1][j][k]){
printf("%4d",date[i+1][j][k]);
fprintf(fp,"%4d",date[i+1][j][k]);
}
else{
printf(" ");
fprintf(fp," ");
}
printf(" \n");
fprintf(fp," \n");
}
//键入回车输出下一行的日历
}
}
void main()
{
FILE *fp;
int choice;
if((fp=fopen("E:\\out1.txt","w"))==NULL){
printf("open error!\n");
exit(0);
}
yueli(1,fp);
while(1){
printf("欢迎使用本程序!请按提示选择\n");
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("按[1]查看当天信息和本月月历\n");
printf("按[2]查看当年日历\n");
printf("按[3]查看某年日历\n");
printf("按[4]查看下一年日历\n");
printf("按[5]查看上一年日历\n");
printf("按[0]退出\n");
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("输入你的选择并按回车结束:");
scanf("%d",&choice);
switch(choice){
case 1:yueli(1,fp);break;
case 2:yueli(2,fp);break;
case 3:
printf("请输入你要查询的年份: ");//输入年
scanf("%d",&Year);
rili(Year,fp);
break;
case 4:rili(++Year,fp);break;
case 5:rili(--Year,fp);break;
case 0:printf("感谢您的使用,再见!\n");return;
}
}
fclose(fp);
return;
}本回答被提问者采纳
第2个回答  2010-12-07
给你一个参考例子,你自己做才能学到知识,有什么问题可以给我留言或直接Hi我。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

// the use of setw
#include <iomanip>
using std::setw;

#define BEGINYEAR 2000 // the year i use to begin
#define BEGINDAY 6 // 2000`s first day
const int aiMounth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // everymounth`s days

// the fuction i call
int GetBeginDay( int, int ); // Get the first day of the mounth
int GetDays( int, int ); // Get the number of the days in a mounth
void PrintTitle(); // print the title
void PrintDate( int, int ); // print the date
int GetDaysOfaYear( int ); // get the number of days of a year

// the main begin here
int main()
{
int iYear, iMounth, iBeginDayOfaWeek, iDays;

// prompt and get the paramters i need
cout << "Please Input The Year Of The Date:" << endl;
cin >> iYear;
cout << "Please Input The Mounth Of The Date:" << endl;
cin >> iMounth;

// if Input a wrong num
if( iYear <= 0 || iMounth <= 0 || iMounth > 12 )
{
cout << "Input Error!" << endl;
return 0;
}

// do what you want me to do
iBeginDayOfaWeek = GetBeginDay( iYear, iMounth );
iDays = GetDays( iYear, iMounth );
PrintTitle();
PrintDate( iBeginDayOfaWeek, iDays );

// the fuction end here
return 0;
}

int GetBeginDay( int iYear, int iMounth )
{
int iResult;
int iDifference = iYear - BEGINYEAR; // i want to know if input if larger than my begin year
long lSumOfDays = 0;

if( iDifference >= 0 ) // if input is larger than my begin year
{
for( int i = BEGINYEAR; i < iYear; i++ )
lSumOfDays += GetDaysOfaYear( i );
for( int k = 1; k < iMounth; k++ )
if( 2 == k && ( ( iYear % 4 == 0 && iYear % 100 != 0 ) || iYear % 400 == 0 ) )
lSumOfDays += 29;
else
lSumOfDays += aiMounth[k-1];
iResult = ( int )( ( lSumOfDays + 6 ) % 7 );
} // end the if part of if/else
else // if input is smaller than my begin year
{
for( int j = iYear + 1; j < BEGINYEAR; j++ )
lSumOfDays += GetDaysOfaYear( j );
for( int m = iMounth; m <= 12 ; m++ )
if( 2 == m && ( ( iYear % 4 == 0 && iYear % 100 != 0 ) || iYear % 400 == 0 ) )
lSumOfDays += 29;
else
lSumOfDays += aiMounth[m-1];
iResult = ( int )( ( lSumOfDays + 1 ) % 7 );

// change the code
switch( iResult )
{
case 1:
iResult = 6;
break;
case 2:
iResult = 5;
break;
case 3:
iResult = 4;
break;
case 4:
iResult = 3;
break;
case 5:
iResult = 2;
break;
case 6:
iResult = 1;
break;
default:
break;
} // end switch inside
} // end else part of if/else

return iResult;
}

int GetDays( int iYear, int iMounth )
{
if( 2 == iMounth && ( ( iYear % 4 == 0 && iYear % 100 != 0 ) || iYear % 400 == 0 ) )
return 29; // if the year is a leap year
return aiMounth[ iMounth - 1 ]; // normal year
}

void PrintTitle()
{
cout << setw(10) << "Sunday" << setw(10) << "Monday" << setw(10) << "Tuesday"
<< setw(10) << "Wednesday" << setw(10) << "Thursday"
<< setw(10) << "Friday" << setw(10) << "Saturday" << endl;
}

void PrintDate( int iBeginDayOfaWeek, int iDays )
{
// format the output
for( int i = 0; i < iBeginDayOfaWeek; i++ )
cout << " ";

// out put the date
for( int i = 1; i <= iDays; i++ )
{
if( ( 0 == ( i + iBeginDayOfaWeek - 1 ) % 7 ) && i != 1 )
cout << endl; // if the day is Sunday
cout << setw(10) << i;
}
cout << endl;
}

int GetDaysOfaYear( int iYear )
{
if( ( iYear % 4 == 0 && iYear % 100 != 0 ) || iYear % 400 == 0 )
return 366; // if the year is a leap year
return 365; // normal year
}

// 先输入年份,然后输入月份,就可以得到该年该月的的日历了。本回答被网友采纳
第3个回答  2010-12-08
不可以,你所谓的阴历是指农历的话。农历是难以预测的