程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 代碼不執行結果隨機數-這是一道簡短函數題:輸入年月日要求輸出這是該年第幾天?我寫的中間代碼為什麼不執行,求大手指點迷津。

代碼不執行結果隨機數-這是一道簡短函數題:輸入年月日要求輸出這是該年第幾天?我寫的中間代碼為什麼不執行,求大手指點迷津。

編輯:編程解疑
這是一道簡短函數題:輸入年月日要求輸出這是該年第幾天?我寫的中間代碼為什麼不執行,求大手指點迷津。

#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));
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved