公历和农历之间如何转换?

#include<stdio.h> 
//#include<bios.h>
//bios.h是基本输入输入函数的头文件,在DOS下的TC或者BC都有该头文件; 
//在windows的VC6换成include<io.h>可以解决。

#include<io.h> 
struct data 
 { 
 int year1; 
 int specialmonth; 
 int month1[13]; 
 }; 
struct day{ 
 int year2; 
 int month2; 
 int day2; 
 }; 

int dis_day1(struct day *op,int *p) 
 { 
 int n=0,x; 
 n+=(op->year2-1980)*365; 
 for(x=1980;x<op->year2;x++) 
 if((x%4==0&&x%100!=0)||(x%100==0&&x%400==0))n+=1; 
 for(x=1;x<(op->month2);x++) 
 n+=*(p+x-1); 
 x=op->year2; 
 if(((x%4==0&&x%100!=0)||(x%100==0&&x%400==0))&&op->month2>2)n+=1; 
 n+=(op->day2-1); 
 return n; 

int dis_day2(struct day *op,struct data *p) 

 int n=0,maxmonth,i,x; 
 for(x=1980;x<op->year2;x++) 
 { if((p+x-1979)->specialmonth==0)maxmonth=12; 
 else maxmonth=13; 
 for(i=0;i<maxmonth;i++) 
 if((p+x-1979)->month1[i]==0)n+=29; 
 else n+=30; 
 } 
 if((p+op->year2-1979)->specialmonth!=0) 
 if((p+op->year2-1979)->specialmonth<op->month2)op->month2++; 
for(i=1;i<op->month2;i++) 
 { 
 if((p+op->year2-1979)->month1[i-1]==0)n+=29; 
 else n+=30;} 
 n+=(op->day2-1); 
 n+=46; 
return n; 

void work_out2(struct day *re,struct data *p,int n) 

 int n1=0,maxday,maxmonth,month,day,judge=0; 
 re->year2=1979;re->month2=11;re->day2=14; 
 while(1) 
 { 
 if(judge==1)break; 
 if(p->specialmonth==0)maxmonth=12; 
 else maxmonth=13; 
 if(p->year1==1979)month=12; 
 else month=1; 
 for(;month<=maxmonth;month++) 
 { 
 if(p->month1[month-1]==0)maxday=29; 
 else maxday=30; 
 if((p->year1==1979)&&(maxday==30))day=14; 
 else day=1; 
 for(;day<=maxday;day++) 
 { 
 if(n1==n){if(maxmonth==13&&month>p->specialmonth)month-=1; 
 re->year2=p->year1;re->month2=month;re->day2=day;judge=1;break;} 
else n1+=1; 
 } 


if(judge==1)break;} 
 p++; 
 } 


void work_out1(struct day *re,int n,int *p) 
 { 
 int year=1980,n1=0,month,day,maxday,judge=0; 
 re->year2=1980;re->month2=1;re->day2=1; 
 while(1) 
 { 
 *(p+1)=28; 
 if(judge==1)break; 
 if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))*(p+1)=29; 
 for(month=1;month<=12;month++) 
 { 
 maxday=*(p+month-1); 
 for(day=1;day<=maxday;day++) 
 { 

if(n1==n){re->year2=year;re->month2=month;re->day2=day;judge=1;break;} 
 else n1++;  
 } 

 if(judge==1)break;} 
 year++; 
 } 
 } 



main() 

 int n,c; 
 int sun[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 
 int *p1=sun; 
 struct day ob,re; struct data *p2; 
 struct data year[37]={ 
{1979,6, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 }, 
{1980,0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0 }, 
{1981,0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0 }, 
{1982,4, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1 }, 
{1983,0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 }, 
{1984,10,1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1 }, 
{1985,0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 }, 
{1986,0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0 }, 
{1987,6, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 }, 
{1988,0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0 }, 
{1989,0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }, 
{1990,5, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1 },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 0 }, 
{2012,4, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }, 
{2013,0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }, 
{2014,9, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 }, 
{2015,0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0 } 
}; 
 p2=year; 
 printf(" WARNING \n"); 
 printf(" you should take care of it :\n"); 
 printf(" the limted range:\n"); 
 printf(" lunar calendar: 1980.1.1----2015.11.12\n"); 
 printf(" soalr calendar: 1980.1.1-----2015.12.31\n"); 

while(1) 
 { printf(" please choose one way a/b?\n"); 
 printf(" a: soalr calendar---->lunar calendar\n"); 
 printf(" b: lunar calendar---->soalr calendar\n"); 
 do{c=getchar();}while(c!='a'&&c!='b'); 
 do{ 
 printf("your must pay attention to the form\n"); 
 printf("pleae input your date:\n"); 
 scanf("%d.%d.%d",&ob.year2,&ob.month2,&ob.day2); 
 }while(ob.year2<1980||ob.year2>=2016||ob.month2<1|| 
 ob.month2>12||ob.day2<1||ob.day2>31); 
 switch(c)
 { 
 case 'a': 

 n=dis_day1(&ob, p1);printf("%d\n",n); 
 work_out2(&re,p2,n); 
 printf(" the result is %d.%d.%d\n",re.year2,re.month2,re.day2);break; 
 case 'b': 

 n=dis_day2(&ob,p2);printf("%d\n",n); 
 work_out1(&re,n,p1); 
 printf(" the result is %d.%d.%d\n",re.year2,re.month2,re.day2); 
 if(ob.month2==(p2+ob.year2-1979)->specialmonth) 
 { if((ob.day2!=30)||((p2+ob.year2-1979)->month1[ob.month2]==1)) 
 { n+=((p2+ob.year2-1979)->month1[ob.month2-1]+29); 
 work_out1(&re,n,p1); 
printf("there is a other result\n:\t%d.%d.%d\n",re.year2,re.month2,re.day2); 
 } 
 } 

 break; 
 defluat: break; 
 } 
 printf("if you want to try again,y/n?\n"); 
 do{c=bioskey(0);}while(c!=0x316e&&c!=0x1579);if(c==0x316e)break; 
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值