我是靠谱客的博主 辛勤悟空,最近开发中收集的这篇文章主要介绍C/C++实现图书信息管理系统,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本文实例为大家分享了c/c++实现图书信息管理系统的具体代码,供大家参考,具体内容如下

程序流程图

源代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define N 100

struct type{
 char ISBN[N];//ISBN编号(一般为13位) 
 char bookName[N]; //书名 
 char subject[N];//科目 
 char editor[N];//主编 
 float price;//价格 
 char publish[N];//出版社
 char date[N];//日期 
 int num;//数量 
 struct type *next; 
}Book;
typedef struct type * BooksManage;

void color(short x);//字体颜色 
int password();//管理员密码
void AdministratorMenu();//管理员菜单 
void VisitorMenu();//游客菜单
void SaveBooksdata(BooksManage head);//将数据保存到文件中
void AddBooks(BooksManage *head);//添加图书信息
void ReadBooksdata(BooksManage *head);//将数据从文件中读取 
void DelBooks_Subject(BooksManage head);//按所属学科删除图书信息
void DelBooks_ISBN(BooksManage head);//按ISBN删除图书信息 
void ModifyBooks(BooksManage head);//修改图书信息
void FindBooks(BooksManage head);//查询图书信息
void PrintBooks(BooksManage head);//显示所有图书信息 
void SumBooks(BooksManage head);//求全部图书价值 
int main()
{
 color(10); printf("ttttt* * * * * * * * * * * * * *n");
 color(14); printf("ttttt* 题目:图书信息管理系统  *n");
 color(11); printf("ttttt* 班级:                  *n");
 color(12); printf("ttttt* 组员                    *n");
 color(11); printf("ttttt* 姓名  学号              *n");
 color(10); printf("ttttt* 张三   100001           *n");
 color(11); printf("ttttt* 李四   100002           *n");
 color(14); printf("ttttt* 王五   100003           *n");
 color(12); printf("ttttt* 日期 : 2019.03.19      *n");
 color(10); printf("ttttt* * * * * * * * * * * * * *n");
 system("title 图书信息管理系统");
 color(10);
 BooksManage head = NULL;
 ReadBooksdata(&head);
 int choice,z,c;
 printf("nnn");
 printf("ttt※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
 printf("tttttt欢迎光临图书信息管理系统n");
 printf("ttt--------------------------------------------------------n");
 printf("tttttt【1】游客n");
 printf("tttttt【2】管理员n");
 printf("tttttt【3】退出系统n");
 printf("ttt--------------------------------------------------------n");
 while(printf("tttttt请选择身份:")&&scanf("%d",&c)!=EOF)
 {
  if(c==1)
  {
   VisitorMenu();
   while (printf("tttttt请输入选择项(1-3):")&&scanf("%d",&choice)!=EOF)
      {
       switch (choice)
       {
        case 1:                                                /*查询图书信息*/
         FindBooks(head);
         break;
     case 2:                                                /*显示所有图书信息*/
                  PrintBooks(head);
         break; 
     case 3:                                                /*返回登录界面*/
      main();
      break;
     case 4:
      exit(0); 
        default:
         printf("ttttt输入错误!!!n");
         system("pause");
         break;
    }
    VisitorMenu();
   }
  }
  else if(c==2)
  {
   z = password();
   AdministratorMenu();
   while (printf("t请输入选择项(1-8):") && z == 1 && scanf("%d",&choice) != EOF)
      {
       switch (choice)
       {
        case 1:                                                /*录入图书信息*/
         AddBooks(&head);
         break;  
        case 2:                                                /*删除图书信息*/
         int sign;
         system("cls");
         color(10);printf("ttt* * * * * * * * * * * * * * *n");
         color(11);printf("ttt*---------------------------*n");
         color(12);printf("ttt*  输入选择删除图书的方式   *n");
         color(14);printf("ttt*---------------------------*n");
         color(11);printf("ttt*【 1】按所属学科删除图书   *n");
         color(12);printf("ttt*---------------------------*n");
         color(14);printf("ttt*【 2】按图书ISBN号删除图书 *n");
         color(11);printf("ttt*---------------------------*n");
         color(10);printf("ttt* * * * * * * * * * * * * * *n");
         scanf("ttt%d",&sign);
         if(sign == 1)
          DelBooks_Subject(head);
         else if(sign == 2)
       DelBooks_ISBN(head);
         break;
        case 3:                                               
         ModifyBooks(head);
          break;
        case 4:                                                
         FindBooks(head);
         break;
     case 5:                                                
                  PrintBooks(head);
         break;
        case 6:                                                
                  SumBooks(head);
         break; 
        case 7:                                                
      main();
      break; 
     case 8:                                                
         SaveBooksdata(head);
      exit(0);
        default:
         printf("t输入错误!!!n");
         system("pause");
         break;
    }
    AdministratorMenu();
   }
  }
  else if(c==3)
   exit(0);
  else
  {
   printf("ttt输入错误!!!nn");
  }
 }
 return 0;
}
 
/**管理员密码*/ 
int password()
{
 int i=0;
 char user[10]="admin";
 char Code[10]="admin";
 char getuser[10];
 char Getskey[10];
 
 while(1)
 {
  printf("ttt请输入账号:");
  scanf("%s",getuser);
  printf("ttt请输入密码:");
  while(1)
  {
   Getskey[i]=getch();       /*getch()函数在头文件conio.h中,函数作用为输入的内容不会在屏幕上显示*/
   if(Getskey[i]=='r')        /* b = 退格键(backspace)*/
    break;     /* r = 回车键(carrige return)*/
   else if(Getskey[i]=='b')
   {
    if(i==0)
     continue;
    printf("b");
    printf(" ");
    printf("b");
    i--;
   }
   else
   {
    printf("*");
    i++;
   }
  }
  Getskey[i]='';
  if(strcmp(user, getuser) == 0 && strcmp(Code, Getskey) == 0){
   printf("nnnttt密码正确,请稍等");
   printf(".");
   Sleep(300);
   printf(".");
   Sleep(300);
   printf(".");
   Sleep(300);
   printf(".");
   Sleep(300);
   printf(".");
   Sleep(300);
   printf(".");
   Sleep(300);
   return 1;
  }
   
  else
  {
   printf("nnttt账号不存在或密码输入错误,请重新输入n");
   i = 0;
  }
 }
}

/**管理员菜单*/ 
void AdministratorMenu()                                                
{
 system("cls");//清屏 
 printf("nnnnn");
 printf("t************************************************************n");
    printf("t*                                                          *n");
    printf("t*                 欢迎来到图书信息管理系统                 *n");
    printf("t*                                                          *n");
    printf("t*                                                          *n");
 printf("t*                          主菜单                          *n");
 printf("t*                                                          *n");
 printf("t*                     1.录入图书信息                       *n");
    printf("t*                                                          *n");
 printf("t*                     2.删除图书信息                       *n");          
 printf("t*                                                          *n");
 printf("t*                     3.修改图书信息                       *n");
 printf("t*                                                          *n");
 printf("t*                     4.查询图书信息                       *n");
 printf("t*                                                          *n");
 printf("t*                     5.显示所有图书信息                   *n");        
    printf("t*                                                          *n");
    printf("t*                     6.全部图书的总价值                   *n");
    printf("t*                                                          *n");
    printf("t*                     7.返回登录界面                       *n");
    printf("t*                                                          *n");
 printf("t*                     8.保存数据并退出系统                 *n"); 
    printf("t*                                                          *n");
    printf("t************************************************************nn");
}


/**游客菜单*/ 
void VisitorMenu()                                                 
{
 system("cls");
 printf("nnnnn");
 printf("t************************************************************n");
    printf("t*                                                          *n");
    printf("t*                 欢迎来到图书信息管理系统                 *n");
    printf("t*                                                          *n");
    printf("t*                                                          *n");
 printf("t*                          主菜单                          *n");
 printf("t*                                                          *n");
 printf("t*                     1.查询图书信息                       *n");
 printf("t*                                                          *n");
 printf("t*                     2.显示所有图书信息                   *n");        
    printf("t*                                                          *n");
    printf("t*                     3.返回登录界面                       *n");
    printf("t*                                                          *n");
 printf("t*                     4.退出系统                           *n"); 
    printf("t*                                                          *n");
    printf("t************************************************************nn");
}

/*将数据保存到文件中*/ 
void SaveBooksdata(BooksManage head)                                    
{
 BooksManage p;
 FILE *fp;
 fp = fopen("BooksDatabase.txt","w");
 p = head->next;
 while(p->next != NULL){
  fprintf(fp,"%st",p->ISBN);
  fprintf(fp,"%st",p->bookName);         
  fprintf(fp,"%st",p->subject);          
  fprintf(fp,"%st",p->editor);
  fprintf(fp,"%.2ft",p->price);
  fprintf(fp,"%st",p->publish);
  fprintf(fp,"%st",p->date);
  fprintf(fp,"%dn",p->num);
     p=p->next;                
 }
 if(p->next == NULL){
  fprintf(fp,"%st",p->ISBN);
  fprintf(fp,"%st",p->bookName);         
  fprintf(fp,"%st",p->subject);          
  fprintf(fp,"%st",p->editor);
  fprintf(fp,"%.2ft",p->price);
  fprintf(fp,"%st",p->publish);
  fprintf(fp,"%st",p->date);
  fprintf(fp,"%d",p->num);
 } 
 fclose(fp);
 return ;
}


/**将数据从文件中读取*/  
void ReadBooksdata(BooksManage *head)                                   
{
        int n;
    BooksManage s, p;
    FILE *fp;
    
    fp=fopen("BooksDatabase.txt","r");

    if(NULL == fp) { 
        head = NULL; 
        return ;
    } 
     
    *head = (BooksManage)calloc(1, sizeof(Book));
    p = *head; 
     
   while (!feof(fp))
 {
        s = (BooksManage)calloc(1, sizeof(Book));
  fscanf(fp,"%s",s->ISBN);              /*为下个结点分配内存空间 */
  fscanf(fp,"%s",s->bookName);
  fscanf(fp,"%s",s->subject);
  fscanf(fp,"%s",s->editor);
  fscanf(fp,"%f",&s->price);
  fscanf(fp,"%s",s->publish);
  fscanf(fp,"%s",s->date);
  fscanf(fp,"%d",&s->num);
  p->next=s;   /*保存该结点*/
  p=p->next;   /*切换到下一个结点*/
  p->next=NULL; /*保证最后一个结点为NULL*/
 }
     
    fclose(fp);/*关闭文件 */
    return ; 
}

/**字体颜色*/ 
void color(short x)
{
    if(x>=0 && x<=15)
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    else
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

/**添加图书信息*/ 
void AddBooks(BooksManage *head)                                   
{
 int i;
 BooksManage p, s;
 system("cls");
 if(*head == NULL) {
        *head = (BooksManage)calloc(1,sizeof(Book));
        p = *head;
    } else {                            
        p = *head;
        while(p->next) 
        p = p->next; 
    }
    printf("nnn");
 printf("t※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
    printf("ttt请问添加几本书:"); 
    scanf("%d",&i);
    printf("t--------------------------------------------------------n");
    while(i)
    {
     s = (BooksManage)calloc(1,sizeof(Book));
     printf("ttt请输入ISBN号:");
  scanf("%s",s->ISBN);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入书名:");
  scanf("%s",s->bookName);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入书本的所属学科:");
  scanf("%s",s->subject);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入主编:");
  scanf("%s",s->editor);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入价格:");
  scanf("%f",&s->price);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入出版社:");
  scanf("%s",s->publish);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入出版日期:");
  scanf("%s",s->date);
  printf("t--------------------------------------------------------n");
  printf("ttt请输入数量:");
  scanf("%d",&s->num);
  printf("t--------------------------------------------------------n");
  printf("n");
  printf("t--------------------------------------------------------n");
  i--;
   
  p->next = s;    /*保存输进去的这个结点*/
  p = p->next;    /*切换到下一个结点*/ 
  p->next = NULL; /*最后一个结点为NULL*/
 }
         
    printf("ttt图书添加成功!!!n");
 system("pause");
}

/*按所属学科删除图书信息*/ 
void DelBooks_Subject(BooksManage head)                                     
{
 system("cls");
 int flag=0;
 char name[100],ch[10];
 BooksManage p, t;
 
 /**如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!n");
        system("pause");
        return;
    }
 
 p = head;
 printf("nnn");
    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
 printf("请输入要删除的学科名字;");
 scanf("%s",&name);
 printf("------------------------------------------------------------------------------------n");
 printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");   
 printf("------------------------------------------------------------------------------------n");                                                                                                                                    
 while(p->next)
 {
  t=p;
  p=p->next;                                               /*转换到下一个结点*/
     if(strcmp(name,p->subject)==0)
     {
      flag=1;
         printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
         break;
  }
 }
  
 if(flag==1)
 {
  printf("是否删除该本图书信息(y/n)");
  scanf("%s",ch);
  printf("----------------------------------------------------------------------n");
  if(strcmp(ch,"Y") == 0 || strcmp(ch,"y") == 0)
  {
   t->next = p->next;
   free(p);
   printf("该书已删除!n");
  }
  else
      return;
 }
 else if(flag == 0)
     printf("没找到那本书!n");
 
 system("pause");
}

/*按ISBN删除图书信息*/ 
void DelBooks_ISBN(BooksManage head)                                     
{
 system("cls");
 int flag=0;
 char ch[10];
 char ISBN[N];
 BooksManage p, t;
 
 /**如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!n");
        system("pause");
        return;
    }
 
 p = head;
 printf("nnn");
    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
 printf("请输入要删除图书的ISBN号;");
 scanf("%s",ISBN);
 printf("------------------------------------------------------------------------------------n");
 printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");   
 printf("------------------------------------------------------------------------------------n");                                                                                                                                   
 while(p->next)
 {
  t=p;
  p=p->next;                                               /*转换到下一个结点*/
     if(strcmp(ISBN,p->ISBN)==0)
     {
      flag=1;
         printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
         break;
  }
 }
  
 if(flag==1)
 {
  printf("是否删除该本图书信息(y/n)");
  scanf("%s",ch);
  printf("----------------------------------------------------------------------n");
  if(strcmp(ch,"Y") == 0 || strcmp(ch,"y") == 0)
  {
   t->next = p->next;
   free(p); 
   printf("该书已删除!n");
  }
  else
      return;
 }
 else if(flag == 0)
     printf("没找到那本书!n");
 
 system("pause");
}

/*修改图书信息*/ 
void ModifyBooks(BooksManage head)                                     
{
 system("cls");
 int flag=0;
 char name[100],ch[5];
 BooksManage p, t;
 
 /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!n");
        system("pause");
        return ;
    }
 
 p = head;
 
 printf("nnn");
 printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
 printf("请输入要修改的书名:");
 scanf("%s",name);
 printf("--------------------------------------------------------------------n");
 
 while(p->next)
 {
  p=p->next;                 /*切换到下一个结点*/
     if(strcmp(name,p->bookName)==0)
     {
      flag = 1;
      t = p;
   printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");   
   printf("------------------------------------------------------------------------------------n");
      printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
  }
 }
 
 if(flag==1)
 {
  printf("--------------------------------------------------------------------n");
  printf("tt是否修改该本图书信息(y/n):");
  scanf("%s",ch);
  printf("t--------------------------------------------------------------------n");
  if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
  {
   int c;
   printf("tt【1】ISBN号n");
   printf("--------------------------------------------------------------------n");
   printf("tt【2】书名n");
   printf("--------------------------------------------------------------------n");
   printf("tt【3】所属学科n");
   printf("--------------------------------------------------------------------n");
   printf("tt【4】主编n");
   printf("--------------------------------------------------------------------n");
   printf("tt【5】价格n");
   printf("--------------------------------------------------------------------n");
   printf("tt【6】出版社n");
   printf("--------------------------------------------------------------------n");
   printf("tt【7】出版日期n");
   printf("--------------------------------------------------------------------n");
   printf("tt【8】数量n");
   printf("--------------------------------------------------------------------n");
   printf("t请输入你要修改的内容;");
   scanf("%d",&c);
   printf("--------------------------------------------------------------------n");
   p = t;
   switch(c)
   {
    case 1:
        printf("t请输入ISBN号:");
           scanf("%s",p->ISBN);
           break;
       case 2:
           printf("t请输入书名:");
           scanf("%s",p->bookName);
           break;
       case 3:
           printf("t请输入所属学科:");
           scanf("%s",p->subject);
           break;
       case 4:
           printf("t请输入主编:");
           scanf("%s",p->editor);
           break;
       case 5:
           printf("t请输入价格:");
           scanf("%f",&p->price);
           break;
       case 6:
           printf("t请输入出版社:");
           scanf("%s",p->publish);
           break;
       case 7:
           printf("t请输入出版日期:");
           scanf("%s",p->date);
           break;
       case 8:
           printf("t请输入数量:");
           scanf("%d",&p->num);
           break;
   }
   printf("--------------------------------------------------------------------n");
      printf("t修改成功!!!n");
  }
 }
 
 else if(flag == 0)
     printf("对不起,该书库没有该书n");
     system("pause");
}

