#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#include"conio.h"

#define PAGE 3

#define MAX 1000

#define N 5

int k=0;

  /*结构体类型*/

typedef struct 

{   int num;/*图书编号*/

    char name[20];/*书名*/

         char sex[5];/*图书类型*/

         int age;/*价格*/

         char studentclass[20];/*图书分组*/

         int score;/*数量*/

}STUDENTS;

 

int read_file(STUDENTS stu[])

{        FILE *fp;

    int i=0;

         if((fp=fopen("stu.txt","rt"))==NULL)

         {printf("\n\n*****库存文件不存在!请创");

          return 0;

         }

         while(feof(fp)!=1)

    {

         fread(&stu[i],sizeof(STUDENTS),1,fp);

         if(stu[i].num==0)

                   break;

         else

                   i++;

         }

         fclose(fp);

         return i;

}

void save_file(STUDENTS stu[],int sum)

{FILE*fp;

 int i;

 if((fp=fopen("stu.txt","wb"))==NULL)

 {printf("写文件错误!\n");

  return;

 }

   for(i=0;i<sum;i++)

            if(fwrite(&stu[i],sizeof(STUDENTS),1,fp)!=1)

                      printf("写文件错误!\n");

            fclose(fp);

}

 

 

/*创建图书信息*/

int input(STUDENTS stu[])

{  int i,x;

   for(i=0;i<1000;i++)

   {

    system("cls"); 

    printf("\n\n                 录入图书信息  (最多%d)\n",MAX);

    printf("               ----------------------------\n");

  

        printf("\n                     %d个学生",k+1);

             printf("\n 请输入图书的编号:");

        scanf("%d",&stu[k].num);

        printf("\n 请输入图书的名字:");

        scanf("%s",stu[k].name);

                printf("\n 请输入图书的类型:");

        scanf("%s",stu[k].sex);

        printf("\n 请输入图书的价格:");  

        scanf("%d",&stu[k].age); 

        printf("\n 请输入图书的分组:");

        scanf("%s",stu[k].studentclass);       

        printf("\n 请输入图书的数量:");

        scanf("%d",&stu[k++].score);

        printf("\n 请按1键返回菜单或按0键继续创建");

                   scanf("%d",&x);

                   if(x)

                     break;

   }

     

       return k;

}

 

 

/*删除学生信息*/

void deletel(STUDENTS stu[])

  { system("cls");

   char Stuname2[20];

   int i,j;

   printf("请输入书名:");

   scanf("%s",Stuname2);

   printf("\n");

   for(i=0;i<k;i++)

   if(strcmp(stu[i].name,Stuname2)==0)  

     for(j=0;j<20;j++)

       stu[i].name[j]=stu[i+1].name[j];

   k--;

        

 

   printf("删除成功\n");

   printf("按任意键加回车返回主菜单!");

   scanf("%d",&i);

   getchar();

}

 

/*打印学生信息*/                                                                                                                                                                                                                                                        

void output(STUDENTS stu[])

