順位付け/同率順位について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
as

順位付け/同率順位について

#1

投稿記事 by as » 8年前

英語・数学・社会の点数とそれぞれの氏名が入力されたファイルを使い、個人の合計・平均・標準偏差を算出し、合計点が高い順にランンキングをつける という問題なのですが、順位付けのところのプログラムがわかりません。(同率順位を含みます)

結果例
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;
}

Math

Re: 順位付け/同率順位について

#2

投稿記事 by Math » 8年前

motofile.dat

コード:

insatsu.dat

コード:

としてコマンドライン・コンパイルが終わると自動実行させるバッチを作って
c.bat

コード:

rem コンパイル後リンク
cl /TC c1.txt
rem 実行結果
c1.exe motofile.dat insatsu.dat
実行結果

コード:

D:\h\z10\01\13\c\2>c

D:\h\z10\01\13\c\2>rem コンパイル後リンク

D:\h\z10\01\13\c\2>cl /TC c1.txt
Microsoft(R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

c1.txt
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c1.exe
c1.obj

D:\h\z10\01\13\c\2>rem 実行結果

D:\h\z10\01\13\c\2>c1.exe motofile.dat insatsu.dat

D:\h\z10\01\13\c\2>
なのでmotofile.datと”順番・計算結果などは正しく出せました”というinsatsu.datを提示してくださいませんか。
なので

閉鎖

“C言語何でも質問掲示板” へ戻る