使用C++来完成这个编程
时间: 2023-09-17 08:08:48 浏览: 97
这个是一个C++的程序
好的,以下是一个简单的C++程序,实现了学生成绩管理系统的基本功能。你可以在此基础上进行修改和扩展。
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
struct Student {
char id[12]; // 学号
char name[21]; // 姓名
char gender; // 性别
double score; // 成绩
};
const int MAX_SIZE = 100; // 学生记录的最大数量
Student students[MAX_SIZE]; // 学生记录数组
int size = 0; // 学生记录数量
void addStudent() { // 添加学生记录
if (size == MAX_SIZE) {
cout << "学生记录已满,无法添加!" << endl;
return;
}
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> students[size].id;
cout << "姓名:";
cin >> students[size].name;
cout << "性别(m/f):";
cin >> students[size].gender;
cout << "成绩:";
cin >> students[size].score;
size++;
cout << "添加成功!" << endl;
}
void displayStudents() { // 显示学生记录
if (size == 0) {
cout << "学生记录为空!" << endl;
return;
}
cout << left << setw(12) << "学号" << setw(21) << "姓名" << setw(6) << "性别" << setw(8) << "成绩" << endl;
for (int i = 0; i < size; i++) {
cout << left << setw(12) << students[i].id << setw(21) << students[i].name << setw(6) << students[i].gender << setw(8) << students[i].score << endl;
}
}
void searchStudent() { // 查找学生记录
if (size == 0) {
cout << "学生记录为空!" << endl;
return;
}
char id[12];
cout << "请输入要查找的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < size; i++) {
if (strcmp(students[i].id, id) == 0) {
cout << left << setw(12) << "学号" << setw(21) << "姓名" << setw(6) << "性别" << setw(8) << "成绩" << endl;
cout << left << setw(12) << students[i].id << setw(21) << students[i].name << setw(6) << students[i].gender << setw(8) << students[i].score << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生记录!" << endl;
}
}
void deleteStudent() { // 删除学生记录
if (size == 0) {
cout << "学生记录为空!" << endl;
return;
}
char id[12];
cout << "请输入要删除的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < size; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < size - 1; j++) {
students[j] = students[j + 1];
}
size--;
found = true;
cout << "删除成功!" << endl;
break;
}
}
if (!found) {
cout << "未找到该学生记录!" << endl;
}
}
void saveStudents() { // 保存学生记录到文件
ofstream fout("students.txt");
if (!fout) {
cout << "无法打开文件!" << endl;
return;
}
for (int i = 0; i < size; i++) {
fout << students[i].id << " " << students[i].name << " " << students[i].gender << " " << students[i].score << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
void loadStudents() { // 从文件中加载学生记录
ifstream fin("students.txt");
if (!fin) {
cout << "无法打开文件!" << endl;
return;
}
size = 0;
while (fin >> students[size].id >> students[size].name >> students[size].gender >> students[size].score) {
size++;
}
fin.close();
cout << "加载成功!" << endl;
}
void exportStudents() { // 导出学生记录到文件
ofstream fout("students.csv");
if (!fout) {
cout << "无法打开文件!" << endl;
return;
}
fout << "学号,姓名,性别,成绩" << endl;
for (int i = 0; i < size; i++) {
fout << students[i].id << "," << students[i].name << "," << students[i].gender << "," << students[i].score << endl;
}
fout.close();
cout << "导出成功!" << endl;
}
int main() {
int choice;
while (true) {
cout << "学生成绩管理系统" << endl;
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();
break;
case 2:
displayStudents();
break;
case 3:
searchStudent();
break;
case 4:
deleteStudent();
break;
case 5:
saveStudents();
break;
case 6:
exportStudents();
break;
case 0:
cout << "程序已退出!" << endl;
return 0;
default:
cout << "请输入正确的操作!" << endl;
break;
}
}
return 0;
}
```
该程序使用了结构体存储学生记录,实现了添加、显示、查找、删除、保存和导出学生记录的功能,并提供了一个主菜单。你可以根据需要对其进行修改和扩展。
阅读全文