#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct record {
char title[64];
char author[24];
char isbn[24];
double price;
int copies;
struct record *next;
}Node,*LinkList,*pNode;
LinkList InitList() {
LinkList head = (pNode)malloc(sizeof(Node));
head->title[0] = '\0';
head->author[0] = '\0';
head->isbn[0] = '\0';
head->copies = 0;
head->price = 0.0;
head->next = NULL;
return head;
}
pNode ReadData() {
pNode p = (pNode)malloc(sizeof(Node));
fflush(stdin);
printf("书 名 : ");
gets(p->title);
printf("作 者 : ");
gets(p->author);
printf("图书编号 : ");
gets(p->isbn);
printf("单 价 : ");
scanf("%lf",&p->price);
printf("数 量 : ");
scanf("%d",&p->copies);
return p;
}
void Add(LinkList head) { // 头插法
pNode p = ReadData();
p->next = head->next;
head->next = p;
}
void ReadFile(LinkList head) {
char filename[60];
FILE *inf;
int n = 0;
pNode p;
printf("请输入文件名:");
fflush(stdin);
fgets(filename,60,stdin);
if((inf = fopen(filename,"rb")) == NULL) {
printf("无法打开数据文件:%s\n",filename);
system("pause");
return;
}
p = (pNode)malloc(sizeof(Node));
while(fread((void *)p,sizeof(Node),1,inf) == 1) {
p->next = head->next;
head->next = p;
p = (pNode)malloc(sizeof(Node));
++n;
}
free(p);
fclose(inf);
printf("共读入%d条图书信息!\n",n);
system("pause");
}
void ShowData(pNode pnode) {
printf("书名 : %s\n",pnode->title);
printf("作者 : %s\n",pnode->author);
printf("编号 : %s\n",pnode->isbn);
printf("单价 : %.2lf元\n",pnode->price);
printf("数量 : %d本\n\n",pnode->copies);
}
void PrintBookList(LinkList head) {
pNode p = head->next;
while(p) {
ShowData(p);
p = p->next;
}
system("pause");
}
void WriteFile(LinkList head) {
char filename[60];
FILE *outf;
int n = 0;
pNode p = head->next;
printf("请输入文件名:");
fflush(stdin);
fgets(filename,60,stdin);
if((outf = fopen(filename,"wb")) == NULL) {
printf("无法打开数据文件:%s\n",filename);
system("pause");
return;
}
while(p) {
fwrite((void *)p,sizeof(Node),1,outf);
p = p->next;
}
fclose(outf);
printf("共写出%d条图书信息!\n",n);
system("pause");
}
void Sort(LinkList head) { // 选择排序(按ISBN)
pNode pt,qt,q,p;
for(p = head; p->next; p = p->next) {
qt = p;
for(q = p->next; q->next; q = q->next)
if(strcmp(qt->next->isbn,q->next->isbn) > 0)
qt = q;
if(p != qt) { // 调整节点位置
pt = p->next;
p->next = qt->next;
qt->next = qt->next->next;
p->next->next = pt;
}
}
}
int Delete(LinkList head) {
pNode p,q;
char isbn[50],an[5];
printf("请输入欲删除图书ISBN : ");
gets(isbn);
for(p = head; p->next; p = p->next) {
if(strcmp(p->next->isbn,isbn) == 0) {
printf("将要删除图书《%s》!\n",p->next->title);
ShowData(p->next);
printf("1、删除,0、放弃!\n");
printf("请选择 : ");
fflush(stdin);
fgets(an,5,stdin);
if(an[0] == '1') {
q = p->next;
p->next = q->next;
free(q);
}
return 1;
}
}
printf("对不起,没有找到编号为%s的图书!\n",isbn);
return 0;
}
void Modify(LinkList head) {
pNode p = head->next;
char isbn[50],an[5];
int flag;
do {
printf("欲修改图书编号 : ");
fflush(stdin);
gets(isbn);
flag = 1;
while(p) {
if(strcmp(isbn,p->isbn) == 0) {
p = ReadData();
flag = 0;
break;
}
p = p->next;
}
if(flag) printf("没有找到编号为%s的图书。\n",isbn);
else printf("修改成功。\n\n");
printf("1、继续修改 0、返回主菜单\n");
fflush(stdin);
gets(an);
}while(an[0] == '1');
}
void FreeList(LinkList head) {
pNode q,p = head;
while(p) {
q = p->next;
free(p);
p = q;
}
}
void Menu(void) {
system("cls");
printf("**************************************************\n");
printf("* 1、添加 2、显示 *\n");
printf("* 3、排序 4、删除 *\n");
printf("* 5、修改 6、读文件 *\n");
printf("* 7、写文件 0、退出 *\n");
printf("**************************************************\n\n");
}
int main() {
int choice;
LinkList head = InitList();
do {
Menu();
printf("请选择 : ");
scanf("%d",&choice);
switch(choice) {
case 1 : Add(head); break;
case 2 : PrintBookList(head); break;
case 3 : Sort(head); break;
case 4 : Delete(head); break;
case 5 : Modify(head); break;
case 6 : ReadFile(head); break;
case 7 : WriteFile(head); break;
default : break;
}
}while(choice);
FreeList(head);
printf("End!\n");
return 0;
}
追问和要求不同,不需要排序,数量什么的
追答去掉不需要的是简单的,远比添加轻松的多。