C++成绩排序

C++成绩排序


初学C++老师安排的作业,使用C++实现成绩排序,目前实现为:从.txt文档读入学生成绩,并显示。然后选择按照哪科成绩来排序。

//main.cpp
#include"main.h"
int main()
{
	ifstream in("score.txt");
	if (!in)
	{
		cout << "Fail to open the file!" << endl;
		exit(1);
	}
	vector<Student>s;
	string line, name, id;
	while (getline(in, line))
	{
		Student temp;
		istringstream record(line);
		record >> temp.Name;
		record >> temp.ID;
		record >> temp.Chinese;
		record >> temp.Math;
		record >> temp.English;
		temp.Sum_all();
		s.push_back(temp);
		
	}
	Disapaly_Student(s);
	cout << "Which subject do you want to rank?" << endl;
	string sub;
	cin >> sub;
	Sort_Score(s,sub);
	Disapaly_Student(s);

}
//main.h
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
class Student
{
public:
	string Name;
	string ID;
	int Chinese, Math, English,Total;
	Student()
	{
		;
	}
	Student(const char &Name,const char &ID,int Chinese,int Math,int English)
	{
		this->Name=Name;
		this->ID = ID;
		this->Chinese = Chinese;
		this->Math = Math;
		this->English = English;
	}
	//Student& operator=(vector<Student> &s)
	//{
	//	this->Name = s.;
	//	this->ID = ID;
	//	this->Chinese = Chinese;
	//	this->Math = Math;
	//	this->English = English;
	//	return *this;
	//}
	void Sum_all()
	{
		Total = Chinese + Math + English;
	}
};


void Disapaly_Student(vector<Student>&s)
{
	for (auto iter = s.begin(); iter != s.end(); iter++)
	{
		cout << "Name:" << iter->Name <<"  "<< "ID:" << iter->ID << "  "
			<< "Chinese:" << iter->Chinese << "  " << "Math:" <<
			iter->Math << "  " << "English:" << iter->English << 
			"  " << "Total:" << iter->Total << endl;
	}
}

void Sort_Score(vector<Student>&s,string & sub)
{
	Student temp;
	if (sub == "Chinese")
	{
		for (int i = 0; i != s.size(); i++)
			for (int j = 0; j != s.size() - i - 1; j++)
				if (s[j].Chinese < s[j + 1].Chinese)
				{
					temp = s[j];
					s[j] = s[j + 1];
					s[j + 1] = temp;
				}
	}
	if (sub == "Math")
	{
		for (int i = 0; i != s.size(); i++)
			for (int j = 0; j != s.size() - i - 1; j++)
				if (s[j].Math < s[j + 1].Math)
				{
					temp = s[j];
					s[j] = s[j + 1];
					s[j + 1] = temp;
				}
	}
	if (sub == "English")
	{
		for (int i = 0; i != s.size(); i++)
			for (int j = 0; j != s.size() - i - 1; j++)
				if (s[j].English < s[j + 1].English)
				{
					temp = s[j];
					s[j] = s[j + 1];
					s[j + 1] = temp;
				}
	}
	if (sub == "Total")
	{
		for (int i = 0; i != s.size(); i++)
			for (int j = 0; j != s.size() - i - 1; j++)
				if (s[j].Total < s[j + 1].Total)
				{
					temp = s[j];
					s[j] = s[j + 1];
					s[j + 1] = temp;
				}
	}
}

在这里插入图片描述
该.txt文档保存选择ANSI编码,不然在cmd中,中文显示会乱码。
在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值