fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct person {
  5. int age;
  6. double height;
  7. double weight;
  8. } person;
  9.  
  10. int cmpDesHeight(const void* n1, const void* n2) {
  11. if (((person*)n1)->height > ((person*)n2)->height) {
  12. return -1;
  13. } else if (((person*)n1)->height < ((person*)n2)->height) {
  14. return 1;
  15. } else {
  16. return 0;
  17. }
  18. }
  19.  
  20. int cmpAscWeight(const void* n1, const void* n2) {
  21. if (((person*)n1)->weight > ((person*)n2)->weight) {
  22. return 1;
  23. } else if (((person*)n1)->weight < ((person*)n2)->weight) {
  24. return -1;
  25. } else {
  26. return 0;
  27. }
  28. }
  29.  
  30. char* msg[] = {"年齢", "身長", "体重"};
  31.  
  32. int main(void) {
  33. person nums[5];
  34. char a[5];
  35. for (int i = 0; i < 5; i++) {
  36. for (int j = 0; j < 3; j++) {
  37. printf("%d人目の%sを入力してください。\n", i + 1, msg[j]);
  38. scanf("%4s%*[^\n]", a);
  39. switch (j) {
  40. case 0: nums[i].age = strtol(a, NULL, 10); break;
  41. case 1: nums[i].height = strtod(a, NULL); break;
  42. default: nums[i].weight = strtod(a, NULL); break;
  43. }
  44. }
  45. }
  46. qsort(nums, sizeof(nums)/sizeof(nums[0]), sizeof(person), cmpDesHeight);
  47. printf("最も身長が高い人は年齢:%d才, 身長:%lfcm, 体重%lfkgです。\n", nums[0].age, nums[0].height, nums[0].weight);
  48. qsort(nums, sizeof(nums)/sizeof(nums[0]), sizeof(person), cmpAscWeight);
  49. printf("最も体重が軽い人は年齢:%d才, 身長:%lfcm, 体重%lfkgです。\n", nums[0].age, nums[0].height, nums[0].weight);
  50. return EXIT_SUCCESS;
  51. }
  52.  
Success #stdin #stdout 0s 5496KB
stdin
Standard input is empty
stdout
1人目の年齢を入力してください。
1人目の身長を入力してください。
1人目の体重を入力してください。
2人目の年齢を入力してください。
2人目の身長を入力してください。
2人目の体重を入力してください。
3人目の年齢を入力してください。
3人目の身長を入力してください。
3人目の体重を入力してください。
4人目の年齢を入力してください。
4人目の身長を入力してください。
4人目の体重を入力してください。
5人目の年齢を入力してください。
5人目の身長を入力してください。
5人目の体重を入力してください。
最も身長が高い人は年齢:0才, 身長:0.000000cm, 体重0.000000kgです。
最も体重が軽い人は年齢:0才, 身長:0.000000cm, 体重0.000000kgです。