⑶选择“添加学生成绩记录”后,输入要插入记录的位置。按程序提示依次输入学号,姓名,性别,成绩,插入记录成功。若追加记录,可连续添加。 ⑷选择“显示学生成绩记录”,显示学生成绩表中已有的学生记录的个数,并显示出各个学生记录的相关信息。 ⑸选择“查找学生成绩记录”,按提示输入要查找的学生的学号,输入学号,若该学号存在,则输出相应的学生记录,如该学号不存在,则按任意键返回主菜单。 ⑹选择“删除学生成绩记录”,按提示输入要删除的学生的学号,输入学号,若该学号存在,则删除相应的学生记录,如果该学号不存在,则按任意键返回主菜单。 ⑺选择“保存学生成绩记录”,系统会将已有的学生记录保存在默认的文件名为fname.txt的文件中。 ⑻选择“导出学生成绩记录”,系统会提示输入文件名,输入默认的文件名为fname.txt,则可将文件中的相关信息导出。c++代码
时间: 2024-03-07 17:50:32 浏览: 150
程序设计基础学生成绩记录薄设计
5星 · 资源好评率100%
下面是一个简单的学生成绩管理系统的C++代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
string id; // 学号
string name; // 姓名
string gender; // 性别
float score; // 成绩
};
void addStudent(Student students[], int& count) {
int pos;
cout << "请输入要插入的位置:";
cin >> pos;
if (pos < 1 || pos > count + 1) {
cout << "位置不合法!" << endl;
return;
}
Student stu;
cout << "请输入学号:";
cin >> stu.id;
cout << "请输入姓名:";
cin >> stu.name;
cout << "请输入性别:";
cin >> stu.gender;
cout << "请输入成绩:";
cin >> stu.score;
if (pos == count + 1) {
students[count] = stu;
} else {
for (int i = count; i >= pos; i--) {
students[i] = students[i - 1];
}
students[pos - 1] = stu;
}
count++;
cout << "插入记录成功!" << endl;
}
void showStudents(Student students[], int count) {
cout << "学生记录个数:" << count << endl;
for (int i = 0; i < count; i++) {
cout << "学号:" << students[i].id << "\t姓名:" << students[i].name << "\t性别:" << students[i].gender << "\t成绩:" << students[i].score << endl;
}
}
void searchStudent(Student students[], int count) {
string id;
cout << "请输入要查找的学号:";
cin >> id;
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
cout << "学号:" << students[i].id << "\t姓名:" << students[i].name << "\t性别:" << students[i].gender << "\t成绩:" << students[i].score << endl;
return;
}
}
cout << "该学号不存在!" << endl;
}
void deleteStudent(Student students[], int& count) {
string id;
cout << "请输入要删除的学号:";
cin >> id;
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
for (int j = i + 1; j < count; j++) {
students[j - 1] = students[j];
}
count--;
cout << "删除成功!" << endl;
return;
}
}
cout << "该学号不存在!" << endl;
}
void saveStudents(Student students[], int count) {
ofstream outfile("fname.txt");
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
outfile << "学号\t姓名\t性别\t成绩" << endl;
for (int i = 0; i < count; i++) {
outfile << students[i].id << "\t" << students[i].name << "\t" << students[i].gender << "\t" << students[i].score << endl;
}
outfile.close();
cout << "保存成功!" << endl;
}
void exportStudents(Student students[], int count) {
string filename;
cout << "请输入导出文件名:";
cin >> filename;
ofstream outfile(filename);
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
outfile << "学号\t姓名\t性别\t成绩" << endl;
for (int i = 0; i < count; i++) {
outfile << students[i].id << "\t" << students[i].name << "\t" << students[i].gender << "\t" << students[i].score << endl;
}
outfile.close();
cout << "导出成功!" << endl;
}
int main() {
Student students[100];
int count = 0;
int choice;
while (true) {
cout << "1. 添加学生成绩记录" << endl;
cout << "2. 显示学生成绩记录" << endl;
cout << "3. 查找学生成绩记录" << endl;
cout << "4. 删除学生成绩记录" << endl;
cout << "5. 保存学生成绩记录" << endl;
cout << "6. 导出学生成绩记录" << endl;
cout << "0. 退出程序" << endl;
cout << "请选择:";
cin >> choice;
switch (choice) {
case 1:
addStudent(students, count);
break;
case 2:
showStudents(students, count);
break;
case 3:
searchStudent(students, count);
break;
case 4:
deleteStudent(students, count);
break;
case 5:
saveStudents(students, count);
break;
case 6:
exportStudents(students, count);
break;
case 0:
return 0;
default:
cout << "选择错误!" << endl;
break;
}
}
return 0;
}
```
该程序使用结构体存储学生信息,可以添加、显示、查找、删除、保存和导出学生成绩记录。其中保存和导出功能将学生记录写入文件中。
阅读全文