parent
4d4800652c
commit
befa1c8531
@ -1,2 +1,183 @@
|
|||||||
# GradeBookPlus
|
# GradeBookPlus
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// 学生信息结构体
|
||||||
|
struct Student {
|
||||||
|
string name;
|
||||||
|
int id;
|
||||||
|
float score;
|
||||||
|
};
|
||||||
|
|
||||||
|
const int MAX_STUDENTS = 100;
|
||||||
|
Student students[MAX_STUDENTS];
|
||||||
|
int numStudents = 0; // 实际学生数量
|
||||||
|
|
||||||
|
// 函数声明
|
||||||
|
void displayMenu();
|
||||||
|
void inputData();
|
||||||
|
void modifyData();
|
||||||
|
void deleteData();
|
||||||
|
void viewStatistics();
|
||||||
|
void saveToFile();
|
||||||
|
void readFromFile();
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
readFromFile(); // 启动时读取文件中的数据
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do {
|
||||||
|
displayMenu();
|
||||||
|
cin >> choice;
|
||||||
|
switch(choice) {
|
||||||
|
case 1:
|
||||||
|
inputData();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
modifyData();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
deleteData();
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
viewStatistics();
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
saveToFile();
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
cout << "Exiting the program...\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
cout << "Invalid choice. Please enter again.\n";
|
||||||
|
}
|
||||||
|
} while(choice != 6);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示菜单
|
||||||
|
void displayMenu() {
|
||||||
|
cout << "\n===== Student Grade Management System =====\n";
|
||||||
|
cout << "1. Input Student Data\n";
|
||||||
|
cout << "2. Modify Student Data\n";
|
||||||
|
cout << "3. Delete Student Data\n";
|
||||||
|
cout << "4. View Statistics\n";
|
||||||
|
cout << "5. Save Data to File\n";
|
||||||
|
cout << "6. Exit\n";
|
||||||
|
cout << "Enter your choice: ";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据录入
|
||||||
|
void inputData() {
|
||||||
|
if (numStudents >= MAX_STUDENTS) {
|
||||||
|
cout << "Cannot add more students. Array is full.\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student newStudent;
|
||||||
|
cout << "Enter student name: ";
|
||||||
|
cin >> newStudent.name;
|
||||||
|
cout << "Enter student ID: ";
|
||||||
|
cin >> newStudent.id;
|
||||||
|
cout << "Enter student score: ";
|
||||||
|
cin >> newStudent.score;
|
||||||
|
|
||||||
|
// 在这里可以添加数据有效性检查
|
||||||
|
|
||||||
|
students[numStudents++] = newStudent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据修改
|
||||||
|
void modifyData() {
|
||||||
|
int index;
|
||||||
|
cout << "Enter the index of student to modify (0-" << numStudents-1 << "): ";
|
||||||
|
cin >> index;
|
||||||
|
|
||||||
|
if (index >= 0 && index < numStudents) {
|
||||||
|
cout << "Enter new score for " << students[index].name << ": ";
|
||||||
|
cin >> students[index].score;
|
||||||
|
} else {
|
||||||
|
cout << "Invalid index.\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据删除
|
||||||
|
void deleteData() {
|
||||||
|
int index;
|
||||||
|
cout << "Enter the index of student to delete (0-" << numStudents-1 << "): ";
|
||||||
|
cin >> index;
|
||||||
|
|
||||||
|
if (index >= 0 && index < numStudents) {
|
||||||
|
for (int i = index; i < numStudents - 1; ++i) {
|
||||||
|
students[i] = students[i + 1];
|
||||||
|
}
|
||||||
|
numStudents--;
|
||||||
|
cout << "Student data deleted.\n";
|
||||||
|
} else {
|
||||||
|
cout << "Invalid index.\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查看统计信息
|
||||||
|
void viewStatistics() {
|
||||||
|
if (numStudents == 0) {
|
||||||
|
cout << "No students data available.\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float sum = 0, highest = students[0].score, lowest = students[0].score;
|
||||||
|
for (int i = 0; i < numStudents; ++i) {
|
||||||
|
sum += students[i].score;
|
||||||
|
if (students[i].score > highest) {
|
||||||
|
highest = students[i].score;
|
||||||
|
}
|
||||||
|
if (students[i].score < lowest) {
|
||||||
|
lowest = students[i].score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
float average = sum / numStudents;
|
||||||
|
|
||||||
|
cout << "Statistics:\n";
|
||||||
|
cout << "Number of students: " << numStudents << "\n";
|
||||||
|
cout << "Highest score: " << highest << "\n";
|
||||||
|
cout << "Lowest score: " << lowest << "\n";
|
||||||
|
cout << "Average score: " << average << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将数据保存至文件
|
||||||
|
void saveToFile() {
|
||||||
|
ofstream outFile("students.txt");
|
||||||
|
if (!outFile) {
|
||||||
|
cerr << "Error opening file for writing.\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < numStudents; ++i) {
|
||||||
|
outFile << students[i].name << " " << students[i].id << " " << students[i].score << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
outFile.close();
|
||||||
|
cout << "Data saved to file successfully.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从文件中读取数据
|
||||||
|
void readFromFile() {
|
||||||
|
ifstream inFile("students.txt");
|
||||||
|
if (!inFile) {
|
||||||
|
cout << "No previous data found. Starting with an empty database.\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
numStudents = 0;
|
||||||
|
while (inFile >> students[numStudents].name >> students[numStudents].id >> students[numStudents].score) {
|
||||||
|
numStudents++;
|
||||||
|
}
|
||||||
|
|
||||||
|
inFile.close();
|
||||||
|
cout << "Data loaded from file successfully.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in new issue