我的问题,很菜

bsautoland 2002-09-24 09:01:08
我是一个c的新手,有好多东西注意的不够。
这是我写的一个数天数的东西。但总不对,我不知怎了,还请大家多多帮忙。
可能有好多处错误,还请大家多多费心。
谢谢。
# include <stdio.h>
# define PP printf
# define FF "ERROR.Year%d,Day%d\n"
struct time
{
int year;
int mouth;
int day;
};
int calculus ( struct time *p)
{
int judge,i,date;
if ( p->mouth==1&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==3&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==5&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==7&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==8&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==10&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==12&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==4&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==6&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==9&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==11&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->year%4==0 )
judge=1;/*If "judge" is 1,this is leap year.*/
if (judge==1){
if(p->mouth==2&&p->day>29)
PP(FF,p->mouth,p->day);
for (i=1;i<p->mouth;++i){
if ( i==1||i==3||i==5||i==7||i==8||i==10||i==12 )
date=date+31;
if ( i==4||i==6||i==9||i==11)
date=date+30;
if ( i==2 )
date=date+29;
}
date=date+p->day;
}
if (judge!=1){
if(p->mouth==2&&p->day>28)
PP(FF,p->mouth,p->day);
for (i=1;i<p->mouth;++i){
if ( i==1||i==3||i==5||i==7||i==8||i==10||i==12 )
date=date+31;
if ( i==4||i==6||i==9||i==11)
date=date+30;
if ( i==2 )
date=date+28;
}
date=date+p->day;
}
return(date);
}
main()
{
struct time a;
int result;
scanf("%d,%d,%d",&a.year,&a.mouth,&a.day);
result=calculus( &a );
PP("date%d",result);
return(0);
}
...全文
26 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mattrew 2002-09-24
  • 打赏
  • 举报
回复
if ( p->mouth==1&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==3&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==5&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==7&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==8&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==10&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==12&&p->day>31)
PP(FF,p->mouth,p->day);
if ( p->mouth==4&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==6&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==9&&p->day>30)
PP(FF,p->mouth,p->day);
if ( p->mouth==11&&p->day>30)
太繁琐了
你完全可以用一个判断语句来进行阿
还有宏定义# define PP printf 有点画蛇添足
使用printf 可读性更好
主要程序的设计算法和可读性

netbian 2002-09-24
  • 打赏
  • 举报
回复
对阿
可以简化的
scorpiotianyawei 2002-09-24
  • 打赏
  • 举报
回复
写的太繁琐了,简化一下
program2100 2002-09-24
  • 打赏
  • 举报
回复
同ashchan((www.ashchan.com))的建议,程序要尽量写的简潔
djwinter 2002-09-24
  • 打赏
  • 举报
回复
是呀
这么长
好难看呀
而且都是重复:)
ashchan 2002-09-24
  • 打赏
  • 举报
回复
呵呵,写得是有点那个?
不如换个算法吧,比如
int day[2][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
};
然后,day[是否闰年][月份n]就能直接取 月份n 的天数了。

70,037

社区成员

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

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