说明:这是本人2021年寒假在家写的一个C语言大作业,供大家参考,写的不好的地方大家指正,有需要的朋友可以拿走,题目要求如下:
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct Number
{
char alph;
int num;
};
struct Book
{
char title[128];
char public[128];
char author[128];
int price;
struct Number number;
struct Book *next;
};
void menu()
{
printf("=====菜单=====\n");
printf("1.插入书包\n");
printf("2.删除书本\n");
printf("3.修改书本\n");
printf("4.打印书籍信息\n");
printf("5.退出\n");
printf("请输入你要执行的操作:");
}
void inseart(struct Book **head)
{
srand(time(0));
struct Book *previous;
struct Book *current;
struct Book *new;
new=(struct Book*)malloc(sizeof(struct Book));
int num=rand()%900000+100000;
int alph=rand()%26;
alph+='A';
new->number.num=num;
new->number.alph=alph;
printf("请输入书名:");
scanf("%s",&new->title);
printf("请输入出版社:");
scanf("%s",&new->public);
printf("请输入作者:");
scanf("%s",&new->author);
printf("请输入价格:");
scanf("%d",&new->price);
current=*head;
previous=NULL;
while((current!=NULL)&&(current->number.num>num))
{
previous=current;
current=current->next;
}
new->next=current;
if(previous==NULL)
{
*head=new;
}
else
{
previous->next=new;
}
}
void delete(struct Book **head)
{
int num;
printf("请输入你要删除的书的编号(数字部分):");
scanf("%d",&num);
struct Book *previous;
struct Book *current;
current=*head;
previous=NULL;
while(current!=NULL&¤t->number.num!=num)
{
previous=current;
current=current->next;
}
if(current==NULL)
{
printf("找不到匹配的结点\n");
return;
}
else
{
if(previous==NULL)
{
*head=current->next;
}
else
{
previous->next=current->next;
}
free(current);
printf("删除成功!\n");
}
}
void print(struct Book *head)
{
struct Book *book;
int cnt=1;
book=head;
while(book!=NULL)
{
printf("Book%d\n",cnt);
printf("书名:%s\n",book->title);
printf("出版社:%s\n",book->public);
printf("作者:%s\n",book->author);
printf("价格:%d\n",book->price);
printf("编号:%c%d\n",book->number.alph,book->number.num);
book=book->next;
}
printf("========\n");
}
void modify(struct Book *head)
{
int num;
struct Book *book;
book=head;
printf("请输入你要修改的书籍的编号(数字部分):");
scanf("%d",&num);
while(book->number.num!=num)
{
book=book->next;
}
if(book==NULL)
{
printf("很抱歉没有找到书籍信息,你输入的编号有误\n");
}
else
{
printf("已找到你要修改的书籍.....\n");
printf("书名:%s\n",book->title);
printf("出版社:%s\n",book->public);
printf("作者:%s\n",book->author);
printf("价格:%d\n",book->price);
printf("编号:%c%d\n",book->number.alph,book->number.num);
modifybook();
int get;
scanf("%d",&get);
if(get==1)
{
printf("请输入修改后的书名:");
scanf("%s",&book->title);
}
else if(get==2)
{
printf("请输入修改后的出版社:");
scanf("%s",&book->public);
}
else if(get==3)
{
printf("请输入修改后的作者:");
scanf("%s",&book->author);
}
else if(get==4)
{
printf("请输入修改后的价格:");
scanf("%d",&book->price);
}
printf("修改成功!\n");
}
}
void modifybook()
{
printf("=====请选择要修改的信息=====\n");
printf("1.书名\n");
printf("2.出版社\n");
printf("3.作者\n");
printf("4.价格\n");
printf("请输入:");
}
void release(struct Book *head)
{
struct Book *book;
book=head;
while(book!=NULL)
{
free(book);
book=book->next;
}
}
int main(void)
{
struct Book *head=NULL;
while(1)
{
int ch;
menu();
scanf("%d",&ch);
if(ch==1)
{
inseart(&head);
}
else if(ch==2)
{
delete(&head);
}
else if(ch==3)
{
modify(head);
}
else if(ch==4)
{
print(head);
}
else if(ch==5)
{
break;
}
else
{
printf("你输入的操作有误\n");
}
}
release(head);
return 0;
}