SHARE
TWEET

Untitled

a guest Jun 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 100
  5. #define LEN 50
  6.  
  7. typedef struct person
  8. {
  9.     char family[LEN];
  10.     int height;
  11.     float weight;
  12.     float imt;
  13. } person_t;
  14.  
  15. float IMT(float h, float m)
  16. {
  17.     h /= 100;
  18.     return m/h/h;
  19. }
  20. void input(person_t *person, int count)
  21. {
  22.     for (int i = 0; i < count; i++)
  23.     {
  24.         printf("input family, height, weight: ");
  25.         scanf("%s%d%f", person[i].family, &person[i].height, &person[i].weight);
  26.         person[i].imt = IMT(person[i].height, person[i].weight);
  27.         printf("%f\n", person[i].imt);
  28.     }
  29. }
  30. void print(person_t *person, int count)
  31. {
  32.     for (int i = 0; i<count; i++)
  33.         printf("%s %d %.2f\n", person[i].family, person[i].height,
  34.                person[i].weight);
  35. }
  36. void sortIns(person_t *person, int n, int key)
  37. {
  38.     person_t newElement;
  39.     int location;
  40.  
  41.     for (int i = 1; i < n; i++)
  42.     {
  43.         newElement = person[i];
  44.         location = i - 1;
  45.         while(location >= 0 && ((key) ? person[location].imt < newElement.imt : person[location].imt > newElement.imt))
  46.         {
  47.             person[location+1] = person[location];
  48.             location = location - 1;
  49.         }
  50.         person[location+1] = newElement;
  51.     }
  52. }
  53.  
  54. int main()
  55. {
  56.     person_t people[N];
  57.     int count;
  58.     printf("input count person: ");
  59.     scanf("%d", &count);
  60.     input(people, count);
  61.     print(people, count);
  62.     int key;
  63.     printf("0. increase sort\n1. decrease sort\nInput key: ");
  64.     scanf("%d", &key);
  65.     sortIns(people, count, key);
  66.  
  67.     puts("Result:");
  68.     print(people, count);
  69.     return 0;
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top