當前位置:首頁 » 編程語言 » c語言判斷哪個月是大月
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言判斷哪個月是大月

發布時間: 2022-10-28 12:57:06

c語言判斷日期

首先這個函數的輸入是什麼?
(年,月,日)

當不考慮閏年時,只須(月,日)

然後先考查月的取值范圍(1~12)的整數

此時可以進行第一步判斷

接下來,你要作的很簡單

比如你可以用個switch來作這些事情

比如我來作:

switch(月)
case 1,3,5,7,8,10,12:
return (日 > 0 && 日 <= 31) ?true:false; break;
case 4,6,9,11:
return (日 > 0 && 日 <= 30) ?true:false; break;
case 2:
return (日 > 0 && 日 <= 28) ?true:false; break;

defalts:
...............

另外若加上閏年的情況也不會很復雜,只是須要以年來判斷是不是閏年,然後把那個28改成一個返回函數即可

② c語言編寫程序從鍵盤輸入年份和月份,計算出這一年的這一個月有多少天

#include<stdio.h>

int main()

{

int year,month,days,day;

printf("請輸入年月日");

scanf("%d-%d-%d",&year,&month,&days);

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: day=31;break;

case 4:

case 6:

case 9:

case 11:day=30;break;

case 2:

if(year%4==0&&year%100!=0||year%400==0)

day=29;

else

day=28;break;

default :printf("error ");

}

printf("這個月有%d天",day);

}

③ 跪求C語言編寫的萬年歷

我自己寫的,不過時輸入年份顯示該年的年歷,比月歷要稍微難一點,稍微改一下就行了,你看看。我們當時用的環境是DEVC++,是通過的。
#include<stdio.h>
int monthDay(int,int);
int IsLeapYear(int);
main()
{
int days,weekDay,year,month=1,d,i;
printf("please input the year:\n");
scanf("%d",&year);
days=year-1+(year-1)/400+(year-1)/4-(year-1)/100;

while(month<=12){
printf("\t\t\t%d-%d\n",year,month);
printf("Sun\tMon\tTues\tWed\tThur\tFir\tSat\n");
d=1,i=1;
weekDay=(days+1)%7;
while(i<=weekDay)
{
printf("\t");
i++;
}
while(d<=monthDay(month,year))
{

weekDay=(days+1)%7;

if(weekDay==6)
printf("%d\n",d);
else
printf("%d\t",d);
if(d==monthDay(month,year))
printf("\n");
d++;
days++;

}
month++;
}
system("PAUSE");
return 0;
}

int monthDay(int month, int year)
{
switch(month){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
return 31;
break;
case 4: case 6:case 9:case 11:
return 30;
break;
case 2:
if((IsLeapYear(year))==1)

return 29;
else
return 28;
break;
}
}
int IsLeapYear(int year)
{

if((year%4==0)&&(year%100!=0))

return 1;
if(year%400==0)
return 1;

return 0;
}
這個是網上找的月歷
#include <stdio.h>
int leap (int year)
{if(year%4==0&&year%100!=0||year%400==0) //判斷是否是閏年
return 1;
else return 0;
}
int days_month (int month,int year) //判斷大月和小月
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //找出大月
return 31;
if(month==4||month==6||month==9||month==11) //找出小月
return 30;
if(month==2&&leap(year)==1) return 29; //判斷二月是29天還是29天
else return 28;
}
int firstday(int month,int year)
{int w;
w=(1+2*month+3*(month+1)/5+year+year/4+year/400-year/100)%7+1; //判斷每個月開始的第一天是星期幾
return w;
}
main()
{int i,j=1,k=1,a,b,month,year;
printf("\n input month and year:\n");
scanf("%d%d",&month,&year); //輸入月和年
b=days_month(month,year);
a=firstday (month,year);
printf(" Sun Mon Tue Wed Thu Fri Sat \n"); //輸出對應當月的日歷
if(a==7)
{for(i=1;i<=b;i++)
{printf("%4d",i);
if(i%7==0)
{printf("\n");
}
}
}
if(a!=7)
{while (j<=4*a)
{printf(" ");
j++;
}
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7*k-a)
{printf("\n");
k++;
}

}
}
printf("\n");
}

④ c語言判斷月份是否是大月

用 if 判斷是否閏年。月份的天數存於數組,用月做下標。
#include <stdio.h>

int main(){
int MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y,m;
printf("input year month:2018 2\n");
scanf("%d %d",&y,&m);
if (((y%4==0)&&(y%100!=0)||(y%400==0)))MonthDay[2]=29; //閏年
if (m<0 || m >12)m=0; //錯誤的月
printf("%d\n",MonthDay[m]); //輸出結果
return 0;
}

⑤ c語言判斷大小月

#include<stdio.h>
#include<stdlib.h>
int main(){
int month[12]={31,0,31,30,31,30,31,31,30,31,30,31};
//題目要求將2月劃為小月 ,此處用0標記
int n;
scanf("%d",&n);
if (n<=0 || n>12){
printf("error");
}
else if(month[n-1]==31){
printf("solar month");
}
else{
printf("lunar month");
}
return 0;
}

⑥ C語言程序,輸入年月日算出當天星期幾

先做一個函數,計算1900年1月1日到該天的天數,然後用膜除求出星期幾並輸出。

具體代碼如下:

#include<stdio.h>
#include<stdlib.h>

intDay(inty,intm,intd);

intmain(intargc,char*argv[]){
inti;//循環變數
intyear,month,day;//讀取用戶輸入的年月日
intdays;//儲存從1900-1-1到該天的天數
intweek=0;//儲存求余後的星期
//字元串組合輸出星期
charb[]={"星期"};
charc[7][4]={"日","一","二","三","四","五","六"};

loop:printf("請輸入年月日:(空格分隔) ");
scanf("%d%d%d",&year,&month,&day);
days=Day(year,month,day);
//增強程序的健壯性
if(days==-1)
{
printf("輸入格式有誤,");
fflush(stdin);//防止用戶輸入字母等其他非數字字元
gotoloop;
}
else
{
week=days%7;//用求余計算星期幾
printf("%d年%d月%d日是%s%s ",year,month,day,b,c[week]);
}
system("pause");
return0;
}

//函數功能:輸入年月日,輸出1900-1-1到該天的天數,-1位輸入不合法
intDay(inty,intm,intd)
{
inti;
intr=0;//儲存1900到該天的閏年個數
inta[]={31,28,31,30,31,30,31,31,30,31,30};//m給每出月的天數

if(y>=1900&&m>0&&m<13&&d>0&&d<32)
{
for(i=1900;i<=y;i++)//計算閏年的個數
{
if(i%400==0||i%100!=0&&i%4==0)
r++;
}
for(i=0;i<m-1;i++)//統計當年1月1日到當月的天數
{
d+=a[i];
}
if((y%400==0||y%100!=0&&y%4==0)&&m<3)//如果當年是閏年但不到二月就不能算進去
r--;
return365*(y-1900)+r+d;
}
else
return-1;
}

⑦ C語言高手過來幫幫忙

這個程序是誰寫的啊,一點都不規范,而且還不完整,
把main函數里的switch語句後加個(c)就可以正常編譯運行了。
程序的作用是選擇要轉換為陰歷還是陽歷,例如選擇陰歷好後輸入一組年月日(格式例1981.11.30,在1980到2015年之間),之後程序將為你轉換為陰歷(1981.11.5)

幫你注釋了兩個函數,另外兩個函數的思想相同,
總的來說這個程序的邏輯不是很好,不用心看的話很難理解作者的想法,我沒時間,要不幫你重新寫了,

如果還是看不懂你再留言。

#include<stdio.h>
#include<bios.h>
struct data
{
int year1;
int specialmonth;
int month1[13];
};

struct day
{
int year2;
int month2;
int day2;
};

