标签:main strcpy com har turn code numa str 结构
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 typedef struct student { 6 char *name; 7 }; 8 9 void scan(student stu[], int &n){ 10 char str[1024]; 11 scanf("%d", &n); 12 for(int i = 0; i < n; ++i){ 13 scanf("%s", str); 14 int len = strlen(str); 15 stu[i].name = (char *)malloc((len + 1) * sizeof(char)); 16 strcpy(stu[i].name, str); 17 } 18 } 19 20 void print(student stu[], int n){ 21 printf("%d\n", n); 22 for(int i = 0; i < n; ++i){ 23 printf("%s ", stu[i].name); 24 } 25 puts(""); 26 } 27 //按字典序 排序 28 int comp(const void *a, const void *b){ 29 student *numa = (student *)a, *numb = (student *)b; 30 return strcmp(numa->name, numb->name); 31 } 32 33 int main(){ 34 int n; 35 student stu[10]; 36 scan(stu, n); 37 38 print(stu, n); 39 40 qsort(stu, n, sizeof(student), comp); 41 42 print(stu, n); 43 return 0; 44 } 45 /* 46 10 47 9只小昆虫 48 8只小昆虫 49 7只小昆虫 50 6只小昆虫 51 5只小昆虫 52 4只小昆虫 53 3只小昆虫 54 2只小昆虫 55 1只小昆虫 56 0只小昆虫 57 58 */
标签:main strcpy com har turn code numa str 结构
原文地址:http://www.cnblogs.com/xuqiulin/p/6185512.html