21xrx.com
2024-12-22 21:50:00 Sunday
登录
文章检索 我的文章 写文章
【C++语言】学生成绩管理系统代码
2023-07-10 17:26:06 深夜i     --     --
C++ 学生成绩 管理系统 代码

C++是一种面向对象的编程语言,它有广泛的应用场景,其中之一就是用于创建学生成绩管理系统。这种系统可以帮助学校或教师管理学生成绩,提高教学质量。现在,我们来介绍一下C++语言实现学生成绩管理系统的代码。

首先,我们需要定义一个结构体来存储每个学生的信息,包括姓名、学号、年龄和各门课程的成绩等。代码如下:


struct student{

 char name[50];

 int rollno;

 int age;

 float marks[5];

};

接下来,我们可以定义一个类来实现各种管理操作,比如添加学生、删除学生、修改学生成绩等。代码如下:


class StudentManager{

 private:

  student s;

  FILE *fp;

 public:

  // 添加学生信息

  void add_student(){

   fp=fopen("student.dat","ab+");

   cout<<"\nEnter student details:";

   cout<<"\nName:";

   scanf("%s",s.name);

   cout<<"\nRoll:";

   cin>>s.rollno;

   cout<<"\nAge:";

   cin>>s.age;

   for(int i=0;i<5;i++){

    cout<<"\nMarks of subject "<<i+1<<":";

    cin>>s.marks[i];

   }

   fwrite(&s,sizeof(s),1,fp);

   fclose(fp);

   cout<<"\n\nStudent record added successfully";

  }

  

  // 显示学生信息

  void show_student(){

   fp=fopen("student.dat","rb");

   cout<<"\n\n\t\tDISPLAY ALL RECORD !!!\n\n";

   while(fread(&s,sizeof(s),1,fp)==1){

    cout<<"\nName:"<<s.name<<"\nRoll:"<<s.rollno<<"\nAge:"<<s.age<<"\n";

    for(int i=0;i<5;i++){

     cout<<"\nMarks of subject "<<i+1<<":"<<s.marks[i];

    }

    cout<<"\n";

   }

   fclose(fp);

  }

  

  // 修改学生信息

  void modify_student(){

   int found=0;

   int rollno=0;

   fp=fopen("student.dat","rb+");

   cout<<"\nEnter student roll number to modify:";

   cin>>rollno;

   while(fread(&s,sizeof(s),1,fp)==1){

    if(s.rollno==rollno){

     cout<<"\nOld record is:";

     cout<<"\nName:"<<s.name<<"\nRoll:"<<s.rollno<<"\nAge:"<<s.age<<"\n";

     for(int i=0;i<5;i++){

      cout<<"\nMarks of subject "<<i+1<<":"<<s.marks[i];

     }

     cout<<"\nEnter new details:";

     cout<<"\nName:";

     scanf("%s",s.name);

     cout<<"\nRoll:";

     cin>>s.rollno;

     cout<<"\nAge:";

     cin>>s.age;

     for(int i=0;i<5;i++){

      cout<<"\nMarks of subject "<<i+1<<":";

      cin>>s.marks[i];

     }

     fseek(fp,-sizeof(s),SEEK_CUR);

     fwrite(&s,sizeof(s),1,fp);

     fclose(fp);

     found=1;

     break;

    }

   }

   if(found==0){

    cout<<"\nRecord not found";

   }

  }

  

  // 删除学生信息

  void delete_student(){

   int found=0;

   int rollno=0;

   fp=fopen("student.dat","rb+");

   FILE *ft;

   ft=fopen("temp.dat","wb+");

   cout<<"\nEnter student roll number to delete:";

   cin>>rollno;

   rewind(fp);

   while(fread(&s,sizeof(s),1,fp)==1){

    if(s.rollno!=rollno){

     fwrite(&s,sizeof(s),1,ft);

    }

    else

     found=1;

    

   }

   fclose(fp);

   fclose(ft);

   remove("student.dat");

   rename("temp.dat","student.dat");

   if(found==1){

    cout<<"\nRecord deleted successfully";

   }

   else{

    cout<<"\nRecord not found";

   }

  }

  

  // 查找学生信息

  void search_student(){

   int found=0;

   int rollno=0;

   fp=fopen("student.dat","rb");

   cout<<"\nEnter student roll number to search:";

   cin>>rollno;

   while(fread(&s,sizeof(s),1,fp)==1){

    if(s.rollno==rollno){

     cout<<"\nName:"<<s.name<<"\nRoll:"<<s.rollno<<"\nAge:"<<s.age<<"\n";

     for(int i=0;i<5;i++){

      cout<<"\nMarks of subject "<<i+1<<":"<<s.marks[i];

     }

     found=1;

     break;

    }

   }

   if(found==0){

    cout<<"\nRecord not found";

   }

   fclose(fp);

  }

};

最后,我们可以写一个主函数,在里面实例化刚才定义的类,并通过菜单方式来选择不同的操作。代码如下:


int main(){

 StudentManager manager;

 int choice;

 do{

  cout<<"\n\n\t\t\t\t\tSTUDENT MANAGEMENT SYSTEM\n";

  cout<<"\n\n\t\t\t\t===============================================";

  cout<<"\n\n\t\t\t\t\t1.Add student record";

  cout<<"\n\n\t\t\t\t\t2.Show student record";

  cout<<"\n\n\t\t\t\t\t3.Modify student record";

  cout<<"\n\n\t\t\t\t\t4.Delete student record";

  cout<<"\n\n\t\t\t\t\t5.Search student record";

  cout<<"\n\n\t\t\t\t\t6.Exit system";

  cout<<"\n\n\t\t\t\t===============================================";

  cout<<"\n\n\t\t\t\tEnter your choice:";

  cin>>choice;

  switch(choice){

   case 1:

    manager.add_student();

    break;

   case 2:

    manager.show_student();

    break;

   case 3:

    manager.modify_student();

    break;

   case 4:

    manager.delete_student();

    break;

   case 5:

    manager.search_student();

    break;

   case 6:

    exit(0);

    break;

   default:

    cout<<"\nWrong choice! Please try again";

    break;

  }

 }while(choice!=6);

 return 0;

}

这样,我们就完成了一个简单的学生成绩管理系统的代码,在实际应用中,还需要根据需求进行不同的扩展和优化,让其更加实用和健壮。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复