21xrx.com
2025-04-13 17:07:46 Sunday
文章检索 我的文章 写文章
C++ 学生信息管理系统源代码
2023-07-07 15:10:39 深夜i     12     0
C++ 学生信息 管理系统 源代码 数据结构

C++作为一门流行的编程语言,被广泛地应用在各种领域。其中,学生信息管理系统也是其应用之一。 在这个学生信息管理系统源代码中,我们可以看到其具有以下功能:

1. 添加学生信息:输入学生姓名、性别、年龄、班级、学号等信息后,将其存储到文件中。

2. 显示学生信息:读取文件中学生信息,将其显示在屏幕上。

3. 修改学生信息:选择要修改的学生信息,输入修改后的信息进行更新。

4. 删除学生信息:选择要删除的学生信息,将其从文件中删除。

整个程序主要使用文件读写操作来存储学生的信息。因此程序运行时,需要先判断文件是否存在。如果文件不存在,则需要创建一个新的文件。

源代码如下:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct student
  string name;
  char gender;
  int age;
  string grade;
  string id;
;
void add_student(){
  student new_student;
  ofstream out_file("students.dat", ios::app | ios::binary);
  cout << "Enter student name:" << endl;
  cin >> new_student.name;
  cout << "Enter student gender:" << endl;
  cin >> new_student.gender;
  cout << "Enter student age:" << endl;
  cin >> new_student.age;
  cout << "Enter student grade:" << endl;
  cin >> new_student.grade;
  cout << "Enter student ID:" << endl;
  cin >> new_student.id;
  out_file.write(reinterpret_cast<const char*>(&new_student),sizeof(student));
  out_file.close();
}
void display_students(){
  student current_student;
  ifstream in_file("students.dat", ios::binary);
  while(in_file.read(reinterpret_cast<char*>(&current_student), sizeof(student)))
    cout << "Name: " << current_student.name << endl;
    cout << "Gender: " << current_student.gender << endl;
    cout << "Age: " << current_student.age << endl;
    cout << "Grade: " << current_student.grade << endl;
    cout << "ID: " << current_student.id << endl;
    cout << endl;
  
  in_file.close();
}
void modify_student(){
  student current_student;
  fstream file("students.dat", ios::in | ios::out | ios::binary);
  cout << "Enter student ID to modify:" << endl;
  string id_to_modify;
  cin >> id_to_modify;
  while(file.read(reinterpret_cast<char*>(&current_student), sizeof(student))){
    if(current_student.id == id_to_modify){
      cout << "Enter new name:" << endl;
      cin >> current_student.name;
      cout << "Enter new gender:" << endl;
      cin >> current_student.gender;
      cout << "Enter new age:" << endl;
      cin >> current_student.age;
      cout << "Enter new grade:" << endl;
      cin >> current_student.grade;
      cout << "Enter new ID:" << endl;
      cin >> current_student.id;
      file.seekp(-static_cast<int>(sizeof(student)), ios::cur);
      file.write(reinterpret_cast<const char*>(&current_student), sizeof(student));
      break;
    }
  }
  file.close();
}
void delete_student(){
  student current_student;
  ifstream in_file("students.dat", ios::binary);
  ofstream out_file("temp.dat", ios::binary);
  cout << "Enter student ID to delete:" << endl;
  string id_to_delete;
  cin >> id_to_delete;
  while(in_file.read(reinterpret_cast<char*>(&current_student), sizeof(student))){
    if(current_student.id != id_to_delete){
      out_file.write(reinterpret_cast<const char*>(&current_student), sizeof(student));
    }
  }
  in_file.close();
  out_file.close();
  remove("students.dat");
  rename("temp.dat", "students.dat");
}
int main(){
  int choice;
  while(true){
    cout << "Please select the operation you want to perform:" << endl;
    cout << "1. Add new student information" << endl;
    cout << "2. Display all students information" << endl;
    cout << "3. Modify student information" << endl;
    cout << "4. Delete student information" << endl;
    cout << "5. Exit the program" << endl;
    cin >> choice;
    switch(choice){
      case 1:
        add_student();
        break;
      case 2:
        display_students();
        break;
      case 3:
        modify_student();
        break;
      case 4:
        delete_student();
        break;
      case 5:
        exit(0);
      default:
        cout << "Invalid operation, please select again" << endl;
        break;
    }
  }
  return 0;
}

这个学生信息管理系统源代码通过使用文件读写操作来实现对学生信息的增删改查,实现了一个简单的管理系统。在实际教育工作中,通过使用这样的系统可以更加方便地管理学生的信息,从而提高工作效率。

  
  

评论区

请求出错了