栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现学生选课系统

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现学生选课系统

本文实例为大家分享了C++实现学生选课系统的具体代码,供大家参考,具体内容如下

#include 
#include  
#include 
#include
#include
using namespace std;

struct SubList
{
 int num; 
 SubList *next;  
 SubList() :num(-1), next(NULL){} 
};
struct StuList 
{
 int num;   
 float score;   
 StuList *next; 
 StuList() :num(-1), score(0), next(NULL){}
};
class Student 
{
private:
 int Num;  
 char Name[20];    
 int MaxSubNum;   
 int FactSubNum;   
 SubList *Root;   
 float SumXueFen; 
 float FactXueFen; 
 float SumGrade; 
 bool Update; 
public:
 Student *next;
 SubList *GetSubPtr()const{ return Root; }
 Student() :Update(false), Root(new SubList()), FactXueFen(0), MaxSubNum(5), FactSubNum(0), SumXueFen(0), SumGrade(0),next(NULL){}
 void SetName(char N[]){ strcpy(Name, N); } 
 void SetNum(int num){ Num = num; } 
 const char* GetName()const{ return Name; }
 int GetNum()const{ return Num; } 
 int GetFactSubNum()const{ return FactSubNum; } 
 bool FindSub(int num)const;  
 void SetInfo(float sumXueFen, float factXueFen, float sumGrade)
 {
 SumXueFen = sumXueFen;
 FactXueFen = factXueFen;
 SumGrade = sumGrade;
 }
 void SetUpdate(bool t){ Update = t; }
 float GetAveGrade(){ return SumGrade / FactSubNum; }  
 void AddSub(int num); 
 bool IsFull()const{ return MaxSubNum == FactSubNum; }
 void ShowStuInfo(); 
 void DelSub(int num); 
 ~Student()
 {
 SubList *p = Root;
 while (p)
 {
  SubList*t = p->next;
  delete p;
  p = t;
 }
 }
};
//课程类 
class Subject
{
private:
 int MaxStuNum;  
 int FactStuNum;  
 float Credit;  
 char Name[20]; 
 int Num; 
 StuList *Root; 
 float AveGrade; 
 bool Update; 
public:
 StuList *GetStuPtr()const{ return Root; }
 Subject*next;
 Subject() :Root(new StuList()), MaxStuNum(30), FactStuNum(0), Update(false),next(NULL){}
 float GetCredit()const{ return Credit; }  //得到课程的学分 
 void SetCredit(float credit){ Credit = credit; } //设置学分
 const char* GetName()const{ return Name; } //读出课程的名称
 void SetName(char N[]){ strcpy(Name, N); } //设置课程的名称
 int GetNum()const{ return Num; } //读出课程的代号
 void SetNum(int num){ Num = num; } //设置课程的代号
 int GetFactStuNum()const{ return FactStuNum; } //返回实际学生数 
 int GetMaxStuNum()const{ return MaxStuNum; }//最大学生数
 void SetUpdate(bool t){ Update = t; }
 void SetAveGrade(float num){ AveGrade = num/FactStuNum; } //设置平均分
 float GetAveGrade(){ return AveGrade; }  //得到学生的平均成绩
 float GetStuScore(int num); //查找某个学生的成绩
 bool IsFull()const{ return MaxStuNum == FactStuNum; }//是否人数已满
 void DelStu(int num);//删除学生
 void AddStu(int num);//增加学生
 ~Subject()
 {
 StuList *p = Root;
 while (p)
 {
  StuList*t = p->next;
  delete p;
  p = t;
 }
 }
};

float Subject::GetStuScore(int num)
{
 StuList*p = Root->next;
 while (p->num != num)
 p = p->next;
 return p->score;
}

void Student::ShowStuInfo()
{
 cout << Num << " " << Name << " " << "选课数:" << FactSubNum << endl;
 cout << "tt 总成绩" << SumGrade << " 平均分" << GetAveGrade() << endl;
 cout << "tt课程总学分" << SumXueFen << " 获得学分" << FactXueFen << endl; 
 system("pause");
}

bool Student::FindSub(int num)const//查找是否已有此课程,如果有返回,如果没有返回
{
 bool t = false;
 SubList *p = Root->next;
 while (!t && p)
 {
 if (p->num == num)
  t = true;
 p = p->next;
 }
 return t;
}

void Student::AddSub(int num)//给学生增加一门课 
{
 SubList *s = new SubList(), *p = Root;
 while (p->next)
 p = p->next;
 p->next = s;
 s->num = num;
 FactSubNum++;
 Update = true;
}

