这是一道简短函数题:输入年月日要求输出这是该年第几天?我写的中间代码为什么不执行,求大手指点迷津。
#include
#include
void main()
{int calculatedDays(int year,int month,int day);
int year,month,day;
scanf("%4d,%4d,%4d\n",&year,&month,&day);
calculatedDays(year,month,day);
system("pause");
}
int calculatedDays(int year,int month,int day)
{int leap,totaldays;
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
while(leap==1)
{if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+29+day;
else if(month==4)
totaldays=31+29+31+day;
else if(month==5)
totaldays=31+29+31+30+day;
else if(month==6)
totaldays=31+29+31+30+31+day;
else if(month==7)
totaldays=31+29+31+30+31+30+day;
else if(month==8)
totaldays=31+29+31+30+31+30+31+day;
else if(month==9)
totaldays=31+29+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+29+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+29+31+30+31+30+31+31+30+31+day;
else
totaldays=31+29+31+30+31+30+31+31+30+31+30+day;
break;
}
while(leap==0)
{if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+28+day;
else if(month==4)
totaldays=31+28+31+day;
else if(month==5)
totaldays=31+28+31+30+day;
else if(month==6)
totaldays=31+28+31+30+31+day;
else if(month==7)
totaldays=31+28+31+30+31+30+day;
else if(month==8)
totaldays=31+28+31+30+31+30+31+day;
else if(month==9)
totaldays=31+28+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+28+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+28+31+30+31+30+31+31+30+31+day;
else
totaldays=31+28+31+30+31+30+31+31+30+31+30+day;
break;
}
return(printf("该日是该年的第:%4d天\n",totaldays));
}
#include <stdio.h>
#include <stdlib.h>
int calculatedDays(int year,int month,int day);
void main()
{
int year,month,day;
scanf("%d %d %d",&year,&month,&day);
calculatedDays(year,month,day);
system("pause");
}
int calculatedDays(int year,int month,int day)
{
int leap,totaldays;
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
if(leap==1)
{
if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+29+day;
else if(month==4)
totaldays=31+29+31+day;
else if(month==5)
totaldays=31+29+31+30+day;
else if(month==6)
totaldays=31+29+31+30+31+day;
else if(month==7)
totaldays=31+29+31+30+31+30+day;
else if(month==8)
totaldays=31+29+31+30+31+30+31+day;
else if(month==9)
totaldays=31+29+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+29+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+29+31+30+31+30+31+31+30+31+day;
else
totaldays=31+29+31+30+31+30+31+31+30+31+30+day;
}
else
{
if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+28+day;
else if(month==4)
totaldays=31+28+31+day;
else if(month==5)
totaldays=31+28+31+30+day;
else if(month==6)
totaldays=31+28+31+30+31+day;
else if(month==7)
totaldays=31+28+31+30+31+30+day;
else if(month==8)
totaldays=31+28+31+30+31+30+31+day;
else if(month==9)
totaldays=31+28+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+28+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+28+31+30+31+30+31+31+30+31+day;
else
totaldays=31+28+31+30+31+30+31+31+30+31+30+day;
}
return(printf("该日是该年的第:%4d天\n",totaldays));
}
自己编的函数得先声明,在 main 函数前 加上 int calculatedDays(int year,int month,int day);
什么不执行,我怎么感觉应该把while改成if呢?
void main()
{int calculatedDays(int year,int month,int day);
int year,month,day;
scanf("%4d,%4d,%4d\n",&year,&month,&day);
calculatedDays(year,month,day);
system("pause");
}
改成下面试试
int calculatedDays(int year,int month,int day);
void main()
{
int year,month,day;
scanf("%4d,%4d,%4d\n",&year,&month,&day);
calculatedDays(year,month,day);
system("pause");
}
#include <stdio.h>
int clu_day(int y,int m,int d);
int main()
{
int sum=0;
while(1)
{
int year=0,mouth=0,day=0;
printf("璇疯緭鍏ュ勾鏈堟棩鐨勪俊鎭€併€乗n");
scanf("%d %d %d",&year,&mouth,&day);
sum=clu_day(year,mouth,day);
printf("%d鏄?dyear%dmouth%dday\n",sum,year,mouth,day);
}
return 0;
}
int clu_day(int y,int m,int d)
{
int sum=0;
int feb=0;
if(((y%4)==0&&(y%100)!=0)||(y%400==0))
{
feb=29;
}
else
{
feb=28;
}
if(m==1)
{
sum=d;
// return sum;
}
if(m==2)
{
sum=31+d;
// return sum;
}
if(m==3)
{
sum=31+feb+d;
// return sum;
}
if(m==4)
{
sum=31+feb+31+d;
// return sum;
}
if(m==5)
{
sum=31+feb+31+30+d;
// return sum;
}
if(m==6)
{
sum=31+feb+31+30+31+d;
//return sum;
}
if(m==7)
{
sum=31+feb+31+30+31+30+d;
// return sum;
}
if(m==8)
{
sum=31+feb+31+30+31+30+31+d;
/// return sum;
}
if(m==9)
{
sum=31+feb+31+30+31+30+31+31+d;
/// return sum;
}
if(m==10)
{
sum=31+feb+31+30+31+30+31+31+30+d;
// return sum;
}
if(m==11)
{
sum=31+feb+31+30+31+30+31+31+30+31+d;
// return sum;
}
if(m==12)
{
sum=31+feb+31+30+31+30+31+31+30+31+30+d;
// return sum;
}
return sum;
}
上面乱码是ubantu换到source insight 上面出现的 不影响结果
讲真的,这代码。。。为啥不用一个数组存月份,再来一个for循环呢,几行代码就搞定了啊