学校要交期末课程设计,已经下到可用的代码,但是算法不行不知道怎么加大年份,哪位高手能帮忙改改,农历公历互换的问题.

moheng 2007-11-06 02:09:11
以上的代码已经是在turboc 2.0 可以运行,但是年份只是在(1980.1.1 --- 2015.12.1),作业要求是在(1950.1.1---2050.12.1),哪们大侠能帮忙改改,100分不够再加.

#include<stdio.h>
#include<bios.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++;
}
}
...全文
96 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2007-11-07
  • 打赏
  • 举报
回复
已经 下 到代码,也有了 akirya 的提示,多少自己动动脑子吧。
moheng 2007-11-07
  • 打赏
  • 举报
回复
上面的代码还是不行
chlaws 2007-11-06
  • 打赏
  • 举报
回复
看着眼花
mLee79 2007-11-06
  • 打赏
  • 举报
回复
写的是相当的乱 ....



#include <stdio.h>

static const int g_lunarInfo[] = {
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
0x14b63 };
#define YEARS_N (sizeof(g_lunarInfo)/sizeof(g_lunarInfo[0]))
static int g_daysInfo[ YEARS_N + 1 ];
static void x_init ()
{
int i,j;
g_daysInfo [0] = 0;
for( i = 0; i < YEARS_N; ++i )
{
g_daysInfo[i+1] = g_daysInfo[i];
if(g_lunarInfo[i]&0xf)
g_daysInfo[i+1] += (g_lunarInfo[i]&0x10000)?30:29;
for(j=1;j<=12;++j)
g_daysInfo[i+1] += (g_lunarInfo[i]&(0x10000>>j))?30:29;
}
}
static int mk_days( int y , int m , int d )
{
static const int m_days[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
return y*365 + y/ 4 - y / 100 + y / 400 + m_days[m-1] + d -
((m<=2&&((y&3)==0) && ((y%100)!=0||y%400==0)));
}
#define DAYS_1900_ADJ ((693961) + (30))
int get_lunar( int y , int m , int d , int *oy , int *om , int *od , int* f )
{
int doff , b = 0, e = YEARS_N, x , i;
if( y < 1900 || y > 2050 || m < 1 || m > 12 || d < 1 || d > 31 )
return 0;
*f = 0;
doff = mk_days( y , m , d ) - DAYS_1900_ADJ;
while( b + 1 < e )
{
x = ( b + e ) / 2 ;
if( g_daysInfo[x] == doff )
return *oy = 1900 + x , *om = 1 , *od = 1;
else if( g_daysInfo[x] > doff )
e = x;
else
b = x;
}
doff -= g_daysInfo[b], *oy = 1900 + b;
for( i = 1; i <= 12; ++i )
{
d = g_lunarInfo[b]&(0x10000>>i)?30:29;
if( doff < d ) {
*om = i , *od = doff + 1;
return 1;
}
else
doff -= d;
if( (g_lunarInfo[b]&0xf) == i )
{
if( doff < ((g_lunarInfo[b]&0x10000)?30:29) )
{
*f = 1 , *om = i , *od = doff + 1;
return 1;
}
else
doff -= ((g_lunarInfo[b]&0x10000)?30:29);
}
}

return 1;
}


int main()
{
int y , m , d , f;
x_init();
while( (fflush(stdin),printf("input y m d : "),fflush(stdout),3==scanf("%d%d%d",&y,&m,&d) ) )
{
if( get_lunar( y , m , d , &y , &m , &d , &f ) )
printf( "%c %d-%d-%d\n" , " *"[f!=0] , y , m , d );
else
printf( "error\n" );
}

return 0;
}


moheng 2007-11-06
  • 打赏
  • 举报
回复
还是不明白,我就是不知道那个表怎么加,唉.看不懂算法.
lucian2007 2007-11-06
  • 打赏
  • 举报
回复
我想不用查表也应当可以吧!!!
不过你要研究一下农历的历法!!
公历我知道1年有365天,闰年多1天,2月29天;
就是1,3,5,7,8,10,12(31),4,6,9,11(30),2(28)
例如,公历:2007.11.06 转换农历显示:2007.09.27已知,求2008.08.08
下面的只是简单的举例,没有写实现:
ClassDate today=new ClassDate("2007.11.06");
ClassDate queryday=new ClassDate("2008.08.08");
if(queryday.isLeap()&&queryday.iMonth>2)
{
days+=1;
}
days+=(queryday.iYear-today.iYear)/4;
days+=(iqueryday.iYear-today.iYear)*356;
......
......
.....
要写的东西比较多,我是这么想的可能不是很好,望指教!!
  • 打赏
  • 举报
回复
你要知道你的表如何生成的就好
这里是万年历.
http://site.baidu.com/list/wannianli.htm
你看这个万年历对照吧.这是js版本的万年历
moheng 2007-11-06
  • 打赏
  • 举报
回复
有没有相关表的资料呢?
  • 打赏
  • 举报
回复
你看到你帖的那个表了么?
自己研究一下算法吧,公历->农历只能查表.然后补充一下表就可以了
moheng 2007-11-06
  • 打赏
  • 举报
回复
农历的显示方法和公历的格式一样就行了,比如公历:2007.11.06 转换农历显示:2007.09.27
  • 打赏
  • 举报
回复
这个能显示农历么?
moheng 2007-11-06
  • 打赏
  • 举报
回复
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 },
{1991,0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0 },
{1992,0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0 },
{1993,3, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1,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, 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;
}
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