本文主要是介绍【定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
我们首先判断该年是不是闰年,代码简单明了,就是有点费手。
#include<stdio.h>
struct data{
int year;
int month;
int day;
};
int main(){
struct data a[1];
int year;
int month;
int day;
scanf("%d %d %d",&a[0].year,&a[0].month,&a[0].day);
if (a[0].year%4==0&&a[0].year%100!=0||a[0].year%400==0)
switch(a[0].month){
case 1:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,a[0].day,a[0].year);break;
case 2:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,a[0].day+31,a[0].year);break;
case 3:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+29+a[0].day,a[0].year);break;
case 4:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+31+29+a[0].day,a[0].year);break;
case 5:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+31+29+a[0].day,a[0].year);break;
case 6:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+31+29+a[0].day,a[0].year);break;
case 7:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+30+31+31+29+a[0].day,a[0].year);break;
case 8:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+30+31+31+29+a[0].day,a[0].year);break;
case 9:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+31+30+31+30+31+31+29+a[0].day,a[0].year);break;
case 10:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+31+30+31+30+31+31+29+a[0].day,a[0].year);break;
case 11:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+31+30+31+30+31+31+29+a[0].day,a[0].year);break;
case 12:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+30+31+31+30+31+30+31+31+29+a[0].day,a[0].year);break;
}
else
switch(a[0].month){
case 1:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,a[0].day,a[0].year);break;
case 2:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,a[0].day+31,a[0].year);break;
case 3:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+28+a[0].day,a[0].year);break;
case 4:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+31+28+a[0].day,a[0].year);break;
case 5:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+31+28+a[0].day,a[0].year);break;
case 6:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+31+28+a[0].day,a[0].year);break;
case 7:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+30+31+31+28+a[0].day,a[0].year);break;
case 8:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+30+31+31+28+a[0].day,a[0].year);break;
case 9:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+31+30+31+30+31+31+28+a[0].day,a[0].year);break;
case 10:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+31+30+31+30+31+31+28+a[0].day,a[0].year);break;
case 11:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,31+30+31+31+30+31+30+31+31+28+a[0].day,a[0].year);break;
case 12:printf("%d/%d is the %dth day in %d.",a[0].month,a[0].day,30+31+30+31+31+30+31+30+31+31+28+a[0].day,a[0].year);break;
return 0;
}
}
这篇关于【定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!