Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define N 100
- #define LEN 50
- typedef struct person
- {
- char family[LEN];
- int height;
- float weight;
- } person_t;
- float IMT(float h, float m)
- {
- h /= 100;
- return m/h/h;
- }
- void input(person_t *person, int count, float imt[])
- {
- for (int i = 0; i < count; i++)
- {
- printf("input family, height, weight: ");
- scanf("%s%d%f", person[i].family, &person[i].height, &person[i].weight);
- imt[i] = IMT(person[i].height, person[i].weight);
- printf("%f ", imt[i]);
- }
- }
- void print(person_t *person, int count)
- {
- for (int i = 0; i<count; i++)
- printf("%s %d %.2f\n", person[i].family, person[i].height,
- person[i].weight);
- }
- void sortIns_increase(person_t *person, int n, float imt[])
- {
- int elem, location;
- for (int i = 1; i < n; i++)
- {
- elem = imt[i];
- location = i - 1;
- while(location >= 0 && imt[location] > elem)
- {
- float tmp1 = imt[location + 1];
- imt[location+1] = imt[location];
- imt[location] = tmp1;
- person_t tmp2 = person[location + 1];
- person[location+1] = person[location];
- person[location] = tmp2;
- location = location - 1;
- }
- imt[location+1] = elem;
- }
- }
- void sortIns_decrease(person_t *person, int n, float imt[])
- {
- int elem, location;
- for (int i = 1; i < n; i++)
- {
- elem = imt[i];
- location = i - 1;
- while(location >= 0 && imt[location] < elem)
- {
- float tmp1 = imt[location + 1];
- imt[location+1] = imt[location];
- imt[location] = tmp1;
- person_t tmp2 = person[location + 1];
- person[location+1] = person[location];
- person[location] = tmp2;
- location = location - 1;
- }
- imt[location+1] = elem;
- }
- }
- int main()
- {
- person_t people[N];
- int count;
- printf("input count person: ");
- scanf("%d", &count);
- float imt[count];
- input(people, count, imt);
- print(people, count);
- int key;
- printf("0. increase sort\n1. decrease sort\nInput key: ");
- scanf("%d", &key);
- if (!key)
- sortIns_increase(people, count, imt);
- else
- sortIns_decrease(people, count, imt);
- puts("Result:");
- print(people, count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement