鱼C论坛

 找回密码
 立即注册
查看: 331|回复: 1

[已解决]问题求助

[复制链接]
发表于 2023-12-7 16:51:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问这一题怎么解决?(用c语言)(并解决闰年的问题)
9.2. Exercise
Calling time (NULL) gives you a large integer. Use this value to calculate today's year, date, hours, and minutes.
  Simplifying assumption:we pretend that each year has exactly365 days. The 15th of March is therefore day 83 in year 2011. (use that date to figure out today's "day number")(hint: how many seconds are there in a day?)
You cannot use struct for this assignment.
You cannot use any time-based functions from the standard library such asasctime or strftime.
  You must use one function to calculate the year and day(pass inthe total seconds, and a reference to the year and day).
  You must use a separate function to calculate the hours andminutes.(again, pass the total seconds, and a reference to the hours and minutes)
  You must use a separate function to print the time; this functioncan only take as arguments the year, day, hour, and minutes.
       handle leap-years correctly, and print the month as well.
最佳答案
2023-12-7 16:59:46
首先,对于计算年份和日期,我们可以使用以下方法:

1. 获取当前时间的总秒数:
  
  1. time_t currentTime = time(NULL);
复制代码

2. 计算今天是一年中的第几天:
  
  1. int dayOfYear = ((currentTime / 60) / 60) / 24;
复制代码

3. 计算今年的年份:
  
  1. int currentYear = 2011 + dayOfYear / 365;
复制代码

4. 计算月份:
  
  1. int month;
  2.    if (dayOfYear >= 0 && dayOfYear < 31)
  3.        month = 1;
  4.    else if (dayOfYear >= 31 && dayOfYear < 59)
  5.        month = 2;
  6.    else if (dayOfYear >= 59 && dayOfYear < 90)
  7.        month = 3;
  8.    // 其他月份省略...
复制代码

5. 计算日期:
  
  1. int dayOfMonth;
  2.    switch (month) {
  3.    case 1:
  4.        dayOfMonth = dayOfYear + 1;
  5.        break;
  6.    case 2:
  7.        dayOfMonth = dayOfYear - 30;
  8.        break;
  9.    case 3:
  10.        dayOfMonth = dayOfYear - 58;
  11.        break;
  12.    // 其他月份省略...
  13.    }
复制代码

接下来,我们来处理小时和分钟的计算。我们需要一个函数来计算这个时间段中的小时和分钟数。代码示例如下:
  1. void calcHoursMinutes(int totalSeconds, int* hours, int* minutes) {
  2.     *hours = (totalSeconds/ 3600) % 24;
  3.     *minutes = (totalSeconds / 60) % 60;
  4. }
复制代码

最后,我们需要编写一个函数来打印时间。代码示例如下:
  1. void printTime(int year, int month, int day, int hour, int minute) {
  2.     printf("当前时间:%d年%d月%d日 %d时%d分\n", year, month, day, hour, minute);
  3. }
复制代码

至于闰年的问题,可以使用以下方法进行判断:
  1. int isLeapYear(int year) {
  2.     if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  3.         return 1;
  4.     else
  5.         return 0;
  6. }
复制代码

在计算年份时,可以考虑闰年对天数的影响,例如2月份有28或29天。

希望这些代码对你有帮助!

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-7 16:59:46 | 显示全部楼层    本楼为最佳答案   
首先,对于计算年份和日期,我们可以使用以下方法:

1. 获取当前时间的总秒数:
  
  1. time_t currentTime = time(NULL);
复制代码

2. 计算今天是一年中的第几天:
  
  1. int dayOfYear = ((currentTime / 60) / 60) / 24;
复制代码

3. 计算今年的年份:
  
  1. int currentYear = 2011 + dayOfYear / 365;
复制代码

4. 计算月份:
  
  1. int month;
  2.    if (dayOfYear >= 0 && dayOfYear < 31)
  3.        month = 1;
  4.    else if (dayOfYear >= 31 && dayOfYear < 59)
  5.        month = 2;
  6.    else if (dayOfYear >= 59 && dayOfYear < 90)
  7.        month = 3;
  8.    // 其他月份省略...
复制代码

5. 计算日期:
  
  1. int dayOfMonth;
  2.    switch (month) {
  3.    case 1:
  4.        dayOfMonth = dayOfYear + 1;
  5.        break;
  6.    case 2:
  7.        dayOfMonth = dayOfYear - 30;
  8.        break;
  9.    case 3:
  10.        dayOfMonth = dayOfYear - 58;
  11.        break;
  12.    // 其他月份省略...
  13.    }
复制代码

接下来,我们来处理小时和分钟的计算。我们需要一个函数来计算这个时间段中的小时和分钟数。代码示例如下:
  1. void calcHoursMinutes(int totalSeconds, int* hours, int* minutes) {
  2.     *hours = (totalSeconds/ 3600) % 24;
  3.     *minutes = (totalSeconds / 60) % 60;
  4. }
复制代码

最后,我们需要编写一个函数来打印时间。代码示例如下:
  1. void printTime(int year, int month, int day, int hour, int minute) {
  2.     printf("当前时间:%d年%d月%d日 %d时%d分\n", year, month, day, hour, minute);
  3. }
复制代码

至于闰年的问题,可以使用以下方法进行判断:
  1. int isLeapYear(int year) {
  2.     if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  3.         return 1;
  4.     else
  5.         return 0;
  6. }
复制代码

在计算年份时,可以考虑闰年对天数的影响,例如2月份有28或29天。

希望这些代码对你有帮助!

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-14 02:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表