void Student::DelSub(int num)
{
 SubList*p = Root, *t;

 while (p->next->num != num)
 p = p->next;
 t = p->next;
 p->next = t->next;

 FactSubNum--;
 Update = true;
 delete t;
}
//////////////////////////Subject//////////////////////////////////
void Subject::AddStu(int num)// 
{
 StuList *s = new StuList(), *p = Root;
 while (p->next)
 p = p->next;
 p->next = s;
 s->num = num;
 FactStuNum++;
 Update = true;
}

void Subject::DelStu(int num)
{
 StuList*p = Root, *t;

 while (p->next->num != num)
 p = p->next;
 t = p->next;
 p->next = t->next;

 FactStuNum--;
 Update = true;
 delete t;
}

//////////////////////////////////////////////////////////
class Manage
{
private:
 Student*StuRoot;
 Subject*SubRoot;
 bool Update;
public:
 Manage() :StuRoot(new Student()), SubRoot(new Subject()),Update(false){}
 Manage(const Manage&p){}
 void AddStu();
 void AddSub();

 Student*FindStu(int num);
 Subject*FindSub(int num);

 int ShowSub()const;//显示可选课程
 int ShowStu()const;
 void ShowStuSubInfo(Student*p);

 void DelStu(Student*p);
 void DelSub(Subject*p);

 void Start();
 int menu();
 int custom();
 int server();

 Student* password1();
 bool password2();

 void menu_1_1(Student*p);

 void menu_2_1();
 void menu_2_2();
 void menu_2_3();
 void menu_2_4();
 void menu_2_5();
 void menu_2_6();
 void menu_2_7();
 void menu_2_8();
 void menu_2_9();

 void IsUpdate()
 {
 if (Update == true)
 {
  Student*p = StuRoot->next;
  Subject*q = SubRoot->next;
  while (q)
  {
  float sum = 0;
  StuList *t =q->GetStuPtr()->next;
  while (t)
  {
   sum += t->score;
   t = t->next;
  }
  q->SetAveGrade(sum);
  q->SetUpdate(false);
  q = q->next;
  }
  while (p)
  {
  float sum = 0, xuefen = 0, xuefen1 = 0;
  SubList *t = p->GetSubPtr()->next;
  while (t)
  {
   Subject*tt = FindSub(t->num);
   xuefen += tt->GetCredit();

   float f = tt->GetStuScore(p->GetNum());
   sum += f;
   if (f>= 60)
   xuefen1 += tt->GetCredit();
   t = t->next;
  }
  p->SetInfo(xuefen, xuefen1, sum);
  p->SetUpdate(false);
  p= p->next;
  }
  Update = false;
 }
 }
};

void Manage::DelStu(Student*p)
{
 SubList *l = p->GetSubPtr()->next;
 while (l)
 {
 FindSub(l->num)->DelStu(p->GetNum());
 l = l->next;
 }

 Student*t = StuRoot;
 while (t->next != p)
 t = t->next;
 t->next = p->next;
 delete p;
 Update = true;
}

void Manage::DelSub(Subject*p)
{
 StuList *l = p->GetStuPtr()->next;
 while (l)
 {
 FindStu(l->num)->DelSub(p->GetNum());
 l = l->next;
 }

 Subject*t = SubRoot;
 while (t->next != p)
 t = t->next;
 t->next = p->next;
 delete p;
 Update = true;
}

int Manage::ShowSub()const
{
 Subject*p = SubRoot->next;
 int n = 0;
 while (p)
 {
 cout << p->GetNum() << " " << p->GetName() << " 学分:" << p->GetCredit() << endl;
 n++;
 p = p->next;
 }
 return n;
}

int Manage::ShowStu()const
{
 Student*p = StuRoot->next;
 int n = 0;
 while (p)
 {
 cout << p->GetNum() << " " << p->GetName() << endl;
 n++;
 p = p->next;
 }
 return n;
}

Student*Manage::FindStu(int num)
{
 Student*p = StuRoot->next;
 while (p)
 {
 if (p->GetNum() == num)
  break;
 p = p->next;
 }
 return p;
}

Subject*Manage::FindSub(int num)
{
 Subject*p = SubRoot->next;
 while (p)
 {
 if (p->GetNum() == num)
  break;
 p = p->next;
 }
 return p;
}

