fork download
  1. //動態配置結構陣列,
  2. //讓使用者輸入n後,
  3. //先讓使用者依序輸入n個學生姓名、身高、體重
  4. //算出身高及體重平均,並輸出平均
  5. //最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
  6. //名字最長不會超過20字元
  7.  
  8. //承上題,
  9. //輸入改為由檔案輸入
  10. //輸出改為輸出至檔案
  11. //程式流程:
  12. //先後讀取輸入檔名與輸出檔名後,過程如上題,
  13. //檔名不會超過50字元,程式畫面如下圖,
  14. //讀入檔名時請分兩次(兩次 gets 或 scanf)
  15.  
  16. //測試資料可以在課程網頁下載
  17. //內容有:
  18. //輸入檔:可用複製貼上至黑黑的那個視窗
  19. //輸出檔:答案,請仔細比較
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. struct student{
  26. char name[21];
  27. int height, weight;
  28. };
  29.  
  30. int main(){
  31. FILE *in, *out;
  32. int i, n;
  33. char str[51], string[51];
  34. double sum_h = 0, sum_w = 0, H_avg, W_avg;
  35.  
  36. printf("Input file:%s", str);
  37. scanf("%s", &str);
  38.  
  39. in = fopen(str, "r"); //
  40.  
  41. if (in == NULL) {
  42. printf("open input file failed!\n");
  43. return 0;
  44. }
  45.  
  46. printf("Output file:%s", string);
  47. scanf("%s", &string);
  48.  
  49. out = fopen(string, "w"); //
  50.  
  51. fscanf(in, "%d", &n);
  52.  
  53. struct student *a = (struct student *)malloc(sizeof(struct student)*n);
  54.  
  55. for (i = 0; i < n; i++){
  56. fscanf(in, "%s %d %d", a[i].name, &a[i].height, &a[i].weight);
  57. }
  58.  
  59. for (i = 0; i < n; i++){
  60. sum_h += a[i].height;
  61. sum_w += a[i].weight;
  62. }
  63.  
  64. H_avg = sum_h / n;
  65. W_avg = sum_w / n;
  66.  
  67. fprintf(out, "H_avg:%.2lf\n", H_avg);
  68. fprintf(out, "W_avg:%.2lf\n", W_avg);
  69.  
  70. for (i = 0; i < n; i++){
  71. if(a[i].height < H_avg || a[i].weight < W_avg){
  72. fprintf(out, "%s\n", a[i].name);
  73. //動態配置結構陣列,
  74. //讓使用者輸入n後,
  75. //先讓使用者依序輸入n個學生姓名、身高、體重
  76. //算出身高及體重平均,並輸出平均
  77. //最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
  78. //名字最長不會超過20字元
  79.  
  80. //承上題,
  81. //輸入改為由檔案輸入
  82. //輸出改為輸出至檔案
  83. //程式流程:
  84. //先後讀取輸入檔名與輸出檔名後,過程如上題,
  85. //檔名不會超過50字元,程式畫面如下圖,
  86. //讀入檔名時請分兩次(兩次 gets 或 scanf)
  87.  
  88. //測試資料可以在課程網頁下載
  89. //內容有:
  90. //輸入檔:可用複製貼上至黑黑的那個視窗
  91. //輸出檔:答案,請仔細比較
  92.  
  93.  
  94. #include <stdio.h>
  95. #include <stdlib.h>
  96.  
  97. struct student{
  98. char name[21];
  99. int height, weight;
  100. };
  101.  
  102. int main(){
  103. FILE *in, *out;
  104. int i, n;
  105. char str[51], string[51];
  106. double sum_h = 0, sum_w = 0, H_avg, W_avg;
  107.  
  108. printf("Input file:%s", str);
  109. scanf("%s", &str);
  110.  
  111. in = fopen(str, "r"); //
  112.  
  113. if (in == NULL) {
  114. printf("open input file failed!\n");
  115. return 0;
  116. }
  117.  
  118. printf("Output file:%s", string);
  119. scanf("%s", &string);
  120.  
  121. out = fopen(string, "w"); //
  122.  
  123. fscanf(in, "%d", &n);
  124.  
  125. struct student *a = (struct student *)malloc(sizeof(struct student)*n);
  126.  
  127. for (i = 0; i < n; i++){
  128. fscanf(in, "%s %d %d", a[i].name, &a[i].height, &a[i].weight);
  129. }
  130.  
  131. for (i = 0; i < n; i++){
  132. sum_h += a[i].height;
  133. sum_w += a[i].weight;
  134. }
  135.  
  136. H_avg = sum_h / n;
  137. W_avg = sum_w / n;
  138.  
  139. fprintf(out, "H_avg:%.2lf\n", H_avg);
  140. fprintf(out, "W_avg:%.2lf\n", W_avg);
  141.  
  142. for (i = 0; i < n; i++){
  143. if(a[i].height < H_avg || a[i].weight < W_avg){
  144. fprintf(out, "%s\n", a[i].name);
  145. }
  146. }
  147.  
  148. fclose(in);
  149. fclose(out);
  150.  
  151. free(a);
  152.  
  153. system("pause");
  154. return 0;
  155. }
  156.  
  157.  
  158. }
  159. }
  160.  
  161. fclose(in);
  162. fclose(out);
  163.  
  164. free(a);
  165.  
  166. system("pause");
  167. return 0;
  168. }
  169.  
  170.  
  171.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Input file:�r?v�open input file failed!