图书馆管理系统
C语言实现一个简易的图书馆管理系统,利用线性表的链式存储结构。
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
#include "Common.h"
typedef struct book
{
char no[20];
char name[30];
float price;
}Book;
typedef struct Node
{
Book* elem;
int length;
int caption;
}Sqlist;
void menu() {
printf("----------------------------------------");
printf("\n1.查找图书\n2.插入一本书\n3.打印图书\n");
printf("4.取值\n");
printf("5.删除\n");
printf("6.清屏\n");
printf("7.退出系统\n");
printf("----------------------------------------\n");
}
Status Init(Sqlist& L)
{
L.elem = (Book*)malloc(sizeof(Book));
if (!L.elem) exit(OVERFLOW);
L.length = 0;
L.caption = MAXSIZE;
return OK;
}
Status Get(Sqlist L, int i, Book& m)
{
if (i<0 || i>L.length)
return ERROR;
m = L.elem[i - 1];
return OK;
}
Status Create(Sqlist& L)
{
int n;
printf("请输入图书数量:");
scanf("%d", &n);
int i;
for (i = 0; i < n; i++) {
printf("请输入第%d本图书的编号、名字、价格:",i+1);
scanf("%s %s %f", &L.elem[i].no, &L.elem[i].name, &L.elem[i].price);
}
L.length = i;
return OK;
}
Status Dele(Sqlist& L, int i, Book& e)
{
if (i<0 || i>L.length)
return ERROR;
e = L.elem[i - 1];
for (int j = i - 1; j < L.length; j++)
{
L.elem[j] = L.elem[j + 1];
}
--L.length;
return OK;
}
Status local(Sqlist L, char a[20])
{
for (int i = 0; i < L.length; i++)
{
if (strcmp(L.elem[i].name, a) == 0)
return i + 1;
}
return ERROR;
}
Status Insert(Sqlist& L, int i, Book e)
{
if (i<0 || i>L.length)
return ERROR;
for (int j = L.length; j >= i; j--)
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
++L.length;
return OK;
}
void print(Sqlist L)
{
for (int i = 0; i < L.length; i++)
printf("%-5s %-5s %.1f\n", L.elem[i].no, L.elem[i].name, L.elem[i].price);
}
int main()
{
Sqlist S;
Init(S);
Create(S);
menu();
Book e, k;
int count = 0, i = 0, m = 0;
char a[20];
while (count != 7)
{
printf("请选择:\n");
scanf("%d", &count);
switch (count)
{
case 1:
printf("请输入要查找图书的名字:");
scanf("%s", a);
if (local(S, a))
m = local(S, a);
if (m != 0) {
printf("图书位置:%d\n", m);
}
else printf("没有这本书\n");;
break;
case 2:
printf("请输入要插入图书的编号、名字、价格、位置:");
scanf("%s %s %f %d", &e.no, &e.name, &e.price, &i);
Insert(S, i, e);
break;
case 3:
print(S);
break;
case 4:
printf("请输入要取值的位置:");
int n;
scanf("%d", &n);
Get(S, n, k);
printf("%-5s %-5s %.1f\n", k.no, k.name, k.price);
break;
case 5:
printf("请输入要删除图书的位置:");
int d;
Book q;
scanf("%d", &d);
Dele(S, d, q);
printf("您删除的书是:%s %s %f\n", q.no, q.name, q.price);
break;
case 6:
system("cls");
menu();
break;
default:
printf("已退出系统!\n");
break;
}
}
printf("操作后的图书:\n");
print(S);
return 0;
}
|