//总菜单 
int Manage::menu()
{
 int k = 0, n;
 while (1)
 {
 system("cls");
 cout << endl << endl;
 cout << "*******************************************n"
  << "**n"
  << "*学生选修课系统*n"
  << "**n"
  << "*   操作方式:*n"
  << "* 1.选修课系统学生端    *n"
  << "**n"
  << "* 2.选修课系统管理端    *n"
  << "* 0. 退出   *n"
  << "*******************************************n"
  << endl;
 cout << "ntt请选择登入方式: ";

 cin >> n;
 if (n > -1 && n < 3)
  return n;
 else
 {
  cerr << "nntttt输入有误!n" << endl;
  k++;
 }
 if (k == 3)
 {
  cerr << "nnntt~~提示~~:错误输入次数超过三次,你将被强制退出!!nn" << endl;
  return 0;
 }
 system("pause");
 }
}

//选修课系统端操作 
int Manage::custom()
{
 int n, k = 0;
 while (1)
 {
 system("cls");;
 cout << "nnn" << "※※※※※※※※※※※※※※※※※※※※※※※※※※n"
  << "※ 选修课系统学生端 ※n"
  << "※   ※n"
  << "※   操作方式:.  ※n"
  << "※   1、 学生选课  ※n"
  << "※   2、 学生情况  ※n"
  << "※   3、 选课情况  ※n"
  << "※   4、 退出系统  ※n"
  << "※   ※n"
  << "※※※※※※※※※※※※※※※※※※※※※※※※※※n" << endl;
 cout << "ttt请选择操作方式: ";
 cin >> n;
 if (n > 0 && n < 5)
  return n;
 else
 {
  cerr << "ntttt输入有误!请重新输入n" << endl;
  k++;
 }
 if (k == 3)
 {
  system("cls");
  cerr << "错误输入超过三次!你将被强制退出!!n" << endl;
  return 4;
 }
 system("pause");
 }
}

int Manage::server()
{
 int n, k = 0;
 while (1)
 {
 system("cls");;
 cout << "nnn" << "※※※※※※※※※※※※※※※※※※※※※※※※※n"
  << "※  ※n"
  << "※  选修课系统管理端※n"
  << "※  ※n"
  << "※  操作方式:   ※n"
  << "※      1.增加学生   2.增加课程     ※n"
  << "※      3.删除学生   4.删除课程     ※n"
  << "※      5.填写成绩   6.更改学分     ※n"
  << "※      7.学生情况   8.选课情况     ※n"
  << "※      9.保存数据   0.退出系统     ※n"
  << "※  ※n"
  << "※※※※※※※※※※※※※※※※※※※※※※※※※n" << endl;
 cout << "tt 请选择操作方式: " << endl;
 cin >> n;
 if (n > -1 && n < 10)
  return n;
 else
 {
  cerr << "ntttt输入有误!n" << endl;
  k++;
 }
 if (k == 3)
 {
  cerr << "错误输入超过三次!n";
  return 0;
 }
 system("pause");
 }
}

Student* Manage::password1()
{
 system("cls");
 int k = 0;
 int num;
 while (1)
 {
 cout << "请输入你的学号:" << endl;
 cin >> num;
 Student*p = FindStu(num);
 if (p == NULL)
 {
  cout << "无此学号!!请重新输入 " << endl;
  k++; 
  system("pause");
 }
 else
 {
  cout << "你的名字为:" << p->GetName() << endl;
  cout << "请按任意键进入选课系统" << endl;
  return p;
 }
 if (k >= 3)
 {
  system("cls");
  cerr << "nnttt输入错误密码超过三次!请按任意键退出.." << endl;
  return NULL;
 }
 
 }
}
//密码检查 
bool Manage::password2()
{
 int k = 0, i;
 const char A[] = "admin";
 char B[10];

 system("cls");
 for (i = 0; i < 8; i++)
 cout << endl;

 while (1)
 {
 printf("ttt请输入管理员密码:");
 i = 0;
 cin >> B;
 if (strcmp(A, B) == 0)
  return true;
 else
 {
  k++;
  cerr << "nnttt密码输入错误!请重新输入!n" << endl;
  system("pause");
 }
 if (k ==3)
 {
  system("cls");
  cerr << "nnttt输入错误密码超过三次!请按任意键退出.." << endl;
  return false;
 }
 }
}
void Manage::Start()
{
 int n = 1;
 while (n)
 {
 n = menu();
 if (n == 1)
 {
  Student*p = password1();
  while (p)
  {

  int c = custom();

  switch (c)
  {
  case 1: menu_1_1(p); break;//学生选课  

  case 2:p->ShowStuInfo(); break;//学生情况 

  case 3:ShowStuSubInfo(p);; break;//选课情况 

  }
  IsUpdate();
  if (c == 4) break;//退出系统 
  }
  if (p == NULL)
  n = 0;
 }
 else if (n == 2)
 {
  bool t = password2();
  while (t)
  {
  int c = server();
  switch (c)
  {
  case 1:menu_2_1();  break;  //增加学生 
  case 2:menu_2_2(); break;  //增加课程 
  case 3: menu_2_3(); break;  //删除学生  
  case 4:menu_2_4();  break;  //删除课程  
  case 5: menu_2_5(); break;  //填写成绩 
  case 6: menu_2_6(); break;  //更改学分 
  case 7:menu_2_7();  break;  //学生情况 
  case 8:menu_2_8();  break;  //选课情况 
  case 9: menu_2_9(); break;  //保存数据 
  }
  IsUpdate();
  if (c == 0)   break;  //退出系统 
  }
  if (!t)
  n = 0;
 }
 }
}

