活动介绍
file-type

Python脚本实现闰年判定与天数计算

ZIP文件

下载需积分: 5 | 1KB | 更新于2024-12-16 | 29 浏览量 | 0 下载量 举报 收藏
download 立即下载
是一个Python程序,它的主要功能是判断一个给定的年份是否为闰年,并且能够计算出该年之后的天数。该程序根据闰年的定义来进行判断,即如果一个年份能被4整除且不能被100整除,或者能被400整除,那么这个年份就是闰年。在闰年中,2月会有29天,而非闰年的2月只有28天。程序通过计算一年剩余的天数来提供关于日期的有用信息。 对于闰年的判断逻辑,程序会首先检查输入的年份是否满足上述条件。如果满足,程序会输出该年为闰年,并且可能还会提供从该年第一天开始到年底的总天数。如果不是闰年,程序也会告知用户,并给出相应的天数。 要实现这样的程序,Python语言提供了一些内置的函数和特性,例如使用 % 运算符来进行模运算,以及使用 if-else 条件语句来根据年份的特定条件做出逻辑判断。此外,计算一年中的天数时,程序可能还会涉及到日期的处理,Python中可以使用 datetime 或 timedelta 等模块来方便地处理日期和时间。 Python是一种高级编程语言,它强调简洁明了的语法和代码可读性,非常适合用于快速开发各种应用程序。在处理日期和时间方面,Python内置了专门的模块,如 time 和 datetime,这些模块提供了丰富的函数和方法来获取当前时间、格式化日期时间对象、计算日期之间的差异等。在这个 "Year_check" 程序中,可能会用到这些模块中的某些功能来帮助确定年份是否为闰年以及计算天数。 在编写此类程序时,程序员需要对Python编程语言有深入的理解,特别是对条件判断、函数定义、模块导入等基础概念。同时,还需要掌握相关的库和模块的使用方法。对于"Year_check"这样的程序,除了Python的基础知识,还需要对日期和时间的计算有一定的了解。 总的来说,"Year_check"这个程序是对Python编程技能的实际应用,它通过一个具体的场景—判断闰年并计算后续天数,展示了如何利用Python编程解决问题的能力。在开发这样的程序时,程序员不仅需要掌握Python语言的基本语法,还需要对程序设计的逻辑结构有清晰的认识,以及对相关模块的熟练使用。这个程序可以作为学习Python编程的一个实战案例,帮助学习者更好地理解和掌握编程知识。

相关推荐

filetype

其能够通过串口输入一次性配置好RTC时钟,不用分步进行配置。例如串口2025-01-01 15:00:10,就成功配置RTC时钟#include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); /* get RTC clock entry selection */ RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9); /* check if RTC has aready been configured */ if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){ /* backup data register value is not correct or not yet programmed or RTC clock source is not configured (when the first time the program is executed or data in RCU_BDCTL is lost due to Vbat feeding) */ rtc_setup(); }else{ /* detect the reset source */ if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){ }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){ } rtc_show_time(); // rtc_show_alarm(); } rcu_all_reset_flag_clear(); /* configure the leds */ // gd_eval_led_init(LED1); // gd_eval_led_on(LED1); // gd_eval_led_init(LED2); // gd_eval_led_on(LED2); // rtc_flag_clear(RTC_FLAG_ALRM0); // exti_flag_clear(EXTI_17); // /* RTC alarm interrupt configuration */ // exti_init(EXTI_17,EXTI_INTERRUPT,EXTI_TRIG_RISING); // nvic_irq_enable(RTC_Alarm_IRQn,0,0); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_setup(void) { /* setup RTC time value */ uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; /* current time input */ printf(" please set the last two digits of current year:\n\r"); while(tmp_year == 0xFF) { tmp_year = usart_input_threshold(99); rtc_initpara.year = tmp_year; } printf(" 20%0.2x\n\r", tmp_year); printf(" please input month:\n\r"); while(tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); rtc_initpara.month = tmp_month; } printf(" %0.2x\n\r", tmp_month); printf(" please input day:\n\r"); while(tmp_day == 0xFF) { tmp_day = usart_input_threshold(31); rtc_initpara.date = tmp_day; } printf(" %0.2x\n\r", tmp_day); printf(" please input hour:\n\r"); while (0xFF == tmp_hh){ tmp_hh = usart_input_threshold(23); rtc_initpara.hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); printf(" please input minute:\n\r"); while (0xFF == tmp_mm){ tmp_mm = usart_input_threshold(59); rtc_initpara.minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf(" please input second:\n\r"); while (0xFF == tmp_ss){ tmp_ss = usart_input_threshold(59); rtc_initpara.second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); /* RTC current time configuration */ if(ERROR == rtc_init(&rtc_initpara)){ printf("\n\r** RTC time configuration failed! **\n\r"); }else{ printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } // /* setup RTC alarm */ // tmp_hh = 0xFF; // tmp_mm = 0xFF; // tmp_ss = 0xFF; // rtc_alarm_disable(RTC_ALARM0); // printf("=======Input Alarm Value=======\n\r"); // rtc_alarm.alarm_mask = RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; // rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED; // rtc_alarm.alarm_day = 0x31; // rtc_alarm.am_pm = RTC_AM; // /* RTC alarm input */ // printf(" please input Alarm Hour:\n\r"); // while (0xFF == tmp_hh){ // tmp_hh = usart_input_threshold(23); // rtc_alarm.alarm_hour = tmp_hh; // } // printf(" %0.2x\n\r", tmp_hh); // printf(" Please Input Alarm Minute:\n\r"); // while (0xFF == tmp_mm){ // tmp_mm = usart_input_threshold(59); // rtc_alarm.alarm_minute = tmp_mm; // } // printf(" %0.2x\n\r", tmp_mm); // printf(" Please Input Alarm Second:\n\r"); // while (0xFF == tmp_ss){ // tmp_ss = usart_input_threshold(59); // rtc_alarm.alarm_second = tmp_ss; // } // printf(" %0.2x", tmp_ss); // /* RTC alarm configuration */ // rtc_alarm_config(RTC_ALARM0,&rtc_alarm); // printf("\n\r** RTC Set Alarm Success! **\n\r"); // rtc_show_alarm(); // rtc_interrupt_enable(RTC_INT_ALARM0); // rtc_alarm_enable(RTC_ALARM0); } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { // uint32_t time_subsecond = 0; // uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0; rtc_current_time_get(&rtc_initpara); /* get the subsecond value of current time, and convert it into fractional format */ // time_subsecond = rtc_subsecond_get(); // subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100; // subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10; // subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10; printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \ rtc_initpara.year, rtc_initpara.month, rtc_initpara.date); printf(" : %0.2x:%0.2x:%0.2x \r\n", \ rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; }

可爱的小树懒
  • 粉丝: 28
上传资源 快速赚钱