|
发表于 2023-12-24 20:27:45
|
显示全部楼层
当然可以!下面是一个简单的C++程序,用于实现你描述的学生信息管理系统。
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct Student {
- string id;
- string name;
- int age;
- string gender;
- string birthdate;
- string address;
- string phone;
- string email;
- };
- // 函数声明
- void displayMenu();
- void addStudent();
- void viewStudents();
- void searchStudentsById();
- void searchStudentsByName();
- bool deleteStudent();
- bool modifyStudent();
- int main() {
- int choice;
- do {
- displayMenu();
- cin >> choice;
- cin.ignore(); // 忽略输入缓冲中的换行符
-
- switch (choice) {
- case 1:
- addStudent();
- break;
- case 2:
- viewStudents();
- break;
- case 3:
- searchStudentsById();
- break;
- case 4:
- searchStudentsByName();
- break;
- case 5:
- if (!deleteStudent())
- cout << "学生信息删除失败!" << endl;
- break;
- case 6:
- if (!modifyStudent())
- cout << "学生信息修改失败!" << endl;
- break;
- case 0:
- cout << "感谢使用学生信息管理系统,再见!" << endl;
- break;
- default:
- cout << "无效的选择,请重新输入!" << endl;
- }
- cout << endl;
- } while (choice != 0);
- return 0;
- }
- void displayMenu() {
- cout << "====== 学生信息管理系统 ======" << endl;
- cout << "1. 录入学生信息" << endl;
- cout << "2. 浏览学生信息" << endl;
- cout << "3. 按学号查询学生信息" << endl;
- cout << "4. 按姓名查询学生信息" << endl;
- cout << "5. 删除学生信息" << endl;
- cout << "6. 修改学生信息" << endl;
- cout << "0. 退出系统" << endl;
- cout << "请输入选项:";
- }
- void addStudent() {
- Student newStudent;
- cout << "学号:";
- getline(cin, newStudent.id);
- cout << "姓名:";
- getline(cin, newStudent.name);
- cout << "年龄:";
- cin >> newStudent.age;
- cin.ignore();
- cout << "性别:";
- getline(cin, newStudent.gender);
- cout << "出生年月:";
- getline(cin, newStudent.birthdate);
- cout << "地址:";
- getline(cin, newStudent.address);
- cout << "电话:";
- getline(cin, newStudent.phone);
- cout << "E-mail:";
- getline(cin, newStudent.email);
- // 将学生信息写入文件
- ofstream outFile("students.txt", ios::app); // 打开文件以追加写入数据
- outFile << newStudent.id << ' '
- << newStudent.name << ' '
- << newStudent.age << ' '
- << newStudent.gender << ' '
- << newStudent.birthdate << ' '
- << newStudent.address << ' '
- << newStudent.phone << ' '
- << newStudent.email << endl;
- outFile.close(); // 关闭文件
- cout << "学生信息录入成功!" << endl;
- }
- void viewStudents() {
- ifstream inFile("students.txt");
- if (!inFile) {
- cout << "无法打开学生信息文件!" << endl;
- return;
- }
- string line;
- while (getline(inFile, line)) {
- cout << line << endl;
- }
- inFile.close();
- }
- void searchStudentsById() {
- string targetId;
- cout << "请输入要查询的学号:";
- getline(cin, targetId);
- ifstream inFile("students.txt");
- if (!inFile) {
- cout << "无法打开学生信息文件!" << endl;
- return;
- }
- string line;
- bool found = false;
- while (getline(inFile, line)) {
- size_t pos = line.find(targetId);
- if (pos != string::npos) {
- cout << line << endl;
- found = true;
- }
- }
- if (!found) {
- cout << "未找到该学号对应的学生信息!" << endl;
- }
- inFile.close();
- }
- void searchStudentsByName() {
- string targetName;
- cout << "请输入要查询的姓名:";
- getline(cin, targetName);
- ifstream inFile("students.txt");
- if (!inFile) {
- cout << "无法打开学生信息文件!" << endl;
- return;
- }
- string line;
- bool found = false;
- while (getline(inFile, line)) {
- size_t pos = line.find(targetName);
- if (pos != string::npos) {
- cout << line << endl;
- found = true;
- }
- }
- if (!found) {
- cout << "未找到该姓名对应的学生信息!" << endl;
- }
- inFile.close();
- }
- bool deleteStudent() {
- string targetId;
- cout << "请输入要删除的学号:";
- getline(cin, targetId);
- ifstream inFile("students.txt");
- if (!inFile) {
- cout << "无法打开学生信息文件!" << endl;
- return false;
- }
- ofstream outFile("temp.txt"); // 创建一个临时文件存放其他学生的信息
- string line;
- bool found = false;
- while (getline(inFile, line)) {
- size_t pos = line.find(targetId);
- if (pos == string::npos) {
- outFile << line << endl;
- } else {
- found = true;
- }
- }
- inFile.close();
- outFile.close();
- // 删除旧的学生信息文件,并将临时文件重命名为新的学生信息文件
- if (remove("students.txt") != 0 || rename("temp.txt", "students.txt") != 0) {
- cout << "删除学生信息失败!" << endl;
- return false;
- }
- if (found) {
- cout << "学生信息删除成功!" << endl;
- } else {
- cout << "未找到该学号对应的学生信息,无法删除!" << endl;
- }
- return found;
- }
- bool modifyStudent() {
- string targetId;
- cout << "请输入要修改的学号:";
- getline(cin, targetId);
- fstream file("students.txt");
- if (!file) {
- cout << "无法打开学生信息文件!" << endl;
- return false;
- }
- ofstream tempFile("temp.txt");
- string line;
- bool found = false;
- while (getline(file, line)) {
- size_t pos = line.find(targetId);
- if (pos != string::npos) {
- cout << "原信息:" << line << endl;
- Student modifiedStudent;
- modifiedStudent.id = targetId;
- cout << "新姓名(不修改请直接回车):";
- getline(cin, modifiedStudent.name);
- cout << "新年龄(不修改请直接回车):";
- cin >> modifiedStudent.age;
- cin.ignore();
- cout << "新性别(不修改请直接回车):";
- getline(cin, modifiedStudent.gender);
- cout << "新出生年月(不修改请直接回车):";
- getline(cin, modifiedStudent.birthdate);
- cout << "新地址(不修改请直接回车):";
- getline(cin, modifiedStudent.address);
- cout << "新电话(不修改请直接回车):";
- getline(cin, modifiedStudent.phone);
- cout << "新E-mail(不修改请直接回车):";
- getline(cin, modifiedStudent.email);
- if (modifiedStudent.name.empty()) {
- size_t endPos = line.find(' ', pos + 1);
- modifiedStudent.name = line.substr(pos + targetId.length() + 1, endPos - pos - targetId.length() - 1);
- }
- if (modifiedStudent.age == 0) {
- size_t startPos = line.find_last_of(' ') + 1;
- size_t endPos = line.find(' ', startPos);
- modifiedStudent.age = stoi(line.substr(startPos, endPos - startPos));
- }
- if (modifiedStudent.gender.empty()) {
- size_t startPos = line.find_last_of(' ') + 1;
- size_t endPos = line.find(' ', startPos);
- modifiedStudent.gender = line.substr(startPos, endPos - startPos);
- }
- if (modifiedStudent.birthdate.empty()) {
- size_t startPos = line.find_last_of(' ') + 1;
- size_t endPos = line.find(' ', startPos);
- modifiedStudent.birthdate = line.substr(startPos, endPos - startPos);
- }
- if (modifiedStudent.address.empty()) {
- size_t startPos = line.find_last_of(' ') + 1;
- size_t endPos = line.find(' ', startPos);
- modifiedStudent.address = line.substr(startPos, endPos - startPos);
- }
- if (modifiedStudent.phone.empty()) {
- size_t startPos = line.find_last_of(' ') + 1;
- size_t endPos = line.find(' ', startPos);
- modifiedStudent.phone = line.substr(startPos, endPos - startPos);
- }
- if (modifiedStudent.email.empty()) {
- size_t startPos = line.find_last_of(' ') + 1;
- modifiedStudent.email = line.substr(startPos);
- }
- cout << "修改后的信息:" << modifiedStudent.id << ' '
- << modifiedStudent.name << ' '
- << modifiedStudent.age << ' '
- << modifiedStudent.gender << ' '
- << modifiedStudent.birthdate << ' '
- << modifiedStudent.address << ' '
- << modifiedStudent.phone << ' '
- << modifiedStudent.email << endl;
- tempFile << modifiedStudent.id << ' '
- << modifiedStudent.name << ' '
- << modifiedStudent.age << ' '
- << modifiedStudent.gender << ' '
- << modifiedStudent.birthdate << ' '
- << modifiedStudent.address << ' '
- << modifiedStudent.phone << ' '
- << modifiedStudent.email << endl;
- found = true;
- } else {
- tempFile << line << endl;
- }
- }
- file.close();
- tempFile.close();
- // 删除旧的学生信息文件,并将临时文件重命名为新的学生信息文件
- if (remove("students.txt") != 0 || rename("temp.txt", "students.txt") != 0) {
- cout << "修改学生信息失败!" << endl;
- return false;
- }
- if (found) {
- cout << "学生信息修改成功!" << endl;
- } else {
- cout << "未找到该学号对应的学生信息,无法修改!" << endl;
- }
- return found;
- }
复制代码
这个程序使用了一个结构体`Student`来表示学生信息。主要功能包括学生信息录入、浏览、查询、删除和修改。学生信息会保存在一个名为`students.txt`的文件中。
你可以根据需要自行扩展其他功能,比如按年龄查询、按性别查询等。另外,由于这只是一个简单的示例程序,没有进行输入验证、异常处理等,在实际应用中需要添加这些功能以提高系统的健壮性。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|