C语言
主页 > 软件编程 > C语言 >

C++使用链表实现图书管理系统的教程

2022-03-12 | 秩名 | 点击:

一、程序实现功能

1.录入书籍:将书籍录入图书管理系统

2.浏览书籍:查看图书管理系统里的所有书籍

3.借阅书籍:书籍存在可以借阅,库存-1,书的库存不足则无法借阅

4.归还书籍:库存+1,如果该书不是图书馆里的书籍,则无法录入

5.删除书籍:以书名为基础从图书管理系统中删除该书籍

6.查找书籍:按书名查找书籍,显示书籍的基本信息

7.排序书籍:按价格将书籍排序(降序)

二、要求

使用函数、指针和链表编写。

三、程序功能图

四、具体函数

五、程序代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

  

struct bookinfo

{

    char name[20];   //书名

    char author[10]; //作者

    char date[20];   //出版日期

    float price;     //价格

    int num;         //数量

};

  

struct Node

{

    struct bookinfo data;

    struct Node* next;

};

  

/*全局链表*/

struct Node* list = NULL;

  

/*创建表头*/

struct Node* createhead()

{

    /*动态内存申请*/

    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));

    headNode->next = NULL;

    return headNode;

}

  

/*创建节点*/

struct Node* createNode(struct bookinfo data)