//学生端功能函数 
void Manage::menu_1_1(Student*p)  //学生选课
{
 if (p->IsFull())
 cout << "你的课程已经选满了" << endl;
 else
 {
 int num;
 if (ShowSub()==0)
  cout << "无课程可以选择" << endl;
 else
 {
  cout << "请输入你的选择" << endl;
  cin >> num;
  if (p->FindSub(num))
  cout << "此课程你已经选择" << endl;
  else if (FindSub(num)->IsFull())
  cout << "课程人数已满" << endl;
  else
  {
  p->AddSub(num);
  FindSub(num)->AddStu(p->GetNum());
  cout << "选课成功" << endl;
  }
 }
 }
 Update = true;
 system("pause");
}

void Manage::ShowStuSubInfo(Student*p)
{
 SubList*l = p->GetSubPtr()->next;
 cout << p->GetNum() << " " << p->GetName() << " 选课数" << p->GetFactSubNum() << endl;
 while (l)
 {
 Subject*s = FindSub(l->num);
 cout << "tt" << s->GetNum() << " " << s->GetName() << " " << s->GetCredit() << endl;
 float f = s->GetStuScore(p->GetNum());
 cout << "tt 成绩 " << f << endl;
 cout << "------------------------------" << endl;
 l = l->next;
 }
}

//管理端功能函数
void Manage::menu_2_1()  //增加学生 
{
 system("cls");
 int num;
 char name[20];

 cout << "nntttt增加学生操作n" << endl;

 cout << "nntt请输入学生学号:";
 cin >> num;

 if (FindStu(num))
 cout << "学生已经存在" << endl;
 else
 {
 cout << "nntt请输入学生姓名:";
 cin >> name;
 Student*p = new Student();
 p->SetNum(num);
 p->SetName(name);
 Student *root = StuRoot;
 while (root->next)
  root = root->next;
 root->next = p;
 cout << endl << "tt增加学生操作成功,按任意键继续" << endl;
 }
 system("pause");
}

void Manage::menu_2_2()  //增加课程 
{
 system("cls");
 int num;
 float credit;
 char name[20];
 cout << "nntttt增加课程操作n" << endl;
 cout << "nntt 请输入课程代号:";
 cin >> num;
 if (FindSub(num))
 cout << "ntt此课程已经存在,按任意键继续" << endl;
 else
 {
 cout << "nntt请输入课程名:";
 cin >> name;
 cout << "请输入课程学分" << endl;
 cin >> credit;
 Subject*p = new Subject();
 p->SetNum(num);
 p->SetName(name);
 p->SetCredit(credit);
 Subject *root = SubRoot;
 while (root->next)
  root = root->next;
 root->next = p;
 cout << endl << "tt增加课程操作成功,按任意键继续" << endl;
 }
 system("pause");
}

void Manage::menu_2_3() //删除学生 
{
 system("cls");
 cout << "nntttt删除学生操作" << endl;
 if (ShowStu()==0)
 cout << "无学生" << endl;
 else
 {
 int num;
 cout << "nt请输入要删除的学生代学号:";
 cin >> num;
 Student*p = FindStu(num);
 if (p == NULL)
  cout << "无此学生" << endl;
 else
 {
  DelStu(p);
  cout << "ntt删除学生操作成功,按任意键继续.." << endl;
 }
 }
 system("pause");
}

void Manage::menu_2_4()  //删除课程 
{
 system("cls");
 cout << "nntttt删除课程操作" << endl;
 if (ShowSub()==0)
 cout << "无课程" << endl;
 else
 {
 int num;
 cout << "nt请输入要删除的课程代号:";
 cin >> num;
 Subject*p = FindSub(num);
 if (p == NULL)
  cout << "无此课程" << endl;
 else
 {
  DelSub(p);
  cout << "ntt删除课程操作成功,按任意键继续.." << endl;
 }
 }
 system("pause");
}

