Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- FILE *infile, *outfile;
- void input_scores_file(FILE*infile,float score[], int n)
- {
- infile = fopen("scores.txt", "r");
- if (infile == NULL)
- {
- printf("에러: 입력 파일을 열 수 없음!!!\n\n");
- return;
- }
- /else fscanf(infile, "%f", &score[n]);
- fclose(infile);
- }
- float max = 0.0;
- float min = 100.0;
- float sum = 0.0;
- float mean = 0.0;
- float max_score(float score[], int n)
- {
- if (n == 0)
- return max;
- if (score[n - 1] >= max)
- {
- max = score[n - 1];
- n--;
- return max_score(score, n);
- }
- else
- n--;
- return max_score(score, n);
- }
- float min_score(float score[], int n)
- {
- if (n == 0)
- return min;
- if (score[n - 1] <= min)
- {
- min = score[n - 1];
- n--;
- return min_score(score, n);
- }
- else
- n--;
- return min_score(score, n);
- }
- float sum_scores(float score[], int n)
- {
- if (n == 0)
- return sum;
- if (n >= 0)
- sum += score[n - 1];
- n--;
- return sum_scores(score, n);
- }
- void print_result_file(FILE*outfile,float max, float min, float sum, float mean)
- {
- outfile = (FILE *)fopen("result.txt", "w");
- if (outfile == NULL)
- {
- printf("에러: 출력 파일을 열 수 없음!!!\n\n");
- return;
- }
- fprintf(outfile,"최고점수: %f\n", max);
- fprintf(outfile,"최저점수: %f\n", min);
- fprintf(outfile,"합계점수: %f\n", sum);
- fprintf(outfile,"평균점수: %f\n", mean);
- fclose(outfile);
- }
- void main()
- {
- int n;
- float score[30];
- float max, min, sum, mean;
- input_scores_file(infile, score, n);
- max = max_score(score, n);
- min = min_score(score, n);
- sum = sum_scores(score, n);
- mean = sum / (float)n;
- print_result_file(outfile,max, min, sum, mean);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement