解决问题
请问这一题怎么解决?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官方接口),如未能正确解答您的问题,请继续追问。 本帖最后由 jackz007 于 2023-12-7 17:00 编辑
#include <stdio.h>
#include <time.h>
int leapyear(int year)
{
return (! (year % 400) || (! (year % 4) && (year % 100))) ? 1 : 0 ;
}
int mdays(int year , int moonth)
{
int e[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} ;
return (leapyear(year) && moonth == 2) ? e + 1 : e ;
}
int ydays(int year)
{
return (leapyear(year)) ? 366 : 365 ;
}
int main(void)
{
time_t ds , ts , tk ;
int year , moonth , day , hour , minute , second;
time(& ts) ; // ts 为从格林威治时间 1970年1月1日0时0分0秒起算,到当前所经历的总秒数
ts += 3600 * 8 ; // 东8区时差(单位:秒),如果不加,算出来的日期将是格林威治标准时间
ds = ts / (3600 * 24) ; // ds 为通过 ts 计算的从 1970年1月1日起,到当前所经历的总天数
tk = ts % (3600 * 24) ; // tk 为 ts 去除天数后,所剩余不足 1 天的零头秒数
year = 1970 ;
while(ds >= ydays(year)) {
ds -= ydays(year) ;
year += 1 ;
}
moonth = 1 ;
while(ds >= mdays(year , moonth - 1)) {
moonth ++ ;
ds -= mdays(year , moonth - 1) ;
}
day = ds + 1 ;
hour = tk / 3600 ;
minute = tk % 3600 / 60 ;
second = tk % 3600 % 60 ;
printf("%d-%02d-%02d %02d:%02d:%02d\n" , year , moonth , day , hour , minute , second) ;
}
页:
[1]