C4 Comparing Structure

  • Upload
    justme8

  • View
    221

  • Download
    0

Embed Size (px)

DESCRIPTION

as

Citation preview

  • 7/18/2019 C4 Comparing Structure

    1/2

    #include #include

    typedef struct { char name[30]; int quantity; }fruit;

    int compare ( fruit a, fruit b);int compare_advance ( fruit a, fruit b);

    int main() { int result; int result_2; fruit fruit_1, fruit_2; printf ("Please enter the name of the first fruit and the quantity.\n"); scanf ("%s" "%d",&fruit_1.name,&fruit_1.quantity); printf ("Please enter the name of the second fruit and the quantity.\n"); scanf ("%s" "%d",&fruit_2.name,&fruit_2.quantity); result= compare (fruit_1, fruit_2); result_2= compare_advance (fruit_1, fruit_2); printf ("comparison (only 0 and 1): %d\n", result);

    printf ("comparison (0, 1 and -1): %d", result_2); }

    /*return 0 or 1 ( 0 when same and 1 when different).*/

    int compare ( fruit a, fruit b) { fruit *ptr_1, *ptr_2; ptr_1=&a; ptr_2=&b; if (strcmp((ptr_1->name),(ptr_2->name))==0 && ((ptr_1->quantity)==(ptr_2->qu

    antity))) { return 0; } else { return 1; }

    }

    /*return 0 (when same) and 1 when the quantity is larger and -1 when the quantity

    is smaller*/

    int compare_advance ( fruit a, fruit b) { fruit *ptr_1, *ptr_2; ptr_1=&a; ptr_2=&b; if (strcmp((ptr_1->name),(ptr_2->name))==0 && ((ptr_1->quantity)==(ptr_2->quantity)))

  • 7/18/2019 C4 Comparing Structure

    2/2

    { return 0; } else { if (ptr_1->quantity > ptr_2->quantity) //comparing the quantity ofthe fruits { return 1; } else { return -1; } } }