C语言程序练习——结构体的运用 写一个函数days,实现主函数将年、月、日(结构体类型)传递给days函数,days函数计算该年该月该日是该年的第几天并传回主函数输出。

写一个函数days,实现主函数将年、月、日(结构体类型)传递给days函数,days函数计算该年该月该日是该年的第几天并传回主函数输出。
程序的运行示例如下:
请输入日期(年,月,日)
1990,2,14
2月14日是1990年的第45天。

输入格式:
"请输入日期(年,月,日)\n"
"%d,%d,%d"
输出格式:
"\n%d月%d日是%d年的第%d天。"

代码如下

#include <stdio.h>

struct dt
{  		  
    int year;
    int month;
    int day;
}  		   date;

int days(struct dt date)
{  		  
    int sum;

    switch (date.month)
    {  		  
    case 1:
        sum = date.day;
        break;
    case 2:
        sum = date.day + 31;
        break;
    case 3:
        sum = date.day + 59;
        break;
    case 4:
        sum = date.day + 90;
        break;
    case 5:
        sum = date.day + 120;
        break;
    case 6:
        sum = date.day + 151;
        break;
    case 7:
        sum = date.day + 181;
        break;
    case 8:
        sum = date.day + 212;
        break;
    case 9:
        sum = date.day + 243;
        break;
    case 10:
        sum = date.day + 273;
        break;
    case 11:
        sum = date.day + 304;
        break;
    case 12:
        sum = date.day + 334;
        break;
    }
    if ((date.year % 4 == 0 && date.year % 100 != 0
            || date.year % 400 == 0) && date.month >= 3)
    {  		  
        sum += 1;
    }

    return sum;
}  		  ;

int main(void)
{  		  
    printf("请输入日期(年,月,日)\n");
    scanf("%d,%d,%d", &date.year, &date.month, &date.day);

    printf("\n%d月%d日是%d年的第%d天。", date.month, date.day, date.year, days(date));

    return 0;
}  		  

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 下面是实现上述功能的C语言程序: ```c #include <stdio.h> struct Date { int year; int month; int day; }; int isLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } int days(struct Date date) { int monthDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(date.year)) { monthDays[2] = 29; } int totalDays = 0; for (int i = 1; i < date.month; i++) { totalDays += monthDays[i]; } totalDays += date.day; return totalDays; } int main() { struct Date date; printf("请输入年月(格式为YYYY-MM-DD):"); scanf("%d-%d-%d", &date.year, &date.month, &date.day); int totalDays = days(date); printf("%d年%d月%d是%d年的第%d天。\n", date.year, date.month, date.day, date.year, totalDays); return 0; } ``` 函数`isLeapYear`用于判断给定的年份是否为闰年,若是则返回1,否则返回0。 函数`days`接受一个`Date`类型结构变量,计算出该期在本年中是第几天,并返回总天数。函数内部先根据闰年判断修改2月的天数,然后累加前面所有月份的天数和当前月份的天数即可。 函数中先读入年月,并调用`days`函数计算出总天数,最后输出结果。 ### 回答2: 函数的定义如下: ```c #include <stdio.h> struct Date{ int year; int month; int day; }; int isLeapYear(int year){ if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; else return 0; } int days(struct Date date){ int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day_count = 0; for(int i = 0; i < date.month - 1; ++i){ day_count += daysInMonth[i]; } if(date.month > 2 && isLeapYear(date.year)) day_count++; day_count += date.day; return day_count; } int main(){ struct Date date; printf("请输入年份:"); scanf("%d", &date.year); printf("请输入月份:"); scanf("%d", &date.month); printf("请输入期:"); scanf("%d", &date.day); int day_count = days(date); printf("该在本年中是第%d天。\n", day_count); return 0; } ``` 在这段程序中,我们定义了一个`Date`结构来存储年、月、信息。我们首先定义了一个辅助函数`isLeapYear`来判断是否为闰年,如果是则返回1,不是则返回0。然后,在`days`函数中,我们首先定义了一个数组`daysInMonth`来存储每个月的天数。我们遍历月份,将之前各个月份的天数相加,并判断如果是闰年且月份大于2月,则将天数加1。最后再加上当月的天数,得到该在本年中的天数。在`main`函数中,我们从用户输入读取年、月、的信息,调用`days`函数计算天数,并输出结果。 ### 回答3: 根据题目要求,我们需要编一个函数`days`来计算给定期在该年中的天数。首先,我们需要定义一个结构变量,包括年、月、,可以命名为`Date`。 ```c struct Date { int year; int month; int day; }; ``` 接下来,我们可以开始编`days`函数函数的输入参数为一个`Date`类型结构变量,即期,返回值为该期在本年中的天数。 ```c int days(struct Date date) { int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int totalDays = date.day; // 先加上输入期的天数 // 判断是否为闰年,闰年2月有29天 if ((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0) { monthDays[1] = 29; } // 加上输入期之前的月份天数 for (int i = 0; i < date.month - 1; i++) { totalDays += monthDays[i]; } return totalDays; } ``` 在函数中,我们首先定义一个`Date`类型的变量,然后从用户输入中获取年、月、,再调用`days`函数进行计算,最后输出结果。 ```c #include <stdio.h> int main() { struct Date date; printf("请输入年:"); scanf("%d", &date.year); printf("请输入月:"); scanf("%d", &date.month); printf("请输入:"); scanf("%d", &date.day); int result = days(date); printf("该在本年中是第%d天。\n", result); return 0; } ``` 这样,我们就完成了用C语言一个求给定期在该年中的天数的程序。需要注意的是,程序中对闰年的判断使用了常见的年份规则(能被4整除但不能被100整除,或者能被400整除的年份都是闰年),可以满足大多数情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值