|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
#define NUMBER 5
#define NAME_LEN 64
void swap_str(char *s1, char *s2)
{
char *temp = s1;
s1 = s2;
s2 = temp;
}
void swap_int(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void sort(int num[], char str[][NAME_LEN], int n)
{
int i, j;
for(i = 0; i < n -1; i++)
for(j = 0; j < n - 1 - i; j++)
{
if(num[j] >num[j + 1])
swap_int(&num[j], &num[j + 1]);
swap_str(str[j], str[j + 1]);
}
}
int main()
{
int i;
int height[] = {178, 175, 173, 165, 179};
char name[][NAME_LEN]={"Sato","Sanaka","Takao","Mike","Masaki"};
for(i = 0; i < NUMBER; i++)
printf("%2d : %-8s%4d\n", i + 1, name[i], height[i]);
sort(height, name, NUMBER);
puts("\n按身高进行升序排列。");
for(i = 0; i < NUMBER; i++)
printf("%2d : %-8s%4d\n", i + 1, name[i], height[i]);
return 0;
}
第一个交换字符串的函数,我知道用strcpy实现肯定可以,我想请问一下用指针实现不可以吗? |
|