代码如下
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Book
{
int num; /*编号*/
char cla[10]; /*分类名*/
char name[15]; /*书名*/
char author[8]; /*作者名*/
char company[20]; /*出版单位*/
int count; /*数量*/
float price; /*价格*/
};
typedef struct Book DataType;
typedef struct LNode
{
DataType data;
struct LNode *next;
}LinkList;
LinkList *head;
int R;
void DispList(LinkList *head);
LinkList*InitList()
{
head=(LinkList*)malloc(sizeof(LinkList));
head->next=NULL;
return head;
}
void ReadFile()
{
FILE*fp;
// int i=0;
LinkList Books,*s,*last;
last=head;
if((fp=fopen("jk62107_08_bk.txt","r+"))==NULL)
{
printf("找不到此文件再试试!!!!\n");
return;
}
fscanf(fp,"%s\t %s\t %s %s %s %s %s\n",&Books.data.num,&Books.data.cla,&Books.data.name,&Books.data.author,&Books.data.company,&Books.data.count,&Books.data.price);
printf("\n\n 编号 分类\t 书名\t \t 作者 \t出版社 \t数量 \t\t价格\n");
while(!feof(fp))
{
s=(LinkList*)malloc(sizeof(LinkList));
fscanf(fp,"%d %s %s %s %s %d %f\n",&s->data.num,&s->data.cla,&s->data.name,&s->data.author,&s->data.company,&s->data.count,&s->data.price);
printf(" %d %s\t %s\t %s %s\t %d\t \t%6.2f\n",s->data.num,s->data.cla,s->data.name,s->data.author,s->data.company,s->data.count,s->data.price);
R++;
s->next=NULL;
s->next=s;
last=s;
}
printf("\n\n文件读取成功\n\n");
printf("%d\n",R);
fclose(fp);
}
void Searchname()//按书名查找函数
{
int flag=0;//标记变量的初值
char name[15];
LinkList Books,*s,*p;
p=head;
printf ("\n请输入需要查询的书名:");
scanf("%s",name);
printf("\n\t\t*************以下是您查找的信息***************");
p=head;
while(p!=NULL);
{
if(strcmp(p->name,name)==0)//查找符合的图书
{
printf("\n\t编号:%-10s",p->num);
printf("\n\t书名:%-20s",p->name);
printf("\n\t作者名:%-20s",p->author);
printf("\n\t分类名:%-10s",p->cla);
printf("\n\t出版单位:%-20s",p->company);
printf("\n\t数量:%-20s",p->count);
printf("\n\t价格:¥%-10s",p->price);
flag=1;//找到标记变量设为1
p=p->next;//指针走到下一个节点
s->next=NULL;
s->next=s;
p=s;
}
else
p=p->next;
}
if(flag==0)
printf("\n\t\t没有相同书名纪录");
printf("\n\t\t请按任意键返回主菜单");
getchar();
}
}
int main()
{
ReadFile();
return 0;
}
编译器提示