#include <stdio.h>/*写法一*/
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
return 0;
}
static char dayab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int day_of_year(int year,int month,int day){
int i;
int leap;
if(year<0){
return -1;
}
else if(month<=0||month>12){
return -1;
}
else{
leap=year%4==0&&year%100!=0||year%400==0;
for(i=0;i<month;i++){
day+=dayab[leap][i];
}
return day;
}
}
这个不够完善应该还要注意日子也要进行计算日子是不超过当月的最大天数。
以下是正确的程序:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
return 0;
}
static char dayab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int day_of_year(int year,int month,int day){
int i;
int leap;
if(year<0)
return -1;
if(month<=0||month>12)
return -1;
if(day<1||day>dayab[leap][month])
return -1;
for(i=0;i<month;i++)
day+=dayab[leap][i];
return -1;
}
注意:1各个方面要考虑全面,日子,年,月……
2尽量写的简略比如if语句