|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问这一题怎么解决?
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).
这是一个关于通过给定的时间戳来计算年份、日期、小时和分钟的问题。下面是一个可能的解决方案:
- def calculate_year_day(total_seconds, year, day):
- seconds_per_day = 24 * 60 * 60
- years = total_seconds // (seconds_per_day * 365)
- days = (total_seconds % (seconds_per_day * 365)) // seconds_per_day
- year += years
- day += days
- def calculate_hours_minutes(total_seconds, hours, minutes):
- seconds_per_hour = 60 * 60
- hours = (total_seconds % (24 * 60 * 60)) // seconds_per_hour
- minutes = (total_seconds % (60 * 60)) // 60
- def print_time(year, day, hours, minutes):
- print("Today's date is: Year -", year, "Day -", day)
- print("Current time is:", hours, ":", minutes)
- # 获取当前时间戳
- current_time = time.time()
- # 假设给定时间是2011年3月15日,求出今天的“天数”
- given_time = time.mktime((2011, 3, 15, 0, 0, 0, 0, 0, 0))
- day_number = (current_time - given_time) / (24 * 60 * 60)
- # 初始化年份、日期、小时和分钟的变量
- year = 2011
- day = 83
- hours = 0
- minutes = 0
- # 调用函数来计算年份、日期、小时和分钟
- calculate_year_day(current_time, year, day)
- calculate_hours_minutes(current_time, hours, minutes)
- # 调用函数来打印时间
- print_time(year, day, hours, minutes)
复制代码
这个解决方案中,我们使用了三个函数来处理不同的计算和打印任务。首先,我们使用给定时间和当前时间戳来计算今天的“天数”。然后,我们使用计算得到的“天数”来更新年份和日期。接下来,我们使用当前时间戳来计算小时和分钟。最后,我们使用打印函数来输出计算得到的时间。请注意,这只是一个简化的解决方案,没有考虑闰年和月份。如果你想要处理闰年和打印月份,你可以在相关的函数中加入更多的逻辑。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
|
|