我的問題指出: 編寫一個C語言函數命名爲天()決定之日起1/1/1900天數爲structure.Pass的轉交地址的任何日期days()函數的Date結構變量。在main()中編寫一個程序,用於輸入用戶的月份,日期和年份,將輸入寫入Date結構變量,調用days()函數並顯示結果。使用以下日期結構:C編程傳遞結構到功能
struct date
{
int month;
int day;
int year;
};
在編寫此函數時,使用所有年份爲360天,每個月包含30天的約定。該函數應返回傳遞給它的任何日期結構的天數。
這是我迄今爲止,它是每次計算0:
struct date
{
int month;
int day;
int year;
};
int main()
{
int monthMain, dayMain, yearMain; //declaring the int variables
int totalDays;
printf("Enter a Month: "); //requesting user to input the month
scanf("%d", &monthMain); //accepting the user input for month
printf("Enter a Day: "); //requesting user to input the day
scanf("%d", &dayMain); //accepting the user input for day
printf("Enter a Year: "); //requesting user to input the year
scanf("%d", &yearMain); //accepting the user input for year
totalDays = days();
printf("the date you entered = %d days", totalDays);
return 0;
}
int days(struct date *d)
{
int yearCalc, daysAmount;
int monthMain, dayMain, yearMain; //declaring the int variables
yearCalc = 1900 * 360;
yearMain = (yearMain * 360) - yearCalc;
if(monthMain == 1)
{
monthMain = 0;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 2)
{
monthMain = 30;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 3)
{
monthMain = 60;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 4)
{
monthMain = 90;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 5)
{
monthMain = 120;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 6)
{
monthMain = 150;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 7)
{
monthMain = 180;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 8)
{
monthMain = 210;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 9)
{
monthMain = 240;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 10)
{
monthMain = 270;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 11)
{
monthMain = 300;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 12)
{
monthMain = 360;
daysAmount = monthMain + dayMain + yearMain;
}
return daysAmount;
}
任何幫助將是巨大的:)
由於計算'daysAmount = monthMain + dayMain + yearMain;'每次都是相同的,您可以在'if'語句集之後移動它。在11月和12月之間從300跳到360是錯誤的。有一個簡單的算法給出一個有效的月份數來推算出一年前的天數(對於這些簡單的大小均勻的月份):'(month_number - 1)* 30'給出了你之後的答案。你有沒有添加任何打印語句,看看爲什麼你得到0? –