void Manage::menu_2_5()  //填写成绩 
{
 system("cls");
 cout << "nntttt 填写成绩操作n" << endl;

 if (ShowSub()==0)
 cout << "nnntt对不起,暂时没有任何选修课程。请按任意键继续.." << endl;
 else
 {
 int num;
 cout << "请选择课程代号" << endl;
 cin >> num;
 Subject*p = FindSub(num);
 if (p == NULL)
  cerr << "ntt没有此课程!!请按任意键继续.." << endl;
 else
 {
  cout << p->GetNum() << " 课程名称" << p->GetName() << " 选课人数" << p->GetFactStuNum() << endl;
  int n;
  cout << "请输入1确认开始填写成绩,按其他键退出" << endl;
  cin >> n;
  if (n == 1)
  {
  StuList*q = p->GetStuPtr()->next;
  while (q)
  {
   Student*l = FindStu(q->num);
   cout << "nntt请填写" << q->num << " " << l->GetName() << "的学生成绩n" << endl;
   cin >> q->score;
   l->SetUpdate(true);
   q = q->next;
  }
  p->SetUpdate(true);
  }

 }
 }
 Update = true;
 system("pause");
}

void Manage::menu_2_6()  //更改学分 
{
 system("cls");

 cout << "nnntttt更改学分操作n" << endl;
 if (ShowSub()==0)
 cout << "nnntt对不起,暂时没有任何课程。请按任意键继续.." << endl;
 else
 {
 int num;
 cout << "请选择课程代号" << endl;
 cin >> num;
 Subject*p = FindSub(num);
 if (p == NULL)
  cout << "无此课程" << endl;
 else
 {
  float n;
  cout << "nttt原来学分为:" << p->GetCredit() << endl;
  cout << "nttt现要更改为:";
  cin >> n;
  p->SetCredit(n);

  StuList*q = p->GetStuPtr()->next;
  while (q)
  {
  FindStu(q->num)->SetUpdate(true);
  q = q->next;
  }

  cout << "ntt更改课程学分成功,按任意键继续" << endl;
 }
 }
 Update = true;
 system("pause");
}

void Manage::menu_2_7()
{
 Student *p = StuRoot->next;
 if (p == NULL)
 cout << "无记录" << endl;
 else
 while (p)
 {
  ShowStuSubInfo(p);
  p = p->next;
 }
 system("pause");
}

void Manage::menu_2_8()  //选课情况
{
 system("cls");
 cout << "nntttt选课情况操作" << endl;
 if (ShowSub()==0)
 cout << "nnntt对不起,暂时没有课程!!请按任意键继续.." << endl;
 else
 {
 int num;
 cout << "nt请输入要查看的课程代号:";
 cin >> num;

 Subject*p = FindSub(num);
 if (p == NULL)
  cout << "ntt无此课程!!t请按任意键继续.." << endl;
 else
 {
  cout << p->GetNum() << " " << p->GetName() << " " << p->GetCredit() << endl;
  cout << "tt最大人数:" << p->GetMaxStuNum() << " " << "实际人数" << p->GetFactStuNum() << endl;

  StuList*q = p->GetStuPtr()->next;
  while (q)
  {
  cout <num << " " << FindStu(q->num)->GetName() << " " << q->score << endl;
  q = q->next;
  }
 }
 }
 system("pause");
}

 
void Manage::menu_2_9() 
{
 Student*p = StuRoot->next;
 ofstream o("Student.txt",32);
 ofstream oo("SubList.txt", 32);
 while (p)
 {
 SubList*t=p->GetSubPtr()->next;
 o.write(reinterpret_cast(p), sizeof(*p));
 while (t)
 {
  oo.write(reinterpret_cast(t), sizeof(*t));
  t = t->next;
 }
 p = p->next;
 }
 o.close();
 oo.close();

 Subject*q = SubRoot->next;
 o.open("Subject.txt", 32);
 oo.open("StuList.txt", 32);

 while (q)
 {
 StuList*t = q->GetStuPtr()->next;
 o.write(reinterpret_cast(q), sizeof(*q));
 while (t)
 {
  oo.write(reinterpret_cast(t), sizeof(*t));
  t = t->next;
 }
 q = q->next;
 }
 o.close();
 oo.close();
 cout << "nnnttt保存数据成功!按任意键继续.." << endl;
 system("pause");
}

int main()
{
 Manage m;
 m.Start();
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号