c++设计一个学生成绩管理系统,包含以下功能“1.添加学生成绩记录 2.显示学生成绩记录 3.查找学生成绩记录 4.删除学生成绩记录 5.保存学生成绩记录 6.导出学生成绩记录 0.退出学生成绩管理系统”源代码
时间: 2024-03-06 15:48:25 浏览: 72
C/C++学生成绩管理系统
5星 · 资源好评率100%
好的,以下是一个简单的实现示例:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
// 定义学生结构体
struct Student {
string name;
string id;
float mathScore;
float englishScore;
float computerScore;
};
// 动态数组存储所有学生记录
vector<Student> studentList;
// 显示菜单
void showMenu() {
cout << "**********学生成绩管理系统**********" << endl;
cout << "1.添加学生成绩记录" << endl;
cout << "2.显示学生成绩记录" << endl;
cout << "3.查找学生成绩记录" << endl;
cout << "4.删除学生成绩记录" << endl;
cout << "5.保存学生成绩记录" << endl;
cout << "6.导出学生成绩记录" << endl;
cout << "0.退出学生成绩管理系统" << endl;
cout << "************************************" << endl;
}
// 添加学生记录
void addStudent() {
Student student;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生学号:";
cin >> student.id;
cout << "请输入数学成绩:";
cin >> student.mathScore;
cout << "请输入英语成绩:";
cin >> student.englishScore;
cout << "请输入计算机成绩:";
cin >> student.computerScore;
studentList.push_back(student);
cout << "添加成功!" << endl;
}
// 显示学生记录
void showStudent() {
if (studentList.empty()) {
cout << "暂无学生记录!" << endl;
return;
}
for (int i = 0; i < studentList.size(); i++) {
cout << "姓名:" << studentList[i].name << " 学号:" << studentList[i].id
<< " 数学成绩:" << studentList[i].mathScore << " 英语成绩:"
<< studentList[i].englishScore << " 计算机成绩:" << studentList[i].computerScore << endl;
}
}
// 查找学生记录
void searchStudent() {
string keyword;
cout << "请输入要查找的学生姓名或学号:";
cin >> keyword;
for (int i = 0; i < studentList.size(); i++) {
if (studentList[i].name == keyword || studentList[i].id == keyword) {
cout << "姓名:" << studentList[i].name << " 学号:" << studentList[i].id
<< " 数学成绩:" << studentList[i].mathScore << " 英语成绩:"
<< studentList[i].englishScore << " 计算机成绩:" << studentList[i].computerScore << endl;
return;
}
}
cout << "未找到相关学生记录!" << endl;
}
// 删除学生记录
void deleteStudent() {
string keyword;
cout << "请输入要删除的学生姓名或学号:";
cin >> keyword;
for (int i = 0; i < studentList.size(); i++) {
if (studentList[i].name == keyword || studentList[i].id == keyword) {
studentList.erase(studentList.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到相关学生记录!" << endl;
}
// 保存学生记录到文件
void saveToFile() {
ofstream outFile("students.txt");
if (outFile.fail()) {
cout << "保存失败!" << endl;
return;
}
for (int i = 0; i < studentList.size(); i++) {
outFile << studentList[i].name << " " << studentList[i].id << " "
<< studentList[i].mathScore << " " << studentList[i].englishScore
<< " " << studentList[i].computerScore << endl;
}
outFile.close();
cout << "保存成功!" << endl;
}
// 导出学生记录到Excel表格
void exportToExcel() {
ofstream outFile("students.csv");
if (outFile.fail()) {
cout << "导出失败!" << endl;
return;
}
outFile << "姓名,学号,数学成绩,英语成绩,计算机成绩" << endl;
for (int i = 0; i < studentList.size(); i++) {
outFile << studentList[i].name << "," << studentList[i].id << ","
<< studentList[i].mathScore << "," << studentList[i].englishScore
<< "," << studentList[i].computerScore << endl;
}
outFile.close();
cout << "导出成功!" << endl;
}
// 从文件中加载学生记录
void loadFromFile() {
ifstream inFile("students.txt");
if (inFile.fail()) {
cout << "加载失败!" << endl;
return;
}
string line;
while (getline(inFile, line)) {
stringstream ss(line);
Student student;
ss >> student.name >> student.id >> student.mathScore >> student.englishScore >> student.computerScore;
studentList.push_back(student);
}
inFile.close();
cout << "加载成功!" << endl;
}
int main() {
int choice;
loadFromFile();
while (true) {
showMenu();
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
showStudent();
break;
case 3:
searchStudent();
break;
case 4:
deleteStudent();
break;
case 5:
saveToFile();
break;
case 6:
exportToExcel();
break;
case 0:
cout << "感谢使用!" << endl;
return 0;
default:
cout << "无效操作,请重新输入!" << endl;
}
}
}
```
这是一个比较简单的实现,您可以根据自己的需求进行修改和扩展。
阅读全文