2013-03-17 122 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函数的影响。