Given date output is the day of the week

Source: Internet
Author: User

UINT32 i_32u = 1990u, j_u32 = 0u, Year_flag = 0u, year_day_u32 = 0u, sum_day_u32 = 0u, weekday_u32 = 0u;for (i_32u = 1990 U i_32u <= year_u32; i_32u++) {Year_flag = Is_leap_year (year_u32), if (1u = = Year_flag) {year_day_u32 = 366u;} Else{year_day_u32 = 365u;} Switch (mouth_u32-1u) {case 1u:sum_day_u32 + = 31u;break;case 2u:sum_day_u32 + 31u + 28u + year_flag;break;case 3u:sum_da Y_u32 + + 31u + 28u + year_flag + 31u;break;case 4u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u;break;case 5u:sum_day_ U32 + + 31u + 28u + year_flag + 31u + 30u + 31u;break;case 6u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u + 31u + 30u; Break;case 7u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u;break;case 8u:sum_day_u32 + + 31u + 28u +  Year_flag + 31u + 30u + 31u + 30u + 31u + 31u;break;case 9u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u;break;case 10u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u + 31u;break ; Case 11u:sum_day_u32 + + 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u + 31u + 30u;break;default:break;}} Sum_day_u32 + = Day_u32;weekday_u32 = sum_day_u32% 7u + 3;if (weekday_u32! = 0u) {(void) osm_printf ("%d%d months%d days is the week%d\n", year _u32, Mouth_u32, Day_u32, weekday_u32);} else{(void) osm_printf ("%d-%d-month-%d-day is Sunday \ n", Year_u32, Mouth_u32, day_u32);} */}

This code is too water, and then think of a way, and this will be the problem is not correctly output the date of the correct date,

So I think of the good number of 1990, should be January 1, 1990 to do well is Monday

So I tidied up the code and wrote a better function.

UINT32 Week (UINT32 year_u32, UINT32 mouth_u32, UINT32 day_u32) {UINT32 day_code = 0u; UINT32 i32i = 0u; UINT32 U32_j = 0u; UINT32 Year_flag = 0u; UINT32 days = 0u; UINT32 i32_sum = 0u; UINT32 mouth[12] = {30u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};if (Mouth_u32 < 1u) | | (Mouth_u32 > 12u) | | (Day_u32 > 31u) | | (Year_u32 < 1990u)) {(void) osm_printf ("Input incorrect Data");} Else{for (i32i = 1990u; i32i < year_u32; i32i++) {Year_flag = Is_leap_year (YEAR_U32); if (1u = = Year_flag) {days + = 366 U }else{days + = 365u;}} for (i32i = 0u; i32i < (MOUTH_U32-1U), i32i++) {days + = mouth[i32i];} U32_j = days% 7u;switch (u32_j) {case 0u: (void) osm_printf ("Sunday \ n"); Break;case 1u: (void) osm_printf ("Monday \ n"); break;case 2u: (void) osm_printf ("Tuesday \ n"), break;case 3u: (void) osm_printf ("Wednesday \ n"); Break;case 4u: (void) osm_printf ("Thursday \ n"); Break;case 5u: (void) osm_printf ("Friday \ n"); Break;case 6u: (void) osm_printf ("Saturday \ n"); break;default:break;} }return 0u;}

So there's a benchmark that looks good.


Given date output is the day of the week

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.