鱼C论坛

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

解决问题

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

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

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

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.
(optional: handle leap-years correctly, and print the month as well).
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-7 16:07:59 | 显示全部楼层
这道题要求你使用C语言来计算当前的年份、日期、小时和分钟。我们可以利用调用time(NULL)函数返回的大整数来进行计算。

首先,我们需要使用一个函数来计算年份和日期。我们可以假设每一年都有365天,并且以2011年3月15日为基准,那么今天是第几天。我们可以通过计算今天到2011年3月15日之间的秒数差来得出答案。提示一下,一天中有多少秒呢?

然后,我们需要使用另一个函数来计算小时和分钟。同样地,我们可以通过将总秒数转换为小时和分钟来得到答案。

最后,我们需要使用一个函数来打印年份、日期、小时和分钟。这个函数只能接受年份、日期、小时和分钟作为参数。

这个题目是可选的,你可以选择正确地处理闰年,并打印出月份。

下面是一个实现的例子:
  1. #include <stdio.h>
  2. void calculateYearAndDay(int totalSeconds, int *year, int *day) {
  3.     *year = totalSeconds / (365 * 24 * 60 * 60);
  4.     *day = (totalSeconds % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
  5. }
  6. void calculateHoursAndMinutes(int totalSeconds, int *hours, int *minutes) {
  7.     *hours = (totalSeconds % (24 * 60 * 60)) / (60 * 60);
  8.     *minutes = (totalSeconds % (60 * 60)) / 60;
  9. }
  10. void printTime(int year, int day, int hours, int minutes) {
  11.     printf("Today's date is ");
  12.     printf("%d-%d, ", year + 2011, day + 15);
  13.     printf("%02d:%02d\n", hours, minutes);
  14. }
  15. int main() {
  16.     int totalSeconds = time(NULL);
  17.     int year, day, hours, minutes;
  18.     calculateYearAndDay(totalSeconds, &year, &day);
  19.     calculateHoursAndMinutes(totalSeconds, &hours, &minutes);
  20.     printTime(year, day, hours, minutes);
  21.     return 0;
  22. }
复制代码

这个例子中,我们使用time(NULL)函数来获取总秒数,然后将它传递给计算年份和日期的函数calculateYearAndDay(),计算小时和分钟的函数calculateHoursAndMinutes(),最后将结果传递给打印时间的函数printTime()。

希望这个例子可以帮助你理解并解决这道题目!

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 02:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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