需要实现查询功能
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BOOKSTORE
{
char title[100];//书名
char author[100];//作者
char press[100];//出版社
float date;//出版日期
float price;//价格
int count;//数量
int position;//清单位置
struct BOOKSTORE *next;
}shudian;
shudian *head;
FILE *fp;
shudian *fd() //创建信息
{
int a;
shudian *p=NULL,*pr=NULL; //定义指针变量
pr=head=(shudian *)malloc(sizeof(shudian)); //申请内存
head->next=NULL;
while(1)
{
p=(shudian*)malloc(sizeof (shudian));
printf("请输入书籍的以下信息:\n");
printf("书名:\n");
scanf("%s",&p->title);
printf("作者:\n");
scanf("%s",&p->author);
printf("出版社:\n");
scanf("%s",&p->press);
printf("出版日期:\n");
scanf("%f",&p->date);
printf("价格:\n");
scanf("%f",&p->price);
printf("库存:\n");
scanf("%d",&p->count);
printf("清单位置:\n");
scanf("%d",&p->position);
printf("\n");
pr->next=p;
pr=p;
printf("是否继续创建书籍信息? 否:输1 继续: 输2\n");
scanf("%d",&a);
if(a==1)
{
pr->next=NULL;
p=NULL; //return head;
break;
}
printf("\n");
if(a>2)
{
printf("输入错误!\n");
system("pause");
}
}
pr->next=NULL; // free(p);
p=NULL;
return head;
}
void save(shudian *head)
{
shudian*p,*p1;
p=head->next;
FILE *fp;
fp=fopen("sbooks.txt","a");
if(fp==NULL) {
printf("文件保存失败!");
return ; }
if(p==NULL) {
printf("链表为空!");
return; }
p=head->next;
while(p!=NULL)
{
fprintf(fp,"%s %s %s %f %f %d %d \n",p->title,p->author,p->press,p->date,p->price,p->count,p->position);
p=p->next;
}
fclose(fp);
printf("信息保存成功!\n");
return ;
}
void find(shudian *head)//查询信息
{
shudian *p=NULL;
int n,i=0;
char name1[100],bookname[100];
printf("\t\t 请选择方式查找书籍信息: \n");
printf("\t\t 1.通过书名查找 2.通过作者查找\n"); //两种方式查询
p=head->next;
if(p==NULL)
{
printf("暂无书籍信息!\n");
}
printf("请输入您的选择: 1 或 2 \n");
scanf("%d",&n);
if(n==1)
{
printf("请输入书名:\n");
scanf("%s",bookname);
while(p!=NULL)
{
if(bookname==p->title)
{
printf("成功找到此书信息!\n");
printf("此书的信息如下:\n");
printf("书名:%s\n",p->title);
printf("作者:%s\n",p->author);
printf("出版社:%s\n",p->press);
printf("出版日期:%f\n",p->date);
printf("价格:%f\n",p->price);
printf("库存:%d\n",p->count);
printf("清单位置:%d\n",p->position);
i=1;
printf("请输入需要购买的数量:\n");
int b;
scanf("%d",&b);
if(b<=p->count)
{
printf("总价为%.2f",b*p->count);break;
}
else
{
printf("库存不足!\n");
break;
}
}
p=p->next;
if(i==0)
{
printf("未查找到此书信息!\n");
}
}
}
else if(n==2)
{
printf("请输入作者:\n");
scanf("%s",&name1);
while(p!=NULL)
{
if(strcmp(p->author,name1)==0)
{
printf("成功找到此书信息!\n");
printf("此书的信息如下:\n");
printf("书名:%s\n",p->title);
printf("作者:%s\n",p->author);
printf("出版社:%s\n",p->press);
printf("出版日期:%f\n",p->date);
printf("价格:%f\n",p->price);
printf("库存:%d\n",p->count);
printf("清单位置:%d\n",p->position);
i=1;
break;
}
p=p->next;
if(i==0)
{
printf("未查找到此书信息!\n");
}
}
}
else
{
printf("操作有误!\n");
find(head);
}
}
void add(shudian *head) //添加信息
{
int n;
FILE *fp;
fp=fopen("sbooks.txt","a");
shudian *p=NULL,*p1=NULL;
p=head->next;
while(p->next!=NULL)
{
p=p->next;
}
while(1)
{
p1=(shudian*)malloc(sizeof(shudian));
printf("请输入你所想添加的信息:\n");
printf("书名:\n");
scanf("%s",&p1->title);
printf("作者:\n");
scanf("%s",&p1->author);
printf("出版社:\n");
scanf("%s",&p1->press);
printf("出版日期:\n");
scanf("%f",&p1->date);
printf("价格:\n");
scanf("%f",&p1->price);
printf("库存:\n");
scanf("%d",&p1->count);
printf("清单位置:\n");
scanf("%d",&p1->position);
p->next=p1;
p=p1;
p1->next=NULL;
save(head);
break;
}
}
void dele(shudian *head) //删除信息
{
int i=0;
shudian *p=NULL,*pr=NULL;
char name4[100];
pr=head;
p=head->next;
if(p==NULL)
{
printf("没有书籍信息!\n");
}
printf("请输入要删除的书籍书名:\n");
scanf("%s",&name4);
while(p!=NULL)
{
if(strcmp(p->title,name4)==0)//比较
{
pr->next=p->next;
free(p);
p=NULL;
printf("已将该书籍信息删除!\n");
i=1;
save(head);
break;
}
pr=p;
p=p->next;
}
if(i==0)
printf("未找到需要删除书籍信息!\n");
}
void menu() //目录
{
printf("1.录入书籍\n");
printf("2.添加书籍\n");
printf("3.查询/购买书籍\n");
printf("4.删除书籍\n");
printf("5.退出系统\n") ;
printf("请输入相关选项 :");
int ch;
scanf("%d",&ch);
switch(ch)
{
case 1:head=fd();save(head);break;
case 2:add(head);break;
case 3:find(head);break;
case 4:dele(head);break;
case 5:printf("欢迎下次使用!\n");break;
default: printf("操作错误!\n");
}
}
int main() //主函数
{
FILE *fp;
menu();
int ch;
scanf("%d",&ch);
save(head);
printf("是否退出系统:1,否 2.是\n");
int m;
scanf("%d",&m);
if(m==1)
{
menu();
}
if(m==2)
{
printf("欢迎下次使用!");
return 0;
}
}
不能进行除添加以外的功能
能够直接进行查询功能
105行应该用strcmp进行字符串相等判断
还是不能查询,怎么在文件中有书籍信息的情况下直接实现查询功能啊
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BOOKSTORE
{
char title[100];//书名
char author[100];//作者
char press[100];//出版社
float date;//出版日期
float price;//价格
int count;//数量
int position;//清单位置
struct BOOKSTORE* next;
}shudian;
void add(shudian* head) //添加信息
{
int n = 1;
shudian* p = NULL, * pr = NULL;
if (head == NULL || head->next == NULL)
{
printf("\n没有书籍信息!\n\n");
return;
}
p = head->next;
while (p != NULL)
{
pr = p;
p = p->next;
}
while (n)
{
p = (shudian*)malloc(sizeof(shudian));
p->next = NULL;
printf("请输入你所想添加的信息:\n\n");
printf("书名:");
scanf("%s", p->title);
printf("作者:");
scanf("%s", p->author);
printf("出版社:");
scanf("%s", p->press);
printf("出版日期:");
scanf("%f", &p->date);
printf("价格:");
scanf("%f", &p->price);
printf("库存:");
scanf("%d", &p->count);
printf("清单位置:");
scanf("%d", &p->position);
if (head->next == NULL)
head->next = p;
else
pr->next = p;
pr = p;
printf("是否继续创建书籍信息? 否:1 继续: 2\n\n");
scanf("%d", &n);
if (n == 1)
{
n = 0;
break;
}
}
}
shudian* fd(shudian *head) //从文件或手工创建信息
{
FILE* fp;
shudian* p = NULL, * pr = NULL; //定义指针变量
if (head == NULL) {
pr = head = (shudian*)malloc(sizeof(shudian)); //申请内存
head->next = NULL;
fp = fopen("sbooks.txt", "r");
if (fp == NULL) {
printf("\n文件打开失败,手工添加书籍信息:\n\n");
add(head);
}
else {
while (1)
{
p = (shudian*)malloc(sizeof(shudian));
p->next = NULL;
if (fscanf(fp, "%s %s %s %f %f %d %d\n", p->title, p->author,
p->press, &p->date, &p->price, &p->count, &p->position) != 7) break;
pr->next = p;
pr = p;
}
free(p);
fclose(fp);
printf("\n文件载入成功!\n\n");
}
}
else {
printf("\n文件已载入,手工创建书籍信息:\n\n");
add(head);
}
return head;
}
void save(shudian* head)
{
shudian* p = NULL;
FILE* fp;
fp = fopen("sbooks.txt", "w");
if (fp == NULL) {
printf("文件保存失败!");
}
else{
p = head;
if (p == NULL) {
printf("链表为空!");
}
else {
p = p->next;
while (p != NULL)
{
fprintf(fp, "%s %s %s %f %f %d %d\n", p->title, p->author,
p->press, p->date, p->price, p->count, p->position);
p = p->next;
}
printf("信息保存成功!\n");
}
fclose(fp);
}
}
void find(shudian* head)//查询信息
{
shudian* p = NULL;
int n, i = 0;
char name1[100], bookname[100];
if (head == NULL || head->next == NULL) {
printf("暂无书籍信息!\n");
return;
}
printf("\t\t 请选择方式查找书籍信息:\n");
printf("\t\t 1.通过书名查找 2.通过作者查找\n"); //两种方式查询
printf("请输入您的选择(1 或 2 ):");
scanf("%d", &n);
p = head->next;
if (n == 1)
{
printf("请输入书名:");
scanf("%s", bookname);
while (p != NULL)
{
if (strcmp(bookname, p->title) == 0)//(bookname == p->title)
{
printf("成功找到此书信息!\n\n");
printf("此书的信息如下:\n\n");
printf("书名:%s\n", p->title);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->press);
printf("出版日期:%f\n", p->date);
printf("价格:%f\n", p->price);
printf("库存:%d\n", p->count);
printf("清单位置:%d\n", p->position);
i = 1;
printf("请输入需要购买的数量:");
int b;
scanf("%d", &b);
if (b <= p->count)
{
printf("总价为:%.2f\n", b * p->price);
p->count -= b;
break;
}
else
{
printf("库存不足!\n\n");
break;
}
}
p = p->next;
}
if (i == 0)
{
printf("\n未查找到此书信息!\n\n");
}
}
else if (n == 2)
{
printf("请输入作者:");
scanf("%s", name1);
while (p != NULL)
{
if (strcmp(p->author, name1) == 0)
{
printf("成功找到此书信息!\n\n");
printf("此书的信息如下:\n\n");
printf("书名:%s\n", p->title);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->press);
printf("出版日期:%f\n", p->date);
printf("价格:%f\n", p->price);
printf("库存:%d\n", p->count);
printf("清单位置:%d\n", p->position);
i = 1;
break;
}
p = p->next;
}
if (i == 0)
{
printf("\n未查找到此书信息!\n\n");
}
}
else
{
printf("操作有误!\n\n");
find(head);
}
}
void dele(shudian* head) //删除信息
{
int i = 0;
shudian* p = NULL, * pr = NULL;
char name4[100];
if (head == NULL || head->next == NULL)
{
printf("\n没有书籍信息!\n\n");
return;
}
pr = head;
p = head->next;
printf("请输入要删除的书籍书名:");
scanf("%s", name4);
while (p != NULL)
{
if (strcmp(p->title, name4) == 0)//比较
{
pr->next = p->next;
free(p);
p = NULL;
printf("\n已将该书籍信息删除!\n\n");
i = 1;
break;
}
pr = p;
p = p->next;
}
if (i == 0)
printf("\n未找到需要删除书籍信息!\n\n");
}
void print(shudian* head)
{
shudian* p = NULL;
if (head == NULL || head->next == NULL)
{
printf("\n没有书籍信息!\n\n");
return;
}
p = head->next;
while (p) {
printf("\n%s %s %s %.2f %.2f %d %d\n", p->title, p->author,
p->press, p->date, p->price, p->count, p->position);
p = p->next;
}
}
void menu() //目录
{
printf("\t1.录入书籍\n");
printf("\t2.添加书籍\n");
printf("\t3.查询/购买书籍\n");
printf("\t4.删除书籍\n");
printf("\t5.浏览书籍\n");
printf("\t6.退出系统\n");
printf("\n请输入相关选项 :");
}
int main() //主函数
{
shudian* head = NULL;
int m = 1;
while (m)
{
int ch;
menu();
scanf("%d", &ch);
switch (ch)
{
case 1:head = fd(head); break;
case 2:add(head); break;
case 3:find(head); break;
case 4:dele(head); break;
case 5:print(head); break;
case 6:printf("\n确认退出系统:1,否 2.是\n");
scanf("%d", &m);
if (m == 1)
m = 1;
else{
save(head);
printf("欢迎下次使用!");
m = 0;
}
break;
default:printf("操作错误!\n"); break;
}
}
return 0;
}