`
txf2004
  • 浏览: 6864877 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

《C++第十六周实验报告2-1》---学生成绩排序

 
阅读更多
/*
【任务2】学生成绩排序
文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩。
(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据
需要确定。
(2)读入这名学生的成绩,用对象数组进行存储。
(3)求出各科和总分的最高分。
(4)请按总分的降序(高成绩在前,低成绩在后)排序
(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat
中。
*/
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:   Student.cpp                           
* 作    者:   计114-3 王兴锋     
* 完成日期:    2012  年  6  月   4  日
* 版 本 号:       V 6.0
* 对任务及求解方法的描述部分 
* 程序头部的注释结束
*/
#include <fstream>
#include <iostream>
#include "iomanip"
#include "string"

using namespace std;

class Student
{
public:
	void set_name(string na);
	string get_name();
	void setcpp_score(double sc);
	double getcpp_score();
	void setmath_score(double sc);
	double getmath_score();
	void seten_score(double sc);
	double geten_score();
	void settotal_score(double sc);
	double gettotal_score();
	void setaver_score(double sc);
	double getaver_score();
	//
	void show_score();//输出成绩;
private:dc
	string name;
	double cpp_score;
	double math_score;
	double en_score;
	double total_score;
	double aver_score;
};
void Student::set_name(string na)
{
	name = na;
}
string Student::get_name()
{
	return name;
}
void Student::setcpp_score(double sc)
{
	cpp_score = sc;
}
double Student::getcpp_score()
{
	return cpp_score;
}
void Student::setmath_score(double sc)
{
	math_score = sc;
}
double Student::getmath_score()
{
	return math_score;
}
void Student::seten_score(double sc)
{
	en_score = sc;
}
double Student::geten_score()
{
	return en_score;
}
void Student::settotal_score(double sc)
{
	total_score = sc;
}
double Student::gettotal_score()
{
	return total_score;
}
void Student::setaver_score(double sc)
{
	aver_score = sc;
}
double Student::getaver_score()
{
	return aver_score;
}
//定义void show_score函数;
void Student::show_score()
{
	cout << setw(6) << setiosflags(ios::left) << name << " " 
		<< cpp_score << '\t' << math_score << '\t' << en_score << '\t' << total_score << '\t' << aver_score << '\t';		
	cout<<endl;
}
////////////////////////////////////////////////////////////////////////////
void cin_score(int num, Student st[]);//从文件得到成绩;
void cout_score(int num, Student st[]);//储存成绩
void array_score(int num, Student st[]);
void get_cpp(int num, double sc[], Student st[]);
void get_math(int num, double sc[], Student st[]);
void get_en(int num, double sc[], Student st[]);
void getmax_indexs(int num, double sc[], int index[]);

int main( )
{	
	Student st[100];
	double sc[100];
	int cpp[25], math[25], en[25];
	//从文件得到学生成绩
	cin_score(100, st);
	array_score(100, st);//对成绩按最高分进行排序
	//得到C++成绩
	get_cpp(100, sc, st);
	getmax_indexs(100, sc, cpp);//得到C++最高分的下标
	//得到高数成绩
	get_math(100, sc, st);
	getmax_indexs(100, sc, math);//得到高数最高分的下标
	//得到英语成绩
	get_en(100, sc, st);
	getmax_indexs(100, sc, en);//得到英语最高分的下标
	//输出排序后的成绩
	for (int i = 0; i < 100; i++)
	{
		st[i].show_score();
	}
	//保存成绩
	cout_score(100, st);

	cout << "C++的最高分是:" << endl;
	for (int i1 = 0; cpp[i1] != -1; i1++)
		cout << "  " << st[cpp[i1]].get_name() << " " << st[cpp[i1]].getcpp_score() << endl;

	cout << "高数的最高分是:" << endl;
	for (int i2 = 0; math[i2] != -1; i2++)
		cout << "  " << st[math[i2]].get_name() << " " << st[math[i2]].getmath_score() << endl;

	cout << "英语的最高分是:" << endl;
	for (int i3 = 0; en[i3] != -1; i3++)
		cout << "  " << st[en[i3]].get_name() << " " << st[en[i3]].geten_score() << endl;

	cout << "总分的最高分是:" << endl;
	do{
		static int i = 0;
	
		cout << "  " << st[i].get_name() << " " << st[i].gettotal_score() << endl;

	}while(st[0].gettotal_score() == st[++i].gettotal_score());

	system("pause");
	return 0;
}
//定义从文件输入函数;
void cin_score(int num, Student st[])
{
	string name;
	double cpp_score;
	double math_score;
	double en_score;

	ifstream infile("score.dat", ios::in);
	if (!infile)
	{
		cerr << "open error!" << endl;
		exit(1);
	}
	for (int i = 0; i < num; i++)
	{
		infile >> name >> cpp_score >> math_score >> en_score;//读入
		st[i].set_name(name); 
		st[i].setcpp_score(cpp_score); 
		st[i].setmath_score(math_score); 
		st[i].seten_score(en_score);
		st[i].settotal_score(cpp_score+math_score+en_score);
		st[i].setaver_score((cpp_score+math_score+en_score)/3);
	}
	infile.close();
}
//定义输出函数
void cout_score(int num, Student st[])
{
	ofstream outfile("odered_score.dat",ios::out);
	if (!outfile)                        
	{
		cerr << "open error!" << endl;
		exit(1);
	}
	else
	{
		for (int i = 0; i < num; i++)
		{
			outfile << st[i].get_name() << '\t' << st[i].getcpp_score() 
				<< '\t' <<  st[i].getmath_score() << '\t' <<  st[i].geten_score() << '\t' << st[i].gettotal_score() << '\n';//输出成绩;   
		}
		outfile.close();

		cout<<"成绩已保存!"<<endl;
	}
}
//定义排序函数;
void array_score(int num, Student st[])
{
	Student store;

	for (int i = 0; i < num-1; i++)
		for (int j = 0; j < num-i-1; j++)
		{
			if (st[j].gettotal_score() <= st[j+1].gettotal_score())
			{
				store = st[j];
				st[j] = st[j+1];
				st[j+1] = store;
			}
		}
}
void get_cpp(int num, double sc[], Student st[])
{
	for (int i = 0; i < num; i++)
	{
		sc[i] = st[i].getcpp_score();
	}
}
void get_math(int num, double sc[], Student st[])
{
	for (int i = 0; i < num; i++)
	{
		sc[i] = st[i].getmath_score();
	}
}
void get_en(int num, double sc[], Student st[])
{
	for (int i = 0; i < num; i++)
	{
		sc[i] = st[i].geten_score();
	}
}
void getmax_indexs(int num, double sc[], int index[])
{
	int max_index = 0;
	int count = 0;
	index[count] = max_index;

	for (int i = 1; i < num; i++)
	{
		if (sc[i] > sc[max_index])
		{
			max_index = i;
			count = 0;
			index[count++] = i;
		}
		else if (sc[i] == sc[max_index])
		{
			index[count++] = i;
		}
	}
	index[count] = -1;
}
/*
王琦   98       95      98      291     97
宋宗杰 94       100     92      286     95.3333
杨阔   90       91      98      279     93
冼丹   100      89      89      278     92.6667
魏佳   100      94      80      274     91.3333
范振光 98       87      89      274     91.3333
张昊   94       83      96      273     91
赵旭洋 87       91      94      272     90.6667
吴清正 89       97      85      271     90.3333
高举   81       99      91      271     90.3333
冯松   89       98      83      270     90
马婧   98       84      87      269     89.6667
李朋   90       82      97      269     89.6667
韩明   83       97      88      268     89.3333
王芳   71       97      99      267     89
张迪   99       88      80      267     89
文静   93       88      85      266     88.6667
王磊   87       86      92      265     88.3333
刘盈   99       72      93      264     88
王瑞麒 89       83      91      263     87.6667
叶丹   87       80      96      263     87.6667
杨洁   96       79      87      262     87.3333
李桐   93       83      86      262     87.3333
董一伟 93       88      80      261     87
张佳玮 61       98      96      255     85
杨梦婕 89       99      67      255     85
刘紫亮 72       98      84      254     84.6667
刘亚新 77       81      95      253     84.3333
王蒙   67       97      89      253     84.3333
黄金龙 85       90      78      253     84.3333
徐嘉琦 90       75      87      252     84
王姝   70       91      90      251     83.6667
崔赞   91       67      93      251     83.6667
葛志伟 100      79      71      250     83.3333
张笑   86       88      76      250     83.3333
马佳   60       90      100     250     83.3333
王锐   63       90      96      249     83
张敏   85       75      89      249     83
裴培   75       82      91      248     82.6667
冷云   89       88      71      248     82.6667
宋媛媛 61       94      92      247     82.3333
马立   73       90      83      246     82
张里响 85       65      96      246     82
何煜中 90       73      82      245     81.6667
王竞   90       87      67      244     81.3333
高清   76       83      84      243     81
梁雅宁 55       88      100     243     81
吴佳林 96       65      82      243     81
唐楠   68       97      77      242     80.6667
宋航彬 80       71      91      242     80.6667
蔺剑飞 88       75      79      242     80.6667
马里   73       95      73      241     80.3333
张龙   62       100     78      240     80
李悦   63       79      97      239     79.6667
贾伟林 63       90      86      239     79.6667
鲁继森 84       79      75      238     79.3333
周恒   87       82      69      238     79.3333
田苗苗 75       91      71      237     79
徐金竹 75       89      73      237     79
陈美珠 82       72      83      237     79
佟欣   60       79      98      237     79
边里   56       94      87      237     79
薛淇文 89       71      75      235     78.3333
张扬   77       65      93      235     78.3333
高路   63       74      98      235     78.3333
印虹   92       68      75      235     78.3333
于浩   78       84      72      234     78
刘得意 60       98      75      233     77.6667
黄京   62       75      96      233     77.6667
张雯   69       70      93      232     77.3333
王欣欣 71       83      78      232     77.3333
苏明霞 59       79      94      232     77.3333
孙大伟 65       69      98      232     77.3333
郭倩   69       94      69      232     77.3333
任盛达 57       86      88      231     77
王悦   79       82      70      231     77
杨华鑫 81       81      68      230     76.6667
贺祺   61       96      72      229     76.3333
金昕   92       67      69      228     76
陈世勃 70       92      65      227     75.6667
宋静   69       85      73      227     75.6667
王磊   71       78      77      226     75.3333
方圆   70       79      76      225     75
林倩   67       77      80      224     74.6667
汤娜   68       85      71      224     74.6667
兰天   83       66      74      223     74.3333
刘京西 67       78      78      223     74.3333
何佳成 70       75      78      223     74.3333
杨超   67       73      82      222     74
冯佳媛 61       79      81      221     73.6667
周俊升 57       68      96      221     73.6667
马骁   62       67      90      219     73
赵媛媛 77       75      66      218     72.6667
卫青   66       73      77      216     72
白涛   57       82      75      214     71.3333
吴玮   69       76      68      213     71
于莉   55       66      78      199     66.3333
桂佳   60       73      65      198     66
徐一菡 85       45      62      192     64
王欢欢 57       33      66      156     52
成绩已保存!
C++的最高分是:
  冼丹 100
  魏佳 100
  葛志伟 100
高数的最高分是:
  宋宗杰 100
  张龙 100
英语的最高分是:
  马佳 100
  梁雅宁 100
总分的最高分是:
  王琦 291
Press any key to continue
*/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics