C++小项目(超详细注释适合新手参考)

奖学金评定系统

一,系统功能

(1)能输入若干个学生的科目成绩,每个学生一组成绩数据;

(2)计算每个学生的平均成绩,并对有不及格学生做标记;

(3)用户可选择不同的排序方法对学生平均成绩进行排序;

(4)选取平均成绩前5位的同学为奖学金获得者(如果该生有成绩不及格则不能获得奖学金),并输出学生的姓名及综合成绩(平均分);

(5)用户可选择是否输出含有不及格科目的学生。

注:假设考试科目满分都为100,及格线都为60。

二,项目涉及:

1,类与对象,对象数组,

2,根据对象的某个成员变量把对象数组排序

3,多种不同的排序方式(冒泡排序 插入排序 快速排序)

4,结构体,指针,链表的操作(创建和插入)

三,操作演示

1,显示询问界面

2,引导用户完成输入

3,打印输出

 四,附源代码

student.h

#pragma once
#include<string>
using namespace std;

class stu
{
private:
	int kemu = 0;//学生考试科目数
	double fen[100] = { 0 };//用于存储学生各科目分数  最多100科

public:
	string mingzi;//学生姓名
	double ping = 0;//平均分数
	int bujige = 0;//不及格科目数

	void get(string s, int ke, double f[]);
	//获取学生各项信息  包括 姓名 考试科目数 各科分数

	bool pass();
	//统计不及格科目数,判断是否含有不及格科目
};

student.cpp

#include"student.h"
#include<iostream>
using namespace std;
void stu::get(string s, int ke, double f[])
{
	//把获取到的学生信息放入类内的变量存储
	mingzi = s;
	kemu = ke;
	double t = 0;//求和 用于求平均
	for (int i = 0; i < kemu; i++)
	{
		t += f[i];
		fen[i] = f[i];//记录各科目分数
	}
	ping = t / kemu;
}
bool stu::pass()
{
	for (int i = 0; i < kemu; i++)
		if (fen[i] < 60)
			bujige++;//统计不及格科目数

	for (int i = 0; i < kemu; i++)
	{
		if (fen[i] < 60)
			return false;//含有不及格科目 不参与奖学金评选
	}
	cout << endl;
	return true;

}

zhu.cpp

#include"student.h"
#include<iostream>
#include<string>
using namespace std;
int s_count;//学生数目
int k_count;//考试科目数
string name;//学生姓名
double f[100];//临时存放学生各科目分数
int xuan;//用户交互  排序方法(或退出)的选择

