请设计一个简单的图书目录管理系统(初级版)。
图书目录信息包括:
统一书号(不超过13字符)
书名(不超过30字符)
作者(不超过20字符)
出版社(不超过30字符)
出版日期(包括:年、月、日)
价格(精确到分)
注意:为了保证运行窗口能正确地显示,请右击运行窗口,修改“属性”:
在“选项”选项卡中,选中“使用旧版本控制台”
在“字体”选项卡中,选择“8×16”“点阵字体”
在“布局”选项卡中,将屏幕窗口宽度均设为“120”。
具体需求如下:
显示如下的主菜单:
Append Find Remove Modify Show Quit > _
如果用户输入 A 或 a、F 或 f、R 或 r、M 或 m、S 或 s,则可完成相应的操作。如果用户输入其它字符,则显示错误信息。
程序将反复显示主菜单,让用户持续工作。如果用户输入 Q 或 q,则程序结束。
Append Find Remove Modify Show Quit > Q
Thank you! Goodbye!
若用户输入其它字符,则显示错误信息。
Append Find Remove Modify Show Quit > B
Incorrect choice!
Append Find Remove Modify Show Quit > 9
Incorrect choice!
Append Find Remove Modify Show Quit > +
Incorrect choice!
若用户输入 A 或 a,则可以输入新书的信息,将其添加到图书目录中。
Append Find Remove Modify Show Quit > a
ISBN: 9780439227148
Title: The Call of the Wild
Author: Jack London
Publisher: Scholastic Press
Pub date: 2001/1/1
Price: 39.4
Append Find Remove Modify Show Quit > A
ISBN: 9781772262902
Title: Oliver Twist
Author: Charles Dickens
Publisher: Engage Books
Pub date: 2016/9/15
Price: 648
Append Find Remove Modify Show Quit > a
ISBN: 9787515911076
Title: The Call of the Wild
Author: Jack London
Publisher: Aerospace Publishing House
Pub date: 2016/5/1
Price: 29.8
若用户输入的日期信息不正确,则显示错误信息,并要求用户重新输入。
Append Find Remove Modify Show Quit > A
ISBN: 9787501592401
Title: The Old Man and the Sea
Author: Ernest Hemingway
Publisher: Knowledge Press
Pub date: 2016/2/30
Incorrect date! Please reenter: 2016/6/31
Incorrect date! Please reenter: 2016/8/1
Price: 25.8
若用户输入 S 或 s,则按书号升序排序,然后列表显示全部图书。
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
若用户输入 F 或 f,则输入书名,然后显示该书名的图书。如果没有对应的图书,则显示错误信息。
Append Find Remove Modify Show Quit > F
Title: The Call of the Wild
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > f
Title: Gulliver's Travels
Not found!
说明:输出查找结果时,不作排序操作。
若用户输入 R 或 r,则输入书号,然后将删除该书号的图书。如果没有对应的图书,则显示错误信息。
Append Find Remove Modify Show Quit > r
ISBN: 9781772262902
Remove(y/n)? n
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > R
ISBN: 9780439227148
Remove(y/n)? Y
Append Find Remove Modify Show Quit > s
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > r
ISBN: 9787515914145
Not found!
Append Find Remove Modify Show Quit > s
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
要求:用户回答是否删除时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。
Append Find Remove Modify Show Quit > R
ISBN: 9781772262902
Remove(y/n)? k
Incorrect answer!
Remove(y/n)? $
Incorrect answer!
Remove(y/n)? N
若用户输入 M 或 m,则可以修改图书信息。首先按书号查找,然后重新输入该图书的信息。
Append Find Remove Modify Show Quit > m
ISBN: 9787515911076
Modify(y/n)? y
ISBN: 9787544724968
Title: The House on Mango Street
Author: Sandra Heathneros
Publisher: Yilin Press
Pub date: 2012/1/1
Price: 30
Append Find Remove Modify Show Quit > M
ISBN: 9787501592401
Modify(y/n)? n
Append Find Remove Modify Show Quit > m
ISBN: 9787515914145
Not found!
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787544724968 The House on Mango Street Sandra Heathneros Yilin Press 2012/01/01 30.00
要求:用户回答是否修改时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。
Append Find Remove Modify Show Quit > M
ISBN: 9787544724968
Modify(y/n)? K
Incorrect answer!
Modify(y/n)? *
Incorrect answer!
Modify(y/n)? n
相关习题:图书目录管理系统(高级版)。
Append Find Remove Modify Show Quit > Thank you! Goodbye!
q
代码
注:代码还有很多bug,请各路大神指点一二
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct bookInfo //创建结构体存储书籍数据
{
char ISBN[13];
char Title[30];
char Author[20];
char Publisher[30];
char Pub_date[10];
double Price;
};
struct Node
{
struct bookInfo data;
struct Node* next;
};
struct Node* list = NULL;//初始化
struct Node* createHead()//建立表头
{
//动态分配内存
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//变量初始化
headNode->next = NULL;
return headNode;
}
struct Node* createNode(struct bookInfo data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//表头插入法
void Append(struct Node* headNode,struct bookInfo data)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//查找
struct Node* Find(struct Node* headNode,char* Title)
{
struct Node* posNode = headNode->next;
while (posNode != NULL && strcmp(posNode->data.Title, Title))
{
posNode = posNode->next;
}
return posNode;
}
void Remove(struct Node* headNode,char* ISBN)
{
struct Node* posLeftNode = headNode;
struct Node* posNode = headNode->next;
while (posNode != NULL && strcmp(posNode->data.ISBN, ISBN))
{
posLeftNode = posNode;
posNode = posLeftNode->next;
}
if (posNode == NULL)
{
return;
}
else
{
posLeftNode->next = posNode->next;
free(posNode);
posNode = NULL;
}
}
/*若用户输入 M 或 m,则可以修改图书信息。首先按书号查找,然后重新输入该图书的信息。*/
void Modify(struct Node* headNode,char* ISBN )
{
struct Node* posNode = headNode->next;
while (posNode != NULL && strcmp(posNode->data.ISBN, ISBN))
{
posNode = posNode->next;
}
char userKey;
printf("Modify(y/n)? ");
userKey = getchar();
if (posNode != NULL && (userKey == 'y'|| userKey == 'Y'))
{
scanf("%s%s%s%s%s%.2f\n", &posNode->data.ISBN, posNode->data.Title, posNode->data.Author, posNode->data.Publisher, posNode->data.Pub_date, &posNode->data.Price);
//scanf("%s", posNode->data.ISBN);
//scanf("%s", posNode->data.Title);
//scanf("%s", posNode->data.Author);
//scanf("%s", posNode->data.Publisher);
//scanf("%s", posNode->data.Pub_date);
//scanf("%.2f", &posNode->data.Price);
fflush(stdin);
}
else
{
return;
}
}
void bubluSortList(struct Node* headNode)
{
for (struct Node* p = headNode->next; p != NULL; p = p->next)
{
for (struct Node* q = p; q != NULL; q = q->next)
{
if (q->data.ISBN > q->next->data.ISBN)
{
struct bookInfo tempData = q->data;
q->data = q->next->data;
q->next->data = tempData;
}
}
}
}
void Show(struct Node* headNode)
{
bubluSortList(headNode);
struct Node* pMove = headNode->next;
printf("ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---\n");
while (pMove != NULL)
{
printf("%s\t%s\t%s\t%s\t%s\t%.2f\n", pMove->data.ISBN, pMove->data.Title, pMove->data.Author, pMove->data.Publisher, pMove->data.Pub_date, pMove->data.Price);
//printf("%s\t", pMove->data.ISBN);
//printf("%s\t", pMove->data.Title);
//printf("%s\t", pMove->data.Author);
//printf("%s\t", pMove->data.Publisher);
//printf("%s\t", pMove->data.Pub_date);
//printf("%.2f\n", pMove->data.Price);
fflush(stdin);
pMove = pMove->next;
}
}
void saveInfoToFile(const char* fileName, struct Node* headNode)
{
FILE* fp = fopen(fileName, "w");
struct Node* pMove = headNode->next;
while (pMove != NULL)
{
fprintf("%s\t%s\t%\ts%\ts%\ts%.2f\n", pMove->data.ISBN, pMove->data.Title, pMove->data.Author, pMove->data.Publisher, pMove->data.Pub_date, pMove->data.Price);
//fprintf("%s\t", pMove->data.ISBN);
//fprintf("%s\t", pMove->data.Title);
//fprintf("%s\t", pMove->data.Author);
//fprintf("%s\t", pMove->data.Publisher);
//fprintf("%s\t", pMove->data.Pub_date);
//fprintf("%.2f\n", &pMove->data.Price);
pMove = pMove->next;
}
fclose(fp);
}
void readInfoToFile(const char* fileName, struct Node* headNode)
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
fp = fopen(fileName, "w+");
}
struct bookInfo tempData ;
while (fscanf("%s %s %s %s %s %.2f\n", tempData.ISBN, tempData.Title, tempData.Author, tempData.Publisher, tempData.Pub_date, &tempData.Price) != EOF)
{
Append(list,tempData);
}
fclose(fp);
}
void menu()
{
char ret=0;
struct bookInfo tempData ={0,0,0,0,0,0};
struct Node* result = NULL;
ret = getchar();
switch (ret) {
case 'A':
case 'a':
//scanf("%s", tempData.ISBN);
//scanf("%s", tempData.Title);
//scanf("%s", tempData.Author);
//scanf("%s", tempData.Publisher);
//scanf("%s", tempData.Pub_date);
//scanf("%.2f", &tempData.Price);
scanf_s("%s%s%s%s%s%.2f\n", tempData.ISBN, tempData.Title, tempData.Author, tempData.Publisher, tempData.Pub_date, &tempData.Price);
Append(list,tempData);
saveInfoToFile("bookinfo.txt", list);
fflush(stdin);
break;
case 'F':
case 'f':
scanf("%s", tempData.ISBN);
result=Find(list,tempData.ISBN);
if (result == NULL)
{
printf("Not founf!\n");
}
else
{
printf("ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---\n");
printf("%s\t%s\t%\ts%\ts%\ts%.2f\n", result->data.ISBN, result->data.Title, result->data.Author, result->data.Publisher, result->data.Pub_date, result->data.Price);
//printf("%s\t", result->data.ISBN);
//printf("%s\t", result->data.Title);
//printf("%s\t", result->data.Author);
//printf("%s\t", result->data.Publisher);
//printf("%s\t", result->data.Pub_date);
//printf("%.2f\n", result->data.Price);
fflush(stdin);
}
break;
case 'R':
case 'r':
printf("ISBN :");
scanf("%s", tempData.ISBN);
Remove(list,tempData.ISBN);
saveInfoToFile("bookinfo.txt", list);
fflush(stdin);
break;
case 'M':
case 'm':
printf("ISBN :");
scanf("%s", tempData.ISBN);
Modify(list, tempData.ISBN);
saveInfoToFile("bookinfo.txt", list);
fflush(stdin);
break;
case 'S':
case 's':
Show(list);
break;
case 'Q':
case 'q':
printf("Thank you! Goodbye!");
system("pause");
exit(0);
//Quit();
break;
default:
printf("Incorrect choice!\n");
fflush(stdin);
break;
}
}
int main()
{
list = createHead();
readInfoToFile("bookinfo.txt",list);
while (1)
{
printf("Append Find Remove Modify Show Quit > \n");
menu();
system("pause");
system("cls");
}
system("pause");
return 0;
}