#include <iostream>
#include <fstream>
#define FILENAME "data.txt"
using namespace std;
struct Teacher
{
 char no[11]; /*编号*/
 char name[15]; /*姓名*/
 char sex[5]; /*性别*/
 char profess[10]; /*职称*/
 char dept[15]; /*部门*/
 char leason[15]; /*课程*/
 float workload; /*工作量*/
 float lessonf;/*代课费*/
 Teacher *next; /*指向后续结点的指针*/
};
Teacher *f, *l;
void printMenu();
void addNode();
void searchNode();
void deleteNode();
void listNode();
void printNode(Teacher *no);
void saveNode();
void loadNode();
void main()
{
 int job;
 bool cirFlag = true;
 f = l = NULL;
 loadNode();
 while (cirFlag)
 {
  printMenu();
  cin>>job;
  cout<<"---------------"<<endl;
  switch (job)
  {
   case 1:
    addNode();
    break;
   case 2:
    searchNode();
    break;
   case 3:
    deleteNode();
    break;
   case 4:
    listNode();
    break;
   case 5:
    saveNode();
    break;
   case 6:
    cirFlag = false;
    break;
  }
 }
}
void printMenu()
{
 cout<<"1. 创建"<<endl
     <<"2. 查找"<<endl
     <<"3. 删除"<<endl
     <<"4. 显示所有"<<endl
     <<"5. 保存"<<endl
     <<"6. 退出"<<endl
     <<"选择:";
}
void addNode()
{
 Teacher *no;
 no = new Teacher;
 cout<<"编号:";
 cin>>no->no;
 cout<<"姓名:";
 cin>>no->name;
 cout<<"性别:";
 cin>>no->sex;
 cout<<"职称:";
 cin>>no->profess;
 cout<<"部门:";
 cin>>no->dept;
 cout<<"课程:";
 cin>>no->leason;
 cout<<"工作量:";
 cin>>no->workload;
 cout<<"代课费:";
 cin>>no->lessonf;
 no->next = NULL;
 if (f)
 {
  l = l->next = no;
 } else f = l = no;
}
void searchNode()
{
 Teacher *no;
 char sno[11];
 int count = 5;
 bool searchFlag = false;
 cout<<"请输入要查找的教师编号:";
 cin>>sno;
 no = f;
 while (no)
 {
  if (strcmp(no->no, sno) == 0)
  {
   searchFlag = true;
   printNode(no);
   count--;
   if (!count)
   {
    system("pause");
    count = 5;
    cout<<"---------------"<<endl;
   }
  }
  no = no->next;
 }
 if (!searchFlag)
  cout<<"没有找到记录"<<endl;
}
void deleteNode()
{
 Teacher *no, *pre;
 char sno[11];
 int count = 0;
 cout<<"请输入要删除的教师编号:";
 cin>>sno;
 no = pre = f;
 while (no)
 {
  if (strcmp(no->no, sno) == 0)
  {
   if (no == f)
    f = no->next;
   pre->next = no->next;
   count++;
  }
  pre = no;
  no = no->next;
 }
 cout<<"共删除 "<<count<<" 条记录"<<endl;
}
void listNode()
{
 if (!f)
 {
  cout<<"没有找到记录。"<<endl;
  return;
 }
 int count = 5;
 Teacher *no;
 no = f;
 while (no)
 {
  printNode(no);
  count--;
  if (!count)
  {
   system("pause");
   count = 5;
   cout<<"---------------"<<endl;
  }
  no = no->next;
 }
}
void printNode(Teacher *no)
{
 cout<<"编号:"<<no->no<<endl
     <<"姓名:"<<no->name<<endl
     <<"性别:"<<no->sex<<endl
     <<"职称:"<<no->profess<<endl
     <<"部门:"<<no->dept<<endl
     <<"课程:"<<no->leason<<endl
     <<"工作量:"<<no->workload<<endl
     <<"代课费:"<<no->lessonf<<endl
     <<"---------------"<<endl;
}
void saveNode()
{
 fstream fs(FILENAME, ios::out);
 Teacher *no;
 no = f;
 while (no)
 {
  fs<<no->no<<endl
    <<no->name<<endl
    <<no->sex<<endl
    <<no->profess<<endl
    <<no->dept<<endl
    <<no->leason<<endl
    <<no->workload<<endl
    <<no->lessonf<<endl;
  no = no->next;
 }
 fs.close();
}
void loadNode()
{
 fstream fs(FILENAME, ios::in);
 Teacher *no;
 int count = 0;
 no = new Teacher;
 while (fs>>no->no)
 {
  count++;
  fs>>no->name
    >>no->sex
    >>no->profess
    >>no->dept
    >>no->leason
    >>no->workload
    >>no->lessonf;
  no->next = NULL;
  if (f)
  {
   l = l->next = no;
  } else f = l = no;
  no = new Teacher;
 }
 delete no;
 fs.close();
 cout<<"共加载 "<<count<<" 条记录"<<endl
     <<"---------------"<<endl;
}

 
											





