关闭

链表法实现学生数据库管理(我是菜鸟,见笑了!)

531人阅读 评论(0) 收藏 举报

//功能:链表法实现学生数据库管理
# include <iostream>
# include <iomanip.h>         //调用setw()
# include <stdlib.h>          //调用exit()
# include <string.h>          //调用strlen()判断字符串长度
# define NULL 0
# define ERROR1 0
# define ERROR "/n/nWANNING!DATA ERROR.PLEASE INPUT AGAIN./n/n"
# define OK 1

typedef struct student
 {
  int num;            //学号
  char name[8];       //姓名
  int age;            //年龄
  int score;          //成绩
 }STU;  

typedef struct LNODE
{
  STU data;
  struct LNODE *next;
}Lnode,*Linklist;

void check(STU &e)
{
 do                                                     //保证输入数据的合法性
 {  
    cout<<"/nPLEASE INPUT THE THE STUDENT'S NUM(0000-9999):/n";
    cin>>e.num;
       if (e.num<0||e.num>9999||isdigit(e.num)==0)
    {
    cout<<ERROR;
    }
 }while (e.num<0||e.num>9999||isdigit(e.num)==0);
    do
 {                                                                       
    cout<<"/nPLEASE INPUT THE THE STUDENT'S NAME(NOT MORE THAN 8):/n";
       cin>>e.name;
    if (strlen(e.name)>8)
    {
    cout<<ERROR;
    }
 }while (strlen(e.name)>8);                                               
    do
 {                                                                    
    cout<<"/nPLEASE INPUT THE THE STUDENT'S AGE(000-100):/n";
    cin>>e.age;
       if (e.age<0||e.age>100)
    {
     cout<<ERROR;
    }
 }while (e.age<0||e.age>100);
    do
 {                                                                     
    cout<<"/nPLEASE INPUT THE THE STUDENT'S SCORE(000-100):/n";
    cin>>e.score;
       if (e.score<0||e.score>100)
    {
    cout<<ERROR;
    }
 }while (e.score<0||e.score>100);
}

int check1(int i,Linklist p)
{
   if ((p->data.num<0||p->data.num>9999)||(strlen(p->data.name)>8)
    ||(p->data.age<0||p->data.age>100)||(p->data.score<0||p->data.score>100))
   {
    cout<<ERROR;
    return 1;
   }
   else
   {
    return 0;
   }
}

//打印出数据  
void print(Linklist L)
{
   int i=1;
   Linklist p=L->next;  
   cout<<"/n/n/t/tTHESE "<<L->data.num<<" RECORDS ARE:"<<endl;  //输出数据数

   if (L->data.num)
   {
      cout<<"/n/t/tno./tnum/tname/tage/tscore/n";               //输出表头
   }
 
   while (p&&i<=L->data.num+1)                                    //输出数据
   {
      cout<<"/n/t/t"<<setw(3)<<i++;
      cout<<"/t"<<setw(4)<<p->data.num<<setw(9)<<p->data.name<<setw(6)<<p->data.age<<setw(8)<<p->data.score;
   p=p->next;
   }
   cout<<endl;
}

//功能:将数据插入到指定点之前
int listinsert(Linklist L,int i,STU e)
{
    Linklist p=L;
 Linklist s;

 s=(Linklist)malloc(sizeof(Lnode));
 if (!s)
 {
  return ERROR1;
 }
 s->data=e;

 while (p&&i-1)                                            //指向插入点之前
 {
  p=p->next;
  i--;
 }

 s->next=p->next;                                          //插入s
 p->next=s;
 
 ++L->data.num;
 return OK;
}

//功能:删除指定数据
void listdelete(Linklist L,int i)
{
  Linklist p=L;                                

  while (p&&i-1)                                            //指向插入点之前
 {
  p=p->next;
  i--;
 }

  p->next=p->next->next;                                //删除节点
  --L->data.num;
}

