Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. FILE *infile, *outfile;
  3.  
  4.  
  5. void input_scores_file(FILE*infile,float score[], int n)
  6. {
  7.  
  8. infile = fopen("scores.txt", "r");
  9. if (infile == NULL)
  10. {
  11. printf("에러: 입력 파일을 열 수 없음!!!\n\n");
  12. return;
  13. }
  14. /else fscanf(infile, "%f", &score[n]);
  15.  
  16. fclose(infile);
  17.  
  18. }
  19.  
  20.  
  21. float max = 0.0;
  22. float min = 100.0;
  23. float sum = 0.0;
  24. float mean = 0.0;
  25.  
  26. float max_score(float score[], int n)
  27. {
  28.  
  29. if (n == 0)
  30. return max;
  31.  
  32. if (score[n - 1] >= max)
  33. {
  34. max = score[n - 1];
  35. n--;
  36. return max_score(score, n);
  37. }
  38. else
  39. n--;
  40. return max_score(score, n);
  41.  
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. float min_score(float score[], int n)
  52. {
  53.  
  54. if (n == 0)
  55. return min;
  56.  
  57. if (score[n - 1] <= min)
  58. {
  59. min = score[n - 1];
  60. n--;
  61. return min_score(score, n);
  62. }
  63. else
  64. n--;
  65. return min_score(score, n);
  66. }
  67.  
  68.  
  69.  
  70. float sum_scores(float score[], int n)
  71. {
  72. if (n == 0)
  73. return sum;
  74.  
  75. if (n >= 0)
  76. sum += score[n - 1];
  77. n--;
  78. return sum_scores(score, n);
  79.  
  80. }
  81.  
  82.  
  83.  
  84. void print_result_file(FILE*outfile,float max, float min, float sum, float mean)
  85. {
  86. outfile = (FILE *)fopen("result.txt", "w");
  87. if (outfile == NULL)
  88. {
  89. printf("에러: 출력 파일을 열 수 없음!!!\n\n");
  90. return;
  91. }
  92.  
  93. fprintf(outfile,"최고점수: %f\n", max);
  94. fprintf(outfile,"최저점수: %f\n", min);
  95. fprintf(outfile,"합계점수: %f\n", sum);
  96. fprintf(outfile,"평균점수: %f\n", mean);
  97.  
  98. fclose(outfile);
  99.  
  100. }
  101.  
  102.  
  103. void main()
  104. {
  105. int n;
  106. float score[30];
  107. float max, min, sum, mean;
  108.  
  109.  
  110. input_scores_file(infile, score, n);
  111.  
  112.  
  113. max = max_score(score, n);
  114. min = min_score(score, n);
  115. sum = sum_scores(score, n);
  116. mean = sum / (float)n;
  117.  
  118. print_result_file(outfile,max, min, sum, mean);
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement