C语言实现一个简易的图书馆管理系统,利用线性表的链式存储结构。
- 头文件Common.h
#pragma once //包含头文件 #define _CRT_SECURE_NO_WARNINGS #include#include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 typedef int Status;//自定义类型 typedef int ElemType;//自定义数据类型
- main.c
#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 %.1fn", 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("图书位置:%dn", 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 %.1fn", k.no, k.name, k.price); break; case 5: printf("请输入要删除图书的位置:"); int d; Book q; scanf("%d", &d); Dele(S, d, q); printf("您删除的书是:%s %s %fn", q.no, q.name, q.price); break; case 6: system("cls"); menu(); break; default: printf("已退出系统!n"); break; } } printf("操作后的图书:n"); print(S); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)