我先上传两个程序:
第一個:
/*通过输入当天的日期算出第二天的日期*/
#include "Stdio.h"
struct date
{ int day;
int month;
int year;
};
int main(void) //主程序
{
struct date today,tomorrow;
int chazhao(struct date d);
int ruinian(struct date x);
struct date update(struct date d);
printf ("pleae input today's date(mm/dd/yyyy): ");
scanf ("%i%i%i",&today.month,&today.day,&today.year);
tomorrow=update(today);
printf ("next date is %i/%i/%i\n",tomorrow.month,tomorrow.day,tomorrow.year);
getch();
}
int chazhao(struct date d) //查找月分的天数
{ const int endday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if ( ruinian(d)==1&&d.month==2)
days=29;
else
days=endday[d.month-1];
return days;
}
int ruinian(struct date x) //是否为润年
{ int t;
if ((x.year%4==0&&x.year%100!=0)||x.year%400==0)
t=1;
else t=-1;
return t;
}
struct date update(struct date d) //第二天的日期
{ struct date next;
if (d.day!=chazhao(d))
{next.day=d.day+1;
next.month=d.month;
next.year=d.year;
}
else if (d.month==12)
{next.day=1;
next.month=1;
next.year=d.year+1;
}
else
{next.day=1;
next.month=d.month+1;
next.year=d.year;
}
return next;
}
第二個:
/*通过输入当天日期算出第二天的日期*/
#include "Stdio.h"
struct date
{ int day;
int month;
int year;
};
int main(void) /*主程序*/
{
struct date today,tomorrow;
int chazhao(struct date d);
int ruinian(struct date x);
struct date update(struct date d);
printf ("pleae input today's date(mm/dd/yyyy): ");
scanf ("%i%i%i",&today.month,&today.day,&today.year);
tomorrow=update(today);
printf ("next date is %i/%i/%i\n",tomorrow.month,tomorrow.day,tomorrow.year);
getch();
}
int chazhao(struct date d) /*查找月分的天数*/
{ const int endday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if ( ruinian(d)==1&&d.month==2)
days=29;
else
days=endday[d.month-1];
return days;
}
int ruinian(struct date x) /*是否为润年*/
{ int t;
if ((x.year%4==0&&x.year%100!=0)||x.year%400==0)
t=1;
else t=-1;
return t;
}
struct date update(struct date d) /*利用复合字面量计算第二天的日期*/
{ struct date next;
if (d.day!=chazhao(d))
next=(struct date){d.day+1,d.month,d.year};
else if (d.month==12)
next=(struct date){1,1,d.year+1};
else
next=(struct date){1,d.month+1,d.year};
return next;
}
以上两个程序功能是一样的。。大家看我用红字标出来的地方。。我是根据书上写的。为什么有错误呀,书上写的是可以使用复合字面量呀。。
大家幫幫我呀。。到底怎麽回事呀
[此贴子已经被作者于2007-7-15 22:29:18编辑过]