fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char name[256];
  7. int age;
  8. int sex;
  9. } People;
  10.  
  11. void InputPeople(People* data);
  12. void ShowPeople(People data);
  13.  
  14. int main(void)
  15. {
  16. int i, count, datasize;
  17. People* data;
  18.  
  19. datasize = 10;
  20. data = (People*)malloc(sizeof(People) * datasize);
  21.  
  22. count = 0;
  23.  
  24. while (1) {
  25. InputPeople(&data[count]);
  26. if (data[count].age == -1)
  27. break;
  28. count++;
  29.  
  30. if (count >= datasize) {
  31. datasize += 10;
  32. data = (People*)realloc(data, sizeof(People) * datasize);
  33. }
  34. }
  35.  
  36. for (i = 0; i < count; i++) {
  37. ShowPeople(data[i]);
  38. }
  39.  
  40. free(data);
  41.  
  42. return 0;
  43. }
  44.  
  45. void InputPeople(People* data)
  46. {
  47. printf("名前:");
  48. scanf("%s", data->name);
  49. printf("年齢:");
  50. scanf("%d", &data->age);
  51. printf("性別(1-男性、2-女性):");
  52. scanf("%d", &data->sex);
  53. printf("\n");
  54. }
  55.  
  56. void ShowPeople(People data)
  57. {
  58. char sex[16];
  59.  
  60. printf("名前:%s\n", data.name);
  61. printf("年齢:%d\n", data.age);
  62.  
  63. if (data.sex == 1) {
  64. strcpy(sex, "男性");
  65. } else {
  66. strcpy(sex, "女性");
  67. }
  68.  
  69. printf("性別:%s\n", sex);
  70. printf("\n");
  71. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
田中啓二
16
1
斎藤家時
17
1
みはるかなえ
19
0
おとこはつらいよどこまでも
-1
1
かすお
21
1
stdout
名前:年齢:性別(1-男性、2-女性):
名前:年齢:性別(1-男性、2-女性):
名前:年齢:性別(1-男性、2-女性):
名前:年齢:性別(1-男性、2-女性):
名前:年齢:性別(1-男性、2-女性):
名前:5
年齢:0
性別:女性

名前:田中啓二
年齢:16
性別:男性

名前:斎藤家時
年齢:17
性別:男性

名前:みはるかなえ
年齢:19
性別:女性