/**显示所有图书信息*/ 
void PrintBooks(BooksManage head)                                               
{
 BooksManage p;
 system("cls");
 
 /**如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!n");
        system("pause");//提示任意键继续 
        return ;
    }
 
    p = head->next;
    printf("nnn");
    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
    printf("                                  图书列表n");
    printf("------------------------------------------------------------------------------------n");
 printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");   
 printf("------------------------------------------------------------------------------------n");
    
 do
 {
     printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
     p = p->next;                           
 }while(p!=NULL);
 
 system("pause");
}

/**求全部图书价值*/
void SumBooks(BooksManage head){
 float sum = 0;
 system("cls");
 BooksManage p;
 /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("tttt没有图书信息!n");
        return;
    }
        for(p = head;p != NULL; p = p->next){
         sum += p->price * p->num;
  }
 printf("nnnnnn"); 
   printf("ttt***********************************n");
   color(12);printf("ttt*    全部图书的总价值为:%.2f     *n",sum);
   color(10);printf("ttt***********************************n");
 system("pause");
}

/*查询图书信息*/ 
void FindBooks(BooksManage head)                                     
{
 system("cls");
 int choose,flag = 0;
 BooksManage p;
 char b[100];
 char ISBN[N];
 
 /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!n");
        system("pause");
        return ;
    }
    
    do
 {
  system("cls");
  printf("nnn");
  printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
  printf("tt1.按书名查找n");
  printf("----------------------------------------------------------------------n");
  printf("tt2.按ISBN号查找n");
  printf("----------------------------------------------------------------------n");
  printf("tt返回主菜单(其他数字)n");
  printf("----------------------------------------------------------------------n");
  printf("tt请输入您的选择:");
  scanf("%d",&choose);
  printf("----------------------------------------------------------------------n");
  p=head;
  
  if(choose == 1)
  {
   printf("输入所查书名:");
   scanf("%s",b);
   printf("----------------------------------------------------------------------nn");
   while(p->next)
   {
    p=p->next;                           /*转换到下一个结点*/
    if(strcmp(b,p->bookName)==0)         /*判断是否找到并输出*/
       {
         flag=1;
         printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
      printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");
             printf("------------------------------------------------------------------------------------n");
         printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
       }
   }
   if(flag==0)
            printf("不存在该信息n");
  }
  
  else if(choose==2)
  {
   printf("输入所查ISBN号:");
   scanf("%s",ISBN);
   printf("----------------------------------------------------------------------nn");
   while(p->next)
   {
    p=p->next;                         /*转换到下一个结点*/
    if(strcmp(p->ISBN,ISBN) == 0)         /*判断是否找到并输出*/
    {
     flag=1;
       printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※n");
                 printf("ISBNt书名t所属学科t主编t价格t出版单位t出版时间t数量n");
                printf("------------------------------------------------------------------------------------n");
        printf("%st%st%stt%st%.2ft%stt%st%dn",p->ISBN,p->bookName,p->subject,p->editor,p->price,p->publish,p->date,p->num);
    }
   }
   if(flag == 0)
           printf("不存在该信息n");
  }
  else
      return;
  system("pause");
 }while(1);
}

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

最后

以上就是辛勤悟空为你收集整理的C/C++实现图书信息管理系统的全部内容,希望文章能够帮你解决C/C++实现图书信息管理系统所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部