方法一:使用if……else語(yǔ)句?
#include"stdio.h"
#include"conio.h"
int main()
{
int year,month,days;
printf("請(qǐng)依次輸入整數(shù)的某年某月:");
scanf("%d%d",&year,&month);
if(month>12||month<1) //判斷輸入的月份數(shù)據(jù)是否正確
printf("輸入的月份數(shù)據(jù)錯(cuò)誤\n");
else
{
if((year%4==0&&year%100!=0)||(year%400==0)) //閏年的判斷
{
if(month==2)
days = 29;
else if(month==4||month==6||month==9||month==11)
days = 30;
else
days = 31;
}
else
{
if(month==2)
days = 28;
else if(month==4||month==6||month==9||month==11)
days = 30;
else
days = 31;
}
}
printf("the month of this year has these days: %d\n",days);
getch();
return 0;
}
方法二:使用switch語(yǔ)句
#include"stdio.h"
#include"conio.h"
int runnian(int year);//自定義求潤(rùn)年函數(shù)
void main()
{
int year,month,days;
printf("請(qǐng)依次輸入整數(shù)的某年某月:");
scanf("%d%d",&year,&month);
if(month>12||month<1)
printf("輸入的月份數(shù)據(jù)錯(cuò)誤\n");
else
{
int days = 31;
switch(month)
{
case 4:
case 6:
case 9:
case 11:
{
days = 30;
break;
}
case 2:
{
if(runnian(year))
days = 29;
else
days = 28;
break;
}
}
printf("該年該月有: %d 天\n",days);
}
getch();
}
int runnian(int year)//自定義求閏年函數(shù)
{
if((year%4==0&&year%100!=0)||(year%400==0))//符合閏年的條件
{
return 1;
}
return 0;
}
考慮到一些讀者需要相關(guān)資料和解決一些疑問,因此我新建立了一個(gè)學(xué)習(xí)交流群,我在群文件里上傳了一些資料,需要的讀者可以入群下載。
群中文件資料我會(huì)時(shí)常更新,主要資料是51單片機(jī)開發(fā)、32單片機(jī)開發(fā)、編程、嵌入式學(xué)習(xí)、算法控制等。
由于群中文件還在不斷更新上傳,有些方面的資料還不太完善,希望大家理解。若群中涉及違規(guī)行為,歡迎舉報(bào)!
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-502586.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-502586.html
到了這里,關(guān)于使用c語(yǔ)言編程時(shí)輸入具體的年份和月份,求月份的天數(shù)的兩種編程方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!