1 #ifndef __FUNC_C__
2 #define __FUNC_C__
3
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #define LEN 128
9 #define M 100 //账户最多人数
10 #define N 1000 //学生最多人数
11 #define USR_NAME_LEN 20
12 #define USR_PWD_LEN 20
13
14 typedef int USR_ROLE;
15
16 //账号信息
17 typedef struct usr_account_tag
18 {
19 char usr_name[USR_NAME_LEN];
20 char usr_pwd[USR_PWD_LEN];
21 USR_ROLE usr_role; //管理员设置为1,其它设置为0
22 struct usr_account_tag *pNext_Usr_Account;
23 }USR_ACCOUNT, *pUSR_ACCOUNT;
24
25
26 //学生信息
27 typedef struct usr_infor_tag
28 {
29 int usr_id; //学号
30 char usr_name[USR_NAME_LEN]; //姓名
31 int usr_course_id; //课程号
32 double usr_course_score; //课程分数
33 struct usr_infor_tag *pNext_Usr_Infor; //下一个学生信息
34
35 }USR_INFOR, *pUSR_INFOR;
36
37
38 USR_ROLE system_init(pUSR_ACCOUNT *account_head, pUSR_INFOR *usr_infor_head, char *addres);
39
40 USR_ROLE system_menu(USR_ROLE type);
41 USR_ROLE search_menu(USR_ROLE type);
42 void usr_infor_delete_menu(void);
43 void account_search_menu(void);
44 void show_usr_info(pUSR_INFOR pphead);
45 void show_usr_account(pUSR_ACCOUNT pphead);
46
47 void search_usr_info_id(pUSR_INFOR *pphead, int id);
48 void search_usr_info_name(pUSR_INFOR *pphead, char *name);
49
50 void save_usr_info(pUSR_INFOR *pphead, char *addres);
51 void add_usr_info(pUSR_INFOR *pphead, char *addres);
52 void delet_usr_info_by_id(pUSR_INFOR *pphead, int id);
53 void delet_usr_info_by_name(pUSR_INFOR *pphead, char name);
54 void delet_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name);
55 void updat_usr_infor_by_id(pUSR_INFOR *pphead, int id);
56 void update_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name);
57 void add_usr_account(pUSR_ACCOUNT *pphead, char *addres);
58 void save_usr_account(pUSR_ACCOUNT *pphead, char *addres);
59 void search_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name);
60
61
62
63 #endif
1 #include "sims.h"
2
3 USR_ROLE system_menu(USR_ROLE type)
4 {
5 int number;
6 int search_number;
7 if (type == 1){
8 system("cls");
9 printf("
*******************************************************************************
");
10 printf("***************Student Information Management System*****************
");
11 printf("*******************************************************************************
");
12 printf("
1. search student information");
13 printf("
2. add student information");
14 printf("
3. update student information");
15 printf("
4. delete student information");
16 printf("
5. add user account");
17 printf("
6. update user account");
18 printf("
7. delete user account");
19 printf("
8. search user account");
20 printf("
9. exit");
21 printf("
select a number between 1--9:");
22 return type;
23 }
24 else if (type == 2)
25 {
26 system("cls");
27 printf("
*******************************************************************************
");
28 printf("***************Student Information Management System****************
");
29 printf("*******************************************************************************
");
30 printf("
1. search student information");
31 /*
32 printf("
2. add student information");
33 printf("
3. update student information");
34 printf("
4. delete student information");
35 printf("
5. add user account");
36 printf("
6. update user account");
37 printf("
7. delete user account");
38 printf("
8. search user account");
39 */
40 printf("
9. exit");
41 printf("
select a number:");
42 return type;
43 }
44 else
45 {
46 return;
47 }
48 }
49
50
51
52 USR_ROLE search_menu(USR_ROLE type)
53 {
54 system("cls");
55 if (type == 1){
56 printf("
0. search all");
57 printf("
1. search by id");
58 printf("
2. search by name");
59 printf("
3. return");
60 printf("
select a number:");
61 }
62 else
63 {
64 printf("
1. search by id");
65 printf("
2. search by name");
66 printf("
3. return");
67 printf("
select a number:");
68
69 }
70 return type;
71 }
72
73 void account_search_menu(void)
74 {
75 system("cls");
76 printf("
0. search all account");
77 printf("
1. search account by name");
78 printf("
2. exit");
79 printf("
select a number:");
80
81 }
82
83 void usr_infor_delete_menu(void)
84 {
85 system("cls");
86 printf("
1. delete by id");
87 printf("
2. delete by name");
88 printf("
3. return");
89 printf("
select a number:");
90 fflush(stdin);
91 }
92
93
94 void show_usr_info(pUSR_INFOR pphead)
95 {
96 system("cls");
97 while (pphead != NULL){
98 printf("%-5d%-10s%-3d%7.2f
", pphead->usr_id, pphead->usr_name, pphead->usr_course_id, pphead->usr_course_score);
99 pphead = pphead->pNext_Usr_Infor;
100 }
101 printf("
");
102 }
103
104 void show_usr_account(pUSR_ACCOUNT pphead)
105 {
106 system("cls");
107 while (pphead != NULL){
108 fprintf(stdout, "%-15s%-15s%3d
", pphead->usr_name, pphead->usr_pwd, pphead->usr_role);
109 pphead = pphead->pNext_Usr_Account;
110 printf("
");
111 }
112 }
113
114
115 void search_usr_info_id(pUSR_INFOR *pphead, int id)
116 {
117 pUSR_INFOR pcur;
118 FILE *fp;
119 pcur = *pphead;
120 while (pcur != NULL){
121 if (pcur->usr_id == id){
122 break;
123 }
124 else
125 pcur = pcur->pNext_Usr_Infor;
126 }
127 if (pcur != NULL){
128 printf("%-5d%-10s%-3d%7.2f
", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score);
129 }
130 else
131 {
132 printf("cannot find
");
133 }
134 }
135
136 void search_usr_info_name(pUSR_INFOR *pphead, char *name)
137 {
138 pUSR_INFOR pcur;
139 FILE *fp;
140 pcur = *pphead;
141 while (pcur != NULL){
142 if (strcmp(name,pcur->usr_name)==0){
143 break;
144 }
145 else
146 pcur = pcur->pNext_Usr_Infor;
147 }
148 if (pcur != NULL){
149 printf("%-5d%-10s%-3d%7.2f
", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score);
150 }
151 else
152 {
153 printf("cannot find
");
154 }
155 }
156
157 void search_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name)
158 {
159 pUSR_ACCOUNT pcur;
160 pcur = *pphead;
161 while (pcur != NULL){
162 if (strcmp(pcur->usr_name, name) == 0)
163 break;
164 else
165 pcur = pcur->pNext_Usr_Account;
166 }
167 if (pcur != NULL){
168 fprintf(stdout, "%-15s%-15s%3d
", pcur->usr_name, pcur->usr_pwd, pcur->usr_role);
169 }
170 else{
171 fprintf(stdout, "donot exist the name
");
172 }
173 }
174
175
176 void add_usr_info(pUSR_INFOR *pphead, char *addres)
177 {
178 pUSR_INFOR pnew = NULL;
179 pUSR_INFOR pcur, ppre;
180 char line[LEN];
181 system("cls");
182 fflush(stdin);
183 printf("input: id name course_id course_score
");
184 while (memset(line, 0, sizeof(line)), fgets(line, sizeof(line), stdin) != NULL){
185 pnew = (pUSR_INFOR)calloc(1, sizeof(USR_INFOR));
186 sscanf(line, "%d%s%d%lf", &pnew->usr_id, pnew->usr_name, &pnew->usr_course_id, &pnew->usr_course_score);
187 pcur = *pphead;
188 ppre = NULL;
189 //想增加判断学号是否已经存在,存在就拒接添加。
190 while (pcur != NULL)
191 {
192 if (pcur->usr_id == pnew->usr_id)
193 break;
194 else
195 pcur = pcur->pNext_Usr_Infor;
196 }
197 if (pcur == NULL){
198 pcur = *pphead;
199 ppre = NULL;
200 while (pcur != NULL && pcur->usr_id <= pnew->usr_id){ //按照学号升序排列
201 ppre = pcur;
202 pcur = pcur->pNext_Usr_Infor;
203 }
204 if (ppre == NULL){
205 pnew->pNext_Usr_Infor = *pphead;
206 *pphead = pnew;
207 }
208 else
209 {//中间和结尾都是这样,结尾时pcur为空
210 ppre->pNext_Usr_Infor = pnew;
211 pnew->pNext_Usr_Infor = pcur;
212 }
213 }
214 else
215 {
216 fprintf(stdout, "the %d usr_infor already exist.
",pcur->usr_id);
217 }
218 }
219
220
221
222 }
223
224 void add_usr_account(pUSR_ACCOUNT *pphead, char *addres)
225 {
226 pUSR_ACCOUNT pnew = NULL;
227 pUSR_ACCOUNT pcur, ppre;
228 char line[LEN];
229 system("cls");
230 fflush(stdin);
231 printf("input: usr_name usr_pwd usr_role
");
232 while (memset(line, 0, sizeof(line)), fgets(line, sizeof(line), stdin) != NULL){
233 pnew = (pUSR_ACCOUNT)calloc(1, sizeof(USR_ACCOUNT));
234 sscanf(line, "%s%s%d", pnew->usr_name, pnew->usr_pwd, &pnew->usr_role);
235 pnew->pNext_Usr_Account = *pphead;
236 *pphead = pnew;
237 }
238 }
239
240
241 void save_usr_account(pUSR_ACCOUNT *pphead, char *addres)
242 {
243 pUSR_ACCOUNT pcur;
244 FILE *fp_config, *fp_usr_account;
245 char line[LEN];
246 pcur = *pphead;
247 if ((fp_config = fopen(addres, "r")) == NULL){
248 printf("file cannot open fp_config
");
249 return;
250 }
251 //已知usr_account在第一行,不知道怎么判断?
252 memset(line, 0, sizeof(line));
253 fgets(line, sizeof(line), fp_config);
254 line[strlen(line)-1] = '