MeShootIn

лаба_для_макса_2

Oct 24th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <locale.h>
  6.  
  7. #define MAXLEN 100 // максимальная длина строки
  8.  
  9. // структура одной строки
  10. struct Line{
  11.     char name[MAXLEN]; // название впадины суши
  12.     int depth; // глубина
  13.     char location[MAXLEN]; // местоположение
  14. };
  15.  
  16. // сортировка выбором
  17. void selection_sort(struct Line * list, int N){
  18.     int index;
  19.     for(int i = 0; i < N - 1; i++){
  20.         index = i;
  21.         for(int j = i + 1; j < N; j++){
  22.             if(list[j].depth < list[index].depth){
  23.                 index = j;
  24.             }
  25.         }
  26.         struct Line tmp = list[i];
  27.         list[i] = list[index];
  28.         list[index] = tmp;
  29.     }
  30. }
  31.  
  32. int main(){
  33.     setlocale(0, ""); // русский язык
  34.    
  35.     int N = 15;
  36.     struct Line * list = (struct Line *) malloc(N * sizeof(struct Line)); // выделяем память под массив типа Line размера N
  37.    
  38.     printf("Глубочайшие впадины суши\nВведите %d строк:\n", N);
  39.     for(int i = 0; i < N; i++){
  40.         scanf("%s%d%s", &list[i].name, &list[i].depth, &list[i].location);
  41.     }
  42.    
  43.     selection_sort(list, N); // сортируем
  44.     printf("Отсортированный список:\n");
  45.     // построчно выводим
  46.     for(int i = 0; i < N; i++){
  47.         printf("%s %d %s\n", list[i].name, list[i].depth, list[i].location);
  48.     }
  49.    
  50.     free(list); // освобождаем память
  51.    
  52.     getch();
  53.     return 0;
  54. }
Add Comment
Please, Sign In to add comment