❶ 用c語言中的數組編程,當填寫年月日時,輸出是這年的第幾天.
方法一:
#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int day,month,year,sum,leap;
do{
printf("\nplease input year month day\n");
printf("\n請輸入年 月 日\n");
scanf("%d %d %d",&year,&month,&day);
if((month==1&&day<=31&&day>=1)||
((year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=29&&day>=1))||
(!(year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=28&&day>=1))||
(month==3&&day<=31&&day>=1)||(month==4&&day<=30&&day>=1)||
(month==5&&day<=31&&day>=1)||(month==6&&day<=30&&day>=1)||
(month==7&&day<=31&&day>=1)||(month==8&&day<=31&&day>=1)||
(month==9&&day<=30&&day>=1)||(month==10&&day<=31&&day>=1)||
(month==11&&day<=30&&day>=1)||(month==12&&day<=31&&day>=1))
{ switch(month)//先計算某月以前月份的總天數
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");
break;
}
sum=sum+day; //再加上某天的天數
if(year%400==0||(year%4==0&&year%100!=0)) //判斷是不是閏年
leap=1;
else
leap=0;
if(leap==1&&month>2) //如果是閏年且月份大於2,總天數應該加一天
sum++;
//clrscr(); //清屏
system("cls"); //清屏
printf("%d-%d-%dis the year\'s first %d days.\n\n",year,month,day,sum);
printf("%d-%d-%d是這年的第%d天.\n\n",year,month,day,sum);
}
else printf("\tData errors!!!\n\n");
}while(1);
}
方法二:
#include<stdio.h>
#include <stdlib.h>
struct
{
int year;
int month;
int day;
}date;
void main()
{int i,days;
int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
do{
printf("Input year,month,day:\n");
scanf("%d %d %d",&date.year,&date.month,&date.day);
system("cls"); //清屏
days=0;
if((date.month==1&&date.day<=31&&date.day>=1)||
((date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=29&&date.day>=1))||
(!(date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=28&&date.day>=1))||
(date.month==3&&date.day<=31&&date.day>=1)||(date.month==4&&date.day<=30&&date.day>=1)||
(date.month==5&&date.day<=31&&date.day>=1)||(date.month==6&&date.day<=30&&date.day>=1)||
(date.month==7&&date.day<=31&&date.day>=1)||(date.month==8&&date.day<=31&&date.day>=1)||
(date.month==9&&date.day<=30&&date.day>=1)||(date.month==10&&date.day<=31&&date.day>=1)||
(date.month==11&&date.day<=30&&date.day>=1)||(date.month==12&&date.day<=31&&date.day>=1))
{
for(i=1;i<date.month;i++)
days+=day_tab[i];
days+=date.day;
if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)
days++;
printf("\n\n");
printf("\t%d/%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
}
else printf("\tData errors!!!\n\n");
}while(1);
}
❷ 杈撳叆鏌愪竴騫寸殑絎鍑犲ぉ錛岃$畻騫惰緭鍑哄畠鏄榪欎竴騫寸殑絎鍑犳湀絎鍑犳棩錛宑璇璦錛屾庝箞鍐欙紝璋㈣阿
#include<stdio.h>
#include<stdlib.h>
voidMonthDay(intyear,intyearDay,int*pMonth,int*pDay);
intmain()
{
intyear,yearDay,pMonth,pDay;
scanf("%d",&year);
scanf("%d",&yearDay);
MonthDay(year,yearDay,&pMonth,&pDay);
printf(" %d%d",pMonth,pDay);
return0;
}
voidMonthDay(intyear,intyearDay,int*pMonth,int*pDay)
{
intday[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
intflag=0,i=0;
if(((year%4==0)&&(year%100)!=0)||(year%400==0))
flag=1;
if(flag==0&&yearDay>365)
{
printf("澶╂暟杈撳叆涓嶅悎娉昞n");
exit(0);
}
elseif(flag==1&&yearDay>366)
{
printf("澶╂暟杈撳叆涓嶅悎娉昞n");
exit(0);
}
while(yearDay>day[flag][i])
{
yearDay-=day[flag][i];
i++;
}
*pMonth=i+1;
*pDay=yearDay;
}