int dis_day1(struct day *op,int *p) /*計算並返回當年距1980年的天數*/
{
int n=0,x;
n+=(op->year2-1980)*365;
for(x=1980;x<op->year2;x++) /*如果當年是閏年,天數加1*/
if((x%4==0&&x%100!=0)||(x%100==0&&x%400==0))
n+=1;
for(x=1;x<(op->month2);x++)
n+=*(p+x-1);
x=op->year2;
if(((x%4==0&&x%100!=0)||(x%100==0&&x%400==0))&&op->month2>2) /*閏年月份大於2月才多1天*/
n+=1;
n+=(op->day2-1); /*小於2月的話,就算當年是閏年,也要減1天*/
return n;
}

int dis_day2(struct day *op,struct data *p)
{
int n=0,maxmonth,i,x;
for(x=1980;x<op->year2;x++)
{
if((p+x-1979)->specialmonth==0)
maxmonth=12;
else maxmonth=13;
for(i=0;i<maxmonth;i++)
if((p+x-1979)->month1[i]==0)
n+=29;
else n+=30;
}
if((p+op->year2-1979)->specialmonth!=0)
if((p+op->year2-1979)->specialmonth<op->month2)
op->month2++;
for(i=1;i<op->month2;i++)
{
if((p+op->year2-1979)->month1[i-1]==0)
n+=29;
else n+=30;
}
n+=(op->day2-1);
n+=46;
return n;
}

/*陽轉陰;這里比較復雜,作者的演算法大概是:利用前面算出來的天數,直接在自己定義的陰歷表裡面*/
/*以1979.11.14為起點去代換,*/
void work_out2(struct day *re,struct data *p,int n)
{
int n1=0,maxday,maxmonth,month,day,judge=0;
re->year2=1979;re->month2=11;re->day2=14; /*這里的1979.11.14是1980.1.1的陰歷*/
while(1)
{
if(judge==1)
break;
if(p->specialmonth==0)
maxmonth=12;
else
maxmonth=13; /*有閏月的年份多1個月*/
/*下面的if語句是判斷當年還剩幾個月(maxmonth-month),如果是79年的話,因為是從*/
/*11.14開始,因此只剩1個月(maxmonth-month=1)。 */
if(p->year1==1979)
month=12;
/*其他年份將有(maxmonth-month)12到13個月,至於能否全用到就要看前面算出來的天數n了*/
else
month=1;
for(;month<=maxmonth;month++)
{
if(p->month1[month-1]==0) /*判斷當月是大月還是小月*/
maxday=29;
else maxday=30;
if((p->year1==1979)&&(maxday==30)) /*79年的11月是大月,當月日期計數從14號開始*/
day=14;
else day=1; /*其他年份的當月計數從1號開始*/
for(;day<=maxday;day++)
{
if(n1==n)
{
if(maxmonth==13&&month>p->specialmonth)
month-=1;
re->year2=p->year1;
re->month2=month;
re->day2=day;
judge=1;
break;
}
else n1+=1;
}
if(judge==1)break;
}
p++;
}
}

void work_out1(struct day *re,int n,int *p)
{
int year=1980,n1=0,month,day,maxday,judge=0;
re->year2=1980;re->month2=1;re->day2=1;
while(1)
{
*(p+1)=28;
if(judge==1)
break;
if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
*(p+1)=29;
for(month=1;month<=12;month++)
{
maxday=*(p+month-1);
for(day=1;day<=maxday;day++)
{
if(n1==n)
{
re->year2=year;
re->month2=month;
re->day2=day;
judge=1;
break;
}
else n1++;
}
if(judge==1)
break;
}
year++;
}
}

