I don't really have the conditional branch in mind to determine the date and time.

Asked 2 years ago, Updated 2 years ago, 33 views


When a date is entered prior to October 14, 1582, the output reads
Not supported for the number of days prior to that date. In other words, under the condition that you print the day of the week after that day,

By entering the Western calendar, month, and date, I would like to create a program that prints the day of the week.
I don't know how to create a program that distinguishes between "before October 14, 1582" and "after October 14, 1582", so
For example, if you enter it like January 12, 2001,
"Not available before October 14, 1582.
will be printed as

I created the program as follows.
(The method we used this time is Zeller's formula.)

#include<stdio.h>
int main (void)
{
    int year, m, q, k, j, h;
    k = year%100;
    j = year/100;
    h=(q+(m+1)*26/10+k+k/4+j/4+5*j)%7;
    printf("year:");
    scanf("%d", & year);
    printf("Month:");
    scanf("%d", & m);
    printf("Day:");
    scanf("%d", & q);


    /* On or after October 15, 1582, */
    if(year>=1582&(m>=10||q>=15)&h==1){
    printf("%4d year %2d month %2d day is Sunday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==2){
    printf("%4d year %2d month %2d day is Monday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==3){
    printf("%4d year %2d month %2d day is Tuesday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==4){
    printf("%4d year %2d month %2d day is Wednesday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==5){
    printf("%4d year %2d month %2d day is Thursday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==6){
    printf("%4d year %2d month %2d day is Friday", year, m, q);
    }
    else if(year>=1582&&(m>=10||q>=15)&h==7){
    printf("%4d year %2d month %2d day is Saturday", year, m, q);
    }

        /* On or before October 14, 1582, */
    else{
    printf("Not supported before October 14, 1582);
    }
       
    return 0;
}

Run Results
$ ./a.out
Year: 2001
Month: 1
Day: 12
Not available before October 14, 1582.

How should I modify the ( ) part of if()

c

2022-09-30 17:10

2 Answers

The year, month, and day are determined separately, so the conditions are not met.
If you're using Zeller's formula, why don't you use Fairfield's formula to calculate the number of days after October 14, 1582.


2022-09-30 17:10

Answer your question (solved).
As Metropolis said, bringing the three lines above the if statement resulted in the expected result.


2022-09-30 17:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.