struct student//含有不及格科目的人员结构体
{
	int num;//不及格科目数
	string ming;//名字
	student* next;//结构体指针
};
void List_pushback(student** pphead, int num, string ming)
{
	//尾插法插入
	student* newnode = new student{ num,ming,NULL };//不及格科目数,名字,next为空
	if (*pphead == NULL)//头结点是否为空
	{
		*pphead = newnode;//为空的话 直接赋值给头结点
	}
	else
	{
		student* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;//寻找尾结点
		}
		tail->next = newnode;//成为新的尾结点
	}
}
void List_printf(student* phead)//打印不及格人员链表
{
	student* t = phead;
	if (t == NULL)
		cout << "大家都很棒,没有不及格人员" << endl;

	while (t != NULL)//从头打印到尾
	{
		cout << "姓名:" << t->ming << "  " << "不及格科目数:" << t->num << endl;
		t = t->next;
	}
}
void List_creat(stu array[])
{
	//尾插法插入不及格人员
	student* phead = NULL;
	for(int i=0;i<s_count;i++)
	{
		if (array[i].bujige != 0)//检测到有人 含有不及格科目
		{
			List_pushback(&phead, array[i].bujige, array[i].mingzi);
			//调用插入函数,把他的信息放入链表
			
		}
	}
	List_printf(phead);//打印
}
void xuanze()
{
	//用户交互界面  排序方法的选择
	cout << "1、退出系统" << endl;
	cout << "2、冒泡排序" << endl;
	cout << "3、插入排序" << endl;
	cout << "4、快速排序" << endl;
	cin >> xuan;//用户输入他的选择
	std::system("cls");//清理屏幕
}
void shuru(stu sss[])
{
	int t = s_count;//学生数目
	while(t--)
	{
		cout << "输入第" << s_count - t << "个学生的名字" << endl;
		cin >> name;
		cout << "输入各科分数(" << k_count << ")" << endl;
		for (int i = 0; i < k_count; i++)
			cin >> f[i];
		std::system("cls");//清理屏幕

		sss[t].get(name, k_count, f);
		//把用户输入的各个学生信息 放入对象存储
	}
}
void QuickSort(stu array[], int low, int high) {
	//快速排序
	if (low >= high) {
		return;
	}
	int i = low;
	int j = high;	
	stu key = array[low];
	while (i < j) {
		while (array[j].ping <= key.ping && i < j) {
			j--;
		}
		array[i] = array[j];
		while (array[i].ping >= key.ping && i < j) {	
			i++;
		}
		array[j] = array[i];	
	}
	array[i] = key;	
	QuickSort(array, low, i - 1);	
	QuickSort(array, i + 1, high);	
}
void bubblesort(stu array[], int len)	//形参a取到实参a传递过来的数组首地址
//然后解引用,取到数组的值 
{
	//冒泡排序
	for (int i = 0; i < len - 1; i++)	//i控制排序的轮数 
	{
		for (int j = 0; j < len - i - 1; j++)	//j控制每轮需要比较的次数 
		{
			if (array[j + 1].ping > array[j].ping)	//不满足正序要求,交换顺序 
			{
				stu temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}
	}
}
void InsertSort(stu array[],int len)
{
	//插入排序
	stu temp;
	int i, j;
	for (int i = 1; i < len; i++) 
	{  
		// 数组的下标是从0开始的
		//这里从第二个数开始枚举  即假定第一个数是有序的

		temp = array[i]; 
		j = i;     // 这里temp 临时储存每一次需要排序的数
		while (j >= 1 && temp.ping > array[j - 1].ping) {
			array[j] = array[j - 1];
			j--;
		}
		array[j] = temp;
	}
}
void shuchu(stu sss[])
{
	switch (xuan)
	{
	case2:cout << "冒泡排序" << endl; break;
	case3:cout << "插入排序" << endl; break;
	default:cout << "快速排序" << endl;
	}
	int k = 0;
	for (int i = 0; i < s_count; i++)
	{
		if (sss[i].pass() && k < 5)
		cout << "获奖者:" << sss[i].mingzi << "  "
			<< "综合得分:" << sss[i].ping << endl;
		k++;
	}
	cout << endl;
	cout << endl;
	cout << endl;
	cout << "是否打印含有不及格科目的 人员列表?" << endl;
	cout << "1,是的 打印" << endl;
	cout << "2,不用 谢谢" << endl;
	cin >> xuan;
	if (xuan == 1)
		List_creat(sss);
	else if (xuan == 2)
		return;
	else
		cout << "输入错误" << endl;

}

int main()
{
	xuanze();//选择排序方式
	if (xuan == 1)
		return 0;
	cout << "输入学生数" << endl;
	cin >> s_count;
	cout << "输入科目数" << endl;
	cin >> k_count;
	std::system("cls");//清理屏幕
	stu* sss = new stu[s_count];//创建学生对象的数组1

	shuru(sss);//用户输入各个学生的信息

	stu* S = new stu[s_count];//创建学生对象的数组2
	S = sss;//用于替代sss的后续操作  以便保存原始数据

	switch (xuan)
	{
	case2:bubblesort(S, s_count - 1); break;//冒泡排序
	case3:InsertSort(S, s_count - 1); break;//插入排序
	default:QuickSort(S, 0, s_count - 1);//快速排序
	}
	std::system("cls");//清理屏幕
	shuchu(sss);//降序列出获奖人员名单及其综合得分
	std::system("pause");
}

能力有限  诸多不足,望多指正更不吝赐教。感谢你的阅读和点赞鼓励!

  • 11
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值