fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. struct akb_db
  4. {
  5. char name[30];
  6. char home[10];
  7. int yar;
  8. int mon;
  9. int dy;
  10. };
  11. void sort_al(struct akb_db *, int);
  12. void sort_age(struct akb_db *, int);
  13. void pp(struct akb_db *, int, FILE *);
  14. int main()
  15. {
  16. struct akb_db dat[100];
  17. char buf[100], fn[30], name1[15], name2[15];
  18. int n, m;
  19. FILE *fp;
  20.  
  21. printf("indata file name = ");
  22. scanf("%s", fn);
  23. fp = fopen(fn, "r");
  24. for (n = 0; fgets(buf, 100, fp) != NULL; ++n)
  25. {
  26. sscanf(buf, "%s %s %s %d %d %d",
  27. name1, name2, dat[n].home, &dat[n].yar, &dat[n].mon, &dat[n].dy);
  28. sprintf(dat[n].name, "%s %s", name1, name2);
  29. }
  30. do
  31. {
  32. printf("MENU\n");
  33. printf("1.Sorting in alphabetical order/2.Sorting in order of age.\n");
  34. printf("Please input number:");
  35. scanf("%d", &m);
  36. }
  37. while (m < 1 || m > 2);
  38. if (m == 1)
  39. sort_al(dat, n);
  40. else
  41. sort_age(dat, n);
  42. return 0;
  43. }
  44. void sort_al(struct akb_db *dat, int n)
  45. {
  46. FILE *fp = fopen("member_data_al.dat", "w");
  47. char name_work1[30], name_work2[30], *p;
  48. for (int i = 0; i < n; ++i)
  49. {
  50. for (int j = i + 1; j < n; ++j)
  51. {
  52. p = strchr(dat[i].name, ' ');
  53. sprintf(name_work1, "%s", p + 1);
  54. strncat(name_work1, dat[i].name, p - dat[i].name);
  55. p = strchr(dat[j].name, ' ');
  56. sprintf(name_work2, "%s", p + 1);
  57. strncat(name_work2, dat[j].name, p - dat[j].name);
  58. if (strcmp(name_work1, name_work2) > 0)
  59. {
  60. struct akb_db t = dat[i];
  61. dat[i] = dat[j];
  62. dat[j] = t;
  63. }
  64. }
  65. }
  66. pp(dat, n, fp);
  67. }
  68. void sort_age(struct akb_db *dat, int n)
  69. {
  70. FILE *fp = fopen("member_data_age.dat", "w");
  71. char ymd1[20], ymd2[20];
  72. for (int i = 0; i < n; ++i)
  73. {
  74. for (int j = i + 1; j < n; ++j)
  75. {
  76. sprintf(ymd1, "%d%02d%02d", dat[i].yar, dat[i].mon, dat[i].dy);
  77. sprintf(ymd2, "%d%02d%02d", dat[j].yar, dat[j].mon, dat[j].dy);
  78. if (strcmp(ymd1, ymd2) > 0)
  79. {
  80. struct akb_db t = dat[i];
  81. dat[i] = dat[j];
  82. dat[j] = t;
  83. }
  84. }
  85. }
  86. pp(dat, n, fp);
  87. }
  88. void pp(struct akb_db *dat, int n, FILE *fp)
  89. {
  90. char buf[100];
  91. for (int i = 0; i < n; ++i)
  92. {
  93. sprintf(buf, "%s %s %d %d %d",
  94. dat[i].name, dat[i].home, dat[i].yar, dat[i].mon, dat[i].dy);
  95. printf("%s\n", buf);
  96. fprintf(fp, "%s\n", buf);
  97. }
  98. fclose(fp);
  99. }
  100.  
Runtime error #stdin #stdout 0s 2432KB
stdin
Standard input is empty
stdout
Standard output is empty