-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilm.c
516 lines (514 loc) · 13.6 KB
/
film.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"public.h"
#include"film.h"
#include"struct.h"
#include"util.h"
#include"user.h"
struct film * getFilmByName(char * name, struct film *f)//通过名字查询
{
int index = 0;
FILE *file = fopen("filmbinary", "ab+");
FILMCONUT = getFilmSumFromFilminfo();
while (index < FILMCONUT)
{
fseek(file, FILMSIZE * index, SEEK_SET);
fread(f, FILMSIZE, 1, file);
index++;
if (!strcmp(name, f->film_name))
{
fclose(file);
file = NULL;
return f;
}
}
fclose(file);
file = NULL;
return NULL;
}
void getFilmById(int id, struct film *f)//通过id查询
{
int index = 0;
FILE *file = fopen("filmbinary", "rb");
FILMCONUT = getFilmSumFromFilminfo();
while (index < FILMCONUT)
{
fseek(file, FILMSIZE * index, SEEK_SET);
fread(f, FILMSIZE, 1, file);
index++;
if (id == f->id)
break;
}
fclose(file);
}
void printFilminfo(struct film f)//打印某个结构体内容
{
printf("id 碟名 \
国家 类型 年份 余量 价格 总量 状态 \n");
printf("%-4d %-20s %-3s %-3s %-13s %-6.2f %-4d %-4d ",
f.id, f.film_name,COUNTRY[f.film_country - '0'],
TYPE[f.film_type - '0'],YEAR[f.film_year - '0'],
f.film_price, f.film_sum,f.film_left);
if (f.is_borrow - '0' == 1)
color(10, IS_BORROW[f.is_borrow - '0']);
else
color(12, IS_BORROW[f.is_borrow - '0']);
printf("\n");
return;
}
void insertFilm(char *name, char country, char type,
char year, char is_borrow, float film_price,
char film_sum, char film_left)//插入影片信息
{
FILE *file = fopen("filmbinary", "ab+");
int id;
id = getIdflagFromFilminfo();
/*组装结构体开始*/
struct film f;
strcpy(f.film_name, name);
f.id = id;
f.film_country = country;
f.film_type = type;
f.film_year = year;
f.is_borrow = is_borrow;
f.film_price = film_price;
f.film_sum = film_sum;
f.film_left = film_left;
/*组装结构体结束*/
printf("getfilm:%d %s %s %s %s %s %.2f %d %d ", f.id, f.film_name,
COUNTRY[f.film_country - '0'], TYPE[f.film_type - '0'],
YEAR[f.film_year - '0'], IS_BORROW[f.is_borrow - '0'],
f.film_price, f.film_sum, f.film_left);
if (fwrite(&f, sizeof(struct film), 1, file))
{
color(10,"插入成功!\n");
changeFilmSumInFilminfo(getFilmSumFromFilminfo() + 1, id + 1);//id数和影片+1
}
else
color(12,"插入失败!\n");
fclose(file);
file = NULL;
}
void deleteFilmByName(char * name)
{
struct film f;
int id = getFilmIdByName(name);//如未找到相应电影 返回-1
if (id == -1)
{
color(12,"无此电影\n");
return;
}
int i, j;
i = j = 0;
FILE *newfile = fopen("tempfilmbinary", "wb");
FILE *oldfile = fopen("filmbinary", "rb");
FILMCONUT = getFilmSumFromFilminfo();
do
{
fseek(oldfile, FILMSIZE*i, SEEK_SET);
fread(&f, sizeof(struct film), 1, oldfile);
if (f.id == id)
{
i++;
continue;
}
fseek(newfile, FILMSIZE*j, SEEK_SET);
fwrite(&f, sizeof(struct film), 1, newfile);
i++;
j++;
}while (i < FILMCONUT);
_fcloseall();
if (remove("filmbinary") == 0)//TODO删除判断
{
color(10,"删除成功\n");
FILMCONUT--;
changeFilmSumInFilminfo(FILMCONUT,-1);//TODO
}
else
perror("删除失败:");
rename("tempfilmbinary", "filmbinary");
newfile = NULL;
oldfile = NULL;
}
void showAllFilm()
{
int a = getFilmSumFromFilminfo();
showFilmList(1, a);
return;
}
void showFilmList(int start, int end)//显示一个区间内的影片信息
{
int num = 0;
int max = getFilmSumFromFilminfo();
struct film f;
if (start < 1)
{
color(12,"起始位置出错\n");
return;
}
if (end>max)
{
color(12,"结束位置超过最大电影总数\n");
return;
}
if (end < start)
{
color(12,"结束位置应大于开始位置\n");
return;
}
FILE *file = fopen("filmbinary", "rb");
printf("序号 id 碟名 \
国家 类型 年份 价格 总量 余量 状态\n");
start--;
end--;
while (start <= end)
{
num++;
fseek(file, FILMSIZE * start, SEEK_SET);
fread(&f, sizeof(struct film), 1, file);
printf("%-4d %-4d %-20s %-3s %-3s %-13s \
%-6.2f %-4d %-4d ", num, f.id, f.film_name,
COUNTRY[f.film_country - '0'], TYPE[f.film_type - '0'],
YEAR[f.film_year - '0'],f.film_price, f.film_sum, f.film_left);
if (f.is_borrow - '0' == 1)
color(10, IS_BORROW[f.is_borrow - '0']);
else
color(12, IS_BORROW[f.is_borrow - '0']);
printf("\n");
start++;
}
return;
}
void changeFilm(int id, struct film newfilminfo)
{
struct film tempf;
int i = 0;
FILE *newfile = fopen("tempfilmbinary", "wb");
FILE *oldfile = fopen("filmbinary", "rb");
FILMCONUT = getFilmSumFromFilminfo();//如返回-1 代表没找到
if (id == -1)
return;
while (i < FILMCONUT)
{
fseek(oldfile, FILMSIZE*i, SEEK_SET);
fread(&tempf, sizeof(struct film), 1, oldfile);
if (tempf.id == id)
tempf = newfilminfo;
fseek(newfile, FILMSIZE*i, SEEK_SET);
fwrite(&tempf, sizeof(struct film), 1, newfile);
i++;
}
fclose(oldfile);
fclose(newfile);
remove("filmbinary");
rename("tempfilmbinary", "filmbinary");
oldfile = NULL;
newfile = NULL;
}
char * getFilmNameByid(int id)
{
FILE * file = fopen("filmbinary", "rb");
int i = getFilmSumFromFilminfo(), j;
for (j = 0; j < i; j++)
{
fseek(file, sizeof(struct film)*j, SEEK_SET);
fread(&f, sizeof(struct film), 1, file);
if (f.id == id)
{
fclose(file);
return f.film_name;
}
}
fclose(file);
return NULL;//未找到
}
void borrowfilm(struct cart * head,int uid)
{
FILE * f = fopen("borrowfilm", "rb");
FILE * tempf = fopen("tempborrowfilm", "wb");
FILE * f1 = fopen("vipuser", "rb");
FILE * tempf1 = fopen("tempvipuser", "wb");
FILE * f2 = fopen("vipinfo", "rb");
struct vipinfo vi;
struct filmborrow fb;
struct vip tempv;
struct film tempfilm;
fread(&vi, sizeof(struct vipinfo), 1, f2);
fclose(f2); f2 = NULL;
int vipnum = vi.num, flag = 0, i, j, x;//i用户数量 flag游标
for (j = 0; j < vipnum; j++)
{
fseek(f1, sizeof(struct vip)*j, SEEK_SET);
fread(&tempv, sizeof(struct vip), 1, f1);//读入单个用户信息
if (tempv.filmnum != 0)//判断用户是否借阅影片
{
for (x = 0; x < tempv.filmnum; x++)//电影借阅文件写入
{
fseek(f, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fread(&fb, sizeof(struct filmborrow), 1, f);
fseek(tempf, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fwrite(&fb, sizeof(struct filmborrow), 1, tempf);
}
flag += tempv.filmnum;//游标加上用户借阅影片数量 游标后移
}
if(tempv.id==uid)//找到相应用户 开始写入文件 电影借阅文件以及用户文件
{
i = 0;
struct cart * p = head->next;
while (p)
{
getFilmById(p->fb->film_id, &tempfilm);
tempfilm.film_left--;
changeFilm(tempfilm.id, tempfilm);
fseek(tempf, sizeof(struct filmborrow)*(flag + i), SEEK_SET);
fwrite((p->fb), sizeof(struct filmborrow), 1, tempf);
i++;
p = p->next;
}
flag += i;//游标后移
tempv.filmnum += i;//个人借阅数增加
v.filmnum += i;//全局变量修改
}
fseek(tempf1, sizeof(struct vip)*j, SEEK_SET);
fwrite(&tempv, sizeof(struct vip), 1, tempf1);//写入单个用户信息
}
fclose(f); f = NULL;
fclose(tempf); tempf = NULL;
fclose(f1); f1 = NULL;
fclose(tempf1); tempf1 = NULL;
remove("vipuser");
remove("borrowfilm");
rename("tempborrowfilm", "borrowfilm");
rename("tempvipuser", "vipuser");
}
void borrowsinglefilm(struct cart * head, int uid, int fid)
{
FILE * f = fopen("borrowfilm", "rb");
FILE * tempf = fopen("tempborrowfilm", "wb");
FILE * f1 = fopen("vipuser", "rb");
FILE * tempf1 = fopen("tempvipuser", "wb");
FILE * f2 = fopen("vipinfo", "rb");
struct vipinfo vi;
struct filmborrow fb;
struct vip tempv;
struct film tempfilm;
fread(&vi, sizeof(struct vipinfo), 1, f2);
fclose(f2); f2 = NULL;
//getFilmById(fid, &tempfilm);
int vipnum = vi.num, flag = 0, j, x;//i用户数量 flag游标
for (j = 0; j < vipnum; j++)
{
fseek(f1, sizeof(struct vip)*j, SEEK_SET);
fread(&tempv, sizeof(struct vip), 1, f1);//读入单个用户信息
if (tempv.filmnum != 0)//判断用户是否借阅影片
{
for (x = 0; x < tempv.filmnum; x++)//电影借阅文件写入
{
fseek(f, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fread(&fb, sizeof(struct filmborrow), 1, f);
fseek(tempf, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fwrite(&fb, sizeof(struct filmborrow), 1, tempf);
}
flag += tempv.filmnum;//游标加上用户借阅影片数量 游标后移
}
if (tempv.id == uid)//找到相应用户 开始写入文件 电影借阅文件以及用户文件
{
struct cart * p = head, *temp;
while (p)
{
if (p->next->fb->film_id == fid)
{
getFilmById(p->next->fb->film_id, &tempfilm);
tempfilm.film_left--;
changeFilm(tempfilm.id, tempfilm);
fseek(tempf, sizeof(struct filmborrow)*(flag + 0), SEEK_SET);
fwrite((p->next->fb), sizeof(struct filmborrow), 1, tempf);
temp = p->next;
p->next = p->next->next;//删除购物车的影片
free(temp);
break;
}
p = p->next;
}
flag += 1;//游标后移
tempv.filmnum += 1;//个人借阅数增加
v.filmnum += 1;
}
fseek(tempf1, sizeof(struct vip)*j, SEEK_SET);
fwrite(&tempv, sizeof(struct vip), 1, tempf1);//写入单个用户信息
}
fclose(f); f = NULL;
fclose(tempf); tempf = NULL;
fclose(f1); f1 = NULL;
fclose(tempf1); tempf1 = NULL;
remove("vipuser");
remove("borrowfilm");
rename("tempborrowfilm", "borrowfilm");
rename("tempvipuser", "vipuser");
}
/*配置文件修改*/
void changeFilmSumInFilminfo(int newnum,int newnextid)//修改配置文件中电影数目
{
struct filminfo info;
FILE *oldfile = fopen("filminfo", "rb");
FILE *newfile = fopen("tempfilminfo", "wb");
fread(&info, sizeof(struct filminfo), 1, oldfile);
info.num = newnum;
if (newnextid != -1)//输入-1 代表仅修改影片总数量 不修改下一个影片的id
info.nextid = newnextid;
fwrite(&info, sizeof(struct filminfo), 1, newfile);
_fcloseall();
remove("filminfo");
rename("tempfilminfo", "filminfo");
//FILMCONUT = newnum;TODO
}
int getFilmSumFromFilminfo()//获取配置文件中电影总数
{
int sum = 0;
struct filminfo info;
FILE *f = fopen("filminfo", "rb");
fread(&info, sizeof(struct filminfo), 1, f);
fclose(f);
return info.num;
}
int getIdflagFromFilminfo()//获取配置文件中id标记
{
int sum = 0;
struct filminfo info;
FILE *f = fopen("filminfo", "rb");
fread(&info, sizeof(struct filminfo), 1, f);
fclose(f);
return info.nextid;
}
int getFilmIdByName(char *name)//通过影片名字查找影片id
{
struct film f;
int index = 0;
FILE *file = fopen("filmbinary", "rb");
FILMCONUT = getFilmSumFromFilminfo();
while (index < FILMCONUT)
{
fseek(file, FILMSIZE * index, SEEK_SET);
fread(&f, sizeof(struct film), 1, file);
if (!strcmp(name, f.film_name))
{
fclose(file);
file = NULL;
return f.id;
}
index++;
}
fclose(file);
file = NULL;
return -1;//未找到
}
void returnall(int uid)
{
int pay = 0;
FILE * f = fopen("borrowfilm", "rb");
FILE * tempf = fopen("tempborrowfilm", "wb");
FILE * f1 = fopen("vipuser", "rb");
FILE * f2 = fopen("vipinfo", "rb");
struct vipinfo vi;
struct filmborrow fb;
struct vip tempv;
struct film tempfilm;
fread(&vi, sizeof(struct vipinfo), 1, f2);
fclose(f2); f2 = NULL;
int vipnum = vi.num, flag = 0, j, x;//i用户数量 flag游标
for (j = 0; j < vipnum; j++)
{
fseek(f1, sizeof(struct vip)*j, SEEK_SET);
fread(&tempv, sizeof(struct vip), 1, f1);//读入单个用户信息
if (tempv.filmnum != 0)//判断用户是否借阅影片
{
for (x = 0; x < tempv.filmnum; x++)//电影借阅文件写入
{
fseek(f, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fread(&fb, sizeof(struct filmborrow), 1, f);
if (tempv.id == uid)//匹配用户
{
if (fb.status == 0)//匹配借阅状态 找到要归还的影片
{
fb.status = 1;//修改用户借阅状态
getFilmById(fb.film_id, &tempfilm);//影片数目增加
tempfilm.film_left++;
changeFilm(tempfilm.id, tempfilm);
}
}
fseek(tempf, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fwrite(&fb, sizeof(struct filmborrow), 1, tempf);
}
flag += tempv.filmnum;//游标加上用户借阅影片数量 游标后移
}
}
fclose(f); f = NULL;
fclose(tempf); tempf = NULL;
fclose(f1); f1 = NULL;
remove("borrowfilm");
rename("tempborrowfilm", "borrowfilm");
}
void retursinglefilm(int uid, int fid)
{
int pay = 0;
FILE * f = fopen("borrowfilm", "rb");
FILE * tempf = fopen("tempborrowfilm", "wb");
FILE * f1 = fopen("vipuser", "rb");
FILE * f2 = fopen("vipinfo", "rb");
struct vipinfo vi;
struct filmborrow fb;
struct vip tempv;
struct film tempfilm;
fread(&vi, sizeof(struct vipinfo), 1, f2);
fclose(f2); f2 = NULL;
int vipnum = vi.num, flag = 0, j;//i用户数量 flag游标
for (j = 0; j < vipnum; j++)
{
fseek(f1, sizeof(struct vip)*j, SEEK_SET);
fread(&tempv, sizeof(struct vip), 1, f1);//读入单个用户信息
if (tempv.filmnum != 0)//判断用户是否借阅影片
{
for (int x = 0; x < tempv.filmnum; x++)//电影借阅文件写入
{
fseek(f, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fread(&fb, sizeof(struct filmborrow), 1, f);
if (tempv.id == uid)//匹配用户
if (fb.status == 0)//匹配借阅状态
if (fb.film_id == fid)//匹配影片id 找到要归还的影片
{
fb.status = 1;//修改用户借阅状态
getFilmById(fb.film_id, &tempfilm);//影片数目增加
tempfilm.film_left++;
changeFilm(tempfilm.id, tempfilm);
}
fseek(tempf, sizeof(struct filmborrow)*(flag + x), SEEK_SET);
fwrite(&fb, sizeof(struct filmborrow), 1, tempf);
}
flag += tempv.filmnum;//游标加上用户借阅影片数量 游标后移
}
}
fclose(f); f = NULL;
fclose(tempf); tempf = NULL;
fclose(f1); f1 = NULL;
remove("borrowfilm");
rename("tempborrowfilm", "borrowfilm");
}
void ShowfilmBorrowStatus()//展示所有影片借阅状态
{
system("cls");
int i, filmnum = getFilmSumFromFilminfo(), num;
FILE * file = fopen("filmbinary", "rb");
struct film tempf;
printf("序号 id 影片名 借阅人数\n");
for(i=0;i<filmnum;i++)
{
fseek(file, FILMSIZE * i, SEEK_SET);
fread(&tempf, sizeof(struct film), 1, file);
num = (tempf.film_sum - '0') - (tempf.film_left - '0');
if (num)
printf("%-4d %-3d %-20s %-3d\n", i + 1, tempf.id, tempf.film_name, num);
}
fclose(file);
}