diff --git a/#includeiostrea.txt b/#includeiostrea.txt new file mode 100644 index 0000000..90dbda4 --- /dev/null +++ b/#includeiostrea.txt @@ -0,0 +1,251 @@ +#include +#include // 文件操作 +#include +#include // 用于格式化输出 +using namespace std; + +const int MAX_STUDENTS = 100; // 最大学生数量 + +// 学生信息结构体 +struct Student { + string name; + int scores[5]; // 假设有5门课程 +}; + +// 全局变量 +Student students[MAX_STUDENTS]; +int numStudents = 0; // 实际学生数量 + +// 函数声明 +void displayMenu(); +void inputData(); +void modifyData(); +void viewStatistics(); +void saveToFile(); +void readFromFile(); +void printStudents(); +int findStudentIndex(const string& name); +bool isValidScore(int score); + +int main() { + int choice; + readFromFile(); // 程序启动时读取文件数据 + + do { + displayMenu(); + cin >> choice; + cin.ignore(); // 忽略换行符 + + switch (choice) { + case 1: + inputData(); + break; + case 2: + modifyData(); + break; + case 3: + viewStatistics(); + break; + case 4: + saveToFile(); + break; + case 5: + cout << "Exiting program..." << endl; + break; + default: + cout << "Invalid choice. Please try again." << endl; + } + } while (choice != 5); + + return 0; +} + +void displayMenu() { + cout << "===== Student Grade Management System =====" << endl; + cout << "1. Input student grades" << endl; + cout << "2. Modify/Delete student grades" << endl; + cout << "3. View statistics" << endl; + cout << "4. Save data to file" << endl; + cout << "5. Exit" << endl; + cout << "Enter your choice: "; +} + +void inputData() { + if (numStudents >= MAX_STUDENTS) { + cout << "Database full. Cannot input more students." << endl; + return; + } + + Student newStudent; + cout << "Enter student name: "; + getline(cin, newStudent.name); + + for (int i = 0; i < 5; ++i) { + int score; + do { + cout << "Enter score for Course " << (i + 1) << ": "; + cin >> score; + cin.ignore(); // 忽略换行符 + + if (!isValidScore(score)) { + cout << "Invalid score. Please enter a score between 0 and 100." << endl; + } + } while (!isValidScore(score)); + newStudent.scores[i] = score; + } + + students[numStudents++] = newStudent; + cout << "Student data entered successfully." << endl; +} + +void modifyData() { + if (numStudents == 0) { + cout << "No students in the database." << endl; + return; + } + + string name; + cout << "Enter student name to modify/delete: "; + getline(cin, name); + + int index = findStudentIndex(name); + if (index == -1) { + cout << "Student not found." << endl; + return; + } + + // 显示学生信息 + cout << "Student found:" << endl; + cout << "Name: " << students[index].name << endl; + cout << "Scores:" << endl; + for (int i = 0; i < 5; ++i) { + cout << "Course " << (i + 1) << ": " << students[index].scores[i] << endl; + } + + // 询问用户是否修改或删除 + int choice; + cout << "Do you want to modify (1) or delete (2) this student's data? "; + cin >> choice; + cin.ignore(); // 忽略换行符 + + if (choice == 1) { + // 修改数据 + for (int i = 0; i < 5; ++i) { + int score; + do { + cout << "Enter new score for Course " << (i + 1) << ": "; + cin >> score; + cin.ignore(); // 忽略换行符 + + if (!isValidScore(score)) { + cout << "Invalid score. Please enter a score between 0 and 100." << endl; + } + } while (!isValidScore(score)); + students[index].scores[i] = score; + } + cout << "Student data modified successfully." << endl; + } else if (choice == 2) { + // 删除数据,将后面的学生数据往前移 + for (int i = index; i < numStudents - 1; ++i) { + students[i] = students[i + 1]; + } + numStudents--; + cout << "Student data deleted successfully." << endl; + } else { + cout << "Invalid choice. No changes made." << endl; + } +} + +void viewStatistics() { + if (numStudents == 0) { + cout << "No students in the database." << endl; + return; + } + + int minScore = INT_MAX, maxScore = INT_MIN, totalScore = 0; + + for (int i = 0; i < numStudents; ++i) { + for (int j = 0; j < 5; ++j) { + int score = students[i].scores[j]; + if (score < minScore) minScore = score; + if (score > maxScore) maxScore = score; + totalScore += score; + } + } + + double averageScore = static_cast(totalScore) / (numStudents * 5); + + cout << "===== Statistics =====" << endl; + cout << "Number of students: " << numStudents << endl; + cout << "Minimum score: " << minScore << endl; + cout << "Maximum score: " << maxScore << endl; + cout << "Average score: " << fixed << setprecision(2) << averageScore << endl; +} + +void saveToFile() { + ofstream outFile("students.txt"); + + if (!outFile) { + cout << "Error opening file." << endl; + return; + } + + outFile << numStudents << endl; + + for (int i = 0; i < numStudents; ++i) { + outFile << students[i].name << endl; + for (int j = 0; j < 5; ++j) { + outFile << students[i].scores[j] << " "; + } + outFile << endl; + } + + outFile.close(); + cout << "Data saved to file successfully." << endl; +} + +void readFromFile() { + ifstream inFile("students.txt"); + + if (!inFile) { + cout << "File not found. Starting with an empty database." << endl; + return; + } + + inFile >> numStudents; + inFile.ignore(); // 忽略换行符 + + for (int i = 0; i < numStudents; ++i) { + getline(inFile, students[i].name); + for (int j = 0; j < 5; ++j) { + inFile >> students[i].scores[j]; + } + inFile.ignore(); // 忽略换行符 + } + + inFile.close(); + cout << "Data loaded from file successfully." << endl; +} + +void printStudents() { + cout << "===== Student List =====" << endl; + for (int i = 0; i < numStudents; ++i) { + cout << "Student " << (i + 1) << ": " << students[i].name << endl; + for (int j = 0; j < 5; ++j) { + cout << " Course " << (j + 1) << ": " << students[i].scores[j] << endl; + } + } +} + +int findStudentIndex(const string& name) { + for (int i = 0; i < numStudents; ++i) { + if (students[i].name == name) { + return i; + } + } + return -1; +} + +bool isValidScore(int score) { + return (score >= 0 && score <= 100); +} \ No newline at end of file