BUAA_数据结构_2nd_小型图书管理系统
题目描述
小明同学特别喜欢买书看书。由于书较多,摆放杂乱,找起来非常麻烦。这学期小明同学上了数据结构与程序设计课后,决定改变这种状况:用C开发一个小型图书管理系统。系统中包含的图书信息有:书名、作者、出版社、出版日期等。首先,图书管理系统对已有的书(原始书库,存放在一个文本文件中)按书名字典序进行(按书名中各字符的ASCII码值由小到大排序)摆放(即将原始无序的图书信息文件生成一个有序的文件,即新书库),以便查找。该管理系统可以对新书库中图书条目进行如下操作:
1.录入。新增书录入到书库中(即从输入中读入一条图书信息插入到已排序好的图按书文件相关位置处)
2.查找。按书名或书名中关键字信息在书库中查找相关图书信息,若有多本书,按字典序输出。
3.删除。输入书名或书名中关键字信息,从书库中查找到相关书并将其删除,并更新书库。
输入形式
原始的图书信息(原始书库)保存在当前目录下的books.txt中。
用户操作从控制台读入,首先输入操作功能序号(1代表录入操作,2代表查找操作,3代表删除操作,0代表将已更新的图书信息保存到书库中并退出程序),然后在下一行输入相应的操作信息(录入操作后要输入一条图书信息,查找和删除操作后只要输入书名或书名中部分信息)。程序执行过程中可以进行多次操作,直到退出(输入操作0)程序。
要求:
1、原始文件中的图书信息与录入的图书信息格式相同,每条图书信息都在一行上,包括书名(不超过50个字符)、作者(不超过20个字符)、出版社(不超过30个字符)和出版日期(不超过10个字符),只由英文字母和下划线组成,用一个空格分隔。图书信息总条数不会超过500.
2、下划线字符参加排序。
3、图书不会重名。
输出形式
进行录入和删除操作,系统会更新图书信息,但不会在控制台窗口显示任何信息。
进行查找操作后,将在控制台按书名字典序分行输出查找到的图书信息,书名占50个字符宽度,作者占20个字符宽度,出版社占30个字符宽度,出版日期占10个字符宽度,都靠左对齐输出。
最终按字典排序的图书信息保存在当前目录下的ordered.txt中,每条图书信息占一行,格式与查找输出的图书信息相同。
参考代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct node{//节点的类型
char name[100];
char author[30];
char publish[50];
char date[20];
struct node *next;//指向下一个节点的指针
};
//typedef : 数据类型重命名
typedef struct node node;
typedef struct node *node_list;
//int list_insert_pre_node_temp(node_list *head,node_list *p,int element);//指定结点的前插O(n)
int list_insert_pre_node(node_list p,char name[],char author[],char publish[],char date[]);//指定结点的前插O(1)
int list_insert_next_node(node_list p,char name[],char author[],char publish[],char date[]);//指定结点的后插
int list_delete_node_normal(node_list *head,node *p);//O(n)删除一个指定结点
void list_print(node_list temp);
node_list pubbleSort(node_list head);
int find_inf(char name[],char inf[]);
char name[100];
char author[30];
char publish[50];
char date[20];
char inf[100];
int main()
{
FILE *sin,*sout;
//sin=fopen("books.txt", "r");
sin=fopen("books.txt", "r");
sout=fopen("ordered.txt", "w");
node_list head=(node *)malloc(sizeof(node));
head->next=NULL;
node *now,*tail=head;
while((fscanf(sin,"%s%s%s%s",name,author,publish,date))!=EOF){
//printf("%s %s %s %s\n",name,author,publish,date);
now=(node *)malloc(sizeof(node));
strcpy(now->name,name);
strcpy(now->author,author);
strcpy(now->publish,publish);
strcpy(now->date,date);
tail->next=now;
tail=now;
}
tail->next=NULL;
fclose(sin);
//list_print(head);
if(NULL!=head->next){
head=pubbleSort(head);
//list_print(head);
}
int op;
while(1){
scanf("%d",&op);
if(op==1){//insert
scanf("%s%s%s%s",name,author,publish,date);
if(NULL==head->next){
now=(node *)malloc(sizeof(node));
strcpy(now->name,name);
strcpy(now->author,author);
strcpy(now->publish,publish);
strcpy(now->date,date);
head->next=now;
tail=now;
tail->next=NULL;
}
else{
node *i=head->next;
for(i;NULL!=i;i=i->next){
if(strcmp(i->name,name)>0){
list_insert_pre_node(i,name,author,publish,date);
//list_print(head);
break;
}
if(NULL==i->next){
list_insert_next_node(i,name,author,publish,date);
break;
}
}
}
}
else if(op==2){//find
scanf("%s",inf);
//printf("%s\n",inf);
node *i=head->next;
for(i;NULL!=i;i=i->next){
if(find_inf(i->name,inf)){
printf("%-50s%-20s%-30s%s\n",i->name,i->author,i->publish,i->date);
}
}
}
else if(op==3){//delete
scanf("%s",inf);
node *i=head->next;
for(i;NULL!=i;i=i->next){
if(find_inf(i->name,inf)){
list_delete_node_normal(head,i);
}
}
}
else if(op==0){
node_list temp=head;
temp=temp->next;
while(NULL!=temp){
fprintf(sout,"%-50s%-20s%-30s%s\n",temp->name,temp->author,temp->publish,temp->date);
temp=temp->next;
}
fclose(sout);
break;
}
}
return 0;
}
int find_inf(char name[],char inf[])
{
int len_name=strlen(name);
int len_inf=strlen(inf);
for(int i=0;i<len_name;i++){
if(name[i]==inf[0]){
int look=0;
while(look<len_inf){
if(name[i+look]!=inf[look]){
break;
}
if(look==len_inf-1){
return 1;
}
look++;
}
}
}
return 0;
}
node_list pubbleSort(node_list head)
{
node *i=head->next;
for(i;NULL!=i;i=i->next){
for(node *j=head->next;NULL!=j;j=j->next){
if(strcmp(i->name,j->name)<0){
node *temp=(node *)malloc(sizeof(node));
strcpy(temp->name,i->name);
strcpy(temp->author,i->author);
strcpy(temp->publish,i->publish);
strcpy(temp->date,i->date);
strcpy(i->name,j->name);
strcpy(i->author,j->author);
strcpy(i->publish,j->publish);
strcpy(i->date,j->date);
strcpy(j->name,temp->name);
strcpy(j->author,temp->author);
strcpy(j->publish,temp->publish);
strcpy(j->date,temp->date);
}
}
}
return head;
}
int list_insert_pre_node(node_list p,char name[],char author[],char publish[],char date[])//指定结点的前插O(1)
//偷天换日
{
if(NULL==p) return 0;
node *new_node=(node *)malloc(sizeof(node));
if(NULL==new_node) return 0;
new_node->next=p->next;
p->next=new_node;
strcpy(new_node->name,p->name);
strcpy(new_node->author,p->author);
strcpy(new_node->publish,p->publish);
strcpy(new_node->date,p->date);
strcpy(p->name,name);
strcpy(p->author,author);
strcpy(p->publish,publish);
strcpy(p->date,date);
return 1;
}
int list_insert_next_node(node_list p,char name[],char author[],char publish[],char date[])//指定结点的后插
{
if(NULL==p) return 0;
node *new_node=(node *)malloc(sizeof(node));
if(NULL==new_node) return 0;
strcpy(new_node->name,name);
strcpy(new_node->author,author);
strcpy(new_node->publish,publish);
strcpy(new_node->date,date);
new_node->next=p->next;
p->next=new_node;
return 1;
}
int list_delete_node_normal(node_list *head,node *p)//O(n)删除一个指定结点
{
if(NULL==p) return 0;
node_list pre;
pre=head;
while(pre->next!=p&&pre->next!=NULL){
pre=pre->next;
}
if(pre->next!=NULL){
pre->next=p->next;
free(p);
}
return 1;
}
void list_print(node_list temp)
{
temp=temp->next;
while(temp!=NULL){
printf("%s %s %s %s\n",temp->name,temp->author,temp->publish,temp->date);
temp=temp->next;
}
}
有问题
或bug欢迎私戳/评论