結果例
grade name Eng Mat Sci Total Mean Hensa
1 A 90 85 78 253 84.33 66
2 B 80 75 60 215 71.67 55
2 C 90 85 40 215 71.67 55
・・・
あと、結果の点数・値が何を表しているか、の部分もできてないです。。(grade 、name…の所です)
下のコードは順番・計算結果などは正しく出せました。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAX 20
#define LEN 15
typedef struct seiseki{
char name[LEN];
int eng;
int math;
int sci;
int total;
double mean;
double hensa;
}SEISEKI;
int main(int, char **);
int input(FILE *, SEISEKI *);
void output(FILE *, SEISEKI *, int number, double mean, double deviation);
int compare(const SEISEKI *, const SEISEKI *);
double mean_of_total(SEISEKI * student,int number);
double hensa(SEISEKI * student, int number, double mean);
int main(int argc, char **argv)
{
FILE *infile, *outfile;
SEISEKI student[MAX];
int number;
double mean_total = 0.0;
double deviation = 0.0;
if(argc != 3){
fprintf(stderr, "Example: qtotla motofile.dat insatsu.dat\n");
exit(1);
}
if((infile = fopen(argv[1], "rt")) == NULL){
fprintf(stderr, "Can not open file: %s\n", argv[1]);
exit(1);
}
if((outfile = fopen(argv[2], "wt")) == NULL){
fprintf(stderr, "Can not open file: %s\n", argv[2]);
exit(1);
}
number = input(infile, student);
mean_total = mean_of_total(student, number);
deviation = hensa(student, number, mean_total);
qsort(student, number, sizeof(SEISEKI), (int (*)(const void *, const void *))compare);
output(outfile, student, number, mean_total, deviation);
fclose(infile); fclose(outfile);
return 0;
}
int input(FILE *infile, SEISEKI * student)
{
char n[LEN];
int e,m,s,i = 0;
while(fscanf(infile, "%s %d %d %d", n, &e, &m, &s) != EOF){
strcpy(student[i].name,n);
student[i].eng = e; student[i].math = m; student[i].sci = s;
student[i].total = e + m+ s;
student[i].mean = student[i].total/3.0;
i++;
}
return i;
}
void output(FILE *outfile, SEISEKI *student, int number, double mean, double deviation)
{
int i;
for(i = 0; i < number; i++){
SEISEKI *tmp = &student[i];
fprintf(outfile, "%-14s %3d %3d %3d %3d %6.2f %2.0f\n",
tmp->name, tmp->eng, tmp->math, tmp->sci, tmp->total, tmp->mean, tmp->hensa);
}
fprintf(outfile, "\nMean of Total = %3.2f Standard Deviation of Total = %2.2f\n",
mean, deviation);
return;
}
int compare(const SEISEKI *p, const SEISEKI *q)
{
return -(p->total - q->total);
}
double mean_of_total(SEISEKI * student,int number)
{
int i = 0;
double total = 0.0;
double mean_total = 0.0;
while(i < number)
{
total += student[i].total;
i++;
}
mean_total = total/number;
return mean_total;
}
double hensa(SEISEKI * student, int number, double mean)
{
int i = 0;
double standard_deviation = 0.0;
double total = 0.0;
while(i < number)
{
total += ((student[i].total - mean)*(student[i].total - mean));
i++;
}
total = total / number;
standard_deviation = sqrt(total);
i = 0;
while(i < number)
{
student[i].hensa = (50 + (10*(student[i].total - mean)/standard_deviation));
i++;
}
return standard_deviation;
}