用于在给定月份,日期和给定月份的第1天查找日期的工作日的程序

问题描述 投票:0回答:1

输出示例:

>Hello! Welcome to the day calculator Enter a month to check: (1-Jan, 2-Feb, etc) 2
>Enter day to check: 21
>Enter the weekday of the 1st of the month(1-Sunday, 2-Monday, etc) 5
>The 21.2 will be a Wednesday

我可以使用任何公式来缩短此代码吗?

c algorithm logic
1个回答
0
投票

由于没有给出年份,无法确定2月份是否会有28天或29天。所以,没有费心去处理错误的输入。如果你输入一些不可能的日期值而不是错误,你将得到错误的输出。

void main()
{
    int month, day, date, first;
    printf("enter the month\n");
    scanf("%d",&month);
    printf("enter the date\n");
    scanf("%d",&date);
    printf("enter the 1st day of the month\n");
    scanf("%d",&first);
    day = (date+first-2)%7;
    if(day==0)
        printf("sunday\n");
    else if(day==1)
        printf("monday\n");
    else if(day==2)
        printf("tuesday\n");
    else if(day==3)
        printf("wednesday\n");
    else if(day==4)
        printf("thursday\n");
    else if(day==5)
        printf("friday\n");
    else
        printf("saturday\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.