⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4061014.c

📁 c语言课程设计
💻 C
字号:
/*头文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h>   /*其它说明*/
#include<string.h>   /*字符串函数*/
#include<mem.h>      /*内存操作函数*/
#include<ctype.h>    /*字符操作函数*/
#include<alloc.h>    /*动态地址分配函数*/
#define LEN sizeof(BOOK)
typedef struct book   /*定义结构体数组用于缓存数据*/
{char bno[8];
 char bname[21];
 char pdate[10];
 char state[4];
 char author[9];
 char press[11];
 char digest[30];
 int order; 
 struct book *next;
}BOOK;

/*函数原型*/
BOOK  *init();    /*初始化函数*/
int menu_select();   /*菜单函数*/
BOOK *create();   /*创建链表*/
void print(BOOK *head);   /* 显示全部记录*/
void search(BOOK *head);    /*查找记录*/
BOOK *delete(BOOK *head);   /*删除记录*/
BOOK *sort(BOOK *head);    /*排序*/
BOOK *insert(BOOK *head,BOOK *new);   /*插入记录*/
void save(BOOK *head);     /*保存文件*/
BOOK *load();        /*读文件*/

/*主函数界面*/
main()
{

BOOK *head,new;
 head=init();       /*链表初始化,使head的值为NULL*/
 for(;;)            /*循环无限次*/
   {switch(menu_select())    
      {                     
     case 1:head=create();break;
     case 2:print(head);break;
     case 3:search(head);break;
     case 4:head=delete(head);break;
     case 5:head=insert(head,&new);break;  /*&new表示返回地址*/
     case 6:save(head);break;
     case 7:head=load(); break;
     case 0:exit(0);       /*如菜单返回值为0则程序结束*/
      }
   }
}

/*初始化函数*/
BOOK *init()
{
 return NULL;  /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{int n;
 struct date d;     /*定义时间结构体*/
 getdate(&d);      /*读取系统日期并把它放到结构体d中*/
 printf("press any key to enter the menu......");   /*按任一键进入主菜单*/
 getch();    /*从键盘读取一个字符,但不显示于屏幕*/
 clrscr();   /*清屏*/
 printf("********************************************************************************\n");
 printf("\t\t                Welcome to\n");
 printf("\n\t\t    The book  information manage system\n");
 printf("*************************************MENU***************************************\n");
 printf("\t\t\t1. Enter the record\n");            /*输入图书信息记录*/
 printf("\t\t\t2. Print the record\n");            /*显示*/
 printf("\t\t\t3. Search a record on name\n");       /*寻找*/
 printf("\t\t\t4. Delete a record\n");             /*删除*/
 printf("\t\t\t5. Insert a record to list\n");       /*插入*/
 printf("\t\t\t6. Save the file\n");               /*保存*/
 printf("\t\t\t7. Load the file\n");               /*读取*/
 printf("\t\t\t0. Quit\n");                       /*退出*/
 printf("\n\t\t         4061014 \n");
 printf("********************************************************************************\n");
 printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day);   /*显示当前系统日期*/
 do{
    printf("\n\t\t\tEnter your choice(0~7):");  
    scanf("%d",&n);
   }while(n<0||n>7);    /*如果选择项不在0~7之间则重输*/
 
 return(n);           /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
BOOK *create()
{int i,s;
 BOOK *head=NULL,*p;  /* 定义函数.此函数带回一个指向链表头的指针*/
 clrscr();
 for(;;)
   {p=(BOOK*)malloc(LEN);  /*开辟一个新的单元*/
    if(!p)   /*如果指针p为空*/
      {printf("\nOut of memory.");   /*输出内存溢出*/
       return (head);  /*返回头指针,下同*/
      }
    printf("Enter the book No(press \".\" to end):"); 
    scanf("%s",p->bno);
    printf("\n");
    if(p->bno[0]=='.') break;    /*如果书号首字符为“.”则结束输入*/
     printf("\n");   
    printf("Enter the book name:");
    scanf("%s",p->bname);
    printf("Please enter the publishdate:");  /*提示开始输入出版日期*/
    scanf("%s",p->pdate);
    printf("Please enter the state:");  /*提示开始输入状态*/
    scanf("%s",p->state);
    printf("Please enter the author :");  /*提示开始输入作者*/
    scanf("%s",p->author);
    printf("Please enter the press:");  /*提示开始输入出版社*/
    scanf("%s",p->press);
    printf("Please enter the digest:");  /*提示开始输入文摘*/
    scanf("%s",p->digest);
          p->order=0;             /*未排序前此值为0*/
      p->next=head;           /*将头结点做为新输入结点的后继结点*/
      head=p;                 /*新输入结点为新的头结点*/
   }
   return(head);  
}


/* 显示全部记录函数*/
void print(BOOK *head)
{int i=0;     /* 统计记录条数*/
 BOOK *p;  /*移动指针*/
 clrscr();
 p=head;      /*初值为头指针*/
 printf("\n************************************BOOK************************************\n");
 printf("-------------------------------------------------------------------------------\n");
 printf("-------------------------------------------------------------------------------\n");
 while(p!=NULL)
   {
    i++;
printf("Rec: %d\n",i);
printf(" Book No: %s\n", p->bno);
printf(" Book name :%s\n", p->bname);
printf(" Publish date: %s\n",p->pdate);
printf(" State: %s\n", p->state);
printf(" Author: %s\n", p->author);
printf(" Press: %s\n", p->press);
printf(" Digest: %s\n\n", p->digest);
   p=p->next;
   }
 printf("-------------------------------------------------------------------------------\n");
 printf("**************************************END**************************************\n");
}

/*查找记录函数*/
void search(BOOK *head)
{BOOK *p;    /*  移动指针*/
 char s[21];     /*存放书名用的字符数组*/
 clrscr();
 printf("Please enter book name for searching.\n");
 scanf("%s",s);
 p=head;    /*将头指针赋给p*/
 while(strcmp(p->bname,s) && p != NULL)  /*当记录的姓名不是要找的,或指针不为空时*/
   p=p->next;     /*移动指针,指向下一结点*/
   if(p!=NULL)         /*如果指针不为空*/
   {printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
printf(" Book No: %s\n", p->bno);
printf(" Book name :%s\n", p->bname);
printf(" Publish date: %s\n",p->pdate);
printf(" State: %s\n", p->state);
printf(" Author: %s\n", p->author);
printf(" Press: %s\n", p->press);
printf(" Digest: %s\n\n", p->digest);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
   }
   else
     printf("\nThere is no book No %s book on the list.\n",s);   /*显示没有该书*/
}

/*删除记录函数*/
BOOK *delete(BOOK *head)
{int n;
 BOOK *p1,*p2;  /*p1为查找到要删除的结点指针,p2为其前驱指针*/
 char c,s[8];     /*s[8]用来存放书号,c用来输入字母*/
 clrscr();
 printf("Please enter the deleted book No: ");
 scanf("%s",s);
 p1=p2=head;    /*给p1和p2赋初值头指针*/
 while(strcmp(p1->bno,s) && p1 != NULL)   /*当记录的书号不是要找的,或指针不为空时*/
   {p2=p1;            /*将p1指针值赋给p2作为p1的前驱指针*/
    p1=p1->next;      /*将p1指针指向下一条记录*/
   }
 if(strcmp(p1->bno,s)==0)  /*书号找到了*/
 {printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
printf(" book No: %s\n", p1->bno);
printf(" book name :%s\n", p1->bname);
printf(" publishdate: %s\n",p1->pdate);
printf(" state: %s\n", p1->state);
printf(" author: %s\n", p1->author);
printf(" press: %s\n", p1->press);
printf(" digest: %s\n\n", p1->digest);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
    printf("Are you sure to delete the book  Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
    for(;;)
      {scanf("%c",&c);
       if(c=='n'||c=='N') break;  /*如果不删除,则跳出本循环*/
       if(c=='y'||c=='Y')
         {
          if(p1==head)       /*若p1==head,说明被删结点是首结点*/
          head=p1->next;     /*把第二个结点地址赋予head*/
        else
          p2->next=p1->next;  /*否则将一下结点地址赋给前一结点地址*/
          n=n-1;
          printf("\nBno %s book has been deleted.\n",s);
          printf("Don't forget to save.\n");break;  /*删除后就跳出循环*/
         }
      }
    }
  else
    printf("\nThere is no book No %s book on the list.\n",s);  /*找不到该结点*/
 return(head);
}

/*插入记录函数*/
BOOK *insert(BOOK *head,BOOK *new)
{BOOK *p0,*p1,*p2;
 int n,sum1,i;
 p1=head;  /*使p1指向第一个结点*/
 p0=new;   /*p0指向要插入的结点*/
 printf("\nPlease enter a new record.\n");      /*提示输入记录信息*/
 printf("Enter the book No:");
 scanf("%s",new->bno);
 printf("Enter the book name:");
 scanf("%s",new->bname);
 printf("Please enter the publishdate:");  /*提示开始输入出版日期*/
 scanf("%s",new->pdate);
 printf("Please enter the state:");  /*提示开始输入状态*/
 scanf("%s",new->state);
 printf("Please enter the author:");  /*提示开始输入作者*/
 scanf("%s",new->author);
 printf("Please enter the press:");  /*提示开始输入出版社*/
 scanf("%s",new->press);
 printf("Please enter the digest:");  /*提示开始输入文摘*/
 scanf("%s",new->digest);
 new->order=0;
  if(head==NULL)  /*原来的链表是空表*/
   {head=p0;p0->next=NULL;}  /*使p0指向的结点作为头结点*/
 else
   {p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
 n=n+1; /*结点数加1*/
 printf("\nBook  %s has been inserted.\n",new->bname);    
 printf("Don't forget to save the new file.\n");
 return(head);
}


/*保存数据到文件函数*/
void save(BOOK *head)
{FILE *fp;        /*定义指向文件的指针*/
 BOOK *p;      /* 定义移动指针*/
 char outfile[10];
 printf("Enter outfile name,for example c:\\books\n");
 scanf("%s",outfile);
 if((fp=fopen(outfile,"wb"))==NULL)   /*为输出打开一个二进制文件,为只写方式*/
   {
    printf("Cannot open the file\n");
    return;    /*若打不开则返回菜单*/
   }
 printf("\nSaving the file......\n");
 p=head;                    /*移动指针从头指针开始*/
 while(p!=NULL)        /*如p不为空*/
   {
    fwrite(p,LEN,1,fp);     /*写入一条记录*/
    p=p->next;        /*指针后移*/
   }
 fclose(fp);      /*关闭文件*/
 printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
BOOK *load()
{BOOK *p1,*p2,*head=NULL;    /*定义记录指针变量*/
 FILE *fp;            /* 定义指向文件的指针*/
 char infile[10];
 printf("Enter infile name,for example c:\\books.txt\n");
 scanf("%s",infile);
 if((fp=fopen(infile,"rb"))==NULL)   /*打开一个二进制文件,为只读方式*/
   {
    printf("Can not open the file.\n");
    return(head);
   }
 printf("\nLoading the file!\n");
 p1=(BOOK *)malloc(LEN);   /*开辟一个新单元*/
 if(!p1)
   {
    printf("Out of memory!\n");
    return(head);
   }
 head=p1;         /*申请到空间,将其作为头指针*/
 while(!feof(fp))  /*循环读数据直到文件尾结束*/
   {
    if(fread(p1,LEN,1,fp)!=1) break;  /*如果没读到数据,跳出循环*/
    p1->next=(BOOK *)malloc(LEN);  /*为下一个结点开辟空间*/
    if(!p1->next)
      {
       printf("Out of memory!\n");
       return (head);
      }
 p2=p1;         /*使p2指向刚才p1指向的结点*/
 p1=p1->next;   /*指针后移,新读入数据链到当前表尾*/
   }
 p2->next=NULL;   /*最后一个结点的后继指针为空*/
 fclose(fp);
 printf("You have success to read the data from the file!\n");
 return (head);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -