[原创]百思不解的问题????????????
这个是输入年月日求这一年中第几天的程序,怎么输入2月以后的月份,总的天数多了一天????????????
#include"stdio.h"
main()
{
int days[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}
};
int year,month,i,day1,day2,sum=0;
int leap;
printf("qing shu ru nian yue ri\n");
scanf("%d,%d,%d",&year,&month,&day1);
if((year%4==0&&year%100!=0)||(year%400==0))/*润年判断
leap=1;
else
leap=0;
day2=days[leap][month-1];
if(day1>day2) /****日期合法判断
printf("shu ru you cuo!\n");
else
{
if(month==1) /*1月份的天数
{sum=day1;
printf("%dnian,%dyue,%dri shi di %d tian\n",year,month,day1,sum);}
else
{
for(i=0;i<month-1;++i) /*判断2月以后的天数
sum=sum+days[leap][i];
printf("%d\n",sum);
sum=sum+day1; /*可能是这里出错 有谁能告诉我 为什么??
printf("%dnian,%dyue,%dri shi di %d tian\n",year,month,day1,sum);
}
}
}
----------------解决方案--------------------------------------------------------
我编译了~~没问题啊~~~
2000,3,2,62天
1999,3,2,61天
------------------------------------------------
声明:没问题是指 没发现 问题 哦。
----------------解决方案--------------------------------------------------------
我顺便把程序完善了一下~~~大家扔砖吧。。。
谁有更好的思想请贴出来,大家一起分享阿。。。
#include<stdio.h>
main()
{int i,leap,sum,year,month,day,yearerr,montherr,dayerr;
int tab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
char x;
start: clrscr();
sum=0;
yearerr=0;
montherr=0;
dayerr=0;
printf("please enter the year,month,day:\n");
scanf(" %d,%d,%d",&year,&month,&day);
if(year%4==0&&year%100!=0||year%400==0)
leap=1;
else leap=0;
if(year<=0){
yearerr=1;
printf("year error!\n");}
if(month>12||month<=0){
montherr=1;
printf("month error!\n");}
if(day>31||day<=0){
dayerr=1;
printf("day error!\n");}
if(yearerr||montherr||dayerr)
goto end;
for(i=0;i<month;i++)
sum=sum+tab[leap][i];
sum=sum+day;
printf("the total is %d\n",sum);
end:printf("continue?y/n");
scanf(" %c",&x);
if(x=='y')
goto start;
exit(0);
}
----------------解决方案--------------------------------------------------------