2015年5月9日 student information management system
/*大作业SIMS*/
//头文件
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("\n*******************************************************************************\n"); 10 printf("***************Student Information Management System*****************\n"); 11 printf("*******************************************************************************\n"); 12 printf("\n\n\t\t1. search student information"); 13 printf("\n\t\t2. add student information"); 14 printf("\n\t\t3. update student information"); 15 printf("\n\t\t4. delete student information"); 16 printf("\n\t\t5. add user account"); 17 printf("\n\t\t6. update user account"); 18 printf("\n\t\t7. delete user account"); 19 printf("\n\t\t8. search user account"); 20 printf("\n\t\t9. exit"); 21 printf("\n\n\nselect a number between 1--9:"); 22 return type; 23 } 24 else if (type == 2) 25 { 26 system("cls"); 27 printf("\n*******************************************************************************\n"); 28 printf("***************Student Information Management System****************\n"); 29 printf("*******************************************************************************\n"); 30 printf("\n\n\t\t1. search student information"); 31 /* 32 printf("\n\t\t2. add student information"); 33 printf("\n\t\t3. update student information"); 34 printf("\n\t\t4. delete student information"); 35 printf("\n\t\t5. add user account"); 36 printf("\n\t\t6. update user account"); 37 printf("\n\t\t7. delete user account"); 38 printf("\n\t\t8. search user account"); 39 */ 40 printf("\n\t\t9. exit"); 41 printf("\n\n\nselect 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("\n0. search all"); 57 printf("\n1. search by id"); 58 printf("\n2. search by name"); 59 printf("\n3. return"); 60 printf("\nselect a number:"); 61 } 62 else 63 { 64 printf("\n1. search by id"); 65 printf("\n2. search by name"); 66 printf("\n3. return"); 67 printf("\nselect a number:"); 68 69 } 70 return type; 71 } 72 73 void account_search_menu(void) 74 { 75 system("cls"); 76 printf("\n0. search all account"); 77 printf("\n1. search account by name"); 78 printf("\n2. exit"); 79 printf("\nselect a number:"); 80 81 } 82 83 void usr_infor_delete_menu(void) 84 { 85 system("cls"); 86 printf("\n1. delete by id"); 87 printf("\n2. delete by name"); 88 printf("\n3. return"); 89 printf("\nselect 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\n", pphead->usr_id, pphead->usr_name, pphead->usr_course_id, pphead->usr_course_score); 99 pphead = pphead->pNext_Usr_Infor; 100 } 101 printf("\n"); 102 } 103 104 void show_usr_account(pUSR_ACCOUNT pphead) 105 { 106 system("cls"); 107 while (pphead != NULL){ 108 fprintf(stdout, "%-15s%-15s%3d\n", pphead->usr_name, pphead->usr_pwd, pphead->usr_role); 109 pphead = pphead->pNext_Usr_Account; 110 printf("\n"); 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\n", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score); 129 } 130 else 131 { 132 printf("cannot find\n"); 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\n", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score); 150 } 151 else 152 { 153 printf("cannot find\n"); 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\n", pcur->usr_name, pcur->usr_pwd, pcur->usr_role); 169 } 170 else{ 171 fprintf(stdout, "donot exist the name\n"); 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\n"); 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.\n",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\tusr_pwd\tusr_role\n"); 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\n"); 249 return; 250 } 251 //已知usr_account在第一行,不知道怎么判断? 252 memset(line, 0, sizeof(line)); 253 fgets(line, sizeof(line), fp_config); 254 line[strlen(line)-1] = '\0'; 255 if ((fp_usr_account = fopen(line, "w")) == NULL){ 256 printf("file cannot open fp_usr_account\n"); 257 return; 258 } 259 fclose(fp_config); 260 while (pcur != NULL){ 261 if (fprintf(fp_usr_account, "%-10s%-10s%-5d\n", pcur->usr_name,pcur->usr_pwd,pcur->usr_role) == EOF) 262 printf("write error\n"); 263 pcur = pcur->pNext_Usr_Account; 264 } 265 fclose(fp_usr_account); 266 } 267 268 void save_usr_info(pUSR_INFOR *pphead, char *addres) 269 { 270 pUSR_INFOR pcur; 271 FILE *fp_config, *fp_usr_infor; 272 char line[LEN]; 273 pcur = *pphead; 274 if ((fp_config = fopen(addres, "r")) == NULL){ 275 printf("file cannot open fp_config\n"); 276 return; 277 } 278 //已知usr_info在第二行所以采取读两次来调用,没有想到怎么用数组或者链表? 279 memset(line, 0, sizeof(line)); 280 //第一行有\n,第二行没有\n 281 fgets(line, sizeof(line), fp_config); 282 memset(line, 0, sizeof(line)); 283 fgets(line, sizeof(line), fp_config); 284 if ((fp_usr_infor = fopen(line, "w")) == NULL){ 285 printf("file cannot open fp_usr_infor\n"); 286 return; 287 } 288 fclose(fp_config); 289 while (pcur != NULL){ 290 if (fprintf(fp_usr_infor, "%-5d%-10s%-5d%8.2f\n", pcur->usr_id,pcur->usr_name,pcur->usr_course_id,pcur->usr_course_score) == EOF) 291 printf("write error\n"); 292 pcur = pcur->pNext_Usr_Infor; 293 } 294 fclose(fp_usr_infor); 295 } 296 297 298 void delet_usr_info_by_id(pUSR_INFOR *pphead, int id) 299 { 300 pUSR_INFOR pcur, ppre; 301 FILE *fp; 302 pcur = *pphead; 303 ppre = NULL; 304 while (pcur != NULL){ 305 if (pcur->usr_id == id){ 306 break; 307 } 308 else{ 309 ppre = pcur; 310 pcur = pcur->pNext_Usr_Infor; 311 } 312 313 } 314 if (pcur != NULL){ 315 if (ppre == NULL){ 316 //删除第一个结点 317 *pphead = pcur->pNext_Usr_Infor; 318 } 319 else{ 320 ppre->pNext_Usr_Infor = pcur->pNext_Usr_Infor; 321 322 } 323 free(pcur); 324 pcur = NULL; 325 } 326 else 327 { 328 printf("donot exist in the file.\n"); 329 } 330 if (*pphead == NULL){ 331 free(*pphead); 332 *pphead = NULL; 333 } 334 } 335 336 void delet_usr_info_by_name(pUSR_INFOR *pphead, char name) 337 { 338 pUSR_INFOR pcur, ppre; 339 FILE *fp; 340 pcur = *pphead; 341 ppre = NULL; 342 while (pcur != NULL){ 343 if (strcmp(pcur->usr_name,name)==0){ 344 break; 345 } 346 else{ 347 ppre = pcur; 348 pcur = pcur->pNext_Usr_Infor; 349 } 350 351 } 352 if (pcur != NULL){ 353 if (ppre == NULL){ 354 //删除第一个结点 355 *pphead = pcur->pNext_Usr_Infor; 356 } 357 else{ 358 ppre->pNext_Usr_Infor = pcur->pNext_Usr_Infor; 359 360 } 361 free(pcur); 362 pcur = NULL; 363 } 364 else 365 { 366 printf("donot exist in the file.\n"); 367 } 368 if (*pphead == NULL){ 369 free(*pphead); 370 *pphead = NULL; 371 } 372 } 373 374 //账户信息不能全部删除,需要加判断??? 375 void delet_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name) 376 { 377 pUSR_ACCOUNT pcur, ppre; 378 char line[LEN]; 379 pcur = *pphead; 380 ppre = NULL; 381 while (pcur != NULL && strcmp(pcur->usr_name, name) != 0){ 382 ppre = pcur; 383 pcur = pcur->pNext_Usr_Account; 384 } 385 if (pcur != NULL){ 386 ppre->pNext_Usr_Account = pcur->pNext_Usr_Account; 387 fprintf(stdout, "delete ok.\n"); 388 } 389 else 390 { 391 fprintf(stdout, "delete error, donot exit\n"); 392 } 393 394 } 395 396 397 void updat_usr_infor_by_id(pUSR_INFOR *pphead, int id) 398 { 399 pUSR_INFOR pcur; 400 char line[LEN]; 401 pcur = *pphead; 402 while (pcur != NULL){ 403 if (pcur->usr_id == id){ 404 break; 405 } 406 else 407 pcur = pcur->pNext_Usr_Infor; 408 } 409 if (pcur != NULL){ 410 //只能修改后面三项内容 411 fflush(stdin); 412 memset(line, 0, sizeof(line)); 413 printf("updat NO.%d new infor\nname\tcouse_id\tcours_score:\n ", pcur->usr_id); 414 fgets(line, sizeof(line), stdin); 415 sscanf(line, "%s%d%lf", pcur->usr_name, &pcur->usr_course_id, &pcur->usr_course_score); 416 417 } 418 else 419 { 420 printf("cannot find the id_infor\n"); 421 } 422 } 423 424 void update_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name) 425 { 426 pUSR_ACCOUNT pcur; 427 char line[LEN]; 428 pcur = *pphead; 429 while (pcur != NULL && strcmp(pcur->usr_name, name) != 0){ 430 pcur = pcur->pNext_Usr_Account; 431 } 432 if (pcur != NULL){ 433 memset(line, 0, sizeof(line)); 434 fflush(stdin); 435 fprintf(stdout, "usr_pwd\t\tusr_role:\n"); 436 fgets(line, sizeof(line), stdin); 437 sscanf(line, "%s%d", pcur->usr_pwd, &pcur->usr_role); 438 } 439 else 440 { 441 fprintf(stdout, "updata usr_account error\n"); 442 } 443 444 }
//函数接口设计不好,因此都是主函数调用,程序比较混乱
1 #include "sims.h" 2 3 int main(int argc, char *argv[]) 4 { 5 pUSR_INFOR usr_infor_head = NULL; 6 pUSR_ACCOUNT account_head = NULL; 7 char *paddres; //传递配置文件地址 8 int digit; 9 int s_digit, d_digit; 10 int usr_id, delet_id; 11 char usr_name[LEN] = ""; 12 char usr_account_name[LEN] = ""; 13 USR_ROLE type,s_type; //登陆者身份标示 14 paddres = (char *)calloc(1, sizeof(char)); 15 paddres = argv[1]; 16 //加载初始账户密码和学生信息 17 type = system_init(&account_head, &usr_infor_head, paddres); 18 s_type = system_menu(type); 19 20 //以管理员身份登录 21 if (s_type == 1){ 22 23 fflush(stdin); 24 scanf("%d", &digit); 25 while (digit < 1 || digit > 9) { 26 fflush(stdin); 27 //system("cls"); 28 scanf("%d", &digit); 29 } 30 switch (digit){ 31 case 1: 32 search_menu(s_type); 33 scanf("%d", &s_digit); 34 while (s_digit < 0 || s_digit > 3) { 35 fflush(stdin); 36 //system("cls"); 37 scanf("%d", &s_digit); 38 } 39 switch (s_digit) 40 { 41 case 0: 42 //show all usr_infor 43 show_usr_info(usr_infor_head); 44 break; 45 case 1: 46 //search by id 47 fflush(stdout); 48 system("cls"); 49 fflush(stdin); 50 printf("input usr_id:\n"); 51 scanf("%d", &usr_id); 52 search_usr_info_id(&usr_infor_head, usr_id); 53 break; 54 case 2: 55 //search by name 56 system("cls"); 57 fflush(stdin); 58 printf("input usr_name:\n"); 59 scanf("%s", usr_name); 60 search_usr_info_name(&usr_infor_head, usr_name); 61 break; 62 case 3: 63 return; 64 //s_type = system_menu(type); 65 default: 66 break; 67 } 68 break; 69 70 case 2: 71 //add 72 add_usr_info(&usr_infor_head, argv[1]); 73 save_usr_info(&usr_infor_head, argv[1]); 74 break; 75 case 3: 76 //update by id 77 fflush(stdout); 78 system("cls"); 79 fflush(stdin); 80 printf("input usr_id:\n"); 81 scanf("%d", &usr_id); 82 updat_usr_infor_by_id(&usr_infor_head, usr_id); 83 save_usr_info(&usr_infor_head, argv[1]); 84 break; 85 case 4: 86 //delet by id 87 usr_infor_delete_menu(); 88 scanf("%d", &d_digit); 89 if (d_digit == 1){ 90 system("cls"); 91 fflush(stdin); 92 printf("input delet_usr_id:\n"); 93 scanf("%d", &delet_id); 94 delet_usr_info_by_id(&usr_infor_head, delet_id); 95 save_usr_info(&usr_infor_head, argv[1]); 96 } 97 else if (d_digit == 2){ 98 system("cls"); 99 fflush(stdin); 100 printf("input delet_usr_name:\n"); 101 scanf("%s",usr_name); 102 delet_usr_info_by_name(&usr_infor_head, usr_name); 103 save_usr_info(&usr_infor_head, argv[1]); 104 } 105 else 106 { 107 return; 108 } 109 break; 110 case 5: 111 //add user account 112 add_usr_account(&account_head, argv[1]); 113 //save 114 save_usr_account(&account_head, argv[1]); 115 break; 116 case 6: 117 //update usr_account 118 system("cls"); 119 fflush(stdin); 120 printf("input usr_account_name:\n"); 121 scanf("%s", usr_account_name); 122 update_usr_account_by_name(&account_head, usr_account_name); 123 save_usr_account(&account_head, argv[1]); 124 break; 125 case 7: 126 //delet usr_account_by_name 127 system("cls"); 128 fflush(stdin); 129 printf("input usr_account_name:\n"); 130 scanf("%s", usr_account_name); 131 delet_usr_account_by_name(&account_head, usr_account_name); 132 save_usr_account(&account_head, argv[1]); 133 break; 134 case 8: 135 //search usr_account 136 account_search_menu(); 137 scanf("%d", &s_digit); 138 while (s_digit < 0 || s_digit > 3) { 139 fflush(stdin); 140 //system("cls"); 141 scanf("%d", &s_digit); 142 } 143 144 if (s_digit == 0){ 145 //show all account 146 show_usr_account(account_head); 147 } 148 else if (s_digit == 1){ 149 //search by name 150 system("cls"); 151 fflush(stdin); 152 printf("input usr_name:\n"); 153 scanf("%s", usr_account_name); 154 search_usr_account_by_name(&account_head, usr_account_name); 155 } 156 else 157 exit; 158 break; 159 case 9: 160 exit; 161 break; 162 default: 163 printf("select a digit between 1--9:"); 164 } 165 } 166 else if (s_type == 2) 167 //以访客身份登录 168 { 169 fflush(stdin); 170 scanf("%d", &digit); 171 while (digit < 0 || digit > 3) { 172 fflush(stdin); 173 //system("cls"); 174 scanf("%d", &digit); 175 } 176 177 if (digit == 1){ 178 search_menu(s_type); 179 scanf("%d", &s_digit); 180 while (s_digit < 0 || s_digit > 3) { 181 fflush(stdin); 182 scanf("%d", &s_digit); 183 } 184 } 185 if (s_digit == 1){ 186 //search by id 187 fflush(stdout); 188 system("cls"); 189 fflush(stdin); 190 printf("input usr_id:\n"); 191 scanf("%d", &usr_id); 192 search_usr_info_id(&usr_infor_head, usr_id); 193 } 194 else if (s_digit == 2){ 195 //search by name 196 system("cls"); 197 fflush(stdin); 198 printf("input usr_name:\n"); 199 scanf("%s", usr_name); 200 search_usr_info_name(&usr_infor_head, usr_name); 201 } 202 else //(s_digit == 3) 退出 203 { 204 exit; 205 } 206 } 207 else 208 exit; 209 210 system("pause"); 211 return 0; 212 }
//算是我自己写的比较长的代码mark//