7

2 つの日付の間の日数を検出するプログラムを作成しましたが、問題がいくつかあります。私がそれを読んだとき、ロジックは私の頭の中で完全に理にかなっています。

まず、異なる年の 2 つの日付を入力すると、出力は常に約 1 か月ずれます (ほとんどの場合は 31 ですが、1 つの場合は 32 です...数字を見てください)。次に、ちょうど 1 か月離れた 2 つの日付は、2 番目の月の日数を返します (つまり、1/1/1 から 2/1/1 は 28 になります)。このプログラムには不可避的に他にも奇妙なことがいくつかありますが、私が何を間違っているのかを理解するのに十分な情報であることを願っています. 私の人生では、これを自分で理解することはできません。私はCに比較的慣れていないので、優しくしてください=)

ありがとう

// Calculates the number of calendar days between any two dates in history (beginning with 1/1/1).

#include <stdio.h>
#include <stdlib.h>

void leap(int year1, int year2, int *leap1, int *leap2);
void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2, int *leap1, int *leap2);

int main(void)
{
        int month1, day1, year1, month2, day2, year2, leap1, leap2;
        int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
        int daysPerMonthLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

        leap(year1, year2, &leap1, &leap2);
        date(&month1, &day1, &year1, &month2, &day2, &year2, &leap1, &leap2);

        if(year1 == year2)
        {
                int i, total;

                if(month1 == month2)                            // Total days if month1 == month2
                {
                        total = day2 - day1;
                        printf("There are %d days between the two dates.", total);
                }
                else
                {
                    if(leap1 == 1)
                        total = daysPerMonthLeap[month1] - day1;
                    else
                        total = daysPerMonth[month1] - day1;

                    for(i = month1 + 1; i < month2; i++)        // Days remaining between dates (excluding last month)
                    {
                        if(leap1 == 1)
                            total += daysPerMonthLeap[i];
                        else
                            total += daysPerMonth[i];
                    }

                    total += day2;                              // Final sum of days between dates (including last month)

                    printf("There are %d days between the two dates.", total);
                }
        }
        else                                                    // If year1 != year2 ...
        {
                int i, total, century1 = ((year1 / 100) + 1) * 100, falseleap = 0;

                if(leap1 == 1)
                    total = daysPerMonthLeap[month1] - day1;
                else
                    total = daysPerMonth[month1] - day1;

                for(i = month1 + 1; i <= 12; i++)               // Day remaining in first year
                {
                    if(leap1 == 1)
                        total += daysPerMonthLeap[i];
                    else
                        total += daysPerMonth[i];
                }

                for(i = 1; i < month2; i++)                     // Days remaining in final year (excluding last month)
                {
                    if(leap2 == 1)
                        total += daysPerMonthLeap[i];
                    else
                        total += daysPerMonth[i];
                }

                int leapcount1 = year1 / 4;                     // Leap years prior to and including first year
                int leapcount2 = year2 / 4;                     // Leap years prior to and NOT including final year
                if(year2 % 4 == 0)
                        leapcount2 -= 1;

                int leaptotal = leapcount2 - leapcount1;        // Leap years between dates

                for(i = century1; i < year2; i += 100)          // "False" leap years (divisible by 100 but not 400)
                {
                        if((i % 400) != 0)
                                falseleap += 1;
                }

                total += 365 * (year2 - year1 - 1) + day2 + leaptotal - falseleap;      // Final calculation
                printf("There are %d days between the two dates.", total);
        }
        return 0;
}

void leap(int year1, int year2, int *leap1, int *leap2)             // Determines if first and final years are leap years
{
        if(year1 % 4 == 0)
        {
                if(year1 % 100 == 0)
                {
                        if(year1 % 400 == 0)
                                *leap1 = 1;
                        else
                                *leap1 = 0;
                }
                else
                        *leap1 = 1;
        }
        else
                *leap1 = 0;

        if(year2 % 4 == 0)
        {
                if(year2 % 100 == 0)
                {
                        if(year2 % 400 == 0)
                                *leap2 = 1;
                        else
                                *leap2 = 0;
                                }
                else
                        *leap2 = 1;
        }
        else
                *leap2 = 0;
}

