You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.3 KiB

#include <iostream>
#include <fstream>
#include <iomanip>
#include <limits>
using namespace std;
const int MAX_STUDENTS = 100; // 最大学生数
struct StudentScore {
string name;
int score;
};
int main() {
StudentScore students[MAX_STUDENTS];
int numStudents = 0; // 实际学生数
// 读取文件数据
ifstream inFile("student_scores.txt");
if (inFile.is_open()) {
while (inFile >> students[numStudents].name >> students[numStudents].score) {
numStudents++;
}
inFile.close();
cout << "已从文件中读取 " << numStudents << " 个学生成绩数据." << endl;
} else {
cout << "无法打开文件,将从头开始." << endl;
}
while (true) {
cout << "=== 学生成绩信息管理系统 ===" << endl;
cout << "1. 录入学生成绩" << endl;
cout << "2. 修改/删除学生成绩" << endl;
cout << "3. 查看统计信息" << endl;
cout << "4. 退出系统" << endl;
cout << "请输入您的选择(1-4): ";
int choice;
cin >> choice;
switch (choice) {
case 1: { // 录入学生成绩
if (numStudents == MAX_STUDENTS) {
cout << "已达到最大学生数,无法继续录入." << endl;
break;
}
cout << "请输入学生姓名: ";
cin >> students[numStudents].name;
cout << "请输入学生成绩(0-100): ";
while (!(cin >> students[numStudents].score) || students[numStudents].score < 0 || students[numStudents].score > 100) {
cout << "无效的成绩,请重新输入(0-100): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
numStudents++;
cout << "学生成绩录入成功!" << endl;
break;
}
case 2: { // 修改/删除学生成绩
if (numStudents == 0) {
cout << "当前没有任何学生成绩数据." << endl;
break;
}
cout << "请输入要修改/删除的学生编号(0-" << numStudents - 1 << "): ";
int index;
cin >> index;
if (index < 0 || index >= numStudents) {
cout << "无效的编号!" << endl;
break;
}
cout << "学生姓名: " << students[index].name << ", 当前成绩: " << students[index].score << endl;
cout << "1. 修改成绩" << endl;
cout << "2. 删除学生" << endl;
cout << "请输入您的选择(1-2): ";
int subChoice;
cin >> subChoice;
if (subChoice == 1) {
cout << "请输入新的成绩(0-100): ";
while (!(cin >> students[index].score) || students[index].score < 0 || students[index].score > 100) {
cout << "无效的成绩,请重新输入(0-100): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "成绩修改成功!" << endl;
} else if (subChoice == 2) {
for (int i = index; i < numStudents - 1; i++) {
students[i] = students[i + 1];
}
numStudents--;
cout << "学生信息删除成功!" << endl;
} else {
cout << "无效的选择!" << endl;
}
break;
}
case 3: { // 查看统计信息
if (numStudents == 0) {
cout << "当前没有任何学生成绩数据." << endl;
break;
}
int maxScore = 0, minScore = 100;
int totalScore = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].score > maxScore) {
maxScore = students[i].score;
}
if (students[i].score < minScore) {
minScore = students[i].score;
}
totalScore += students[i].score;
}
double avgScore = static_cast<double>(totalScore) / numStudents;
cout << "学生成绩统计信息:" << endl;
cout << "最高分: " << maxScore << endl;
cout << "最低分: " << minScore << endl;
cout << "平均分: " << fixed << setprecision(2) << avgScore << endl;
break;
}
case 4: { // 退出系统
// 保存数据到文件
ofstream outFile("student_scores.txt");
if (outFile.is_open()) {
for (int i = 0; i < numStudents; i++) {
outFile << students[i].name << " " << students[i].score << endl;
}
outFile.close();
cout << "学生成绩数据已保存到文件." << endl;
} else {
cout << "无法打开文件,学生成绩数据未保存." << endl;
}
cout << "程序已退出." << endl;
return 0;
}
default:
cout << "无效的选择,请重试." << endl;
break;
}
}
return 0;
}