当前位置:编程学习 > C/C++ >>

C++学生选修课程系统

用C++编写学生选修课程系统

假定有n门课程,每门课程有课程编号,课程名称,课程性质,总学时,授课学时,实验或上机学时,学分,开课学期等信息,学生可按要求(如总学分不得少于60)自由选课。设计一选修课程系统,使之能提供以下功能:

1、系统以菜单方式工作

2、课程信息录入功能(课程信息用文件保存)

3、课程信息浏览功能

4、查询功能:(至少一种查询方式)

1)按学分查询

       2)按课程性质查询

5、学生选修课程

答案:#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

struct Course
{
 string num;  //课程编号
 string name; //课程名称
 string type; //课程性质
 int period;  //总学时
 int classPeriod; //授课学时
 int expPeriod;  //实验学时或上机学时
 int credit;  //学分
 string beginDate; //开课时间
 Course *next;
};

void InitList (Course *&L)
{
 L = new Course;
 L->next = NULL;
}

ostream &operator << (ostream &output, Course *&c)
{
 output << c->num << '\t' << c->name << '\t' << c->type << '\t' << c->period
  << '\t' << c->classPeriod << '\t' << c->expPeriod << '\t' << c->credit
  << '\t' << c->beginDate << endl;
 return output;
}

istream &operator >> (istream &input, Course *&c)
{
 //cout << "开始输入……" << endl;
 input >> c->num >> c->name >> c->type >> c->period
  >> c->classPeriod >> c->expPeriod >> c->credit >> c->beginDate;
 return input;
}

void SaveInfo (Course *L)
{
 Course *p = L->next;
 ofstream outfile ("course.txt", ios::out);
 if (!outfile)
 {
  cerr << "open error!" << endl;
  exit (1);
 }
 while (p != NULL)
 {
  outfile << p;
  p = p->next;
 }
 outfile.close ();
}

void ReadInfo (Course *&L)
{
 
 Course *r = L, *s;
 ifstream infile ("course.txt", ios::in);
 if (!infile)
 {
  cerr << "open error!" << endl;
  exit (1);
 }
 while (!infile.eof ())
 {
  s = new Course;
  infile >> s;
  if (!infile.eof ())
  {
   r->next = s;
   r = s;
  }
 }
 r->next = NULL;
 infile.close ();
}

void AddInfo (Course *L)
{
 system ("cls");
 InitList (L);
 Course *s;
 s = new Course;

 cout << "请输入以下信息:" << endl;

 cout << "课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期" << endl;
 cin >> s;
 //cout << "输入完毕……" << endl;

 cout << "是否保存信息?\n(1)是\n(2)否\n" << endl;

 ofstream outfile ("course.txt", ios::app);
 if (!outfile)
 {
  outfile.close ();
  ofstream outfile ("course.txt",ios::out);
 }
 outfile.close ();

 ReadInfo (L);
 switch (getch ())
 {
  case '1':
   s->next = L->next;
   L->next = s;
   SaveInfo (L);
   cout << "保存成功!" << endl;
   break;
  case '2':
   break;
  default:
   break;
 }
 system ("pause");
}

void DeleteInfo (Course *L)
{
 system("cls");
 InitList (L);
 ReadInfo (L);
 Course *p = L, *q = p->next;
 cout << "请输入课程编号:";
 string n;
 cin >> n;

 while ((q != NULL) && (q->num != n))
 {
  p = q;
  q = q->next;
 }
 if (q == NULL)
  cout << "无此编号的课程!" << endl;
 else
 {
  cout << "该课程信息如下:" << endl;
  cout << q << endl;
  cout << "确认删除该课程信息?\n(1)确认\n(2)取消\n" << endl;
  switch (getch ())
  {
   case '1':
    p->next = q->next;
    delete q;
    SaveInfo (L);
    cout << "删除成功!" << endl;
    break;
   case '2':
    break;
   default:
    break;
  }
 }
 system ("pause");
}
void SearchByCredit (Course *L)
{
 system ("cls");
 InitList (L);
 ReadInfo (L);
 Course *p = L->next;

 cout << "请输入学分:";
 int c;
 cin >> c;

 bool flag = false;
 while (p != NULL)
 {
  if (p->credit == c)
  {
   flag = true;
   cout << p;
  }
  p = p->next;
 }
 if (flag == false)
  cout << "没有此学分的课程!" << endl << endl;
 system ("pause");
}

void SearchByType (Course *L)
{
 system ("cls");
 InitList (L);
 ReadInfo (L);
 Course *p = L->next;
 cout << "请输入课程性质:";
 string t;
 cin >> t;

 bool flag = false;
 while (p != NULL)
 {
  if (p->type == t)
  {
   cout << p;
   flag = true;
  }
  p = p->next;
 }
 if (flag == false)
  cout << "无此性质的课程!" << endl << endl;
 system ("pause");
}

void DispInfo (Course *L)
{
 system ("cls");
 InitList (L);
 ReadInfo (L);
 Course *p = L->next;
 while (p != NULL)
 {
  cout << p;
  p = p->next;
 }
 cout << endl << endl;
 system ("pause");
}

void Search ()
{
 Course *l;
 InitList (l);
 while (1)
 {
  system ("cls");
  cout << endl << endl << endl;
  cout << "\t*************************************************" << endl;
  cout << "\t*                                               *" << endl;
  cout << "\t*                请选择查询方式                 *" << endl;
  cout << "\t*                (1)按学分查询                  *" << endl;
  cout << "\t*                (2)按性质查询                  *" << endl;
  cout << "\t*                (3)   返回                     *" << endl;
 

上一个:帮忙把C++程序改改
下一个:C语言和C++哪个更简单

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,