C语言大作业图书管理系统
程序员文章站
2022-05-06 09:28:55
...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
typedef struct Books {
char num[20];
char name[20];
char author[20];
int price;
int total;
char introduce[50];
}BK;
BK bk[N];
void inputBk(BK *p, int i);
void deleteBK(char bkNum[]);
void judge(int jdg);
void sortInfo(BK bk[], int n);
void swapInfo(BK *p, BK *q);
void outputInfo(BK *p);
void updateInfo(char bkNum[]);
void serchInfo(char bkNum[]);
int i, tmp=0, flag = 1, menu;
void main() {
while (flag) {
do {
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("请输入选项:");
scanf_s("%d", &menu);
} while (menu>6 || menu<1);
judge(menu);
}
getchar(); getchar(); getchar();
}
void judge(int jdg) {
int n;
char bkNum[20];
switch (jdg) {
case 1:
printf("请输入要录入书籍的个数:");
scanf_s("%d", &n);
for (i = tmp; i<n + tmp; i++) {
inputBk(&bk[i], i);
}
tmp = n + tmp;//tmp很关键,记录数量避免多次添加,前面数据被覆盖
break;
case 2:
printf("请输入要删除的书号:");
scanf("%s", bkNum);
deleteBK(bkNum);
break;
case 3:
sortInfo(bk,tmp);
printf("排序后:\n");
for (i = 0; i < tmp; i++) {
outputInfo(&bk[i]);
}
break;
case 4:
printf("请输入要修改的书号:");
scanf("%s", bkNum);
updateInfo(bkNum);
break;
case 5:
printf("请输入要查询的书号:");
scanf("%s", bkNum);
serchInfo(bkNum);
break;
case 6:
flag = 0;
break;
}
}
void inputBk(BK *p, int i) {
printf("请录入第%d本书:\n",i+1);
printf("请输入书号:");
scanf("%s",p->num);
printf("请输入书名:");
scanf("%s", p->name);
printf("请输入作者:");
scanf("%s", p->author);
printf("请输入单价:");
scanf_s("%d", &(p->price));
printf("请输入数量:");
scanf_s("%d", &(p->total));
printf("请输入简介:");
scanf("%s", p->introduce);
}
void deleteBK(char bkNum[]) {
int assert=0;
for (i = 0; i<tmp; i++) {
if (strcmp(bkNum, bk[i].num) == 0) {
for (int j = i; j<tmp - 1; j++) {
strcpy(bk[j].num ,bk[j + 1].num);
strcpy(bk[j].name, bk[j + 1].name);
strcpy(bk[j].author, bk[j + 1].author);
strcpy(bk[j].introduce, bk[j + 1].introduce);
bk[j].price = bk[j].price;
bk[j].total = bk[j].total;
tmp = tmp - 1;//记录tmp数量
assert = 1;
break;
}
}
}
if (assert) {
printf("已经删除该书籍信息\n");
}else {
printf("没有找到该书籍\n");
}
}
void sortInfo(BK bk[],int n) {
int i, j;
for (i = 0; i < n - 1;i++) {
for (j = 0; j < n - 1-i;j++) {
if (bk[j].price > bk[j+1].price) {
swapInfo(&bk[j],&bk[j+1]);
}
}
}
}
void swapInfo(BK *p,BK *q) {
BK trmp;
trmp = *p; *p = *q; *q = trmp;
}
void outputInfo(BK *p) {
printf("%s\t%s\t%s\t%d\t%d\t%s\n", p->num, p->name, p->author, p->price, p->total, p->introduce);
}
void updateInfo(char bkNum[]) {
int assert = 0;
for (i = 0; i<tmp; i++) {
if (strcmp(bkNum, bk[i].num) == 0) {
printf("请修改图书馆第%d本书:\n", i + 1);
printf("请修改书号:");
scanf("%s", bk[i].num);
printf("请修改书名:");
scanf("%s", bk[i].name);
printf("请修改作者:");
scanf("%s", bk[i].author);
printf("请修改单价:");
scanf_s("%d", &(bk[i].price));
printf("请修改数量:");
scanf_s("%d", &(bk[i].total));
printf("请修改简介:");
scanf("%s", bk[i].introduce);
assert = 1;
break;
}
}
if (assert) {
printf("已经修改该书籍信息\n");
}
else {
printf("没有找到该名称书籍\n");
}
}
void serchInfo(char bkNum[]) {
int assert = 0;
for (i = 0; i<tmp; i++) {
if (strcmp(bkNum, bk[i].num) == 0) {
outputInfo(&bk[i]);
assert = 1;
break;
}
}
if (assert) {
printf("已经查询到该书籍信息\n");
}
else {
printf("没有找到该名称的书籍\n");
}
}