C/C++程序设计大作业

1.任务描述

写一个命令行交互式 C 语言程序。该程序需要实现下述菜单功能:

1、数据存储。 

你需要将⼀系列的学生期末考试信息进行存储(学生人数不会超过 100)。每个学生的信息包括:姓名(例如 Liuyumeng) ;学号(12 位数字组成,开头 4 位为 2021、2020、2019);C 语言成绩(一个大于等于零的整数); GPA 等级(A+, A, B+, B, C+, C, D, F );班级排名(成绩相同需并列)。其中,姓名,学号,成绩为输入数据,其余数据需要你计算。 同时,你需要添加⼀些维护数据库的功能: 
Add(name, id, score): 新增⼀个学生的信息; 
Delete(id): 根据学号删除某个学生的信息; 
Search(id):根据学号查找某个学生的信息。 

2、数据处理。

Sort_by_id():生成根据学号顺序排列学生信息的表格 
Sort_by_score():生成根据分数由高到低顺序排列学生信息的表格 Max():返回最高分学生的信息 
Min(): 返回最低分学生的信息 
Ave(): 返回所有学生期末成绩平均分 
prime(id): 返回某个学生的成绩是否为素数 
coprime(id, id):返回某两个学生的成绩是否互质/互素 

3、数据分析 

根据 GPA 对学生成绩进行考情分析:A+多少⼈,A 多少⼈,以此类推。GPA 等级参考SZTU 评分等级。 

4、用户界面 

实现⼀个菜单,以供用户决定要使用哪个功能。

菜单参考: 
Hello, pls input a series of student information! 
(用户开始输入) 
Okay, data upload finished. What do you what to do next? You can enter a number to tell me. 1 add 
2 delete 
3 search 
4 sort by id 
5 sort by score 
6 best score 
7 worst score 
8 prime judge 
9 coprime judge 
… 
0 exit 
(用户输入)   
(输出计算结果) 
Do you still need my service? You can enter a number to tell me.  
1 add 
2 delete 
3 search 
4 sort by id 
5 sort by score 
6 best score 
7 worst score 
8 prime judge 
9 coprime judge 
… 
0 exit 
(用户输入)   
(输出计算结果)  

