2013-03-17 117 views
0

我想知道當返回值爲-1時如何打印行。另外,我不知道-1是做什麼的,例如1代表真,0代表假,但是-1代表什麼。返回語句中的-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"); 

} 
+0

「-1」可以表示任何你想要的。 – SLaks 2013-03-17 14:47:38

+0

'compare_dates()'的結果不是一個條件,它是一個數字。 – 2013-03-17 14:56:31

回答

1

如果你有一個名爲dates_are_equal函數,返回0或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函數可以返回三個不同的整數值中的任何一個,具有三個不同的含義。結果是一個數字,而不是一個條件,所以不要把它當作一個條件。相反,通過將結果與另一個整數進行比較來構建條件。

有幾種方法可以做到這一點。通過類比strcmp(),它通過返回的值小於,等於或大於0表示比較的結果(不一定只是-10,或-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()只能返回-10,或1,你可以使用switch語句:

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 */ 
} 
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並將其結果與各種可能性進行比較。

特別是,if(-1)是無稽之談,因爲它永遠是真實的。

2

通過查看compare_dates功能似乎是,如果日D1是greater除日期D2,-1會,如果日期D1將lesser除日期d2和0將被退回,如果這兩個日期都same返回1將被退回。

所以,你的代碼應該象下面這樣:

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"); 

    } 
+0

'1 ==結果'有時被稱爲[「尤達條件」](http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html)。它避免了不小心寫入'if(result = 1)'而不是'if(result == 1)',但恕我直言,它不值得丟失可讀性,特別是因爲大多數編譯器會警告有關作爲條件的分配。 – 2013-03-17 14:54:21

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 usualy表示值相等,-1表示第一個參數是小比第二,1意味着第一個參數大於第二個

+0

使用'!value'而不是'value == 0'是恕我直怕的混淆。 'value'不是布爾值。 – 2013-03-17 14:51:50

+0

這並不令人困惑。它實際上是一種常見的做法,例如:if(!strcmp(str1,str2)){} – 2013-03-17 14:52:47

+0

我知道這是常見的做法。這並不是一個好主意。我發現'if(strcmp(str1,str2)== 0){...}'更容易閱讀。 – 2013-03-17 14:55:50

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; 
    } 
} 
0

實際上,0意味着d1和d2相等。請允許我解釋:

-1表示d1小於d2。因此,對應於-1的「小於」運算符<。 0表示d1等於d2。因此「等於」運算符==對應。 1表示d1大於d2。因此,「大於」運算符>對應於1.

這只是慣例,可能受使用相同約定的C標準strcmp函數的影響。