2939 - 【设计型】第12章:结构体和共同体 第几天
时间限制 : 1 秒
内存限制 : 128 MB
定义一个结构体变量(包括年、月、日)。编写一个函数days,计算该日期在本年中是第几天(注意闰年问题)。由主函数将年月日传递给days函数,计算之后,将结果传回到主函数输出。
题目输入
输入年月日,格式为xxx xx xx
题目输出
先开始提示用户输入日期;然后输出第几天。
输入/输出样例
输入格式
2013 01 02
输出格式
请输入日期 日期2013/1/2是2013年的第2天
C语言解答
#include<stdio.h> struct datetime {int year; int month; int day; }; int days(struct datetime date); int main() { int count_day; struct datetime date; printf("请输入日期\n"); scanf("%d %d %d",&date.year,&date.month,&date.day); count_day=days(date); printf("日期%d/%d/%d是%d年的第%d天\n",date.year,date.month,date.day,date.year,count_day); return 0; } int days(struct datetime date) { int result=0; int year=date.year,month=date.month,day=date.day; if(month>1) { switch(month-1) { case 12: result+=31; case 11: result+=30; case 10: result+=31; case 9: result+=30; case 8: result+=31; case 7: result+=31; case 6: result+=30; case 5: result+=31; case 4: result+=30; case 3: result+=31; case 2: { if(year%400==0||year%100!=0&&year%4==0)result+=29; else result+=28; } case 1: result+=31; } result+=day; } else result=day; return result ; }