C++学生成绩管理系统
时间: 2023-11-16 19:06:47 浏览: 75
c++学生成绩管理系统文档.doc
以下是一个简单的C++学生成绩管理系统的代码,它使用了链表来存储学生信息和成绩,并提供了增删改查和文件保存功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义学生类
class Student {
public:
string name;
int id;
float score;
Student* next;
};
// 定义教师类
class Teacher {
public:
Student* head;
Teacher() {
head = new Student();
head->next = NULL;
}
// 添加学生信息
void addStudent() {
Student* p = head;
while (p->next != NULL) {
p = p->next;
}
Student* newStudent = new Student();
cout << "请输入学生姓名:";
cin >> newStudent->name;
cout << "请输入学生学号:";
cin >> newStudent->id;
cout << "请输入学生成绩:";
cin >> newStudent->score;
newStudent->next = NULL;
p->next = newStudent;
cout << "添加成功!" << endl;
}
// 删除学生信息
void deleteStudent() {
int id;
cout << "请输入要删除的学生学号:";
cin >> id;
Student* p = head->next;
Student* pre = head;
while (p != NULL) {
if (p->id == id) {
pre->next = p->next;
delete p;
cout << "删除成功! << endl;
return;
}
pre = p;
p = p->next;
}
cout << "未找到该学生!" << endl;
}
// 修改学生信息
void modifyStudent() {
int id;
cout << "请输入要修改的学生学号:";
cin >> id;
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
cout << "请输入学生姓名:";
cin >> p->name;
cout << "请输入学生成绩:";
cin >> p->score;
cout << "修改成功! << endl;
return;
}
p = p->next;
}
cout << "未找到该学生! << endl;
}
// 查询学生信息
void queryStudent() {
int id;
cout << "请输入要查询的学生学号:";
cin >> id;
Student* p = head->next;
while (p != NULL) {
if (p->id == id) {
cout << "学生姓名:" << p->name << endl;
cout << "学生学号:" << p->id << endl;
cout << "学生成绩:" << p->score << endl;
return;
}
p = p->next;
}
cout << "未找到该学生!" << endl;
}
// 保存学生信息到文件
void saveToFile() {
ofstream outfile("students.txt");
Student* p = head->next;
while (p != NULL) {
outfile << p->name << " " << p->id << " " << p->score << endl;
p = p->next;
}
outfile.close();
cout << "保存成功!" << endl;
}
};
int main() {
Teacher teacher;
int choice;
while (true) {
cout << "请选择操作:" << endl;
cout << "1.添加学生信息" << endl;
cout << "2.删除学生信息" << endl;
cout << "3.修改学生信息" << endl;
cout << "4.查询学生信息" << endl;
cout << "5.保存学生信息到文件" << endl;
cout << "6.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
teacher.addStudent();
break;
case 2:
teacher.deleteStudent();
break;
case 3:
teacher.modifyStudent();
break;
case 4:
teacher.queryStudent();
break;
case 5:
teacher.saveToFile();
break;
case 6:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
阅读全文