一个很简单的C++问题 求解
输入某月某日,判断这一天是这一年的第几天? 这个题用C语言我不会做希望大家能帮帮我。
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { struct tm date; int year, month, day; printf("Input date in format yyyy/mm/dd: "); scanf("%d/%d/%d", &year, &month, &day); date.tm_year = year - 1900; // year since 1900 date.tm_mon = month - 1; // 0 stands for January. date.tm_mday = day; // Initialize other fields. date.tm_isdst = -1; date.tm_hour = 0; date.tm_min = 0; date.tm_sec = 1; if ( mktime(&date) == (time_t)(-1) ) { fprintf(stderr, "Internal error!\n"); exit(-1); } printf("Days in year: %d\n", date.tm_yday + 1); return 0; }
#include "stdio.h" int sum_day(int(*)[,int,int,int]); int main (int argc, char *argv[]) { int year,month,day,days; int day_tab[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}}; printf("Please input: year,month,day\n"); scanf("%d%d%d",&year,&month,&day); days=sum_day(day_tab,year,month,day); printf("It is %d day\n",days); return 0; } int sum_day(int(*p)[13],int y,int m,int d) { int i,leap=0; leap=y%4==0&&y%100!=0||y%400==0; for(i=1;i<m;i++)d+=*(*(p+leap)+i); return d; }