-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.c
1665 lines (1576 loc) · 32.4 KB
/
func.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =====================================================================================
*
* Filename: func.c
*
* Description: 所有用到的函数的定义
*
* Version: 1.0
* Created: 2014年06月13日 21时12分59秒
* Revision: none
* Compiler: gcc
*
* Author: geekgao, [email protected]
* Company: Class 1302 of Software Engineer
*
* =====================================================================================
*/
#include "finance.h"
#include <stdio.h>
#include <termio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
//======================================================================================
void menu1();
void menu2();
void login(char user[]); //将登陆的用户的用户名放到user指向的空间
void add(const char username[]);
void look(const char username[]);
void edit(const char username[]);
void export(const char username[]);
int change_password(const char username[]);
int del_user(const char username[]);
void creat_user();
int login_with_password(char username[]);
User_list *creat_User_list();
Data_list *creat_Data_list();
void mygets(char str[],int count); //控制输入的字符个数为count个,包括'\0'
void clear_User_list(User_list *head);
void clear_Data_list(Data_list *head);
int read_data_to_list(const char username[],Data_list *head);
int read_user_to_list(User_list *head); //根据返回值判断是否成功
int write_data_to_file(const char username[],Data_list *head);
int write_user_to_file(User_list *head); //返回值表示写入文件的数据个数
void print_in_out(Data_list *node);
void print_type(Data_list *node);
enum In_out select_in_out();
enum Type select_type(enum In_out tmp);
void lock(char str[]);
void data_sort(const char username[]);
int timecmp(char str1[],char str2[]); //str1和str2是函数ctime返回的字符串去掉'\n'那个格式的字符串
int monstr_to_int(char str[]);
int intcmp(const void *a,const void *b);
void moneysort(Data_list *head,int count,int type); //约定type为1按升序排序,为0按降序排序
void timesort(Data_list *head,int count,int type);
void set_time(int year,int month,int mday);
void input_time(char str1[],char str2[]);
//======================================================================================
void menu2()
{
printf("*************************************************************\n");
printf("\t\t\t个人财务管理系统\n");
printf("*************************************************************\n");
printf("\t\t\t(1)记一笔\n");
printf("\t\t\t(2)查看财务报表\n");
printf("\t\t\t(3)编辑信息\n");
printf("\t\t\t(4)数据排序\n");
printf("\t\t\t(5)导出账本到文件\n");
printf("\t\t\t(6)修改密码\n");
printf("\t\t\t(7)删除账户\n");
printf("\t\t\t(8)退出系统\n");
printf("*************************************************************\n");
printf("\t\t\t 请选择:");
}
void menu1()
{
printf("*************************************************************\n");
printf("\t\t\t个人财务管理系统\n");
printf("*************************************************************\n");
printf("\t\t\t(1)登陆系统\n");
printf("\t\t\t(2)创建账户\n");
printf("\t\t\t(3)退出系统\n");
printf("*************************************************************\n");
printf("\t\t\t 请选择:");
}
void login(char user[])
{
int sel = 0;
while (1)
{
system("clear");
menu1();
scanf("%d",&sel);
while (getchar() != '\n') ;
system("clear");
switch(sel)
{
case 1:
if (login_with_password(user)){
return ;
}
break;
case 2:
creat_user();
break;
case 3:
exit(0);
break;
default:
break;
}
}
}
void add(const char username[])
{
Data_list tmp;
FILE *fp;
time_t timep;
int sel = 0,flag = 0;
char name[22]=".";
long filelength = 0;
strcat(name,username);
fp = fopen(name,"a+");
if (fp == NULL) {
printf("文件打开失败!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
fseek(fp,0L,SEEK_END);
filelength = ftell(fp);
rewind(fp);
tmp.next = NULL;
time(&timep);
strcpy(tmp.time,ctime(&timep));
tmp.time[24]= 0; //ctime()返回的字符串后面自带一个回车,这里把那个回车去掉
tmp.in_out = select_in_out();
if (tmp.in_out == back) {
fclose(fp);
if (!filelength) {
remove(name);
}
return ;
}
tmp.type = select_type(tmp.in_out);
if (tmp.type == back) {
fclose(fp);
if (!filelength) {
remove(name);
}
return ;
}
printf("请输入钱数:\n");
while (1)
{
flag = scanf("%lf",&tmp.money);
while (getchar() != '\n') ;
if (flag == 1 && tmp.money >= 0) {
break;
}
}
fwrite(&tmp,sizeof(Data_list),1,fp);
fclose(fp);
printf("%s恭喜您,本次记账成功.\n",username);
printf("按回车返回.\n");
while (getchar() != '\n') ;
}
void look(const char username[])
{
Data_list *head = creat_Data_list(),*p = head,*tmp = NULL;
int sel = 0,count = 1,i = 0,sel_all = 0;
double in_money = 0,out_money = 0;
char nowtime[26],time1[26],time2[26];
time_t timep;
if (!read_data_to_list(username,head)) {
printf("还没有账本!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
while (1)
{
printf("请选择是(1)查看全部(2)查看支出(3)查看收入(4)退出:\n");
scanf("%d",&sel);
while (getchar() != '\n') ;
if (sel >= 1 && sel <= 3) {
break;
}
if (sel == 4) {
clear_Data_list(head);
return ;
}
}
if (sel == 1) {
sel_all = 1;
}
//留下符合要求的节点
while (p->next)
{
if ((sel == 2 && p->next->in_out == in) || (sel == 3 && p->next->in_out == out)) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
printf("请选择是(1)查看全部(2)查看本月(3)查看本年(4)自定义时间段(5)退出:\n");
while (1)
{
scanf("%d",&sel);
while (getchar() != '\n') ;
if (sel >= 1 && sel <= 4) {
break;
}
if (sel == 5) {
clear_Data_list(head);
return ;
}
}
p = head;
time(&timep);
strcpy(nowtime,ctime(&timep));
//留下符合要求的节点
switch (sel)
{
case 1:
break;
case 2:
while (p->next)
{
//月份的三个字母在456位置
for (i = 4;i <= 6 && (nowtime[i] == p->next->time[i]);i++) ;
if (i <= 6) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
break;
case 3:
while (p->next)
{
for (i = 20;i <= 23 && (nowtime[i] == p->next->time[i]);i++) ;
if (i <= 23) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
break;
case 4:
input_time(time1,time2);
while (p->next)
{
if (timecmp(p->next->time,time1) == 1 || timecmp(time2,p->next->time) == 1) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
default:
break;
}
p = head->next;
printf("编号\t时间\t\t\t\t收入/支出\t类别\t\t钱数\n");
while (p)
{
printf("(%d)\t",count);
printf("%s\t",p->time);
print_in_out(p);
printf("\t\t");
print_type(p);
printf("\t");
printf("%.2f元\n",p->money);
if (p->in_out == in) {
in_money += p->money;
}
else if (p->in_out == out) {
out_money += p->money;
}
count++;
p = p->next;
}
printf("支出%.2lf元\n",out_money);
printf("收入%.2lf元\n",in_money);
if (sel_all) {
printf("净收入%.2lf元\n",in_money-out_money);
}
clear_Data_list(head);
printf("按回车程序将返回.\n");
while (getchar() != '\n') ;
}
void edit(const char username[])
{
Data_list *head = creat_Data_list(),*p,*tmp;
int count = 0,num = 0,num2[1000] = {0},sel = 0,flag = 0,one_menu = 0,two_menu = 0,three_menu = 0,i = 0,j = 0,t = 0;
char name[22] = ".",ch = 0,nowtime[26],time1[26],time2[26],y_n = 0;
time_t timep;
if (!read_data_to_list(username,head)) {
printf("还没有任何数据!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
while (1)
{
printf("请选择功能项:\n");
printf("(1)编辑条目(2)删除条目(3)退出\n");
scanf("%d",&one_menu);
while (getchar() != '\n') ;
if (one_menu >= 1 && one_menu <= 3) {
break;
}
}
switch (one_menu)
{
case 1:
printf("编号\t时间\t\t\t\t收入/支出\t类别\t\t钱数\n");
p = head->next;
while (p)
{
count++;
printf("(%d)\t",count);
printf("%s\t",p->time);
print_in_out(p);
printf("\t\t");
print_type(p);
printf("\t");
printf("%.2f元\n",p->money);
p = p->next;
}
while (1)
{
printf("请选择编辑哪个编号的信息:\n");
scanf("%d",&num);
while (getchar() != '\n') ;
if (num >= 1 && num <= count) {
break;
}
}
p = head;
count = 0;
while (p->next)
{
count++;
if (count == num) {
break;
}
p = p->next;
}
while (1)
{
printf("请选择功能项:\n");
printf("(1)修改支出/收入(2)修改类别(3)修改钱数(4)退出\n");
scanf("%d",&two_menu);
while (getchar() != '\n') ;
if (two_menu >= 1 && two_menu <= 4) {
break;
}
}
switch (two_menu)
{
case 1:
p->next->in_out = select_in_out();
if (p->next->in_out == back) {
clear_Data_list(head);
return ;
}
p->next->type = select_type(p->next->in_out);
if (p->next->type == back) {
clear_Data_list(head);
return ;
}
break;
case 2:
p->next->type = select_type(p->next->in_out);
if (p->next->type == back) {
clear_Data_list(head);
return ;
}
break;
case 3:
printf("请输入钱数:\n");
while (1)
{
flag = scanf("%lf",&p->next->money);
while (getchar() != '\n') ;
if (flag == 1 && p->next->money >= 0) {
break;
}
}
break;
case 4:
clear_Data_list(head);
return ;
default:
break;
}
break;
case 2:
while (1)
{
printf("请选择功能项:\n");
printf("(1)按序号删除(2)按时间段删除(3)退出\n");
scanf("%d",&two_menu);
while (getchar() != '\n') ;
if (two_menu >= 1 && two_menu <= 3) {
break;
}
}
switch (two_menu)
{
case 1:
p = head->next;
printf("编号\t时间\t\t\t\t收入/支出\t类别\t\t钱数\n");
while (p)
{
count++;
printf("(%d)\t",count);
printf("%s\t",p->time);
print_in_out(p);
printf("\t\t");
print_type(p);
printf("\t");
printf("%.2f元\n",p->money);
p = p->next;
}
printf("请选择删除哪些编号的信息(逗号隔开):\n");
num = 0;
while (num < 1000)
{
scanf("%d",&num2[num]);
ch = getchar();
if (ch == '\n') {
break;
}
if (num2[num]) { //编号肯定是从0开始
num++;
}
}
//用快排给序号数组排序
qsort(num2,num+1,sizeof(int),intcmp);
p = head;
count = 0;
i = 0;
while (p->next)
{
count++;
if (count == num2[i]) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
i++;
}
else {
p = p->next;
}
if (i == num+1) {
break;
}
}
break;
case 2:
while (1)
{
printf("请选择:\n");
printf("(1)删除本月(2)删除本年(3)自定义时间段(4)退出\n");
scanf("%d",&three_menu);
while (getchar() != '\n') ;
if (three_menu >= 1 && three_menu <= 4) {
break;
}
}
p = head;
time(&timep);
strcpy(nowtime,ctime(&timep));
switch (three_menu)
{
case 1:
while (p->next)
{
//月份的三个字母在456位置
for (i = 4;i <= 6 && (nowtime[i] == p->next->time[i]);i++) ;
if (i > 6) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
break;
case 2:
while (p->next)
{
for (i = 20;i <= 23 && (nowtime[i] == p->next->time[i]);i++) ;
if (i > 23) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
break;
case 3:
input_time(time1,time2);
while (p->next)
{
if (timecmp(time1,p->next->time) == 1 && timecmp(p->next->time,time2) == 1) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
}
else {
p = p->next;
}
}
break;
case 4:
clear_Data_list(head);
return ;
default:
break;
}
break;
case 3:
clear_Data_list(head);
return ;
default:
break;
}
break;
case 3:
clear_Data_list(head);
return ;
default:
break;
}
printf("请确认是否使以上操作生效?(Y确认)");
y_n = getchar();
while (getchar() != '\n') ;
if (y_n != 'y' && y_n != 'Y') {
clear_Data_list(head);
printf("操作取消,程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
count = write_data_to_file(username,head);
if (!count) {
remove(strcat(name,username));
}
clear_Data_list(head);
printf("操作成功!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
}
void export(const char username[])
{
FILE *fp;
char newfile[26];
Data_list *head = creat_Data_list(),*p;
int count = 1;
double in_money = 0,out_money = 0;
if (!read_data_to_list(username,head)) {
printf("还没有账单,请先创建账单.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
strcpy(newfile,username);
strcat(newfile,"_data");
if (!(fp = fopen(newfile,"w"))) {
printf("创建文件失败!程序将返回.");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
fprintf(fp,"编号\t时间\t\t\t\t\t\t收入/支出\t类别\t\t钱数\n");
p = head->next;
while (p)
{
fprintf(fp,"%d\t\t",count++);
fprintf(fp,"%s\t",p->time);
switch(p->in_out)
{
case in:
fprintf(fp,"收入\t\t");
in_money += p->money;
break;
case out:
fprintf(fp,"支出\t\t");
out_money += p->money;
break;
}
switch(p->type)
{
case clothes:
fprintf(fp,"衣服饰品\t");
break;
case food:
fprintf(fp,"食品水酒\t");
break;
case home:
fprintf(fp,"居家物业\t");
break;
case traffic:
fprintf(fp,"行车交通\t");
break;
case communication:
fprintf(fp,"交流通讯\t");
break;
case entertainment:
fprintf(fp,"休闲娱乐\t");
break;
case study:
fprintf(fp,"学习进修\t");
break;
case social:
fprintf(fp,"人情往来\t");
break;
case medical:
fprintf(fp,"医疗保险\t");
break;
case other_out:
fprintf(fp,"其他杂项\t");
break;
case occupation:
fprintf(fp,"职业收入\t");
break;
case other_in:
fprintf(fp,"其他收入\t");
break;
}
fprintf(fp,"%.2f元\n",p->money);
p = p->next;
}
fprintf(fp,"支出:%.2lf元\n",out_money);
fprintf(fp,"收入:%.2lf元\n",in_money);
fprintf(fp,"净收入:%.2lf元",in_money - out_money);
fclose(fp);
clear_Data_list(head);
printf("%s恭喜您,导出到文件成功!\n",username);
printf("账本文件名为:%s\n",newfile);
printf("按回车程序将返回.\n");
while (getchar() != '\n') ;
}
int change_password(const char username[])
{
User_list *head = creat_User_list(),*p = head->next;
char name[21],password[50],*str,y_n = 0;
int count = 1,flag = 0,err = 0;
if (!read_user_to_list(head)) {
printf("读取账户数据出错!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
strcpy(name,username);
lock(name);
system("stty -echo");
while (1)
{
if (count++ > 3) {
clear_User_list(head);
system("stty echo");
printf("对不起,三次尝试错误,程序将退出以保护用户数据!\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
exit(0);
}
printf("请输入旧密码(三次机会):\n");
mygets(password,50);
lock(password);
p = head->next;
while (p)
{
if (!strcmp(password,p->password) && !strcmp(name,p->name)) {
flag = 1;
break;
}
p = p->next;
}
if (flag) {
break;
}
}
printf("请输入新的密码(截取前16个字符,只能含有英文或数字):\n");
str = p->password;
do
{
if (err == 1) {
printf("对不起,输入不符合要求!\n");
printf("请重新年输入:\n");
}
err = 0;
mygets(p->password,17);
if (!strlen(p->password)) {
err = 1;
continue;
}
while (*str)
{
if (!isalpha(*str) && !isalnum(*str)) {
err = 1;
str = p->password;
break;
}
str++;
}
}while (err);
system("stty echo");
lock(p->password);
printf("确认更改密码吗?(Y确认)\n");
y_n = getchar();
while (getchar() != '\n') ;
if (y_n != 'y' && y_n != 'Y') {
clear_User_list(head);
printf("您取消了更改密码,程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return 0;
}
write_user_to_file(head);
clear_User_list(head);
printf("密码已经成功更改!请重新登陆!\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return 1;
}
int del_user(const char username[])
{
User_list *head = creat_User_list(),*p,*tmp;
FILE *fp;
char name[22] = ".",sel = 0,lockname[21],password[50];
int count = 0,flag = 0;
if (!read_user_to_list(head)) {
printf("读取用户信息出错!程序将返回\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return 0;
}
strcpy(lockname,username);
lock(lockname);
p = head;
while (p->next)
{
if(!strcmp(p->next->name,lockname)) {
printf("请输入密码,您有3次机会,请珍惜:\n");
while (count < 3)
{
printf("密码:");
system("stty -echo");
mygets(password,50);
system("stty echo");
lock(password);
if (!strcmp(password,p->next->password)) {
tmp = p->next;
p->next = p->next->next;
free(tmp);
flag = 1;//标记成功输入密码
printf("\n");
break;
}
printf("\n");
count++;
if (count == 3) {
printf("您输入的密码不正确!程序将退出!\n");
printf("按回车继续.");
while (getchar() != '\n') ;
exit(1);
}
}
}
if (flag) {
break;
}
p = p->next;
}
printf("确认要删除账户吗?(Y确认)");
sel = getchar();
while (getchar() != '\n') ;
if (sel != 'y' && sel != 'Y') {
printf("取消删除账户,程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return 0;
}
strcat(name,username);
fp =fopen(name,"r");
if (fp) {
printf("当前账户存在账本数据,是否将账本信息导出到文件后再删除账户?(Y确认)\n");
sel = getchar();
while (getchar() != '\n') ;
if (sel == 'y' || sel == 'Y') {
export(username);
}
fclose(fp);
}
remove(name);
count = write_user_to_file(head);
if (!count) {
remove(".user");
}
clear_User_list(head);
return 1;
}
void creat_user()
{
FILE *fp;
User_list *p = NULL,*head = creat_User_list(),tmp;
int err = 0,had_file = 0;
char *str = tmp.name,true_name[21];
//如果文件存在
if (read_user_to_list(head)) {
had_file = 1;
}
fp = fopen(".user","a+");
if (fp == NULL) {
printf("文件打开失败!程序将返回.\n");
printf("按回车继续.\n");
while (getchar() != '\n') ;
return ;
}
printf("请输入新的用户名(截取前20个字符,只能含有英文或数字):\n");
do
{
if (err == 1) {
printf("对不起,已经含有此用户名!\n");
printf("请重新年输入:\n");
}
if (err == 2) {
printf("对不起,输入不符合要求!\n");
printf("请重新年输入:\n");
}
err = 0;
mygets(tmp.name,21);
if (!strlen(tmp.name)) {
err = 2;
continue;
}
while (*str)
{
if (!isalpha(*str) && !isalnum(*str)) {
err = 2;
str = tmp.name;
break;
}
str++;
}
if (had_file && !err) {
p = head->next;
while (p)
{
if (!strcmp(p->name,tmp.name)) {
err = 1;
break;
}
p = p->next;
}
}
}while (err);
str = tmp.password;
printf("请输入密码(截取前16个字符,只能含有英文或数字):\n");
system("stty -echo");
do
{
if (err == 2) {
printf("对不起,输入不符合要求!\n");
printf("请重新年输入:\n");
}
err = 0;
mygets(tmp.password,17);
if (!strlen(tmp.password)) {
err = 2;
continue;
}
while (*str)
{
if (!isalpha(*str) && !isalnum(*str)) {
err = 2;
str = tmp.password;
break;
}
str++;
}
}while (err);
system("stty echo");
strcpy(true_name,tmp.name);
lock(tmp.password);
lock(tmp.name);
fwrite(&tmp,sizeof(User_list),1,fp);
fclose(fp);
clear_User_list(head);
printf("%s恭喜您,您的账户创建成功!\n",true_name);
printf("按回车返回.\n");
while (getchar() != '\n') ;
}
int login_with_password(char username[])
{
User_list *head = creat_User_list(),*p = NULL;
int err = 1,count = 1;
char name[50],password[50];
//如果文件存在
if (!read_user_to_list(head)) {
printf("对不起,还没有任何用户被创建或者文件打开出错!\n");
printf("按回车返回.\n");
while (getchar() != '\n');
return 0;
}
printf("您有5次机会输入,请珍惜:\n");
do
{
if (count++ >5) {
printf("对不起,您已经尝试达到五次,程序将退出!\n");
exit(0);
}
if (err == 2) {
printf("用户名或密码错误,请重新输入!\n");
}
printf("请输入用户名:");
mygets(name,50);
strcpy(username,name);
lock(name);
printf("请输入密码:");
system("stty -echo");
mygets(password,50);
lock(password);
printf("\n");
system("stty echo");
p = head->next;
while (p)
{
if (!strcmp(name,p->name) && !strcmp(password,p->password)) {
err = 0;
break;
}
p = p->next;
err = 2;