基于顺序表的图书管理系统(C语⾔)Visual Studio编译环境
功能:
0、退出。
1、基于顺序存储结构的图书信息表的创建和输出。
2、基于顺序存储结构的图书信息表的新图书的⼊库。
3、基于顺序存储结构的图书信息表的旧图书的出库。
4、基于顺序存储结构的图书信息表按书号查。
5、基于顺序存储结构的图书信息表按价格区间查。
6、基于顺序存储结构的按图书价格升序排序。
7、基于顺序存储结构的按图书价格修改。
8、基于顺序存储结构的按图书价格普调。
9、基于顺序存储结构的按最贵图书查。
10、基于顺序存储结构的图书去重。
11、遍历。
创建头⽂件 struct.h 定义数据类型和存储结构
1#pragma once
2
3#define OK 1
4#define ERROR 0
5#define OVERFLOW -2
6#define MAXSIZE 100  //顺序表可能达到的最⼤长度
c语言struct头文件7typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
8
9
10struct Book {
11 char id[15];//ISBN//
12 char name[25];//书名
13 double price;//定价
14};
15typedef struct {
16 Book* elem; //存储空间的基地址
17 int length; //当前长度
18} SqList;
创建 function_declare.h 头⽂件把所有的⽅法函数声明
1#pragma once
2#include "struct.h"
3
4#ifndef __FUNCTION_H__
5#define __FUNCTION_H__
6
7int read_books(SqList* L, bool &status);
8Status Traverse(SqList L);
9Status CreateBookList(SqList& L);
10Status Insert_BookList(SqList& L);
11Status WhetherReset(bool& status);
12Status DeleteBookList(SqList& L);
13Status SearchBookIsbn(SqList L);
14Status SearchBookPriceRange(SqList L);
15Status PriceAscendingSort(SqList& L);
16Status ReviseAccordingBookPrice(SqList& L);
17Status PriceIncrease(SqList& L);
18Status SearchBookPriceBest(SqList L);
19Status DeleteDuplicateBooks(SqList& L);
20
21#endif
创建 file_operation.cpp 源⽂件读取本程序⽂件夹⽬录中名为book的txt⽂本⽂件
1#include<string>
2#include "struct.h"
3
4//读书籍数据
5int read_books(SqList* L,bool &status) {
6
7 int i = 0;
8 FILE* fp;
9 if ((fp = fopen("", "r")) == NULL) {
10  printf("\a");
11  puts("Error:Failed to open file!");
12  return 0;
13 }
14
15 while (!feof(fp)) {
16  fscanf(fp, "%s%s%lf\n", L->elem[i].id, L->elem[i].name, &L->elem[i].price);
17  printf("%s\t%s\t%.2lf元\n", L->elem[i].id, L->elem[i].name, L->elem[i].price);
18  i++;
19  L->length++;
20 }
21
22
23 fclose(fp);
24 status = true;
25 return 1;
26}
创建 books_operation.cpp 源⽂件编写系统的功能
1#include "struct.h"
2#include <stdio.h>
3#include<stdlib.h>
4#include<string.h>
5
6Status WhetherReset(bool &status) {
7 char reset;
8 if (status == true) {
9  printf("Are you sure to reset the list based on documents?(Y/N)\a");
10  getchar();
11  scanf("%c", &reset);
12  if (reset == 'Y')return OK;
13  else return ERROR;
14 }return OK;
15}
16
17//遍历内容
18Status Traverse(SqList L) {
19 for (int i = 0; i < L.length; i++) {
20  printf("%s\t%s\t%.2lf元\n", L.elem[i].id, L.elem[i].name, L.elem[i].price);
21 }
22 printf("Current storage space : (% d / % d)\n", L.length, MAXSIZE);
23 return OK;
24}
25
26//1构造⼀个空的顺序表L
27Status CreateBookList(SqList& L) {
28 L.elem = new Book[MAXSIZE]; //为顺序表分配⼀个⼤⼩为MAXSIZE的数组空间
29 if (!L.elem) {
30  printf("Error:Failed to create a order table!\a\n");
31  exit(OVERFLOW); //存储分配失败退出
32 }
33 L.length = 0; //空表长度为0
34 printf("Creating a order table succeeded.\n");
35 return OK;
36}
37
38//2上架新图书
39Status Insert_BookList(SqList& L) {
40 SqList* base;
41 Book e;
42 int location;
43 printf("Please enter the position you want to insert:");
44 scanf("%d", &location);
45 if (L.length < 0) {
46  printf("Error:The order table not created!\a\n");
47  return ERROR;              //当长度⼩于0就是未创建内存,不允许插⼊数据
48 }
49 if (L.length >= MAXSIZE) {        //长度⼤于顺序表最⼤值时不允许插⼊数据
50  printf("Error:The order table is full!\a\n");
51  return ERROR;
52 }
53 if (location <= 0 || location > L.length + 1) {
54  printf("Error:The insertion position is invalid!\a\n");
55  return ERROR;
56 }
57 printf("Isbn:");
58 scanf("%s", &e.id);
59 printf("title:");
60 scanf("%s", &e.name);
61 printf("price:");
62 scanf("%lf", &e.price);
63 for (int i = L.length - 1; i >= location - 1; i--) {
64  L.elem[i + 1] = L.elem[i];
65 }
66 L.elem[location - 1] = e;
67 ++L.length;
68 Traverse(L);
69 printf("Insert the success.\n");
70 return OK;
71}
72
73//3下架旧图书(输⼊书号)
74Status DeleteBookList(SqList& L) {
75 char isbn[15];
76 if (L.length < 0) {
77  printf("Error:No books on the shelves!\a\n");
78  return ERROR;              //当长度⼩于0就是未创建内存,不允许删除数据
79 }
80 printf("Isbn that you want to delete:");
81 getchar();
82 scanf("%s", &isbn);
83 for (int i = 0; i < L.length; i++) {
84  if (!strcmp(isbn, L.elem[i].id)) {
85  for (i; i < L.length; i++) {
86    L.elem[i] = L.elem[i+1];
87  }
88  --L.length;
89  Traverse(L);
90  printf("Delete the success.\n");
91  return OK;
92  }
93 }
94 Traverse(L);
95 printf("Books don't exist!\n\a");
96 return ERROR;
97}
98
99//4看书号查
100Status SearchBookIsbn(SqList L) {
101 char isbn[15];
102 if (L.length < 0) {
103  printf("Error:No book data!\a\n");
104  return ERROR;
105 }
106 printf("Enter the isbn you want to find:");
107 getchar();
108 scanf("%s", &isbn);
109 for (int i = 0; i < L.length; i++) {
110  if (!strcmp(isbn, L.elem[i].id)) {
111  printf("The book with THE ISBN %s has been found for you:\n", isbn); 112  printf("%s\t%s\t%.2lf\n",L.elem[i].id, L.elem[i].name, L.elem[i].price); 113  return OK;
114  }
115 }
116 printf("ERROR:Books don't exist!\n\a");
117 return ERROR;
118}
119
120//5基于价格区间查
121Status SearchBookPriceRange(SqList L) {
122 int num = 0;
123 bool exist=false;
124 double min_price;
125 double max_price;
126 printf("The lowest price:");
127 getchar();
128 scanf("%lf", &min_price);
129 printf("The highest price:");
130 getchar();
131 scanf("%lf", &max_price);
132 if (L.length < 0) {
133  printf("Error:No book data!\a\n");
134  return ERROR;
135 }
136 for (int i = 0; i < L.length; i++) {
137  if (L.elem[i].price >= min_price && L.elem[i].price <= max_price) {
138  printf("%s\t%s\t%.2lf\n", L.elem[i].id, L.elem[i].name, L.elem[i].price);
139  num++;
140  exist = true;
141  }
142 }
143 if (exist == false) {
144  printf("ERROR:Books are not available at this price!\n\a");
145  return ERROR;
146 }
147 printf("There are %d books altogether.\n", num);
148 return OK;
149}
150
151//6价格升序排序
152Status PriceAscendingSort(SqList& L) {
153 if (L.length < 0) {
154  printf("Error:No book data!\a\n");
155  return ERROR;
156 }
157 Book temp;
158 for (int j = L.length-1; j > 0; j--) {
159  for (int i = 0; i < j; i++) {
160  if (L.elem[i].price > L.elem[i + 1].price) {
161    temp = L.elem[i];
162    L.elem[i] = L.elem[i + 1];
163    L.elem[i + 1] = temp;
164  }
165  }
166 }
167 Traverse(L);
168 printf("Order to complete.\n");
169}
170
171//7按图书价格修改
172Status ReviseAccordingBookPrice(SqList& L) {
173 char isbn[15];
174 double price;
175 if (L.length < 0) {
176  printf("Error:No book data!\a\n");
177  return ERROR;
178 }
179 printf("isbn:");
180 getchar();
181 scanf("%s", &isbn);
182 printf("price:");
183 getchar();
184 scanf("%lf", &price);
185 for (int i = 0; i < L.length; i++) {
186  if (!strcmp(isbn, L.elem[i].id)) {
187  L.elem[i].price = price;
188  Traverse(L);
189  printf("Modify the success.\n");
190  return OK;
191  }
192 }
193 printf("ERROR:Books don't exist!\n\a");
194 return ERROR;
195}
196
197/*
198 8按图书价格普调 (计算所有图书的平均价格,将所有低于平均价格的图书价格提⾼ 20%,199所有⾼于或等于平均价格的图书价格提⾼ 10%,最后逐⾏输出价格修改后的全部图书信息。) 200*/
201Status PriceIncrease(SqList& L) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。