求三个数最大值的宏表达式:
#define MAX2NUMBER(a, b) (a) > (b) ? (a) : (b)
#define MAX3NUMBER(a, b, c) MAX2NUMBER(a, b) > (c) ? MAX2NUMBER(a, b) : (c)
字符串交换
void myswap(char *ch1, char *ch2)
{
char temp[1024] = {0};
strcpy(temp, ch1);
strcpy(ch1, ch2);
strcpy(ch2, temp);
}
字符串比较排列:
void sort2(char *ch1, char *ch2)
{
if(strcmp(ch1, ch2) > 0)
{
myswap(ch1, ch2);
}
}
三个字符串排列:
void sort3(char *ch1, char *ch2, char *ch3)
{
sort2(ch1, ch2);
sort2(ch1, ch3);
sort2(ch2, ch3);
}
三个整数排列(指针操作):
void swap(int *p1, int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void sort2(int *p1, int *p2)
{
if (*p1 > *p2)
{
swap(p1,p2);
}
}
void sort3(int *p1, int *p2, int *p3)
{
sort2(p1,p2);
sort2(p1,p3);
sort2(p2,p3);
}
阅读(1226) | 评论(0) | 转发(0) |