Polymorphism in C Language

Source: Internet
Author: User

# If 1

// Using macros in C language for Polymorphism

# Include <stdio. h>

# Define max (A, B) (a)> (B ))? (A): (B ))

Int main ()
{
Int RES1;
Double RES2;

RES1 = max (5, 9 );
RES2 = max (5.6, 4.99 );
Printf ("the max of 5 and 9 is: % d/N", RES1 );
Printf ("the max of 5.5 and 4.9 is: % F/N", RES2 );
Return 0;
}
/*
The max of 5 and 9 is: 9
The max of 5.5 and 4.9 is: 5.600000
Press any key to continue
*/

# Endif

# If 0

// Polymorphism implemented by pointer in the second C Language
/* Qsort. C: This program reads the command-line
* Parameters and uses qsort to sort them. it
* Then displays the sorted arguments.
*/

# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>

// Void qsort (void * base, size_t num, size_t width, INT (_ cdecl * compare) (const void * elem1, const void * elem2 ));

Int compare (const void * arg1, const void * arg2 );

Void main (INT argc, char ** argv)
{
Int I;
/* Eliminate argv [0] From sort :*/
Argv ++;
Argc --;
 
/* Sort remaining ARGs using quicksort algorithm :*/
Qsort (void *) argv, (size_t) argc, sizeof (char *), compare );
 
/* Output sorted list :*/
For (I = 0; I <argc; ++ I)
Printf ("% s", argv [I]);
Printf ("/N ");
}

Int compare (const void * arg1, const void * arg2)
{
/* Compare all of both strings :*/
Return _ stricmp (* (char **) arg1, * (char **) arg2 );
}
# Endif

# If 0

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

# Define max_nums 5

Void qsort (void * base, size_t num, size_t width, INT (_ cdecl * compare) (const void * elem1, const void * elem2 ));
Int vstrcmp (const void * var1, const void * var2 );
Int vintcmp (const void * var1, const void * var2 );

Int main ()
{
Char * Names [max_nums] = {
"Qihailong ",
"Changchao ",
"Lixiaorang ",
"Xiyu ",
"Zhangjuntao"
};
Int ages [max_nums] = {32, 45, 76, 21, 89 };
Int I = 0;

Qsort (names, max_nums, sizeof (char *), vstrcmp );
Qsort (ages, max_nums, sizeof (INT), vintcmp );
For (I = 0; I <max_nums; ++ I)
{
Puts (Names [I]);
}
 
For (I = 0; I <max_nums; ++ I)
{
Printf ("% 5d,", ages [I]);
}
 
Puts ("");
Return 0;
}

Int vstrcmp (const void * var1, const void * var2)
{
Return strcmp (* (char **) var1, * (char **) var2 );
}

Int vintcmp (const void * var1, const void * var2)
{
Return * (int *) var1-* (int *) var2;
}
/*
Changchao
Lixiaorang
Qihailong
Xiyu
Zhangjuntao
21, 32, 45, 76, 89,
Press any key to continue
*/

# Endif

# If 0

Node * search_list (node * node, void const * value, INT (* compare) (void const *, void const *))
{
While (node! = NULL)
{
If (compare (& node-> value, value) = 0)
{
Break;
}
Else
{
Node = node-> next;
}
}
Return node;
}

Int compare_ints (void const * a, void const * B)
{
If (* (int *) A = * (int *) B)
{
Return 0;
}
Else
{
Return 1;
}
}

# Endif

// Use structure and combination in C to realize Polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.