c语言的编程问题,定义一个表示日期的结构体类型,具体题目如图
1个回答
展开全部
#include <stdio.h>
int dayCountArray[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Date{
int year, month, day;
};
int IsLeapYear(struct Date date){
if (0==date.year%400 || (0!= date.year%100 && 0==date.year%4)) {
return 1;
}
return 0;
}
int Dayth(struct Date date){
int sum = 0;
for (int i = 1; i < date.month; ++i) {
sum += dayCountArray[i];
}
sum += date.day;
if (!IsLeapYear(date) && date.month>2) {
--sum;
}
return sum;
}
int Judge(struct Date date){
if (date.year<0
|| date.month < 1 || date.month > 12
|| date.day < 0
|| date.day > dayCountArray[date.month]
|| (2==date.month && !IsLeapYear(date) && date.day > 28)) {
return 0;
}
return 1;
}
int main(int argc, const char * argv[]) {
struct Date date;
for (int i = 0; i < 10; ++i) {
printf("please input date!\n");
scanf("%d %d %d", &date.year, &date.month, &date.day);
if (Judge(date)) {
printf("dayth : %d\n", Dayth(date));
}else{
printf("Error input!\n");
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询