0

returnの値が-1の場合にどのように行を出力するか知りたいです。また、-1が何をするのか(真の場合は1、偽の場合は0など)はわかりませんが、-1の場合はどうなりますか。

#include <stdio.h>

struct date {
    int day, month, year;
};

int compare_dates(struct date d1, struct date d2) {
    if(d1.year < d2.year)
        return -1;
    else if(d1.year > d2.year)
        return 1;
    else if(d1.month < d2.month) 
            return -1;
    else if(d1.month > d2.month) 
            return 1;
    else if(d1.day < d2.day) 
            return -1;
    else if(d1.day > d2.day)
            return 1;
    else
        return 0;


}

int main(void) {
    struct date d1, d2;
    printf("Enter first date (dd/mm/yyyy): ");
    scanf("%2d/%2d/%4d", &d1.day, &d1.month, &d1.year);
    printf("Enter second date (dd/mm/yyyy): ");
    scanf("%2d/%2d/%4d", &d2.day, &d2.month, &d2.year);

    if(compare_dates(d1, d2)) 
        printf("Date1 comes after than Date2");
    else if(!compare_dates(d1, d2))
        printf("Date1 and Date2 are equal");
    else if(-1)
        printf("Date1 comes before than Date2");

}
4

6 に答える 6

2

compare_dates関数を見ると、1日付d1が日付d2よりも大きい場合に返され、日付d1が日付d2よりも大きい場合に返され、両方の日付がである場合に返されるgreaterよう-1lesser見え0ますsame

したがって、コードは次のようになります。

   int main(void) 
   {
        struct date d1, d2;
        int result;
        printf("Enter first date (dd/mm/yyyy): ");
        scanf("%2d/%2d/%4d", &d1.day, &d1.month, &d1.year);
        printf("Enter second date (dd/mm/yyyy): ");
        scanf("%2d/%2d/%4d", &d2.day, &d2.month, &d2.year);

        result = compare_dates(d1, d2);
        if(1 == result)
            printf("Date1 comes after than Date2");
        else if(0 == result)
            printf("Date1 and Date2 are equal");
        else if(-1 == result)
            printf("Date1 comes before than Date2");

   }
于 2013-03-17T14:49:31.260 に答える
1

これ:

if(compare_dates(d1, d2)) 
    printf("Date1 comes after than Date2");
else if(!compare_dates(d1, d2))
    printf("Date1 and Date2 are equal");
else if(-1)
    printf("Date1 comes before than Date2");

めちゃくちゃです。compare_datesを1回呼び出して、その結果をさまざまな可能性と比較する必要があります。

特に、if(-1)それは常に真実であるため、ナンセンスです。

于 2013-03-17T14:48:55.457 に答える
1

Cでは、0は偽、その他はすべて真です。

あなたのコードではあなたがしなければならないでしょう

if(compare_dates(d1, d2) == 1) 
    printf("Date1 comes after than Date2");
else if(!compare_dates(d1, d2))
    printf("Date1 and Date2 are equal");
else if(compare_dates(d1, d2) == -1)
    printf("Date1 comes before than Date2");

または、より効率的

int value = compare_dates(d1, d2)
if(value == 1) 
    printf("Date1 comes after than Date2");
else if(!value)
    printf("Date1 and Date2 are equal");
else if(value == -1)
    printf("Date1 comes before than Date2");

比較関数の場合、通常、0は値が等しいことを意味し、-1は最初の引数が2番目の引数よりも小さいことを意味し、1は最初の引数が2番目の引数よりも大きいことを意味します

于 2013-03-17T14:49:58.187 に答える
1

dates_are_equal0または1(またはより一般的には0または非0)を返す、と呼ばれる関数がある場合、結果を条件として直接使用することは理にかなっています。

if (dates_are_equal(d1, d2)) {
    printf("Date1 and Date2 are equal\n");
}
else {
    printf("Date1 and Date2 are not equal\n");
}

ただし、compare_dates関数は3つの異なる意味を持つ3つの異なる整数値のいずれかを返すことができます。結果は条件ではなく数値になるため、条件として扱わないでください。代わりに、結果を別の整数と比較して条件を作成します。

これを行うにはいくつかの方法があります。strcmp()より小さい、等しい、またはより大きい0(必ずしも、、、またはだけ-1ではない)値を返すことによって比較の結果を示す、との類推により、次のように書くことができます。0-1

int comparison = compare_dates(d1, d2);
if (comparison < 0) {
    printf("Date1 comes before Date2\n");
}
else if(comparison == 0) {
    printf("Date1 and Date2 are equal\n");
}
else { // No need to test again here
    printf("Date1 comes after Date2\n");
}

、、、または、compare_dates()のみを返すことができると想定したい場合は、switchステートメントを使用できます。-101

switch (compare_dates(d1, d2)) {
    case -1:
        printf("Date1 comes before Date2\n");
        break;
    case 0:
        printf("Date1 and Date2 are equal\n");
        break;
    case 1:
        printf("Date1 comes after Date2\n");
        break;
    default:
        fprintf(stderr, "Internal error, unexpected result\n");
        exit(EXIT_FAILURE);
}

の代わりに使用することはお勧めしません。これは完全に合法であり、コンパイラにとってまったく同じことを意味しますが、私の意見では、実際には論理的にブール値である値のために演算子を予約する方が良いと思います。(!comparison)(comparison == 0)!

ブール式以外の式を、書き込みなどの条件として扱うのが一般的な方法です。

if (!strcmp(s1, s2)) {
    /* the strings pointed to by s1 and s2 are equal */
}

また

if (!ptr) {
    /* ptr is a null pointer */
}

しかし、より明示的にすると、コードが読みやすくなることがわかりました。

if (strcmp(s1, s2) == 0) {
    /* the strings pointed to by s1 and s2 are equal */
}

また

if (ptr != NULL) {
    /* ptr is a null pointer */
}
于 2013-03-17T15:14:20.610 に答える
0

Cではそれ!= 0が真実です。

if(-1) // = true
if(0)  // = false
if(1)  // = true
if(1234567) // = true

戻り値に応じてメッセージを出力するには、次を使用しますswitch

switch(compare_dates(d1, d2)){
    case -1: {
        // do whatever should done when compare_dates returns -1
        break; //don't forget the break
    }
    case 0: {
        // do whatever should done when it returns 0
        break;
    }
    case 1: {
        // ...
        break;
    }
}
于 2013-03-17T14:51:06.650 に答える
0

実際、0はd1とd2が等しいことを意味します。説明させてください:

-1は、d1がd2よりも小さいことを意味します。<したがって、 -1に対応する「より小さい」演算子。0は、d1がd2に等しいことを意味します。したがって、対応する「等しい」演算子==。1は、d1がd2より大きいことを意味します。>したがって、 1に対応する「より大きい」演算子。

strcmpこれは単なる慣例であり、おそらく同じ慣習を使用するC標準関数の影響を受けています。

于 2013-03-17T14:52:33.357 に答える