void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2, int *leap1, int *leap2)
{
        for(;;)                     // Infinite loop (exited upon valid input)
        {
                int fail = 0;
                printf("\nEnter first date: ");
                scanf("%d/%d/%d", month1, day1, year1);
                if(*month1 < 1 || *month1 > 12)
                {
                        printf("Invalid entry for month.\n");
                        fail += 1;
                }
                if(*day1 < 1 || *day1 > 31)
                {
                        printf("Invalid entry for day.\n");
                        fail += 1;
                }
                if(*year1 < 1)
                {
                        printf("Invalid entry for year.\n");
                        fail += 1;
                }
                if(daysPerMonth[month1] == 30 && *day1 > 30)
                {
                        printf("Invalid month and day combination.\n");
                        fail += 1;
                }
                if(*month1 == 2)
                {
                        if(*leap1 == 1 && *day1 > 29)
                        {
                            printf("Invalid month and day combination.\n");
                            fail += 1;
                        }
                        else if(*day1 > 28)
                        {
                            printf("Invalid month and day combination.\n");
                            fail += 1;
                        }
                }
                if(fail > 0)
                        continue;
                else
                        break;
        }

        for(;;)
        {
                int fail = 0;
                printf("\nEnter second date: ");
                scanf("%d/%d/%d", month2, day2, year2);
                if(*year1 == *year2)
                {
                        if(*month1 > *month2)
                        {
                                printf("Invalid entry.\n");
                                fail += 1;
                        }
                        if(*month1 == *month2 && *day1 > *day2)
                        {
                                printf("Invalid entry.\n");
                                fail += 1;
                        }
                }
                if(*month2 < 1 || *month2 > 12)
                {
                        printf("Invalid entry for month.\n");
                        fail += 1;
                }
                if(*day2 < 1 || *day2 > 31)
                {
                        printf("Invalid entry for day.\n");
                        fail += 1;
                }
                if(*year2 < 1)
                {
                        printf("Invalid entry for year.\n");
                        fail += 1;
                }
                if(daysPerMonth[month2] == 30 && *day2 > 30)
                {
                        printf("Invalid month and day combination.\n");
                        fail += 1;
                }
                if(*month2 == 2)
                {
                        if(*leap2 == 1 && *day2 > 29)
                        {
                            printf("Invalid month and day combination.\n");
                            fail += 1;
                        }
                        else if(*day2 > 28)
                        {
                            printf("Invalid month and day combination.\n");
                            fail += 1;
                        }
                }
                if(fail > 0)
                        continue;
                else
                        break;
        }
}
4

6 に答える 6

9

まず、そのleap関数は複雑すぎると感じます。1 回の関数呼び出しで両方の日付を指定する必要はありません。より簡潔に記述できるため、より明確に正確になります。これは簡潔ではありませんが、ロジックを簡単に確認できると確信しています。

int is_leap_year(int year) {
        if (year % 400 == 0) {
                return 1;
        } else if (year % 100 == 0) {
                return 0;
        } else if (year % 4 == 0) {
                return 1;
        } else {
                return 0;
        }
}

次のように呼び出すことができます。

int year1, year2, leap1, leap2;
year1 = get_input();
year2 = get_input();
leap1 = is_leap_year(year1);
leap2 = is_leap_year(year2);

ポインターがなく、コードの重複が大幅に少なくなります。はい、それをis_leap_year()1 つのステートメントに減らすことができることはわかっていますif(...)が、これは私にとっては読みやすいものです。

次に、インデックスが 0 の配列とインデックスが 1 の人間の月の間に不一致があると思います。

            if(*month1 < 1 || *month1 > 12)

    int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};

3 番目に、1 か月あたりの日数はもう少し適切に計算できると思います。

