图书信息管理系统源程序).txt
来自「图书管理系统,是以歌很长的程序,有关本科生数据结构课程设计的」· 文本 代码 · 共 272 行
TXT
272 行
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char name[10];
float price;
char pub[15];
struct node *link;
};
struct node *add(struct node *head ) ; /* 函数功能:创建结点,输入结点数据 */
void print(struct node *head); /* 函数功能:链表输出 */
void save(struct node *head); /* 函数功能:链表存储 */
void search(struct node *head); /* 函数功能:链表结点查找 */
struct node *del(struct node *head); /* 函数功能:结点删除 */
struct node *load(void); /* 函数功能:从文件中读入数据,创建链表 */
struct node *mhdel(struct node *head); /* 函数功能:结点模糊删除 */
void mhsearch(struct node *head); /* 函数功能:链表结点模糊查找 */
/*******************************************************/
/* 函数功能:创建结点,输入结点数据 */
/* 输入参数:链表头指针 */
/* 函数输出:链表头指针 */
/*******************************************************/
struct node *add(struct node *head )
{
int i;
float jg;
struct node *p, *pnew;
pnew=(struct node *)malloc(sizeof(struct node));
printf("Please input book name,price,publisher\n");
printf("name:");scanf("%s",pnew->name);
printf("price:");scanf("%f",&jg);pnew->price=jg;
printf("publisher:");scanf("%s",pnew->pub);
pnew->link=NULL;
if (head==NULL)return (pnew);
p=head;
while (p->link!=NULL) /* 找到尾结点 */
p=p->link;
p->link=pnew;
return (head);
}
/*******************************************************/
/* 函数功能:链表输出 */
/* 输入参数:链表头指针 */
/* 函数输出:无 */
/*******************************************************/
void print(struct node *head)
{
struct node *p;
p=head;
printf("name\tprice\tpublisher\n");
while (p!=NULL)
{
printf("%s\t%-5.2f\t%-s\n",p->name,p->price,p->pub);
p=p->link;
}
printf("\n\t\tPress any keys!");
getch();
}
/*******************************************************/
/* 函数功能:链表存储 */
/* 输入参数:链表头指针 */
/* 函数输出:无 */
/*******************************************************/
void save(struct node *head)
{
FILE *fp;
struct node *p;
if ((fp=fopen("library","wb"))==NULL)
{ printf("Can't open file!\n");
exit(1);
}
p=head;
while (p!=NULL)
{
fwrite(p,sizeof(struct node),1,fp);
p=p->link;
}
fclose(fp);
}
/*******************************************************/
/* 函数功能:从文件中读入数据,创建链表 */
/* 输入参数:无 */
/* 函数输出:链表头指针 */
/*******************************************************/
struct node *load(void)
{
FILE *fp;
struct node *p,*pnew,*phead;
if ((fp=fopen("library","rb"))==NULL)
{ printf("Can't open file!\n");
exit(1);
}
pnew=(struct node *)malloc(sizeof(struct node));
fread(pnew, sizeof(struct node),1,fp);
pnew->link=NULL;
phead=pnew;
p=pnew;
while (!feof(fp))
{ pnew=(struct node *)malloc(sizeof(struct node));
if (1!=fread(pnew, sizeof(struct node),1,fp)) break;
pnew->link=NULL;
p->link=pnew;
p=p->link;
}
fclose(fp);
return(phead);
}
/*******************************************************/
/* 函数功能:链表结点查找 */
/* 输入参数:链表头指针 */
/* 函数输出:无 */
/*******************************************************/
void search(struct node *head)
{
char sname[15];
struct node *p;
printf("Please input book name\n");
printf("name:");scanf("%s",sname);
p=head;
while (p!=NULL)
{ if (strcmp(p->name,sname)==0)
{
printf(" name price publshier\n");
printf("Book Found: %s %5.2f %s\n",p->name,p->price,p->pub);
printf("\n\t\tPress any keys!");
getch();
return;
}
p=p->link;
}
printf("Book Not Exist!\n");
printf("\n\t\tPress any keys !"); getch();
}
/*******************************************************/
/* 函数功能:结点删除 */
/* 输入参数:链表头指针 */
/* 函数输出:返回链表头指针 */
/*******************************************************/
struct node *del(struct node *head)
{
char sn[15],s;
int flag=0;
struct node *p,*q;
printf("Please input book name\n");
printf("name:");scanf("%s",sn);
p=head;
while(p!=NULL)
{
while(p!=NULL &&(strcmp(p->name,sn)!=0))
{ q=p;p=p->link;}
if((strcmp(p->name,sn)==0)&& p!=NULL)
{
flag=1;
printf(" name price publshier\n");
printf("Book Found: %s %5.2f %s\n",p->name,p->price,p->pub);
printf("Do you want to del this book(y/n)");
scanf("\n%c",&s);
if(s=='y' || s=='Y')
{
if(p==head)head=p->link;
else q->link=p->link;
free(p);
printf("This book is deleted!\n");
printf("\n\t\tPress any keys !");getch();
}
else
{
printf("\nThis book not delete!\n");
printf("\n\t\tPress any keys!");getch();
return head;
}
}
else
{ if(flag!=1)
{ printf("\nNOT found!");
printf("\n\t\tPress any key!");getch();
}
break;
}
if(p!=NULL)
{ p=p->link; }
}
return head;
}
void mhsearch(struct node *head)
{
char s[15];
int flag=0;
struct node *p;
printf("Please input book name\n");
printf("name:");scanf("%s",s);
p=head;
while(p!=NULL)
{
while(p!=NULL && (strstr(p->name,s)==NULL))
{ p=p->link;}
if(strstr(p->name,s)!=NULL)
{
printf(" name price publshier\n");
printf("Book Found: %s %5.2f %s\n",p->name,p->price,p->pub);
flag=1;
getch();
}
else
{ if(flag!=1)
{ printf("\nNOT found!");
printf("\n\t\tPress any key!");getch();
}
}
if(p!=NULL)
{ p=p->link; }
}
}
struct node *mhdel(struct node *head)
{
char sn[15],s;
int flag=0;
struct node *p,*q;
printf("Please input book name\n");
printf("name:");scanf("%s",sn);
p=head;
while(p!=NULL)
{
while(p!=NULL &&(strstr(p->name,sn)==NULL))
{ q=p;p=p->link;}
if((strcmp(p->name,sn)!=NULL)&& p!=NULL)
{
flag=1;
printf(" name price publshier\n");
printf("Book Found: %s %5.2f %s\n",p->name,p->price,p->pub);
printf("Do you want to del this book(y/n)");
scanf("\n%c",&s);
if(s=='y' || s=='Y')
{
if(p==head)head=p->link;
else q->link=p->link;
free(p);
printf("This book is deleted!\n");
printf("\n\t\tPress any keys !");getch();
}
else
{
printf("\nThis book not delete!\n");
printf("\n\t\tPress any keys!");getch();
return head;
}
}
else
{ if(flag!=1)
{ printf("\nNOT found!");
printf("\n\t\tPress any key!");getch();
}
break;
}
p=head;
}
return head;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?