-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
564 lines (564 loc) · 16.5 KB
/
main.cpp
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct user
{
int ID;//用户编号
char user_name[10];
char user_psw[10];
double money;//余额
} users[61];
void welcome();//欢迎界面
void filewrite();//账户信息写入文件
void fileread();//读取文件中的账户信息
void login();//管理员登录界面
void mainmenu();//主菜单
void new_count();//开户
void take();//取款
void save();//存款
void check();//查询账户
void clean();//销户
int N_new = 0;//账户数量
int N_wrong;//密码错误次数
int flag1;//标记
int i;
/*Copyright [1998-2017] by SDUT,jk1606,[WZZ]. All rights reserved.*/
/*主函数*/
int main()
{
system("color 1E");
welcome();
fileread();
login();
mainmenu();
return 0;
}
/*欢迎界面*/
void welcome()
{
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 山东理工大学银行 ****\n");
printf(" **** 欢迎您!O(∩_∩)O~ ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
}
/*读文件函数*/
void fileread()
{
FILE *fp;
fp = fopen("users_information.txt", "rt+");
i = 0;
while(fscanf(fp, "%d %s %s %lf", &users[i].ID, users[i].user_name, users[i].user_psw, &users[i].money) != EOF)
{
i++;
N_new++;
}
fclose(fp);
}
/*写文件函数*/
void filewrite()
{
FILE *fp;
fp = fopen("users_information.txt", "wt+");
for(i = 0; i < N_new; i++)
{
fprintf(fp, "%d %s %s %.4lf\n", users[i].ID, users[i].user_name, users[i].user_psw, users[i].money);
}
fclose(fp);
}
/*系统登录函数*/
void login()
{
char cname[10] = {"admin"};
N_wrong = 0;
while(1)
{
printf("请输入管理员账号:");
char s1[10];
gets(s1);
if(s1[0] == '\0')
printf("用户名不能为空!\n");
else if(!strcmp(s1,cname))
break;
else
{
int i;
printf("该管理员不存在,请在 3 秒后重新输入!");
Sleep(1000);
for(i = 2; i > 0; i--)
{
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%d 秒后重新输入!", i);
Sleep(1000);
}
system("cls");
welcome();
}
}
while(1)
{
printf("请输入密码:");
char s2[10];
gets(s2);
if(s2[0] == '\0')
printf("密码不能为空!\n");
else if(!strcmp(s2,cname))//密码正确,退出循环
break;
else
{
if(N_wrong >= 2)//密码错误次数限制
{
printf("密码错误次数过多,系统自动关闭!\n");
exit(0);
}
printf("密码错误,请 3 秒后重新输入!");
Sleep(1000);
for(i = 2; i > 0; i--)
{
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%d 秒后重新输入!", i);
Sleep(1000);
}
system("cls");
N_wrong++;
welcome();
}
}
system("cls");
welcome();
return;
}
/*主菜单函数*/
void mainmenu()
{
while(1)
{
printf("-----------------------------\n");
printf("请输入您要进行操作的编号:\n");
printf("-----------------------------\n");
printf("开户<1> 存款<2> 取款<3>\n");
printf("查询<4> 销户<5> 退出<0>\n");
printf("-----------------------------\n");
int select;
scanf("%d", &select);
switch(select)
{
case 0:
{
printf("系统即将关闭,感谢您的使用,期待下次光临!");
Sleep(1500);
return;
}
case 1:
new_count();
break;
case 2:
save();
break;
case 3:
take();
break;
case 4:
check();
break;
case 5:
clean();
default:
printf("请输入正确的指令!\n");
Sleep(2000);
system("cls");
welcome();
}
}
return;
}
/*开户函数*/
void new_count()
{
system("cls");
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 开户界面 ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
if(N_new >= 60)
{
printf("抱歉,注册账号数已达到上限,即将为您跳转到主菜单");
Sleep(2000);
system("cls");
welcome();
return;
}
else
{
int len;
char s3[10], s4[10];
while(1)
{
printf("请输入您的账户名:");
scanf("%s", s3);
len = strlen(s3);
if(len > 6)
{
printf("您输入的账户名过长,请输入6位账户名!\n");
Sleep(2000);
continue;
}
else if(len < 6)
{
printf("您输入的账户名过短,请输入6位账户名!\n");
Sleep(2000);
continue;
}
flag1 = 0;
for(i = 0; i < 6; i++)
{
if(s3[i] < 48 || (s3[i] < 65 && s3[i] > 57) || (s3[i] > 90 && s3[i] < 97)||s3[i] > 122)
{
printf("账户名中不能包含特殊字符,请重新输入!\n");
flag1 = 1;
Sleep(2000);
break;
}
}
if(flag1)continue;
flag1 = 0;
for(i = 0; i < N_new; i++)
{
if(strcmp(s3, users[i].user_name) == 0)
{
printf("该账户名已存在,请重新输入!\n");
flag1 = 1;
Sleep(2000);
break;
}
}
if(flag1)continue;
else break;
}
strcpy(users[N_new].user_name, s3);
while(1)
{
printf("请输入您的密码:");
scanf("%s", s4);
len = strlen(s4);
if(len > 6)
{
printf("您输入的密码过长,请输入6位数密码!\n");
Sleep(2000);
continue;
}
else if(len < 6)
{
printf("您输入的密码过短,请输入6位数密码!\n");
Sleep(2000);
continue;
}
flag1 = 0;
for(i = 0; i < 6; i++)
{
if(s4[i] < 48 || (s4[i] < 65 && s4[i] > 57) || (s4[i] > 90 && s4[i] < 97)||s4[i] > 122)
{
printf("密码中不能包含特殊字符,请重新输入!\n");
flag1 = 1;
Sleep(2000);
break;
}
}
if(!flag1)break;
}
strcpy(users[N_new].user_psw, s4);
printf("请输入您要存款的金额(如暂不存款,请输入0):");
scanf("%lf", &users[N_new].money);
users[N_new].ID = N_new + 1;
printf("开户成功,即将显示您的账户信息:\n");
Sleep(1000);
printf("-----------------------------------------------\n");
printf("ID:%d 账户名:%s 密码:%s 余额:%.4lf\n", users[N_new].ID,users[N_new].user_name,users[N_new].user_psw,users[N_new].money);
printf("-----------------------------------------------\n");
N_new++;
printf("即将为你跳转到主菜单\n");
filewrite();
system("pause");
system("cls");
welcome();
return;
}
}
/*存款函数*/
void save()
{
if(N_new == 0)
{
printf("无可操作账户,即将返回主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
system("cls");
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 存款界面 ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
save1:
printf("请输入您要存款的账户名:");
char s5[10];
scanf("%s", s5);
double n;
flag1 = 0;
for(i = 0; i < N_new; i++)
{
if(strcmp(s5, users[i].user_name) == 0)
{
flag1 = 1;
printf("请输入您要存款的金额:");
scanf("%lf", &n);
users[i].money += n;
printf("存款成功,即将显示您的账户信息:\n");
Sleep(1000);
printf("--------------------------------------------------\n");
printf("ID:%d 账户名:%s 余额:%.4lf\n", users[i].ID,users[i].user_name,users[i].money);
printf("--------------------------------------------------\n");
printf("即将为你跳转到主菜单\n");
filewrite();
system("pause");
system("cls");
welcome();
return;
}
}
if(flag1 == 0)
{
printf("该账户不存在,请重新输入!\n");
Sleep(1000);
goto save1;
}
}
/*取款函数*/
void take()
{
if(N_new == 0)
{
printf("无可操作账户,即将返回主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
system("cls");
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 取款界面 ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
take1:
printf("请输入您要取款的账户名:");
char s8[10], s9[10];
scanf("%s", s8);
double n1;
flag1 = 0;
for(i = 0; i < N_new; i++)
{
if(strcmp(s8, users[i].user_name) == 0)
{
N_wrong = 0;
flag1 = 1;
take2:
printf("请输入密码:");
scanf("%s", s9);
if(strcmp(s9, users[i].user_psw) == 0)
{
printf("-----------------------------------\n");
printf("您的账户可用余额为:%.4lf\n", users[i].money);
printf("-----------------------------------\n");
while(1)
{
printf("请输入您的取款金额:");
scanf("%lf", &n1);
if(n1 > users[i].money)
{
printf("余额不足,请重新输入!\n");
Sleep(1000);
}
else
{
printf("取款成功,请收好您的现金\n");
users[i].money -= n1;
printf("-----------------------------------\n");
printf("您的账户当前可用余额为:%.4lf\n", users[i].money);
printf("-----------------------------------\n");
break;
}
}
printf("即将为你跳转到主菜单\n");
filewrite();
system("pause");
system("cls");
welcome();
return;
}
else
{
if(N_wrong >= 2)//密码错误次数限制
{
printf("密码错误次数过多,即将为你跳转到主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
printf("密码错误,请重新输入!\n");
N_wrong++;
Sleep(1000);
goto take2;
}
}
}
if(flag1 == 0)
{
printf("该账户不存在,请重新输入!\n");
Sleep(1000);
goto take1;
}
}
/*查询余额函数*/
void check()
{
if(N_new == 0)
{
printf("无可操作账户,即将返回主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
system("cls");
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 查询界面 ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
check1:
printf("请输入您要查询的账户名:");
char s10[10], s11[10];
scanf("%s", s10);
flag1 = 0;
for(i = 0; i < N_new; i++)
{
if(strcmp(s10, users[i].user_name) == 0)
{
N_wrong = 0;
flag1 = 1;
check2:
printf("请输入密码:");
scanf("%s", s11);
if(strcmp(s11, users[i].user_psw) == 0)
{
printf("-----------------------------------------------\n");
printf("ID:%d 账户名:%s 余额:%.4lf\n", users[i].ID,users[i].user_name,users[i].money);
printf("-----------------------------------------------\n");
printf("即将为你跳转到主菜单\n");
system("pause");
system("cls");
welcome();
return;
}
else
{
if(N_wrong >= 2)//密码错误次数限制
{
printf("密码错误次数过多,即将为你跳转到主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
printf("密码错误,请重新输入!\n");
N_wrong++;
Sleep(1000);
goto check2;
}
}
}
if(flag1 == 0)
{
printf("该账户不存在,请重新输入!\n");
Sleep(1000);
goto check1;
}
}
/*销户函数*/
void clean()
{
if(N_new == 0)
{
printf("无可操作账户,即将返回主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
system("cls");
printf(" ***********************************************\n");
printf(" **** ****\n");
printf(" **** 销户界面 ****\n");
printf(" **** ****\n");
printf(" ***********************************************\n");
char s6[10], s7[10];
clean1:
printf("请输入您要注销的账户(注销账户余额将被清零,请谨慎操作):");
scanf("%s", s6);
flag1 = 0;
for(i = 0; i < N_new; i++)
{
if(strcmp(s6, users[i].user_name) == 0)
{
N_wrong = 0;
flag1 = 1;
clean2:
printf("请输入密码:");
scanf("%s", s7);
if(strcmp(s7, users[i].user_psw) == 0)
{
printf("正在注销您的账户\n");
Sleep(2000);
printf("注销成功!\n");
for(int j = i+1; j < N_new; j++)
{
users[j].ID--;
}
for(int j = i; j+1 < N_new; j++)
{
users[j] = users[j+1];
}
N_new--;
printf("即将为你跳转到主菜单\n");
filewrite();
system("pause");
system("cls");
welcome();
return;
}
else if(N_wrong >= 2)//密码错误次数限制
{
printf("密码错误次数过多,即将为你跳转到主菜单\n");
Sleep(1000);
system("cls");
welcome();
return;
}
printf("密码错误,请重新输入!\n");
N_wrong++;
Sleep(1000);
goto clean2;
}
}
if(flag1 == 0)
{
printf("该账户不存在,请重新输入!\n");
Sleep(1000);
goto clean1;
}
}