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;
- float imt;
- } person_t;
- float IMT(float h, float m)
- {
- h /= 100;
- return m/h/h;
- }
- void input(person_t *person, int count)
- {
- 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);
- person[i].imt = IMT(person[i].height, person[i].weight);
- printf("%f\n", person[i].imt);
- }
- }
- 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(person_t *person, int n, int key)
- {
- person_t newElement;
- int location;
- for (int i = 1; i < n; i++)
- {
- newElement = person[i];
- location = i - 1;
- while(location >= 0 && ((key) ? person[location].imt < newElement.imt : person[location].imt > newElement.imt))
- {
- person[location+1] = person[location];
- location = location - 1;
- }
- person[location+1] = newElement;
- }
- }
- int main()
- {
- person_t people[N];
- int count;
- printf("input count person: ");
- scanf("%d", &count);
- input(people, count);
- print(people, count);
- int key;
- printf("0. increase sort\n1. decrease sort\nInput key: ");
- scanf("%d", &key);
- sortIns(people, count, key);
- puts("Result:");
- print(people, count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement