一个简单学生信息管理系统。
功能 :
1. 可以读写文件内容
2.可以显示学生信息(只写了id name chinese math ave 五项信息)
3.可以删除学生信息
4.可以添加学生信息
1.头文件
1 #ifndef _HEAD_H_ 2 #define _HEAD_H_ 3 4 #include <stdio.h> 5 #include <string.h> 6 #include <stdlib.h> 7 #include <errno.h> 8 9 #define MAX_NAME 37 10 #define MAX 1024 11 #define MAX_DATA 51 12 13 enum hex{show = 1, sort, add, my_delete, quite = 520}; 14 15 typedef struct students 16 { 17 char name[MAX_NAME]; 18 int seg; 19 int id; 20 float chinese; 21 float math; 22 float ave; 23 struct students *next; 24 }stu_t; 25 26 int read_file(stu_t *); 27 int write_file(stu_t *); 28 void show_student_data(stu_t *); 29 void sort_ave_data(stu_t *); 30 void add_student_data(stu_t *, int); 31 void delete_student_data(stu_t *); 32 33 #endif
2.main函数
1 //main.c 2 #include "head.h" 3 4 int main(int argc, char *argv[]) 5 { 6 stu_t *head; 7 int choose_num = 0, add_num = 0; 8 9 head = calloc(1, sizeof(stu_t)); 10 11 if(-1 == read_file(head)) { 12 printf("------------------------------------------------------------------------------ "); 13 fprintf(stderr,"No File or File no data! "); 14 printf("------------------------------------------------------------------------------ "); 15 } 16 else { 17 printf("Read success! "); 18 printf("---------------------------------Write_list_data------------------------------ "); 19 } 20 21 while(1) 22 { 23 printf("============================================================================== "); 24 printf("1.Display all student's info " 25 "2.Sort by average " 26 "3.Insert a new info " 27 "4.Delete a record "); 28 printf("============================================================================== "); 29 printf("PLease input your choice : "); 30 scanf("%d", &choose_num); 31 getchar(); 32 33 switch(choose_num) 34 { 35 case quite : 36 if(-1 == write_file(head)) { 37 printf("------------------------------------------------------------------------------ "); 38 fprintf(stderr, "Write_file fail %s : %d : %s. ", __FILE__, __LINE__, strerror(errno)); 39 printf("------------------------------------------------------------------------------ "); 40 return -1; 41 } 42 else if(0 == write_file(head)) { 43 printf("------------------------------------------------------------------------------ "); 44 printf("Write_File success! "); 45 } 46 47 printf("Good Lucky! "); 48 printf("------------------------------------------------------------------------------ "); 49 50 free(head); 51 head = NULL; 52 53 return 0; 54 break; 55 56 case show : 57 show_student_data(head); 58 break; 59 60 case sort : 61 printf("--------------------------Before sort_data------------------------------------ "); 62 show_student_data(head); 63 printf("--------------------------Before sort_data------------------------------------ "); 64 sort_ave_data(head); 65 printf("--------------------------After sort_data------------------------------------- "); 66 show_student_data(head); 67 printf("--------------------------After sort_data------------------------------------- "); 68 break; 69 70 case add : 71 printf("Enter the student_num : "); 72 scanf("%d", &add_num); 73 getchar(); 74 75 add_student_data(head, add_num); 76 printf("--------------------------After add_data-------------------------------------- "); 77 show_student_data(head); 78 printf("--------------------------After add_data-------------------------------------- "); 79 80 break; 81 82 case my_delete : 83 printf("--------------------------Before add_data------------------------------------- "); 84 show_student_data(head); 85 printf("--------------------------Before add_data------------------------------------ "); 86 printf("------------------------------------------------------------------------------ "); 87 delete_student_data(head); 88 break; 89 90 default : 91 printf("------------------------------------------------------------------------------ "); 92 printf("Enter error....... "); 93 printf("------------------------------------------------------------------------------ "); 94 break; 95 } 96 } 97 98 return 0; 99 }
3.文件操作
1 //file.c 2 #include "head.h" 3 4 int read_file(stu_t *read_head) 5 { 6 FILE *fp_read; 7 char buffer[MAX], itemp_data[MAX_DATA]; 8 stu_t *data, *itemp_head; 9 int number = 0; 10 11 if(NULL == (fp_read = fopen("student_data.txt", "r"))) { 12 return -1; 13 } 14 15 itemp_head = read_head; 16 while(fgets(buffer, MAX, fp_read) != NULL) 17 { 18 number ++; 19 data = calloc(1, sizeof(stu_t)); 20 21 data->seg = number; 22 23 strcpy(itemp_data, strtok(buffer, " ")); 24 data->id = atoi(itemp_data); 25 26 strcpy(itemp_data, strtok(NULL, " ")); 27 strcpy(data->name, itemp_data); 28 29 strcpy(itemp_data, strtok(NULL, " ")); 30 data->chinese = atof(itemp_data); 31 32 strcpy(itemp_data, strtok(NULL, " ")); 33 data->math = atof(itemp_data); 34 35 strcpy(itemp_data, strtok(NULL, " ")); 36 data->ave = atof(itemp_data); 37 38 itemp_head->next = data; 39 data->next = NULL; 40 itemp_head = data; 41 } 42 43 printf("---------------------------------Write_list_data------------------------------ "); 44 printf("Totai student_data is %d ", number); 45 return 0; 46 } 47 48 int write_file(stu_t *write_head) 49 { 50 FILE *fp_write; 51 stu_t *itemp_head, *delete; 52 53 if(NULL == (fp_write = fopen("student_data.txt", "w"))) { 54 fprintf(stderr, "Error fo fopen %s : %d : %s. ", __FILE__, __LINE__, strerror(errno)); 55 return -1; 56 } 57 58 itemp_head = write_head; 59 for( ; itemp_head->next; ) 60 { 61 delete = itemp_head->next; 62 fprintf(fp_write, "%d %s %.1f %.1f %.1f ", itemp_head->next->id, itemp_head->next->name, itemp_head->next->chinese, itemp_head->next->math, itemp_head->next->ave); 63 itemp_head->next = itemp_head->next->next; 64 free(delete); 65 } 66 67 return 0; 68 }
4.链表操作(使用的单链表)
1 //list.c 2 #include "head.h" 3 4 void show_student_data(stu_t *show_head) 5 { 6 stu_t *itemp_head; 7 8 if(NULL == show_head->next) { 9 printf("------------------------------------------------------------------------------ "); 10 printf("The list is no data to show! "); 11 printf("------------------------------------------------------------------------------ "); 12 return ; 13 } 14 15 printf(" --------------------------Show data----------------------------------------- "); 16 printf("seg ID NAME CHINESE MATH AVERAGE "); 17 itemp_head = show_head; 18 for(; itemp_head->next; itemp_head = itemp_head->next) 19 { 20 printf("%d: %d %s %.1f %.1f %.1f ", itemp_head->next->seg, itemp_head->next->id, itemp_head->next->name, itemp_head->next->chinese, itemp_head->next->math, itemp_head->next->ave); 21 } 22 printf(" --------------------------Show data----------------------------------------- "); 23 24 return ; 25 } 26 27 void sort_ave_data(stu_t *sort_head) 28 { 29 stu_t *itemp_head , *sort_1, *sort_2; 30 31 int change_id = 0; 32 char change_name[MAX_NAME]; 33 float change_number = 0.0; 34 35 itemp_head = sort_head; 36 if(NULL == itemp_head->next) { 37 printf("------------------------------------------------------------------------------ "); 38 printf("The list no data to sort! "); 39 printf("------------------------------------------------------------------------------ "); 40 return ; 41 } 42 43 for(sort_1 = itemp_head->next; sort_1; sort_1 = sort_1->next) 44 { 45 for(sort_2 = sort_1->next; sort_2; sort_2 = sort_2->next) 46 { 47 if(sort_1->ave > sort_2->ave) { 48 49 change_id = sort_1->id; 50 sort_1->id = sort_2->id; 51 sort_2->id = change_id; 52 53 change_number = sort_1->chinese; 54 sort_1->chinese = sort_2->chinese; 55 sort_2->chinese = change_number; 56 57 change_number = sort_1->math; 58 sort_1->math = sort_2->math; 59 sort_2->math = change_number; 60 61 change_number = sort_1->ave; 62 sort_1->ave = sort_2->ave; 63 sort_2->ave = change_number; 64 65 strcpy(change_name, sort_1->name); 66 strcpy(sort_1->name, sort_2->name); 67 strcpy(sort_2->name, change_name); 68 69 } 70 } 71 } 72 73 return ; 74 } 75 76 void add_student_data(stu_t *add_head, int add_number) 77 { 78 stu_t *itemp_head, *data, *end, *find, *find_1, *end_1; 79 char buffer[MAX_DATA]; 80 81 int i = 0, number = 0; 82 83 find_1 = add_head; 84 if(find_1->next != NULL) { 85 for( ; ; ) 86 { 87 if(NULL == find_1->next) { 88 end_1 = find_1; 89 break; 90 } 91 find_1 = find_1->next; 92 } 93 number = end_1->seg; 94 } 95 96 printf("------------------------------------------------------------------------------ "); 97 printf("ID should more than %d. ", number); 98 printf("------------------------------------------------------------------------------ "); 99 100 number = 0; 101 102 itemp_head = add_head; 103 for(i = 0; i != add_number; i ++) 104 { 105 data = calloc(1, sizeof(stu_t)); 106 107 printf("Enter id : "); 108 fgets(buffer, MAX_DATA, stdin); 109 if(' ' == *(buffer + strlen(buffer) - 1)) { 110 *(buffer + strlen(buffer) - 1) = '