#include<iostream>
#include<string>
using namespace std;
class student {
	string name;//姓名
	string num;//学号
	int grape;//C语言成绩
	string gpa;//绩点
	int rank;//排名
public:
	student() {}
	student(string n, string a, int b) :name(n), num(a), grape(b) {
		if (grape >= 93) {
			gpa = "A+";
		}
		else if (grape >= 85 && grape < 93) {
			gpa = "A";
		}
		else if (grape >= 80 && grape < 85) {
			gpa = "B+";
		}
		else if (grape >= 75 && grape < 80) {
			gpa = "B";
		}
		else if (grape >= 70 && grape < 75) {
			gpa = "C+";
		}
		else if (grape >= 65 && grape < 70) {
			gpa = "C";
		}
		else if (grape >= 60 && grape < 65) {
			gpa = "D";
		}
		else if (grape <60) {
			gpa = "F";
		}
	}
	string getid() {
		return num;
	}
	string getgpa() {
		return gpa;
	}
	int getgrape() {
		return grape;
	}
	void print() {//输出学生的个人信息
		cout << "姓名:" << name << " " << "学号:" << num << " " << "C语言成绩:" << grape << endl;
	}
	friend int getmax(student* p, int n);//寻找最大分数学生的序列
	friend int getmin(student* p, int n);//寻找最小分数学生的序列
	friend int find(student* p, int n, string id);//寻找这个学号学生的序列号
};
int getmax(student* p, int n) {
	int max = 0;
	for (int i = 0; i < n; i++) {
		if (p[i].grape > p[max].grape) {
			max = i;
		}
	}
	return max;
}
int getmin(student* p, int n) {
	int min = 0;
	for (int i = 0; i < n; i++) {
		if (p[i].grape < p[min].grape) {
			min = i;
		}
	}
	return min;
}
int find(student* p, int n, string id) {
	for (int i = 0; i < n; i++) {
		if (p[i].num == id) {
			return i;
		}
	}
	return -1;
}
void swap(student& a, student& b) {//定义交换函数,用于交换类
	student t;
	t = a;
	a = b;
	b = t;
}
void pri1() {//菜单输出
	cout << "1 add" << "\n" << "2 delete" << "\n" << "3 search" << "\n" << "4 sort by id" << endl;
	cout << "5 sort by score" << "\n" << "6 best score" << "\n" << "7 worst score" << "\n" << "8 prime judge" << endl;
	cout << "9 coprime judge" << "\n" << "10 GPA analysis" << "\n" << "0 exit" << endl;
}
void pri2() {
	cout << "Do you still need my service? You can enter a number to tell me." << endl;
	pri1();
}
int main() {
	cout << "Hello, pls input a series of student information!" << endl;
	student stu[100];//用于存储学生信息
	int n = 0;//n用来记录学生的个数

	cout << "输入要录入的学生个数:";
	int number;//刚开始用户输入的数据个数
	cin >> number;
	for (int i = 0; i < number; i++) {
		string a, b;
		int c;//a为姓名,b为学号,c为成绩
		cout << "请输入学生的姓名、学号和C语言成绩:";
		cin >> a >> b >> c;
		student temp(a, b, c);
		stu[n++] = temp;
	}
	cout << "Okay, data upload finished. What do you what to do next? You can enter a number to tell me." << endl;
	pri1();
	int x;//x代表菜单编号
	cin >> x;
	while (x != 0) {
		if (x == 1) {
			cout << "请输入要添加学生的姓名,学号和C语言成绩:";
			string name1, id;
			int score;
			cin >> name1 >> id >> score;
			student temp(name1, id, score);
			stu[n++] = temp;
			pri2();
		}
		else if (x == 2) {
			cout << "请输入要删学生的学号:";
			string id;
			cin >> id;
			int t = 0;//t用来记录要删的学生是在原序列的第几个
			t = find(stu, n, id);
			n -= 1;
			for (int i = t; i < n; i++) {
				stu[i] = stu[i + 1];
			}
			pri2();
		}
		else if (x == 3) {
			cout << "请输入要查找学生的学号:";
			string id;
			cin >> id;
			int t = 0;//t用来记录要查找的学生是在原序列的第几个
			t = find(stu, n, id);
			stu[t].print();
			pri2();
		}
		else if (x == 4) {
			cout << "按照学号排序如下所示:" << endl;
			student st[100];//定义一个类来复制原有的类用来排序输出
			for (int i = 0; i < n; i++) {
				st[i] = stu[i];
			}
			for (int i = 0; i < n; i++) {//冒泡排序法
				for (int j = 0; j < n - 1; j++) {
					if (st[j].getid() > st[j + 1].getid()) {
						swap(st[j], st[j + 1]);
					}
				}
			}
			for (int i = 0; i < n; i++) {
				st[i].print();
			}
			pri2();
		}
		else if (x == 5) {
			cout << "按照分数由高到低排序如下所示:" << endl;
			student st[100];//定义一个类来复制原有的类用来排序输出
			for (int i = 0; i < n; i++) {
				st[i] = stu[i];
			}
			for (int i = 0; i < n; i++) {//冒泡排序法
				for (int j = 0; j < n - 1; j++) {
					if (st[j].getgrape() < st[j + 1].getgrape()) {
						swap(st[j], st[j + 1]);
					}
				}
			}
			for (int i = 0; i < n; i++) {
				st[i].print();
			}
			pri2();
		}
		else if (x == 6) {
			cout << "最大分数学生信息如下:" << endl;
			int max = getmax(stu, n);
			stu[max].print();
			pri2();
		}
		else if (x == 7) {
			cout << "最小分数学生信息如下:" << endl;
			int min = getmin(stu, n);
			stu[min].print();
			pri2();
		}
		else if (x == 8) {
			cout << "请输入要判断成绩是否为素数的学生学号:";
			string id;
			cin >> id;
			int t = find(stu, n, id);
			int sum = stu[t].getgrape();
			if (sum == 1 || sum % 2 == 0) {
				cout << "学生成绩不为素数" << endl;
			}
			else {
				cout << "学生成绩为素数" << endl;
			}
			pri2();
		}
		else if (x == 9) {
			cout << "请输入两个学生的学号:";
			string id1, id2;
			cin >> id1 >> id2;
			int t1 = find(stu, n, id1);
			int t2 = find(stu, n, id2);
			int score1 = stu[t1].getgrape();
			int score2 = stu[t2].getgrape();
			int min = score1;
			if (min > score2) {
				min = score2;
			}
			int i;
			for (i = 2; i < min; i++) {
				if (score1 % i == 0 && score2 % i == 0) {
					cout << "学号为:" << id1 << "和学号为:" << id2 << "的两位学生的成绩互质" << endl;
					break;
				}
			}
			if (i == min) {
				cout << "学号为:" << id1 << "和学号为:" << id2 << "的两位学生的成绩不互质" << endl;
			}
			pri2();
		}
		else if (x == 10) {
			int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0;
			//num1-8分别代表绩点有A+到F
			for (int i = 0; i < n; i++) {
				if (stu[i].getgpa() == "A+") {
					num1++;
				}
				else if (stu[i].getgpa() == "A") {
					num2++;
				}
				else if (stu[i].getgpa() == "B+") {
					num3++;
				}
				else if (stu[i].getgpa() == "B") {
					num4++;
				}
				else if (stu[i].getgpa() == "C+") {
					num5++;
				}
				else if (stu[i].getgpa() == "C") {
					num6++;
				}
				else if (stu[i].getgpa() == "D") {
					num7++;
				}
				else if (stu[i].getgpa() == "F") {
					num8++;
				}
			}
			cout << "根据 GPA 对学生成绩进行考情分析:A+有" << num1 << "人,";
			cout << "A有" << num2 << "人,B+有" << num3 << "人,B有" << num4 << "人,C+有" << num5 << "人,C有";
			cout << num6 << "人,D有" << num7 << "人,F有" << num8 << "人" << endl;
			pri2();
		}
		cin >> x;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值