{  system("cls");

   int i;

   for(i=0;i<k;i++) 

     printf("编号:%d,书名:%s,类型:%s,价格:%d,分组:%s,数量: %d\n",stu[i].num,stu[i].name,

                     stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

   printf("按任意键加回车返回主菜单!");

   scanf("%d",&i);

   getchar();

}

 

/*查询学生信息*/

void inquire(STUDENTS stu[])

  { int i;

    int num;

    system("cls");

    printf("     \n\n请输入您要查找的图书的编号");

    scanf("%d",&num);

    for(i=0;i<k;i++)

      if(num==stu[i].num)

         printf("\n\n\n书名:%d,书名:%s,类型:%s,价格:%d,分组:%s,数量: %d\n",stu[i].num,stu[i].name,

                    stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

    printf("按任意键加回车返回主菜单!");

         scanf("%d",&i);

    getchar();

  }

 

 

/*修改学生信息*/

void change(STUDENTS stu[])

  {  int num,i,choice;

     system("cls");

          printf("\n\n\n      请输入您要修改的图书的编号");

     scanf("%d",&num);

     for(i=0;i<k;i++)

     { if(num==stu[i].num)

         printf("\n编号:%d,书名:%s,类型:%s,价格:%d,分组:%s,数量: %d\n",stu[i].num,stu[i].name,

                    stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

 

     printf("\n\n\n     ********请输入您想要修改的数据********\n\n");

     printf("                1. 编号\n\n");

     printf("                2. 书名\n\n");

          printf("                3. 类型\n\n");

     printf("                4. 价格\n\n");

     printf("                5. 分组\n\n");

     printf("                6. 数量\n\n");

     printf("                 请选择(1-6:");

           scanf("%d",&choice);

           switch(choice)

           {case 1:{

                          printf("\n   请输入你改的新编号");

                    scanf("%d",&stu[i].num);

                               break;

                               }

            case 2:{

                          printf("\n   请输入你改的新书名");

                    scanf("%s",stu[i].name);

                               break;

                              }

            case 3:{

                          printf("\n   请输入你改的新类型");

                    scanf("%s",stu[i].sex);

                               break;

                              }

            case 4:{

                          printf("\n   请输入你改的新价格");

                    scanf("%d",&stu[i].age);

                               break;

                              }

            case 5:{

                          printf("\n   请输入你改的新分组");

                    scanf("%s",stu[i].studentclass);

                               break;

                              }

            case 6:{

                          printf("\n   请输入你改的新数量");

                    scanf("%d",&stu[i].score);

                               break;

                              }

           }

          

            printf("编号:%d,书名:%s,类型:%s,价格:%d,分组:%s,价格: %d\n",stu[i].num,stu[i].name,

                    stu[i].sex,stu[i].age,stu[i].studentclass,stu[i].score);

       printf("按任意键加回车返回主菜单!");

            scanf("%d",&i);

            break;

          }

}

 

 

/*学生成绩信息排名*/

void sort(STUDENTS stu[])

  {  int i,j,n=1,x;

     system("cls");

     int t;

      for(i=0;i<k-1;i++)

                   for(j=i+1;j<k;j++)

                            if(stu[i].score<stu[j].score)

                            { t=stu[i].score;

                              stu[i].score=stu[j].score;

                              stu[j].score=t;

              t=stu[i].num;

                              stu[i].num=stu[j].num;

                              stu[j].num=t;

 

                            }

     for(i=0;i<k;i++)

                   printf("排名    编号    数量\n %d       %d       %d\n",n++,stu[i].num,stu[i].score);

     printf("按任意键加回车返回主菜单!");

          scanf("%d",&x);

     getchar();

  }

 

void pagedis()

{

   printf(" \n\n\n                   **********************************\n");

   printf("                   *                                *\n");

   printf("                   *                                *\n");

   printf("                   *                                *\n");

   printf("                   *     欢迎进入图书信息管理系统   *\n");

   printf("                   *                                *\n");

   printf("                   *                                *\n");

   printf("                   *                                *\n");

   printf("                   **********************************\n");

  

}

void check()

{

   char userName[5];/*书名*/

   char userPWD[5];/*密码*/

   int i,sum;

   system("color 4E");

   for(i = 1; i < 4; i++)

   {

      /*用户名和密码均为a;*/

             printf("   用户名和密码均为a\n\n");

        printf("\n       请输入您的用户名:");

        gets(userName);

       

        printf("\n       请输入您的密码:");

        gets(userPWD);

       

        if ((strcmp(userName,"a")==0) && (strcmp(userPWD,"a")==0))/*验证用户名和密码*/

        {

            printf("用户名和密码正确,显示主菜单");

            return;

        }

        else

        {

            if (i < 3)

            {

                printf("用户名或密码错误,提示用户重新输入");

                printf("用户名或密码错误,请重新输入!");

            }

            else

            {

                printf("连续3次输错用户名或密码,退出系统。");

                printf("您已连续3次将用户名或密码输错,系统将退出!");

                exit(1);

            }

        }

   }

}

void menu()

{

  STUDENTS stu[20];

  int choice,k,sum;

  sum=read_file(stu);

  if(sum==0)

   {  printf("首先录入基本库存信息!按回车后进入*****\n");

      getch();

           sum=input(stu);

   }

 

  do

  {  system("cls"); 

     printf("\n\n\n               ********学生信息管理系统********\n\n");

     printf("                      1. 创建图书信息\n\n");

     printf("                      2. 打印图书信息\n\n");

          printf("                      3. 查询图书信息\n\n");

     printf("                      4. 修改图书信息\n\n");

     printf("                      5. 删除图书信息\n\n");

     printf("                      6. 图书信息数量\n\n");

     printf("                      0. 退出系统\n\n");

     printf("                       请选择(0-6:");

     scanf("%d",&choice);

    switch(choice)

    {

      case 1: k=input(stu); break;/*创建图书信息*/

      case 2: output( stu) ; break;/*打印图书信息*/

      case 3: inquire(stu); break;/*查询图书信息*/

      case 4: change(stu); break;/*修改图书信息*/

      case 5: deletel(stu); break;/*删除图书信息*/

      case 6: sort(stu); break;/*图书信息数量*/

      case 0: break;

    }

  }while(choice!=0);

  save_file(stu,sum);

}

int main()

  

 

  int i,sum;

  pagedis();

  check();

  menu();  

 

}