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

时间:2018-12-15 10:56:24

标签: c algorithm logic

输出示例:

>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

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

1 个答案:

答案 0 :(得分: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");
}