Advertisement
zhenialeks

Untitled

Mar 28th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <conio.h>
  4.  
  5. #define NUM_OF_PEOPLE 5
  6. #define LENGTH_OF_NAME 20
  7.  
  8. #define N 3
  9. struct GROUP {
  10.     char surname[20];
  11.     int DAT[3];
  12.     int R[3];
  13. };
  14.  
  15.  
  16. struct People {
  17.     char name[LENGTH_OF_NAME];
  18. };
  19.  
  20. //int compare(char name1[], char name2[]) {
  21. //  for (int i = 0; i < LENGTH_OF_NAME && name1[i] != 0 && name2[i] != 0; ++i) {
  22. //      if (name1[i] > name2[i]) return 0;
  23. //      else if (name1[i] < name2[i]) return 1;
  24. //  }
  25. //  return 0;
  26. //}
  27.  
  28. int recompare(char name1[], char name2[]) {
  29.     for (int i = 0; i < LENGTH_OF_NAME && name1[i] != 0 && name2[i] != 0; i++) {
  30.         int k = 0;
  31.         if (name1[i] == name2[i]) k++;
  32.         if (k == strlen(name2) && k < strlen(name1)) return 1;
  33.     }
  34.     return 0;
  35. }
  36.  
  37. void sort(struct People A[]) {
  38.     struct People temp;
  39.     for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
  40.         for (int j = 0; j < NUM_OF_PEOPLE; ++j) {
  41.             if (strcmp(A[i].name, A[j].name) < 0) {
  42.                 temp = A[i];
  43.                 A[i] = A[j];
  44.                 A[j] = temp;
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50.  
  51. void resort(struct People A[]) {
  52.     struct People temp;
  53.     int o = LENGTH_OF_NAME;
  54.     while (o != 0) {
  55.         o--;
  56.         for (int i = 0; i < NUM_OF_PEOPLE - 1; i++) {
  57.             if (recompare(A[i].name, A[i + 1].name)) {
  58.                 temp = A[i + 1];
  59.                 A[i + 1] = A[i];
  60.                 A[i] = temp;
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. void main() {
  67.     printf("Enter a names of students: \n");
  68.     struct People A[NUM_OF_PEOPLE];
  69.  
  70.     for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
  71.         printf("Student number %d: \n", i + 1);
  72.         gets(A[i].name);
  73.     }
  74.     resort(A);
  75.     sort(A);
  76.     //resort(A);
  77.     printf("Sorted people: \n");
  78.     for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
  79.         printf(A[i].name);
  80.         printf("\n");
  81.     }
  82.     printf("\n");
  83.  
  84.     getch();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement