C语言(不知道哪里出错,求助)
题目:
对参赛结果分数进行处理:参赛选手n人(n>1),评委m人(m>2),评委给每一选手打一个分数score(分数score为小于等于10的一个正实数)。
选手的最后得分lastScore计算方法为:
(1)m<9时,去掉一个最高分和一个最低分后另m-2个得分的平均值。
(2)m≥9时,去掉两个最高分和两个最低分后另m-4个得分的平均值。
假设事先已经建立了文本文件f1.txt,其中依次记录着n个选手的编号(一个正整数)、姓名(一个字符串)以及m个评委给出的得分。编制程序,依次从数据文件file1.txt中读入n个选手的有关信息,而后按上述规定方法计算出每一个选手的最后得分,而且往屏幕上以及另一个文本文件file2.txt中同时输出如下形式的结果信息。
1 zhangjin 8.8 9.3 7.9 8.7 8.9 9.7 9.2
2 lintao 8.9 8.2 8.6 8.8 8.5 9.1 9.3
3 guojian 8.9 8.4 8.7 8.6 8.6 8.4 8.6
4 maling 7.9 8.3 8.5 8.6 8.5 8.9 8.3
5 liuyifan 9.5 9.1 9.8 9.2 9.0 9.5 8.9
<----------------------------------------------------------------------------------------------->
#include<stdio.h>
#include<conio.h>
struct player
{
int number;//编号
char name;//姓名
float *score;
float average_score;//分数平均值
}*pp;
void sortscore(float *score, int n);//将每个人的分数排列
float score_caculation(float *score, int m);//计算平均分
int main()
{
int n = 0; //n个选手
int m = 0; //m个评委
int i, j;
char temp;
FILE *fp1, *fp2;
if ((fp1 = fopen("file1.txt", "r+")) == NULL)//打开file1.txt
{
printf("Cannot open the file1!");
getch();
return -1;
}
while (!feof(fp1))//选手个数n,评委个数m
{
temp = fgetc(fp1);
if (temp == '\n')
n++;
else if (temp == '.')
m++;
}
m /= n;
struct player *list;
list = new struct player;//给player分配内存
for (i = 0;i < m;++i)
{
list.score = new float;//给player中的m个分数分配内存
}
pp = list;
rewind(fp1);
fclose(fp1);
if ((fp1 = fopen("file1.txt", "r+")) == NULL)//打开file1.txt
{
printf("Cannot open the file1!\n");
getch();
return -1;
}
for (i = 0;i < n;i++, pp++)//读入数据
{
fscanf(fp1, "%d%s", &pp->number, pp->name);
for (j = 0;j < m;j++)
{
fscanf(fp1, "%f", &pp->score);
}
}
fclose(fp1);
for (i = 0;i < n;i++)//对每个选手的m个分数进行排序
sortscore(list.score, m);
for (i = 0;i < n;i++)//对每个选手的分数取平均值
list.average_score = score_caculation(list.score, m);
for (i = 0;i < n - 1;i++)//根据选手分数平均值将选手排列
{
struct player temp_player;
temp_player = list;
for (j = 1;j < n;j++)
{
if (list.average_score < list.average_score)
{
list = list;
list = temp_player;
}
}
}
if ((fp2 = fopen("file2.txt", "w")) == NULL)//打开file2.txt,并将数据写入file2.txt
{
printf("Cannot open file2\n");
getch();
return -1;
}
fprintf(fp2, "\t编号\t姓名\t得分\t名次\n");
for (i = 0;i < n;i++)
{
fprintf(fp2, "\t%3d\t%s\t%3f\t%d\n", list.number, list.name, list.average_score, i + 1);
}
fclose(fp2);
}
void sortscore(float *score, int m)//对每个选手的分数进行排序
{
float temp;
int i, j;
for (i = 0;i < m - 1;i++)
{
temp = score;
for (j = i + 1;j < m;j++)
{
if (score < score)
{
temp = score;
score = score;
score = temp;
}
}
}
}
float score_caculation(float *score, int m)//计算每个选手的平均值
{
float sum, average;
int i;
if (m < 9)
{
for (i = 1;i < m - 1;i++)
{
sum = sum + score;
}
average = sum / (m - 2);
}
else
{
for (i = 2;i < m - 2;i++)
{
sum = sum + score;
}
average = sum / (m - 4);
}
return average;
}
<----------------------------------------------------------->
下面是我输出到另一个文件里的内容,得分出现错误
编号 姓名 得分 名次
1 zhangjin -21474828.000000 1
2 lintao -21474828.000000 2
3 guojian -21474828.000000 3
4 maling -21474828.000000 4
5 liuyifan -21474828.000000 5
{:10_269:}{:10_269:}{:10_269:} 数据读取出错,有没有大佬帮助下 linux环境下无法测试,只是把某些bug做了提示,楼主可查看下
#include<stdio.h>
#include<conio.h>
struct player
{
int number;//编号
char name;//姓名
float *score;
float average_score;//分数平均值
}*pp;
void sortscore(float *score, int n);//将每个人的分数排列
float score_caculation(float *score, int m);//计算平均分
int main()
{
int n = 0; //n个选手
int m = 0; //m个评委
int i, j;
char temp;
FILE *fp1, *fp2;
if ((fp1 = fopen("file1.txt", "r+")) == NULL)//打开file1.txt
{
printf("Cannot open the file1!");
getch();
return -1;
}
while (!feof(fp1))//选手个数n,评委个数m
{
temp = fgetc(fp1);
if (temp == '\n')
n++;
else if (temp == '.')
m++;
}
m /= n;
struct player *list;
list = new struct player;//给player分配内存
for (i = 0;i < n;++i)///////////////////////////////////////////////////m--->n
{
list.score = new float;//给player中的m个分数分配内存
}
pp = list;
rewind(fp1);
// fclose(fp1);
// if ((fp1 = fopen("file1.txt", "r+")) == NULL)//打开file1.txt
// {
// printf("Cannot open the file1!\n");
// getch();
// return -1;
// }
for (i = 0;i < n;i++, pp++)//读入数据
{
// fscanf(fp1, "%d %s", &pp->number, pp->name);
fscanf(fp1, "%d", &pp->number);
fscanf(fp1, "%s", &pp->name);
for (j = 0;j < m;j++)
{
fscanf(fp1, "%f", &pp->score);
}
}
fclose(fp1);
for (i = 0;i < n;i++)//对每个选手的m个分数进行排序
sortscore(list.score, m);//////////////////////////////排序
for (i = 0;i < n;i++)//对每个选手的分数取平均值
list.average_score = score_caculation(list.score, m);
for (i = 0;i < n - 1;i++)//根据选手分数平均值将选手排列
{
struct player temp_player;
temp_player = list;
for (j = 1;j < n;j++)
{
if (list.average_score < list.average_score)
{
list = list;
list = temp_player;
}
}
}
if ((fp2 = fopen("file2.txt", "w")) == NULL)//打开file2.txt,并将数据写入file2.txt
{
printf("Cannot open file2\n");
getch();
return -1;
}
fprintf(fp2, "\t编号\t姓名\t得分\t名次\n");
for (i = 0;i < n;i++)
{
fprintf(fp2, "\t%3d\t%s\t%3f\t%d\n", list.number, list.name, list.average_score, i + 1);
}
fclose(fp2);
}
void sortscore(float *score, int m)//对每个选手的分数进行排序
{
float temp;
int i, j;
for (i = 0;i < m - 1;i++)
{
temp = score;
for (j = i + 1;j < m;j++)
{
if (score < score)
{
temp = score;
score = score;
score = temp;
}
}
}
}
float score_caculation(float *score, int m)//计算每个选手的平均值
{
float sum, average;
int i;
if (m < 9)
{
for (i = 1;i < m - 1;i++)
{
sum = sum + score;
}
average = sum / (m - 2);
}
else
{
for (i = 2;i < m - 2;i++)
{
sum = sum + score;
}
average = sum / (m - 4);
}
return average;
}
页:
[1]