设计一个学生学籍管理系统: 学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。 数据录入支持键盘输入和文件导入;同时支持导入+输入,如自动列出“姓名、学号、性别”,而成绩部分由键盘输入。录入结果存入数据文件student.dat。 支持按学生姓名和学号查询,支持指定班级的两位序号范围查询,查询结果回显到屏幕上。 l对所有学生,按照班级计算各科平均成绩。 l支持按单科成绩排序和总分排序,排序结果写入文件并回显,文件名自拟。
时间: 2024-02-12 15:07:50 浏览: 203
学生信息管理系统,实现学生信息包括:学号、姓名、性别、年龄、班级等信息。
5星 · 资源好评率100%
好的,这是一个比较完整的学生学籍管理系统,需要较多的代码实现。我可以给你提供一个大致的框架,但具体的实现过程需要你自己去完成。
1. 定义学生信息的结构体
```c++
struct Student {
string name; // 姓名
int id; // 学号
string gender; // 性别
float english; // 英语成绩
float math; // 数学成绩
float program; // 程序设计成绩
float sports; // 体育成绩
};
```
2. 实现数据录入功能
```c++
void inputStudent(Student &s) {
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生学号:";
cin >> s.id;
cout << "请输入学生性别:";
cin >> s.gender;
cout << "请输入学生英语成绩:";
cin >> s.english;
cout << "请输入学生数学成绩:";
cin >> s.math;
cout << "请输入学生程序设计成绩:";
cin >> s.program;
cout << "请输入学生体育成绩:";
cin >> s.sports;
}
void inputFromFile(vector<Student> &students, string filename) {
ifstream infile(filename);
if (!infile) {
cout << "文件不存在!" << endl;
return;
}
while (!infile.eof()) {
Student s;
infile >> s.name >> s.id >> s.gender >> s.english >> s.math >> s.program >> s.sports;
students.push_back(s);
}
infile.close();
}
void input(vector<Student> &students) {
char choice;
cout << "请选择录入方式(1-键盘输入,2-文件导入,3-导入+输入):";
cin >> choice;
if (choice == '1') {
Student s;
inputStudent(s);
students.push_back(s);
} else if (choice == '2') {
string filename;
cout << "请输入文件名:";
cin >> filename;
inputFromFile(students, filename);
} else if (choice == '3') {
string filename;
cout << "请输入文件名:";
cin >> filename;
inputFromFile(students, filename);
Student s;
inputStudent(s);
students.push_back(s);
} else {
cout << "无效的选项!" << endl;
}
}
```
3. 实现按学生姓名和学号查询功能
```c++
void searchByName(vector<Student> &students, string name) {
for (auto s : students) {
if (s.name == name) {
cout << "姓名:" << s.name << " 学号:" << s.id << " 性别:" << s.gender
<< " 英语成绩:" << s.english << " 数学成绩:" << s.math
<< " 程序设计成绩:" << s.program << " 体育成绩:" << s.sports << endl;
}
}
}
void searchById(vector<Student> &students, int id) {
for (auto s : students) {
if (s.id == id) {
cout << "姓名:" << s.name << " 学号:" << s.id << " 性别:" << s.gender
<< " 英语成绩:" << s.english << " 数学成绩:" << s.math
<< " 程序设计成绩:" << s.program << " 体育成绩:" << s.sports << endl;
}
}
}
```
4. 实现按班级计算各科平均成绩功能
```c++
void calculateAverage(vector<Student> &students) {
map<string, float> englishSum;
map<string, float> mathSum;
map<string, float> programSum;
map<string, float> sportsSum;
map<string, int> count;
for (auto s : students) {
englishSum[s.gender] += s.english;
mathSum[s.gender] += s.math;
programSum[s.gender] += s.program;
sportsSum[s.gender] += s.sports;
count[s.gender]++;
}
cout << "班级\t英语平均成绩\t数学平均成绩\t程序设计平均成绩\t体育平均成绩" << endl;
for (auto it = count.begin(); it != count.end(); it++) {
string gender = it->first;
float englishAvg = englishSum[gender] / count[gender];
float mathAvg = mathSum[gender] / count[gender];
float programAvg = programSum[gender] / count[gender];
float sportsAvg = sportsSum[gender] / count[gender];
cout << gender << "\t" << englishAvg << "\t" << mathAvg << "\t" << programAvg << "\t" << sportsAvg << endl;
}
}
```
5. 实现按单科成绩排序和总分排序功能
```c++
bool compareEnglish(Student s1, Student s2) {
return s1.english > s2.english;
}
bool compareMath(Student s1, Student s2) {
return s1.math > s2.math;
}
bool compareProgram(Student s1, Student s2) {
return s1.program > s2.program;
}
bool compareSports(Student s1, Student s2) {
return s1.sports > s2.sports;
}
bool compareTotal(Student s1, Student s2) {
return (s1.english + s1.math + s1.program + s1.sports) > (s2.english + s2.math + s2.program + s2.sports);
}
void sortByEnglish(vector<Student> &students) {
sort(students.begin(), students.end(), compareEnglish);
}
void sortByMath(vector<Student> &students) {
sort(students.begin(), students.end(), compareMath);
}
void sortByProgram(vector<Student> &students) {
sort(students.begin(), students.end(), compareProgram);
}
void sortBySports(vector<Student> &students) {
sort(students.begin(), students.end(), compareSports);
}
void sortByTotal(vector<Student> &students) {
sort(students.begin(), students.end(), compareTotal);
}
void outputToFile(vector<Student> &students, string filename) {
ofstream outfile(filename);
if (!outfile) {
cout << "文件无法打开!" << endl;
return;
}
for (auto s : students) {
outfile << s.name << "\t" << s.id << "\t" << s.gender << "\t" << s.english << "\t"
<< s.math << "\t" << s.program << "\t" << s.sports << endl;
}
outfile.close();
}
void sortAndOutput(vector<Student> &students) {
char choice;
cout << "请选择排序方式(1-按英语成绩排序,2-按数学成绩排序,3-按程序设计成绩排序,4-按体育成绩排序,5-按总分排序):";
cin >> choice;
if (choice == '1') {
sortByEnglish(students);
outputToFile(students, "EnglishSort.dat");
} else if (choice == '2') {
sortByMath(students);
outputToFile(students, "MathSort.dat");
} else if (choice == '3') {
sortByProgram(students);
outputToFile(students, "ProgramSort.dat");
} else if (choice == '4') {
sortBySports(students);
outputToFile(students, "SportsSort.dat");
} else if (choice == '5') {
sortByTotal(students);
outputToFile(students, "TotalSort.dat");
} else {
cout << "无效的选项!" << endl;
}
}
```
6. 主函数框架
```c++
int main() {
vector<Student> students;
while (true) {
// 显示菜单
// 根据用户选择执行相应的功能
}
return 0;
}
```
这只是一个大致的框架,具体的实现还需要你自己去完成。希望能对你有所帮助!
阅读全文