计算学生成绩总分和查找学生成绩(C++)

某班期末考试科目为数学(MT)、英语(EN)和物理(PH),有最多不超过30人参加考试。考试后要求:
(1)计算每个学生的总分和平均分;
(2)按总分成绩由高到低排出成绩的名次;
(3)打印出名次表,表格内包括学生编号、各科分数、总分和平均分;
(4)任意输入一个学号,能够查找出该学生在班级中的排名及其考试分数。

#include<iostream>
using namespace std;
void bubble(double a[], int);
int main()
{
	string stu_names[]{ "01","02","03","04","05" };
	string course_names[]{ "数学","英语","物理" };
	const int Row = 5;
	const int Col = 3;
	double scores[Row][Col];
	//输入1~30号学生的成绩
	for (int i = 0; i < Row; i++)
	{
		for (int j = 0; j < Col; j++)
		{
			cout << stu_names[i] << "的" << course_names[j] << "成绩:";
			cin >> scores[i][j];
		}
	}
	//求学生的总分与平均分
	const int a = 5, b = 5;
	double zongfen[a];
	double zongfen1[a];//多复制一个总分的数组,因为使用冒泡排序之后数组中的元素顺序已经改变
	double pinjunfen[b];
	for (int i = 0; i < Row; i++)
	{
		double sum = 0;
		double ave = 0;
		for (int j = 0; j < Col; j++)
		{

			sum += scores[i][j];
			ave = sum / 3;

		}
		zongfen[i] = sum;
		zongfen1[i] = sum;
		pinjunfen[i] = ave;
	}
	//因为本人电脑‘/t’无法显示(不知道为什么,所以用空格代替
	cout << "学生编号" << " " << "数学" <<" " << "英语" <<" " << "物理" << " " << "总分" <<" " << "平均分" << endl;
	for (int i = 0; i < Row; i++)
	{
		cout << stu_names[i] << "   ";
		for (int j = 0; j < Col; j++)
		{
			cout << scores[i][j] << "     ";
		}
		cout << zongfen[i] << "   " << pinjunfen[i] << endl;
	}

	//给总分排序
	bubble(zongfen, Row);

	//输入学号查询
	for (int d = 0; d < 30; d++)//多加一个循环可以起到一直查找的作用
	{
		string c;
		cout << "请输入想要查询的学生的成绩:";
		cin >> c;

		//查询学生各科分数与总分
		for (int i = 0; i < Row; i++)
		{
			if (stu_names[i] == c)
			{
				cout << "总分:" << zongfen1[i];
				for (int j = 0; j < Row; j++)
				{
					if (zongfen1[i] == zongfen[j])
						cout << "排名:第" << j + 1 << " " << endl;
				}
			}

		}
	}


	return 0;
}
//冒泡排序
void bubble(double a[], int size)
{
	double temp =0;//临时变量
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size - 1; j++)
		{
			if (a[j] < a[j + 1])
			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
}


**打印结果**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201227003004639.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MDE5MjM5Nw==,size_16,color_FFFFFF,t_70)

初学者,望指正
  • 9
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的学生成绩管理系统的C++代码: ```c++ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public: string name; int mathScore; int englishScore; int computerScore; Student(string name, int mathScore, int englishScore, int computerScore) { this->name = name; this->mathScore = mathScore; this->englishScore = englishScore; this->computerScore = computerScore; } int getTotalScore() { return mathScore + englishScore + computerScore; } }; vector<Student> students; void addStudent() { string name; int mathScore; int englishScore; int computerScore; cout << "请输入学生姓名:"; cin >> name; cout << "请输入数学成绩:"; cin >> mathScore; cout << "请输入英语成绩:"; cin >> englishScore; cout << "请输入计算成绩:"; cin >> computerScore; Student student(name, mathScore, englishScore, computerScore); students.push_back(student); cout << "添加成功!" << endl; } void deleteStudent() { string name; cout << "请输入要删除的学生姓名:"; cin >> name; for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) { if (it->name == name) { students.erase(it); cout << "删除成功!" << endl; return; } } cout << "没有找到该学生!" << endl; } void searchStudent() { string name; cout << "请输入要查询的学生姓名:"; cin >> name; for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) { if (it->name == name) { cout << "姓名:" << it->name << endl; cout << "数学成绩:" << it->mathScore << endl; cout << "英语成绩:" << it->englishScore << endl; cout << "计算成绩:" << it->computerScore << endl; cout << "总分:" << it->getTotalScore() << endl; return; } } cout << "没有找到该学生!" << endl; } void listStudents() { sort(students.begin(), students.end(), [](const Student& a, const Student& b) { return a.getTotalScore() > b.getTotalScore(); }); cout << "姓名\t数学成绩\t英语成绩\t计算成绩\t总分" << endl; for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) { cout << it->name << "\t" << it->mathScore << "\t\t" << it->englishScore << "\t\t" << it->computerScore << "\t\t" << it->getTotalScore() << endl; } } int main() { while (true) { cout << "====================" << endl; cout << "1. 添加学生" << endl; cout << "2. 删除学生" << endl; cout << "3. 查询学生" << endl; cout << "4. 列出学生" << endl; cout << "5. 退出程序" << endl; cout << "====================" << endl; int choice; cout << "请输入要执行的操作:"; cin >> choice; switch (choice) { case 1: addStudent(); break; case 2: deleteStudent(); break; case 3: searchStudent(); break; case 4: listStudents(); break; case 5: return 0; default: cout << "无效的操作!" << endl; break; } } } ``` 这个学生成绩管理系统实现了以下功能: 1. 添加学生:输入学生姓名、数学成绩、英语成绩计算成绩,将学生信息添加到学生列表中。 2. 删除学生:输入学生姓名,从学生列表中删除对应的学生。 3. 查询学生:输入学生姓名,从学生列表中查找对应的学生,并输出学生的姓名、数学成绩、英语成绩计算成绩总分。 4. 列出学生:按照总分从高到低排序,输出学生的姓名、数学成绩、英语成绩计算成绩总分。 5. 退出程序:退出学生成绩管理系统。 当然,这个学生成绩管理系统还可以继续完善,比如可以加入文件读写功能,将学生信息保存到文件中,以便下次启动时能够恢复之前的学生信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值