使用C++语言创建一个学生管理系统,并可以在vc6环境下运行要求如下: 一.使用构造函数,类函数 二.拥有一个主界面,主界面要有1.初始化程序 2.菜单程序 3.退出程序 三.输入主界面2.菜单程序后进入子界面 四.子界面要求实现新增、显示、查找、删除、修改、统计、按照学号排序、退出返回主界面等功能,并将信息保存至txt文件中。
时间: 2024-03-06 10:48:56 浏览: 78
好的,这是一个比较大的项目,需要仔细设计和编写。以下是一些基本的思路和代码示例,希望对你有所帮助:
1. 设计学生类
```c++
class Student {
public:
Student();
Student(int id, string name, int age, string gender, string major, int score);
~Student();
void setId(int id);
int getId();
void setName(string name);
string getName();
void setAge(int age);
int getAge();
void setGender(string gender);
string getGender();
void setMajor(string major);
string getMajor();
void setScore(int score);
int getScore();
void printInfo();
string toString();
private:
int id;
string name;
int age;
string gender;
string major;
int score;
};
```
2. 设计学生管理系统类
```c++
class StudentManagementSystem {
public:
StudentManagementSystem();
~StudentManagementSystem();
void init();
void showMenu();
void submenuAdd();
void submenuShow();
void submenuSearch();
void submenuDelete();
void submenuModify();
void submenuStatistics();
void submenuSort();
void exit();
private:
vector<Student> students;
};
```
3. 实现学生类的构造函数、析构函数和类函数
```c++
Student::Student() {
id = 0;
name = "";
age = 0;
gender = "";
major = "";
score = 0;
}
Student::Student(int id, string name, int age, string gender, string major, int score) {
this->id = id;
this->name = name;
this->age = age;
this->gender = gender;
this->major = major;
this->score = score;
}
Student::~Student() {}
void Student::setId(int id) {
this->id = id;
}
int Student::getId() {
return id;
}
void Student::setName(string name) {
this->name = name;
}
string Student::getName() {
return name;
}
void Student::setAge(int age) {
this->age = age;
}
int Student::getAge() {
return age;
}
void Student::setGender(string gender) {
this->gender = gender;
}
string Student::getGender() {
return gender;
}
void Student::setMajor(string major) {
this->major = major;
}
string Student::getMajor() {
return major;
}
void Student::setScore(int score) {
this->score = score;
}
int Student::getScore() {
return score;
}
void Student::printInfo() {
cout << toString() << endl;
}
string Student::toString() {
stringstream ss;
ss << "ID: " << id << ", Name: " << name << ", Age: " << age << ", Gender: " << gender << ", Major: " << major << ", Score: " << score;
return ss.str();
}
```
4. 实现学生管理系统类的构造函数、析构函数和类函数
```c++
StudentManagementSystem::StudentManagementSystem() {}
StudentManagementSystem::~StudentManagementSystem() {}
void StudentManagementSystem::init() {
// 读取学生信息文件
ifstream fin("students.txt");
if (fin.is_open()) {
int id, age, score;
string name, gender, major;
while (fin >> id >> name >> age >> gender >> major >> score) {
Student student(id, name, age, gender, major, score);
students.push_back(student);
}
fin.close();
}
}
void StudentManagementSystem::showMenu() {
cout << "=================== Menu ===================" << endl;
cout << " 1. Add a student" << endl;
cout << " 2. Show all students" << endl;
cout << " 3. Search a student" << endl;
cout << " 4. Delete a student" << endl;
cout << " 5. Modify a student" << endl;
cout << " 6. Statistics" << endl;
cout << " 7. Sort by ID" << endl;
cout << " 8. Exit" << endl;
cout << "=============================================" << endl;
}
void StudentManagementSystem::submenuAdd() {
int id, age, score;
string name, gender, major;
cout << "Please input student ID: ";
cin >> id;
cout << "Please input student name: ";
cin >> name;
cout << "Please input student age: ";
cin >> age;
cout << "Please input student gender: ";
cin >> gender;
cout << "Please input student major: ";
cin >> major;
cout << "Please input student score: ";
cin >> score;
Student student(id, name, age, gender, major, score);
students.push_back(student);
// 保存学生信息到文件
ofstream fout("students.txt", ios::app);
if (fout.is_open()) {
fout << student.getId() << " " << student.getName() << " " << student.getAge() << " " << student.getGender() << " " << student.getMajor() << " " << student.getScore() << endl;
fout.close();
}
cout << "Add student successfully!" << endl;
}
void StudentManagementSystem::submenuShow() {
cout << "All students:" << endl;
for (int i = 0; i < students.size(); i++) {
students[i].printInfo();
}
}
void StudentManagementSystem::submenuSearch() {
int id;
cout << "Please input student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
students[i].printInfo();
return;
}
}
cout << "Student not found!" << endl;
}
void StudentManagementSystem::submenuDelete() {
int id;
cout << "Please input student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
students.erase(students.begin() + i);
// 重新写入学生信息文件
ofstream fout("students.txt");
if (fout.is_open()) {
for (int j = 0; j < students.size(); j++) {
fout << students[j].getId() << " " << students[j].getName() << " " << students[j].getAge() << " " << students[j].getGender() << " " << students[j].getMajor() << " " << students[j].getScore() << endl;
}
fout.close();
}
cout << "Delete student successfully!" << endl;
return;
}
}
cout << "Student not found!" << endl;
}
void StudentManagementSystem::submenuModify() {
int id, age, score;
string name, gender, major;
cout << "Please input student ID: ";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
cout << "Please input new student name (old: " << students[i].getName() << "): ";
cin >> name;
cout << "Please input new student age (old: " << students[i].getAge() << "): ";
cin >> age;
cout << "Please input new student gender (old: " << students[i].getGender() << "): ";
cin >> gender;
cout << "Please input new student major (old: " << students[i].getMajor() << "): ";
cin >> major;
cout << "Please input new student score (old: " << students[i].getScore() << "): ";
cin >> score;
students[i].setName(name);
students[i].setAge(age);
students[i].setGender(gender);
students[i].setMajor(major);
students[i].setScore(score);
// 重新写入学生信息文件
ofstream fout("students.txt");
if (fout.is_open()) {
for (int j = 0; j < students.size(); j++) {
fout << students[j].getId() << " " << students[j].getName() << " " << students[j].getAge() << " " << students[j].getGender() << " " << students[j].getMajor() << " " << students[j].getScore() << endl;
}
fout.close();
}
cout << "Modify student successfully!" << endl;
return;
}
}
cout << "Student not found!" << endl;
}
void StudentManagementSystem::submenuStatistics() {
int sum = 0;
double avg = 0;
int maxScore = students[0].getScore();
int minScore = students[0].getScore();
for (int i = 0; i < students.size(); i++) {
sum += students[i].getScore();
if (students[i].getScore() > maxScore) {
maxScore = students[i].getScore();
}
if (students[i].getScore() < minScore) {
minScore = students[i].getScore();
}
}
avg = sum / (double)students.size();
cout << "Statistics:" << endl;
cout << " Number of students: " << students.size() << endl;
cout << " Total score: " << sum << endl;
cout << " Average score: " << avg << endl;
cout << " Max score: " << maxScore << endl;
cout << " Min score: " << minScore << endl;
}
void StudentManagementSystem::submenuSort() {
sort(students.begin(), students.end(), [](Student a, Student b) {return a.getId() < b.getId(); });
cout << "Sort by ID:" << endl;
for (int i = 0; i < students.size(); i++) {
students[i].printInfo();
}
}
void StudentManagementSystem::exit() {
cout << "Goodbye!" << endl;
// 清空学生信息文件
ofstream fout("students.txt");
fout.close();
}
```
5. 主程序流程
```c++
int main() {
StudentManagementSystem sms;
sms.init();
while (true) {
sms.showMenu();
int choice;
cout << "Please input your choice: ";
cin >> choice;
switch (choice) {
case 1:
sms.submenuAdd();
break;
case 2:
sms.submenuShow();
break;
case 3:
sms.submenuSearch();
break;
case 4:
sms.submenuDelete();
break;
case 5:
sms.submenuModify();
break;
case 6:
sms.submenuStatistics();
break;
case 7:
sms.submenuSort();
break;
case 8:
sms.exit();
return 0;
default:
cout << "Invalid choice, please try again!" << endl;
break;
}
}
return 0;
}
```
以上是一个简单的学生管理系统的设计和实现,你可以根据实际需求进行修改和完善。
阅读全文