#define MAXSIZE 10000
#define OVERFLOW -2
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct
{
Book data[MAXSIZE];
Book* elem;
int length;
}List;
int InitList(List* pl)
{
pl->elem = (Book*)malloc(MAXSIZE * sizeof(Book));
if (!pl->elem) exit(OVERFLOW);
pl->length = 0;
return 1;
}
void InsertList(List* pl)
{
if (pl->length == MAXSIZE)
{
printf("图书表已满。\n");
}
else {
printf("请输入书号:\n");
scanf("%s", pl->data[pl->length].no);
printf("请输入书名:\n");
scanf("%s", pl->data[pl->length].name);
printf("请输入价格:\n");
scanf("%f", &(pl->data[pl->length].price));
pl->length++;
}
}
void PrintList(List* pl)
{
int i = 0;
printf("%-10s\t %-20s\t %-6s\n", "书号", "书名", "价格");
for (i = 0; i < pl->length; i++)
{
printf("%-10s\t %-20s\t %-6f\n", pl->data[i].no,pl->data[i].name,pl->data[i].price);
}
}
void DeleteList(List* pl)
{
char name[20] = { 0 };
if (pl->length == 0)
{
printf("图书表为空");
}
else {
printf("请输入要删除的书名\n");
scanf("%s", name);
}
int flag = FindByName(pl,name);
if (flag == -1)
{
printf("不存在此书");
return;
}
int i = 0;
for (i = flag; i < pl->length - 1; i++)
{
pl->data[i] = pl->data[i + 1];
}
pl->length--;
}
int FindByName(List* pl, char name[20])
{
int i = 0;
for (i = 0; i < pl->length; i++)
{
if (strcmp(pl->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void Find(List* pl)
{
char name[20] = { 0 };
printf("请输入要查找的书名\n");
scanf("%s", name);
int flag = FindByName(pl, name);
if (flag == -1)
{
printf("不存在此书");
return;
}
else {
printf("%-10s\t %-20s\t %-6f\n", pl->data[flag].no, pl->data[flag].name, pl->data[flag].price);
}
}
void ModifyList(List* pl)
{
char name[20] = { 0 };
printf("请输入要修改的书名\n");
scanf("%s", name);
int flag = FindByName(pl, name);
if (flag == -1)
{
printf("不存在此书");
return;
}
else {
printf("请输入书号:\n");
scanf("%s", pl->data[flag].no);
printf("请输入书名:\n");
scanf("%s", pl->data[flag].name);
printf("请输入价格:\n");
scanf("%f", &(pl->data[flag].price));
}
}
void menu()
{
printf("*****1.增加 2.删除\n");
printf("*****3.查找 4.修改\n");
printf("*****5.排序 6.查询全部\n");
printf("*****0.退出\n");
}
int main() {
int input=0;
List l;
InitList(&l);
do
{
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case 1:
InsertList(&l);
break;
case 2:
DeleteList(&l);
break;
case 3:
Find(&l);
break;
case 4:
ModifyList(&l);
break;
case 5:
break;
case 6:
PrintList(&l);
break;
case 0:
break;
default:
printf("输入有误,请重输\n");
}
}while (input);
return 0;
}
C语言图书管理系统
最新推荐文章于 2024-08-17 09:48:23 发布