{

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

  

void printList();

void display_menu();

void savebookfile();

void insertbook();

void readbookfile();

void deletebook();

struct Node* searchbook();

void sortbook();

void selectkey();

  

/*打印链表*/

void printList(struct Node* headNode)

{

    struct Node* Bmove = headNode->next;

    printf("书名\t作者\t出版日期\t价格\t库存\n");

    while(Bmove != NULL)

    {

        printf("%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);

        Bmove = Bmove->next;

    }

  

}

  

/*菜单登录界面*/

void display_menu()

{

    char str[100];

    FILE *fp;

    char *txt;

    fp = fopen("menu.txt","r");

    txt = fgets(str,100,fp);

    while(txt != NULL)

    {

    printf("%s",str);

    txt = fgets(str,100,fp);

    }

    fclose(fp);

}

  

/*将信息存到文件中*/

void savebookfile(const char* filename,struct Node* headNode)

{

    FILE* fp = fopen(filename,"w");

    struct Node* Bmove = headNode->next;

    while(Bmove != NULL)

    {

        fprintf(fp,"%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);

        Bmove = Bmove->next;

    }

    fclose(fp);

}

  

/*录入书籍*/

void insertbook(struct Node* headNode,struct bookinfo data)

{

    struct Node* newNode = createNode(data);

    newNode->next = headNode->next;

    headNode->next = newNode;

  

}

  

/*读取文件*/

void readbookfile(const char* filename, struct Node* headNode)

{   

    FILE* fp = fopen(filename,"r");

    if(fp == NULL)

    {

        fp = fopen(filename,"w+");

    }

    struct bookinfo tempinfo;

    while(fscanf(fp, "%s\t%s\t%s\t%.1f\t%d\n",tempinfo.name,&tempinfo.author,&tempinfo.date,&tempinfo.price,&tempinfo.num ) != EOF)

    {

        insertbook(list,tempinfo);

    }

    fclose(fp);

}

  

/*删除书籍*/

void deletebook(struct Node* headNode,char *bookname)

{

    struct Node* leftNode = headNode;

    struct Node* rightNode = headNode->next;

    while(rightNode != NULL && strcmp(rightNode->data.name,bookname))

    {

        leftNode = rightNode;

        rightNode = leftNode->next;

    }

    if(leftNode == NULL)

    {

        return;

    }

    else

    {

        printf("删除书籍成功!\n");

        leftNode->next = rightNode->next;

        free(rightNode);

        rightNode = NULL;

    }

}

  

/*查找书籍*/

struct Node* searchbook(struct Node* headNode, char* bookname)

{

    struct Node* rightNode = headNode->next;

    while (rightNode != NULL && strcmp(rightNode->data.name, bookname))

    {

        rightNode = rightNode->next;

    }

    return rightNode;

}

  

/*排序书籍*/

void sortbook(struct Node* headNode)

{

    for(struct Node* i = headNode->next; i != NULL; i = i->next)

    {

        for(struct Node* j = headNode->next;j->next != NULL;j = j->next)

        {

            /*排序书籍(按价格降序)*/

            if (j->data.price < j->next->data.price) 

            {

                /*交换值*/

                struct bookinfo tempdata = j->data;

                j->data = j->next->data;

                j->next->data = tempdata;

            }

        }

    }

    /*排序后查看效果*/

    printList(headNode);

}

  

/*交互界面*/

void selectkey()

{

    int userkey = 0;

    struct bookinfo tempbook;  //生成一个临时的变量存储书籍信息

    struct Node* searchname = NULL; //生成一个临时变量存储查找的书名

    struct Node* borrowbook = NULL; //生成一个临时变量存储要借阅的书名

    struct Node* returnbook = NULL; //生成一个临时变量存储要归还的书名

    scanf("%d",&userkey);

    switch(userkey)

    {

    case 1:

        printf("[ 录入书籍 ]\n");

        printf("输入书籍的信息(name,author,date,price,num):");

        scanf("%s%s%s%f%d",tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num);

        insertbook(list,tempbook);

        /*把书籍信息保存到booksinfo文本文件里*/

        savebookfile("bookinfo.txt",list);

        break;

    case 2:

        printf("[ 浏览书籍 ]\n");

        printList(list);

        break;

    case 3:

        printf("[ 借阅书籍 ]\n");   /*书籍存在可以借阅,库存-1,书的库存不足则无法借阅*/

        printf("请输入要借阅的书名:");

        scanf("%s",tempbook.name);

        borrowbook = searchbook(list,tempbook.name);

        if(borrowbook == NULL)

        {

            printf("不存在该书,无法借阅!\n");

        }

        else

        {

            if(borrowbook->data.num > 0)

            {

                borrowbook->data.num--;

                printf("借阅成功!\n");

                printList(list);

            }

            else

            {

                printf("当前书籍库存不足,借阅失败!\n");

            }

        }

        break;

    case 4:

        printf("[ 归还书籍 ]\n");  //库存+1

        printf("请输入要归还的书名:");

        scanf("%s",tempbook.name);

        returnbook = searchbook(list,tempbook.name);

        if(returnbook == NULL)

        {

            printf("该书不是图书馆里的书籍!\n");

        }

        else

        {

            returnbook->data.num++;

            printf("书籍归还成功!\n");

            printList(list);

        }

        break;

    case 5:

        printf("[ 删除书籍 ]\n");

        printf("请输入要删除的书名:");

        scanf("%s",tempbook.name); 

        deletebook(list,tempbook.name);    /*按书名删除书籍*/

        printList(list);

        break;

    case 6:

        printf("[ 查找书籍 ]\n");

        printf("请输入要查询的书名:");

        scanf("%s",tempbook.name);

        searchname = searchbook(list,tempbook.name);

        if(searchname == NULL)

        {

            printf("不存在该书,请加购录入!\n");

        }

        else

        {

            /*输出该书的信息*/

            printf("书名\t作者\t出版日期\t价格\t库存\n");

            printf("%s\t%s\t%s\t%.1f\t%d\n",searchname->data.name,searchname->data.author,searchname->data.date,searchname->data.price,searchname->data.num);

        }

        break;

    case 7:

        printf("[ 排序书籍 ]\n"); /*按价格排序(降序)*/

        sortbook(list);

        break;

  

    case 8:

        printf("[ 退出系统 ]\n");

        printf("退出成功\n");

        system("pause");

        exit(0);             /*关闭整个程序*/

        break;

    default:

        printf("[ 错误 ]\n");

        break;

    }

}

  

/*主界面*/

int main()

{

    list = createhead();

    readbookfile("bookinfo.txt",list);

    while(1)

    {

        display_menu();

        selectkey();

        system("pause");

        system("cls");

    }

    system("pause");

    return 0;

}

六、效果

1.录入书籍

2.浏览书籍

3.借阅书籍

存在该书时,借阅成功,库存-1:

不存在该书时,无法借阅:

4.归还书籍

当图书管理系统里本不存在该书,则归还失败:

5.查找书籍

不存在该书时,则查找失败:

6.排序书籍

再录入书籍:

排序(降序):

7.删除书籍

以上为该程序的所有功能。

原文链接:https://blog.csdn.net/weixin_44774441/article/details/121685651
相关文章
最新更新