main()
{
int n,c;
int sun[12]=;
int *p1=sun;
struct day ob,re;
struct data *p2;
/*下面是1979年到2015年的農歷表,第1個數字代表年份;第2個數字代表當年的閏月是幾月 */
struct data year[37]={
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,

};
p2=year;
printf(" WARNING \n");
printf(" you should take care of it :\n");
printf(" the limted range:\n");
printf(" lunar calendar: 1980.1.1----2015.11.12\n"); /*陽歷*/
printf(" soalr calendar: 1980.1.1-----2015.12.31\n"); /*陰歷*/

while(1)
{
printf(" please choose one way a/b?\n");
printf(" a: soalr calendar---->lunar calendar\n");
printf(" b: lunar calendar---->soalr calendar\n");
do /*選擇除a,b鍵之外的其他鍵無效*/
{
c=getchar();
}while(c!='a'&&c!='b');
do
{
printf("your must pay attention to the form\n");
printf("pleae input your date:\n");
scanf("%d.%d.%d",&ob.year2,&ob.month2,&ob.day2); /*輸入年、月、日*/
}while(ob.year2<1980||ob.year2>=2016||ob.month2<1||ob.month2>12||ob.day2<1||ob.day2>31);
switch(c)
{
case 'a': /*陽轉陰*/
n=dis_day1(&ob, p1);
printf("%d\n",n);
work_out2(&re,p2,n);
printf(" the result is %d.%d.%d\n",re.year2,re.month2,re.day2);
break;
case 'b': /*陰轉陽*/
n=dis_day2(&ob,p2);
printf("%d\n",n);
work_out1(&re,n,p1);
printf(" the result is %d.%d.%d\n",re.year2,re.month2,re.day2);
if(ob.month2==(p2+ob.year2-1979)->specialmonth)
{
if((ob.day2!=30)||((p2+ob.year2-1979)->month1[ob.month2]==1))
{
n+=((p2+ob.year2-1979)->month1[ob.month2-1]+29);
work_out1(&re,n,p1);
printf("there is a other result\n:\t%d.%d.%d\n",re.year2,re.month2,re.day2);
}
}
break;
defluat: break;
}
printf("if you want to try again,y/n?\n");
do
{
c=bioskey(0);
}while(c!=0x316e&&c!=0x1579);
if(c==0x316e)
break;
}
}

⑧ 判斷月份的天數,用c語言中的if語句寫出來要怎麼寫

用 if 判斷是否閏年。月份的天數存於數組,用月做下標。
#include <stdio.h>

int main(){
int MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y,m;
printf("input year month:2018 2\n");
scanf("%d %d",&y,&m);
if (((y%4==0)&&(y%100!=0)||(y%400==0)))MonthDay[2]=29; //閏年
if (m<0 || m >12)m=0; //錯誤的月
printf("%d\n",MonthDay[m]); //輸出結果
return 0;
}

⑨ C語言程序,輸入年月日算出當天星期幾

#include <stdio.h>
int main()
{
int day,mn,yr,i,days=0,s,k;
int mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char wek[][9]={ {'S','u','n','d','a','y'},
{'M','o','n','d','a','y'},
{'T','u','e','s','d','a','y'},
{'W','e','d','n','s','d','a','y'},
{'T','h','u','r','s','d','a','y'},
{'F','r','i','d','a','y'},
{'S','a','t','u','r','d','a','y'}
};
printf("Inpute the date (year-month-day):");
scanf("%d-%d-%d",&yr,&mn,&day);
if (yr%4==0||yr%100==0||yr%400==0)
mont[2]=29;
else
mont[2]=28;
for (i=0;i<mn;i++)
days+=mont[i];
days+=day;
s=yr-1+(int)((yr-1)/4)-(int)((yr-1)/100)+(int)((yr-1)/400)+days;
k=s%7;
printf("%d-%d-%d is %s.",yr,mn,day,wek[k]);
return 0;
}

⑩ c語言中,判斷大小月的代碼怎麼寫

如下:

int main () {

判斷語句:

C語言提供了以下類型的判斷語句。

語句描述:

if語句一個if語句由一個布爾表達式後跟一個或多個語句組成。

if...else語句一個if語句後可跟一個可選的else語句,else語句在布爾表達式為假時執行。

嵌套if語句您可以在一個if或else if語句內使用另一個if或else if語句。

switch語句一個switch語句允許測試一個變數等於多個值時的情況。

嵌套switch語句您可以在一個switch語句內使用另一個switch語句。