模拟player成绩统计

来源:互联网 发布:鸡蛋进口数据 编辑:程序博客网 时间:2024/06/02 12:26

原始要求:

对参赛结果分数进行处理:参赛选手n人(n>1),评委m人(m>2),评委给每一选手打一个分数score(分数score为小于等于10的一个正实数)。选手的最后得分lastScore计算方法为

1m<9时,去掉一个最高分和一个最低分后另m-2个得分的平均值。

2m9时,去掉两个最高分和两个最低分后另m-4个得分的平均值。

假设事先已经建立了text型的数据文件f1.txt,其中依次记录着n个选手的编号(一个正整数)、姓名(一个字符串)以及m个评委给出的得分。

请编制程序,依次从数据文件f1.txt中读入n个选手的有关信息,而后按上述规定方法计算出每一个选手的最后得分,而且往屏幕上以及另一个text型文件f2.txt中同时输出如下形式的结果信息。

假设参赛选手人数n=5,评委人数m=7,磁盘文件f1.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
那么,程序执行后,屏幕显示结果以及磁盘文件f2.txt中的结果均应该为:
----------------------------------------------------------
 
参赛号    姓 名     最高分    最低分   累积分    最后得分
----------------------------------------------------------
     1  zhangjin       9.7       7.9      44.9        8.98
     2    lintao       9.3       8.2      43.9        8.78
     3   guojian       8.9       8.4      42.9        8.58
     4    maling       8.9       7.9      42.2        8.44
     5  liuyifan       9.8       8.9      46.3        9.26
----------------------------------------------------------

以下是代码部分:

/*编译环境为VC 2005,如果在VC 6.0下运行,需要稍作修改才能通过编译*/

#include <iostream>
#include <fstream>
using namespace std;

struct player
{
 int number;
 char name[40];
 double * score;
};

void Sort(double * s, int n); // 排序函数
double Total(double * s, int n); // 计算总分(去掉最低分和最高分)
double Average(double * s, int n);  // 计算最终成绩函数

int main()
{

/*文件打开操作*/

 ifstream fin;
 fin.open("f1.txt");
 if (fin.fail())
 {
  cout << "文件读取失败..." << endl;
  exit(0);
 }

/*读入数据,并分析出数据总行数与每个选手分数的个数(列数)*/

 int rows = 0;
 int cols = 0;
 char temp;
 while (!fin.eof())
 {
  temp = fin.get();
  if (temp == '/n') // 以回车判定行数,文件最后一行数据一定要以/n结束,否则会出错!
   rows++;
  else if (temp == '.') // 以.判定有多少位裁判给分(总计)
   cols++;
 }
 cols /= rows; // 每位选手有多少个分数

/*为player结构分配空间,然后存入相应数据*/

 player * list = new player[rows];
 for (int i = 0; i < rows; ++i)
  list[i].score = new double[cols];
 fin.clear();
 fin.close();
 fin.open("f1.txt");
 if (fin.fail())
 {
  cout << "文件读取失败..." << endl;
  exit(0);
 }
 for (int i = 0; i < rows; ++i)
 {
  fin >> list[i].number;
  fin >> list[i].name;
  for (int j = 0; j < cols; ++j)
   fin >> list[i].score[j];
  fin.get(); // 屏蔽回车
 }
 fin.close();

/*整理player成绩,先进行排序,然后计算,最后输出到文件*/
 
 for (int i = 0; i < rows; ++i)
  Sort(list[i].score, cols);
 ofstream fout;
 fout.open("f2.txt");
 if(fout.fail())
 {
  cout << "文件建立失败...";
  exit(0);
 }
 fout << "-----------------------------------------------------------/n";
 fout << " 参赛号    姓 名    最低分    最高分    累计分    最后得分/n";
 fout << "-----------------------------------------------------------/n";
 fout.setf(ios_base::fixed);   // 格式控制
 for (int i = 0; i < rows; ++i)
 {
  fout.precision(1);
  fout.width(4);
  fout << list[i].number;
  fout.width(12);
  fout << list[i].name;
  fout.width(8);
  fout << list[i].score[0];
  fout.width(10);
  fout << list[i].score[cols - 1];
  fout.width(11);
  fout << Total(list[i].score, cols);
  fout.width(11);
  fout.precision(2);
  fout << Average(list[i].score, cols);
  fout << endl;
 }
 fout << "-----------------------------------------------------------/n";
 cout << "f2.txt文件成功建立。/n";
 return 0;
}

void Sort(double * s, int n) // 选择排序
{
 double temp;
 int pos;
 for (int i = 0;i < n - 1;++i)
 {
  temp = s[i];
  pos = i;
  for (int j = i + 1;j < n;j++)
  {
   if (s[j] < temp)
   {
     temp = s[j];
     pos = j;
   }
  }
  s[pos] = s[i];
  s[i] = temp;
 }
}

double Total(double * s, int n)
{
 double sum = 0;
 if (n < 9)
 {
  for (int i = 1; i < n - 1; ++i)
   sum += s[i];
  return sum;
 }
 else
 {
  for (int i = 2; i < n - 2; ++i)
   sum += s[i];
  return sum;
 }
}
  
double Average(double * s, int n)
{
 if (n < 9)
  return (Total(s, n) / (n - 2));
 else
  return (Total(s, n) / (n - 4));
}