标题:[求助]帮忙改正一道程序
只看楼主
劲风草
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-3-29
 问题点数:0 回复次数:1 
[求助]帮忙改正一道程序
这是判断一个的日期为该年第几天的程序
原程序是一网友写的,但我发现有一些错误码,改动还是有一点问题,就是当你输入一个不存在的日期时,
它也依然会运行。如输入2006.11.31 会输出335。请高手帮忙改正,并说明为什么!

#include "stdio.h"
main ()
{
int year, month, day;
int total;
int leap;
printf ("enter year.month.day\n");
scanf ("%d.%d.%d.", &year, &month, &day);
if (month>12||day>31)
printf ("error");
else{
switch (month)
{
case 1: total=0+day; break;
case 2: total=31+day; break;
case 3: total=59+day; break;
case 4: total=90+day; break;
case 5: total=120+day; break;
case 6: total=151+day; break;
case 7: total=181+day; break;
case 8: total=212+day; break;
case 9: total=243+day; break;
case 10: total=273+day; break;
case 11: total=304+day; break;
case 12: total=334+day; break;
}
leap=year%4==0&&year%100!=0||year%400==0;
if (leap&&month>2)
total++;
printf ("%d", total);
}

}
搜索更多相关主题的帖子: include 
2007-03-30 03:04
kwbee
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-3-30
得分:0 

没有对各个月份的day进行限制.可稍微调整一下:
#include <stdio.h>
void main ()
{
int year, month, day;
int total;
int leap;
printf ("enter year.month.day\n");
scanf ("%d.%d.%d.", &year, &month, &day);
if (month>12||day>31)
printf ("error");
else{
switch (month)
{
case 1: if(day>=31) printf("error\n"); else total=0+day; break;
case 2: if(day>=29) printf("error\n"); else total=31+day; break;
case 3: if(day>=31) printf("error\n"); else total=59+day; break;
case 4: if(day>=30) printf("error\n"); else total=90+day; break;
case 5: if(day>=31) printf("error\n"); else total=120+day; break;
case 6: if(day>=30) printf("error\n"); else total=151+day; break;
case 7: if(day>=31) printf("error\n"); else total=181+day; break;
case 8: if(day>=31) printf("error\n"); else total=212+day; break;
case 9: if(day>=30) printf("error\n"); else total=243+day; break;
case 10: if(day>=31) printf("error\n"); else total=273+day; break;
case 11: if(day>=30) printf("error\n"); else total=304+day; break;
case 12: if(day>=31) printf("error\n"); else total=334+day; break;
}
leap=year%4==0&&year%100!=0||year%400==0;
if (leap&&month>2)
total++;
printf ("%d", total);
}

}

不过其实这也是不完善的,因为没有考虑到润年平年问题.
另外这样子改只是单纯的避免输出错误的数,但是都没有完全实现题目要实现的功能.
要完全实现的话还是用函数宠幸编写的好.


编程,调试,运行>>>>>>>>>>>
2007-03-30 04:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-127971-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.173244 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved