简体   繁体   English

C中的日期计算器程序

[英]Program for Date calculator in C

I'm trying to write a date calculator in C program with the range from 1/1/1902 to 12/31/2299, I've follow the algorithm from http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week with the century table,month table and day table, but when I tried to print out, this is what I have我正在尝试在 C 程序中编写一个日期计算器,范围从 1902 年 1 月 1 日到 2299 年 12 月 31 日,我遵循http://en.wikipedia.org/wiki_week/theCalculating_the_day_of中的算法世纪表,月表和日表,但是当我尝试打印出来时,这就是我所拥有的

Enter Year, Month and Day as YYYY,M,DD
1982 4 24
Saturday1982 ,6, 24, is  6

Instead of saying 1982 4 24 is a Saturday而不是说1982 4 24 is a Saturday

What is wrong in the program I wrote?我写的程序有什么问题? The placement of the switch cases?开关盒的位置?

#include <stdio.h>

int main (void)
{
    // insert code here...
    int day,month,year;

    printf("Enter Year, Month and Day as YYYY,M,DD\n");

    scanf("%4d%d%2d", &year, &month, &day);

    if (year >= 1901 && year <= 2299 &&
        month >= 1 && month <= 12 &&
        day >= 0 && day <= 31)
    {
        int century = year/100;

        /* making a century table for calculation*/

        switch(century)
        {
            case 19:
                century=0;
                break;
            case 20:
                century=6;
                break;
            case 21:
                century=4;
                break;
            case 22:
                century=2;
                break;
        }

        int last2_of_year= year % 100;

        /* Last 2 digits of the year entered*/

        int last2_div_4 =  last2_of_year/4;

        switch (month)
        {
            case 1:
                month=0;
                break;

            case 2:
                month=3;
                break;
            case 3:
                month=3;
                break;
            case 4:
                month=6;
                break;
            case 5:
                month=1;
                break;
            case 6:
                month=4;
                break;
            case 7:
                month=6;
                break;
            case 8:
                month=2;
                break;
            case 9:
                month=5;
                break;
            case 10:
                month=0;
                break;
            case 11:
                month=3;
                break;
            case 12:
                month=5;
                break;
         }


        int total_num = (century+ last2_of_year +day +month +last2_div_4)%7;

        switch (total_num) 
        {
            case 0:
            printf("Sunday");
                break;
            case 1:
            printf("Monday");
                break;
            case 2:
            printf("Tuesday");
                break;
            case 3:
            printf("Wednesday");
                break;
            case 4:
            printf("Thursday");
                break;
            case 5:
            printf("Friday");
                break;
            case 6:
            printf("Saturday");
                break;
        }

        printf("%d ,%d, %d, is a %d", year,month,day,total_num);
        }
        else
        {
            printf("invalid\n");
        }



    return 0;

    }

You say:你说:

printf("%d ,%d, %d, is a %d", year,month,day,total_num);

That's going to print这是要打印的

L, M, N, is a P

where L , M , N , and P are numbers.其中LMNP是数字。

You need that printf() before the name-of-days switch and need to remove the final %d and the total_num from the printf .您需要在日期名称switch之前使用printf() ,并且需要从printf中删除最后的%dtotal_num Then that printf will print然后printf将打印

L, M, N, is a

and the printf s in the name-of-days switch will print out the name of the day on the same line, giving you并且日期名称switch中的printf将在同一行打印出当天的名称,给你

L, M, N, is a XXXXXXXXXXX

Edited to address comment:编辑地址评论:

Look at your program for output statements.查看您的程序中的 output 语句。

The first output statement encountered are the printf calls in the name-of-days switch that prints out the day names.遇到的第一个 output 语句是打印出日期名称的日期名称开关中的printf调用。 So when your program runs given the input you mention, the first thing that will be printed out is因此,当您的程序根据您提到的输入运行时,将打印出的第一件事是

Saturday

Then after the name-of-days switch the next printf is然后在日期名称切换之后,下一个printf

printf("%d ,%d, %d, is a %d", year,month,day,total_num);

Since year is 1982 , month is 4 , day is 24 , and total_num is 6 , that printf will output由于year1982month4day24total_num6 ,所以printf将 output

1982, 4, 24, is a 6

on the same line as the previous Saturday output, which means the entire output is与上Saturday output在同一行,也就是说整个output是

Saturday1982, 4, 24, is a 6

Looks like @QuantumMechanic found the root cause of the problem, but I'd like to suggest a few changes:看起来@QuantumMechanic 找到了问题的根本原因,但我想建议一些更改:

    int century = year/100;

    /* making a century table for calculation*/

    switch(century)
    {
        case 19:
            century=0;
            break;

I'm very leery of using a single variable to represent two different things.使用单个变量来表示两种不同的事物持怀疑态度。 Here, century represents both the user-input human-readable century and an offset to the first day of the week for the century.在这里, century既代表用户输入的人类可读世纪,又代表该世纪一周的第一天的偏移量。 Two variables would give clearer code, and allow you to re-use the century information later, should the need arise.如果需要,两个变量将提供更清晰的代码,并允许您在以后重新使用century信息。

Second, using a case statement to store offsets for the months feels a little... overdone:其次,使用case语句来存储几个月的偏移量感觉有点……过头了:

    switch (month)
    {
        case 1:
            month=0;
            break;

        case 2:
            month=3;
            break;
        case 3:
            month=3;
            break;

This could instead be handled with an array lookup:这可以通过数组查找来处理:

int leap_month[] = [-1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
int norm_month[] = [-1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];

if (leap_year)
    month_offset = leap_month[month];
else
    month_offset = norm_month[month];

The -1 is just to allow referring to the table with human-friendly indexes ( Jan == 1 ). -1只是为了允许引用具有人类友好索引的表( Jan == 1 )。 Feel free to remove it and use leap_month[month-1] or similar if you find that easier.如果您觉得更容易,请随意删除它并使用leap_month[month-1]或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM