-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
145 lines (120 loc) · 3.25 KB
/
menu.c
File metadata and controls
145 lines (120 loc) · 3.25 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "menu.h"
#include "list.h"
#define TYPE_SIZE 8
#define ID_SIZE 16
#define PWD_SIZE 16
bool help(){
printf("usage:\n");
printf("\ta: addAccount\n");
printf("\ts: showAllAccount\n");
printf("\td: deleteAccount\n");
printf("\tq: quit without save\n");
printf("\tx: quit and save\n");
printf("\th: help\n");
return TRUE;
}
bool login(){
char usr[10];
char pwd[10];
printf("user:");
scanf("%s",usr);
printf("pwd :");
scanf("%s",pwd);
if(strcmp(usr,"root") == 0 && strcmp(pwd,"root") == 0){
initList(&gl_list);
help();
return TRUE;
}else{
return FAIL;
}
}
bool addAccount(){
char acc_type[TYPE_SIZE];
char acc_id[ID_SIZE];
char acc_pwd[PWD_SIZE];
/*here is a big bug ,if user input beyand the array size ,program will be crashed.
is there any way to avoid this problem?
*/
printf("your acc_type:");
scanf("%s",acc_type);
printf("your acc_id:");
scanf("%s",acc_id);
printf("your acc_pwd:");
scanf("%s",acc_pwd);
if(TRUE == insertList(&gl_list,acc_type,acc_id,acc_pwd)){
printf("Congratulation!\nYour new account has been insert into database successfully\n");
return TRUE;
}else{
printf("Sorry!\nI failed to insert your new account into database\n");
return FAIL;
}
}
bool showAccount(){
int i;
Account *cur_paccount;
listNode *cur_node;
cur_node = gl_list.head;
for(i = 0;i < gl_list.len;i++){
cur_paccount = cur_node->value;
if(cur_paccount != NULL){
printf("type:%-10s id:%-10s pwd:%-10s\n",cur_paccount->acc_type,\
cur_paccount->acc_id,cur_paccount->acc_pwd);
}
cur_node=cur_node->next;
}
}
bool deleteAccount(){
char acc_type[TYPE_SIZE];
char acc_id[ID_SIZE];
listNode *node;
printf("your acc_type:");
scanf("%s",acc_type);
printf("your acc_id:");
scanf("%s",acc_id);
if((node = searchListByTypeAndID(&gl_list,acc_type,acc_id)) == NULL){
printf("can't find the account\n");
exit(0);
}
if(TRUE == listDelNode(&gl_list,node)){
printf("SUCCESSFULLY!\nthe account has been delete\n");
return TRUE;
}else{
printf("SORRY!\nwe fail to delete the account!\n");
return FAIL;
}
}
bool quit(){
int i;
Account *cur_paccount;
listNode *cur_node,*next_node;
cur_node = gl_list.head;
for(i = 0;i < gl_list.len;i++){
next_node = cur_node->next;
freeListNode(cur_node);
cur_node = next_node;
}
}
bool saveAndQuit(){
FILE *pf;
int i;
Account *cur_paccount;
listNode *cur_node;
if((pf = fopen(DATA_FILE,"w")) == NULL){
printf("can't open %s",DATA_FILE);
exit(0);
}
cur_node = gl_list.head;
for(i = 0;i < gl_list.len;i++){
cur_paccount = cur_node->value;
if(cur_paccount != NULL){
fprintf(pf,"%s:%s:%s\n",cur_paccount->acc_type,\
cur_paccount->acc_id,cur_paccount->acc_pwd);
}
cur_node=cur_node->next;
}
fclose(pf);
quit();
}