包含的主文件如下
编译软件为vs code,code blocks也可以
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
图书信息的存储主要依靠结构体变量
即把上方的结构体看作“结构体数组的一个元素”,下方为“结构体数组的创建”
//图书信息 编号、名字、价格
typedef struct
{
char num[20];
char name[20];
float price;
}BOOK;
//自定义空表格
typedef struct
{
BOOK *head; //表格头地址 是动态变量 还未定义
BOOK *current;
int length; //表格长度(可容纳书本数量)
}biaoge;
此步骤作用是给结构体是申请足够大的空间
//初始化表格
biaoge *chushihua(biaoge *book)
{
book->head = (BOOK*)malloc(MAX*sizeof(BOOK)); //给head申请足够的内存空间 强制转化类型为“BOOK*”
if(!book->head)
{
printf("初始化失败\n"); //如果申请失败则提示
exit(0);
}
book->length = 0; //表格初始长度为0
return book;
}
然后是录入图书信息(这个函数写的是最不好的,有没有大佬可以帮我优化一下)
//录入图书信息
void shuru(biaoge *book)
{
book->length = 0;
char num[20] = {'0'};
char name[20] = {'0'};
float price;
BOOK *current = book->head;
printf("请输入图书信息(编号 书名 价格)以0 0 0结束:\n");
for(int i = 0;i < MAX;i++)
{
scanf("%s %s %f",&num,&name,&price);
if(strcmp(num,"0")==0)
{
break;
}
strcpy(current->num,num);
strcpy(current->name,name);
current->price=price;
book->length++;
current++;
}
printf("录入成功\n");
}
图书入库:实际上就是数组的插入,图书出库一样的道理
//图书入库
void charu(biaoge *book)
{
char num[20] = {'0'};
char name[20] = {'0'};
float price;
int n;
BOOK *current = book->head;
printf("请输入图书信息(编号 书名 价格):\n");
scanf("%s %s %f",&num,&name,&price);
printf("请输入插入位置(1-100)\n");
scanf("%d",&n);
current += n;
for(int i = book->length;i>=n;i--)
{
*(book->head+(i+1))=*(book->head+i);
}
strcpy(current->num,num);
strcpy(current->name,name);
current->price=price;
book->length++;
printf("入库成功\n");
}
图书查找:暴力遍历
//图书查找
void chazhao(biaoge *book)
{
char a[20] = {'0'};
int b = 0;
BOOK *current = book->head;
printf("请输入图书名称\n");
scanf("%s",&a);
while(strcmp(a,current->name)!=0)
{
if(b==book->length)
{
printf("未找到图书\n");
break;
}
b++;
current++;
}
if(strcmp(a,current->name)==0)
{
printf("图书信息如下\n%s %s %.2f\n",current->num,current->name,current->price);
}
}
然后有些简单的功能就不做介绍了 这是整个的代码
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
//图书信息 编号、名字、价格
typedef struct
{
char num[20];
char name[20];
float price;
}BOOK;
//自定义空表格
typedef struct
{
BOOK *head; //表格头地址 是动态变量 还未定义
BOOK *current;
int length; //表格长度(可容纳书本数量)
}biaoge;
//初始化表格
biaoge *chushihua(biaoge *book)
{
book->head = (BOOK*)malloc(MAX*sizeof(BOOK)); //给head申请足够的内存空间 强制转化类型为“BOOK*”
if(!book->head)
{
printf("初始化失败\n"); //如果申请失败则提示
exit(0);
}
book->length = 0; //表格初始长度为0
return book;
}
//录入图书信息
void shuru(biaoge *book)
{
book->length = 0;
char num[20] = {'0'};
char name[20] = {'0'};
float price;
BOOK *current = book->head;
printf("请输入图书信息(编号 书名 价格)以0 0 0结束:\n");
for(int i = 0;i < MAX;i++)
{
scanf("%s %s %f",&num,&name,&price);
if(strcmp(num,"0")==0)
{
break;
}
strcpy(current->num,num);
strcpy(current->name,name);
current->price=price;
book->length++;
current++;
}
printf("录入成功\n");
}
//打印图书信息
void print(biaoge *book)
{
BOOK *current = book->head;
for(int i = 0;i < book->length;i++)
{
printf("%s %s %.2f\n",current->num,current->name,current->price);
current++;
}
}
//图书查找
void chazhao(biaoge *book)
{
char a[20] = {'0'};
int b = 0;
BOOK *current = book->head;
printf("请输入图书名称\n");
scanf("%s",&a);
while(strcmp(a,current->name)!=0)
{
if(b==book->length)
{
printf("未找到图书\n");
break;
}
b++;
current++;
}
if(strcmp(a,current->name)==0)
{
printf("图书信息如下\n%s %s %.2f\n",current->num,current->name,current->price);
}
}
//图书入库
void charu(biaoge *book)
{
char num[20] = {'0'};
char name[20] = {'0'};
float price;
int n;
BOOK *current = book->head;
printf("请输入图书信息(编号 书名 价格):\n");
scanf("%s %s %f",&num,&name,&price);
printf("请输入插入位置(1-100)\n");
scanf("%d",&n);
current += n;
for(int i = book->length;i>=n;i--)
{
*(book->head+(i+1))=*(book->head+i);
}
strcpy(current->num,num);
strcpy(current->name,name);
current->price=price;
book->length++;
printf("入库成功\n");
}
//寻找价格最贵的图书
void zuigui(biaoge *book)
{
int price1 = book->head->price;
BOOK *current = book->head;
for(int i =0;i<book->length;i++)
{
if(book->head->price>price1)
{
current = book->head;
}
book->head++;
}
printf("最贵图书信息如下\n%s %s %.2f\n",current->num,current->name,current->price);
}
//图书涨价
void zhangjia(biaoge *book)
{
float num = 0;
BOOK *current = book->head;
for(int i = 0;i<book->length;i++)
{
num+=current->price;
current++;
}
float ave = num/(book->length);
current = book->head;
for(int i = 0;i<book->length;i++)
{
if((current->price)>=ave)
{
current->price=current->price*1.1;
}
else
{
current->price=current->price*1.2;
}
current++;
}
current = book->head;
printf("价格修改后信息如下\n");
for(int i = 0;i<book->length;i++)
{
printf("%s %s %.2f\n",current->num,current->name,current->price);
current++;
}
}
//主菜单
void menu(biaoge *book)
{
int a;
printf("---------------------------------------------\n");
printf("---------------------------------------------\n");
printf("-------------- 图书管理系统 -------------\n");
printf("-------------- 1.录入图书信息 -------------\n");
printf("-------------- 2.查找图书 -------------\n");
printf("-------------- 3.新图书入库 -------------\n");
printf("-------------- 4.价格最贵图书 -------------\n");
printf("-------------- 5.图书涨价 -------------\n");
printf("-------------- 6.打印图书信息 -------------\n");
printf("---------------------------------------------\n");
printf("---------------------------------------------\n");
printf("请输入您的选择\n");
scanf("%d",&a);
switch(a)
{
case 1:
shuru(&book);
break;
case 2:
chazhao(&book);
break;
case 3:
charu(&book);
break;
case 4:
zuigui(&book);
break;
case 5:
zhangjia(&book);
break;
case 6:
print(&book);
break;
default:
printf("输入非法\n");
}
}
void main()
{
biaoge book ;
chushihua(&book);
while(1)
{
menu(&book);
}
system("pause");
}