//功能:修改指定数据
void ListRevise(Linklist L,int i)
{  
 int j=0;
 Linklist p=L->next;
 while (p&&i-1)                                            //指向插入点
 {
  p=p->next;
  i--;
 }
    //输出要修改的数据
 cout<<"/t********************************************************/n";
 cout<<"/t/tnum/tname/tage/tscore/n";
 cout<<"/t/t"<<setw(4)<<p->data.num<<setw(9)<<p->data.name<<setw(6)<<p->data.age<<setw(8)<<p->data.score;
    cout<<"/n/t********************************************************/n";
    do                                                           //输出子菜单
 {
       cout<<"/nWHICH DATA DO YOU WANT TO CHANGE:/n ";
    cout<<"/t                        REVISE                          /n";
    cout<<"/t********************************************************/n";
    cout<<"/t/t     1.SUM                       2.NAME/n";
       cout<<"/t/t     3.AGE                       4.SCORE/n";
       cout<<"/t********************************************************/n";
       cin>>i;
       if (i<0||i>4)
    {
    cout<<ERROR;
    }
 }while (i<0||i>4);

 switch (i)                                           //修改数据                            
 {
        case 1:
  {
     do
     {
            cout<<"/nPlease input the new num: ";
            cin>>p->data.num;
     }while (check1(i,p));
      break;
  }
  case 2:
  {
           do
     {
      cout<<"/nPlease input the new name: ";
            cin>>p->data.name;
     }while (check1(i,p));
     break;
  }
        case 3:
  {
           do
     {
            cout<<"/nPlease input the new sorce: ";
            cin>>p->data.age;
     }while (check1(i,p));
     break;
  }
        case 4:
  {
           do
     {
           cout<<"/nPlease input the new sorce: ";
           cin>>p->data.score;
     }while (check1(i,p));
     break;
  }
 }
}

//功能:打印菜单
int printmanu()
{
   int i=0;                                              //选项

   do                                                    //保证选择有效
   {
  cout<<"/t                        MAUN                            /n";
  cout<<"/t********************************************************/n";
  cout<<"/t/t1.INSERT DATA                    2.DELETE DATA/n";
     cout<<"/t/t3.CHANGE DATA                    4.PRINT  DATA/n";
  cout<<"/t/t5.EXIT/n";
     cout<<"/t********************************************************/n";
     cout<<"/n/tPLEASE CHOOSE THE NO.(1-5)/n";
  cin>>i;
  if (i<1||i>5)
  {
   cout<<ERROR;
  }
 }while (i<1||i>5);                                 

   return(i);
}

int main()
{
  int i;
  STU e;
  Linklist L;

  L=(Linklist)malloc(sizeof(Lnode));                  //创建空链表
  if (!L)
  {
   return ERROR1;
  }
  L->data.num=0;
  L->next=NULL;


  e.num=1001;                                         //初始化数据
  strcpy(e.name,"mobin");                           
  e.age=20;                                          
  e.score=87;
  listinsert(L,1,e);
 
  e.num=1002;
  strcpy(e.name,"woniu");
  e.age=20;
  e.score=92; 
  listinsert(L,2,e);
 
  do
  {
    i=printmanu();                                    //打印菜单并取得选择项
    switch (i)
 {
       case 1:
     {
           print(L);
              do
     {
         cout<<"/nPLEASE INPUT THE PODETION WHICH YOU WANT TO INSERT:/n ";
                  cin>>i;
      if (i<1||i>L->data.num+1)
      {
       cout<<ERROR;
      }
     }while (i<1||i>L->data.num+1);
              cout<<"/n/n/t/tPLEASE INPUT THE STUDENT'S DATA./n";

     check(e);

              listinsert(L,i,e);                      //插入数据

              cout<<"/n/nINSERT SUCCESSFUL!/n";
              cout<<"/n/t/tnum/tname/tage/tscore/n";
              cout<<"/t"<<setw(4)<<e.num<<setw(9)<<e.name<<setw(6)<<e.age<<setw(8)<<e.score;
              break;
     }
     
       case 2:
     {
              print(L);
              do
     {
         cout<<"/nPLEASE CHOOSE THE NO. WHICH YOU WANT TO DELETE:/n ";
                  cin>>i;
      if (i<1||i>L->data.num)
      {
       cout<<ERROR;
      }
     }while (i<1||i>L->data.num);
  
              listdelete(L,i);                                            //删除数据

              cout<<endl;
              print(L);
              break;
     }

       case 3:
     {
              do
     {
      print(L);
                  cout<<"/nWHICH NO. DO YOU WANT TO REVISE:/n ";
                  cin>>i;
      if (i<0||i>L->data.num)
      {
       cout<<ERROR;
      }
     }while (i<1||i>L->data.num);

     ListRevise(L,i);                                             //修改数据

     print(L);
     break;
     }

       case 4:
     {
      print(L);
               break;
     }
    default:                                        //结束程序
     {
      exit(0);
     }
   }   
   }while(i=5);
  return OK;
}
      

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:763次
    • 积分:19
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档