1、
#include <stdio.h> #include <string.h> #define NUMBER 5 #define NAME_LEN 64 void swap_int(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } void swap_str(char *s1, char *s2) //字符串的交换程序 { char tmp[NAME_LEN]; strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); } void sort(int x[], char y[][NAME_LEN], int n) { int i, j; for(i = 0; i < n - 1; i++) { for(j = n - 1; j > i; j--) { if(x[j - 1] > x[j]) { swap_int(&x[j - 1], &x[j]); swap_str(y[j - 1], y[j]); } } } } int main(void) { int i; int height[] = {178,175,173,165, 179}; char name[][NAME_LEN] = {"Sato","Sanata","Takao","Mike","Masaki"}; for(i = 0; i < NUMBER; i++) printf("NO-%d: %-8s %3d\n", i + 1, name[i], height[i]); sort(height, name, NUMBER); puts("\n==================\n"); for(i = 0; i < NUMBER; i++) printf("NO-%d: %-8s %3d\n", i+ 1, name[i], height[i]); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。