int days_in_month(int month, int year) {
        int leap = is_leap_year(year);
        /*               J   F   M   A   M   J   J   A   S   O   N   D */
        int days[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                           {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
        if (month < 0 || month > 11 || year < 1753)
                return -1;

        return days[leap][month];
}

ここでは、1 月が 0 であると仮定します。コードの残りの部分を強制的に一致させる必要があります。(この二重配列のトリックは、プログラミング スタイルの要素( 54 ページ) から学びました。) このようなルーチンを使用する最良の部分は、差分計算から飛躍条件を取り除くことです。

第 4 に、境界外の配列にインデックスを付けています。

            for(i = month1 + 1; i <= 12; i++)
            {
                if(leap1 == 1)
                    total += daysPerMonthLeap[i];

これは、インデックスが 0 の配列とインデックスが 1 の月の問題の別の例ですが、月を修正するときは、これも修正するようにしてください。

私はまだすべての問題を見つけていないのではないかと心配しています.入力後の最初と2番目の日付を並べ替えて、その検証コードをすべて削除する方が簡単かもしれません.計算の複雑な核心部分を考えるのが簡単です。beforeafter

于 2012-04-25T04:12:06.203 に答える
3

これは完全な答えではありません。うるう年を計算するためのより良い方法について言及したかっただけです(これはThe C Programming Language-ページ#41から取得)

if ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)
    printf("%d is a leap year \n", year);
else
    printf("%d is not a leap year \n", year);
于 2012-04-25T04:19:26.497 に答える
3

すべての月のインデックスを 1 減らします。

私が言いたいのは、1 月は or に対応し、ordaysPerMonth[0]に対応しdaysPerMonthLeap[0]ないということです。これが配列インデックスである理由は、0 から始まります。daysPerMonth[1]daysPerMonthLeap[1]

したがって、またはmonth1month2中で を使用している場合は、代わりにandを使用してください。daysPerMonth[]daysPerMonthLeap[]month1-1month2-1

これが十分に明確であることを願っています。それ以外の場合は、お気軽にコメントしてください。

于 2012-04-25T04:09:53.963 に答える
2

あなたのコード スニペットには複数の問題があります..しかし、それは非常に良い試みだと言わざるを得ません。あなたが達成しようとしていることへの近道はたくさんあります。

与えられた 2 つの日付の間の日数を求める次のプログラムを作成しました。これを参考にしてください。

#include <stdio.h>
#include <stdlib.h>

char *month[13] = {"None", "Jan", "Feb", "Mar", 
                   "Apr", "May", "June", "July", 
                   "Aug", "Sept", "Oct", 
                   "Nov", "Dec"};

/*
daysPerMonth[0] = non leap year
daysPerMonth[1] = leap year
*/
int daysPerMonth[2][13] = {{-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                           {-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

typedef struct _d {
    int day;        /* 1 to 31 */
    int month;      /* 1 to 12 */
    int year;       /* any */
}dt;

void print_dt(dt d)
{
    printf("%d %s %d \n", d.day, month[d.month], d.year);
    return;
}

int leap(int year)
{
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0) ? 1 : 0;
}

int minus(dt d1, dt d2)
{
    int d1_l = leap(d1.year), d2_l = leap(d2.year);
    int y, m;
    int total_days = 0;

    for (y = d1.year; y >= d2.year ; y--) {
        if (y == d1.year) {
            for (m = d1.month ; m >= 1 ; m--) {
                if (m == d1.month)  total_days += d1.day;
                else                total_days += daysPerMonth[leap(y)][m];
                // printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
            }
        } else if (y == d2.year) {
            for (m = 12 ; m >= d2.month ; m--) {
                if (m == d2.month)  total_days += daysPerMonth[leap(y)][m] - d2.day;
                else                total_days += daysPerMonth[leap(y)][m];
                // printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
            }
        } else {
            for (m = 12 ; m >= 1 ; m--) {
                total_days += daysPerMonth[leap(y)][m];
                // printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
            }
        }

    }

    return total_days;
}

int main(void)
{
    /* 28 Oct 2018 */
    dt d2 = {28, 10, 2018};

    /* 30 June 2006 */
    dt d1 = {30, 6, 2006};

    int days; 

    int d1_pt = 0, d2_pt = 0;

    if (d1.year  > d2.year)     d1_pt += 100;
    else                        d2_pt += 100;
    if (d1.month > d2.month)    d1_pt += 10;
    else                        d2_pt += 10;
    if (d1.day   > d2.day)      d1_pt += 1;
    else                        d2_pt += 1;

    days = (d1_pt > d2_pt) ? minus(d1, d2) : minus(d2, d1);

    print_dt(d1);
    print_dt(d2);
    printf("number of days: %d \n", days);

    return 0;
}

出力は次のとおりです。

$ gcc dates.c 
$ ./a.out 
30 June 2006 
28 Oct 2018 
number of days: 4503 
$ 

注: これは完全なプログラムではありません。入力検証がありません。

それが役に立てば幸い!

于 2012-04-25T06:25:09.680 に答える
2

変化する

int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int daysPerMonthLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

int daysPerMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int daysPerMonthLeap[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};

つまり、すべてのコードは要素 0 ではなく要素 1 から始まる配列値に依存しているため、配列の先頭にパディングします。

それはあなたが不平を言ったエラーを取り除きます。

day2もう 1 つの問題は、合計に加算するときの 1 つずれのエラーです。どちらの場合も、day2 - 1ではなく追加する必要がありますday2。これは、0 ではなく 1 から始まる日付インデックスによるものでもあります。

これらの変更 (およびコードをコンパイルするためのいくつかの変更) を行った後、正常に動作します。

于 2012-04-25T04:34:01.670 に答える
0
//Difference/Duration between two dates
//No need to calculate leap year offset or anything
// Author: Vinay Kaple
# include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int days_add, days_sub, c_date, c_month, b_date, b_month, c_year, b_year;
    cout<<"Current Date(dd mm yyyy): ";
    cin>>c_date>>c_month>>c_year;
    cout<<"Birth Date(dd mm yyyy): ";
    cin>>b_date>>b_month>>b_year;
    int offset_month[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    days_add = c_date + offset_month[c_month-1];
    days_sub = b_date + offset_month[b_month-1];
    int total_days = (c_year-b_year)*365.2422 + days_add - days_sub+1;
    cout<<"Total days: "<<total_days<<"\n";
    int total_seconds = total_days*24*60*60;
    cout<<"Total seconds: "<<total_seconds<<"\n";
    return 0;
}
于 2016-05-07T04:44:18.230 に答える