Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  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. } person_t;
  13.  
  14. float IMT(float h, float m)
  15. {
  16. h /= 100;
  17. return m/h/h;
  18. }
  19. void input(person_t *person, int count, float imt[])
  20. {
  21. for (int i = 0; i < count; i++)
  22. {
  23. printf("input family, height, weight: ");
  24. scanf("%s%d%f", person[i].family, &person[i].height, &person[i].weight);
  25. imt[i] = IMT(person[i].height, person[i].weight);
  26. printf("%f ", imt[i]);
  27. }
  28. }
  29. void print(person_t *person, int count)
  30. {
  31. for (int i = 0; i<count; i++)
  32. printf("%s %d %.2f\n", person[i].family, person[i].height,
  33. person[i].weight);
  34. }
  35. void sortIns_increase(person_t *person, int n, float imt[])
  36. {
  37. int elem, location;
  38.  
  39. for (int i = 1; i < n; i++)
  40. {
  41. elem = imt[i];
  42. location = i - 1;
  43. while(location >= 0 && imt[location] > elem)
  44. {
  45. float tmp1 = imt[location + 1];
  46. imt[location+1] = imt[location];
  47. imt[location] = tmp1;
  48.  
  49. person_t tmp2 = person[location + 1];
  50. person[location+1] = person[location];
  51. person[location] = tmp2;
  52. location = location - 1;
  53. }
  54. imt[location+1] = elem;
  55. }
  56. }
  57.  
  58. void sortIns_decrease(person_t *person, int n, float imt[])
  59. {
  60. int elem, location;
  61.  
  62. for (int i = 1; i < n; i++)
  63. {
  64. elem = imt[i];
  65. location = i - 1;
  66. while(location >= 0 && imt[location] < elem)
  67. {
  68. float tmp1 = imt[location + 1];
  69. imt[location+1] = imt[location];
  70. imt[location] = tmp1;
  71.  
  72. person_t tmp2 = person[location + 1];
  73. person[location+1] = person[location];
  74. person[location] = tmp2;
  75. location = location - 1;
  76. }
  77. imt[location+1] = elem;
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. person_t people[N];
  84. int count;
  85. printf("input count person: ");
  86. scanf("%d", &count);
  87.  
  88. float imt[count];
  89. input(people, count, imt);
  90. print(people, count);
  91. int key;
  92. printf("0. increase sort\n1. decrease sort\nInput key: ");
  93. scanf("%d", &key);
  94. if (!key)
  95. sortIns_increase(people, count, imt);
  96. else
  97. sortIns_decrease(people, count, imt);
  98.  
  99. puts("Result:");
  100. print(people, count);
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement