头文件
1 #ifndef __SIMS__ 2 #define __SIMS__ 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<ctype.h> 7 #include<conio.h> 8 #include<Windows.h> 9 #define PWD_LEN 10 10 #define NAME_LEN 20 11 #define GENDER_LEN 5 12 #define LINE_LEN 512 13 #define CONFIRM_CNT 3 14 #define M1 "1.search stu" 15 #define M2 "2.update stu" 16 #define M3 "3.delete stu" 17 #define M4 "4.add stu" 18 #define M5 "5.search acc" 19 #define M6 "6.update acc" 20 #define M7 "7.delete acc" 21 #define M8 "8.add acc" 22 #define M9 "9.exit" 23 24 25 struct stu_accout 26 { 27 char passwd[PWD_LEN]; 28 char name[NAME_LEN]; 29 int role ; 30 stu_accout* next ; 31 }; 32 33 struct student //定义一个结构体作为全局变量,用于存储学生信息,和作为结点 34 { 35 long int num; //学号 36 char name[NAME_LEN]; //姓名 37 char gender [GENDER_LEN]; 38 int age; //年龄 39 float math; //数学 40 float phy; 41 float eng; 42 float cpu; 43 float sum; 44 student *next; //下一个结点地址 45 }; 46 47 48 //登入函数 49 int login(int,int&,stu_accout*); 50 51 52 //主目录函数 53 int menu(int role); 54 55 //3.查询信息菜单 56 student *menu1(student*); 57 58 59 //7.排序信息菜单 60 student *menu7(student*); 61 62 63 //交换两个结点信息的函数,用于排序函数调用 64 void swap(student *, student *); 65 66 67 //输出信息的函数 68 void print(student *); 69 void Account_print(stu_accout *head); 70 71 //添加学生信息 72 student *add(student *, student *); 73 74 //读取学生文件 75 student *Student_File_Load(student* head); 76 student *Other_Student_File_Load( student *head); 77 78 //读取账户文件 79 stu_accout *Acount_Load(stu_accout* head); //定义函数 80 81 int Save_Stu(student *p1); 82 int Other_Save_Stu(student *p1); 83 int Save_Account(student *p1); 84 85 //查询模块 86 //1.按学号查询 87 student *search1(student *head); 88 89 //2.按姓名查询 90 student *search2(student *head); 91 92 student *search_By_Gender(student *head); 93 94 //3.按年龄查询 95 student *search3(student *head); 96 97 //4.按数学成绩查询 98 student *search4(student *head); 99 100 //5.按物理成绩查询 101 student *search5(student *head); 102 103 //6.按英语成绩查询 104 student *search6(student *head); 105 106 //7.按计算机成绩查询 107 student *search7(student *head); 108 109 //8.按总分成绩查询 110 student *search8(student *head); 111 112 113 //删除模块 114 student *del(student *head); 115 116 //修改 117 student *cha(student *head); 118 stu_accout *Password_cha(stu_accout *head); 119 120 //排序模块 121 //1.按学号排序 122 student *sort1(student *head); 123 124 //2.按年龄排序 125 student *sort2(student *head); 126 127 //3.按数学成绩排序 128 student *sort3(student *head); 129 130 //4.按物理成绩排序 131 student *sort4(student *head); 132 133 //5.按英语成绩排序 134 student *sort5(student *head); 135 136 //6.按计算机成绩排序 137 student *sort6(student *head); 138 139 //7.按总分成绩排序 140 student *sort7(student *head); 141 142 #endif
实现文件
1 #include "My_header.h" 2 3 int n; //定义一个整形变量计数学生人数 4 int x; //定义一个整形变量计数账户人数 5 char UserName[NAME_LEN]; 6 7 8 //***************************************************读取文件模块 9 10 //读取学生文件 11 student *Student_File_Load(student* head) //定义函数 12 { 13 student *p1, *p2 ; 14 int m=0; 15 FILE *fp; 16 17 if((fp=fopen("F:\Student.txt", "rb"))==NULL) //地址为空,则输出提示 18 { 19 printf("不能打开文件"); 20 return 0; 21 } 22 head=NULL; 23 do 24 { 25 p1=(student *)malloc(sizeof(student));//每读取一次创建一个新的结点 26 fscanf(fp, "%ld %s %s %d %f %f %f %f %f ", &p1->num, p1->name,p1->gender, &p1->age, &p1->math, &p1->phy, &p1->eng, &p1->cpu, &p1->sum); //读入内存 27 m=m+1; 28 if(m==1) 29 head=p2=p1; //判断结点个数以添加首地址 30 else 31 { 32 p2->next=p1; 33 p2=p1; 34 } 35 }while(!feof(fp)); 36 37 p2->next=NULL; 38 fclose(fp); 39 n=m; 40 return(head); //返回读取信息的首地址供其他函数使用 41 } 42 43 //另外读取学生文件 44 student *Other_Student_File_Load( student *head) //定义函数 45 { 46 struct student *p1, *p2; 47 int m=0; 48 char fileopen[10]; 49 FILE *fp; 50 printf("请输入文件路径及文件名:"); //输入读取文件的路径和文件名 51 scanf("%s", fileopen); 52 if((fp=fopen(fileopen, "rb"))==NULL) //地址为空,则输出提示 53 { 54 printf("不能打开文件"); 55 return 0; 56 } 57 head=NULL; 58 do 59 { 60 p1=(struct student *)malloc(sizeof(struct student));//每读取一次创建一个新的结点 61 fscanf(fp, "%ld %s %s %d %f %f %f %f %f ", &p1->num, p1->name,p1->gender, &p1->age, &p1->math, &p1->phy, &p1->eng, &p1->cpu, &p1->sum); //读入内存 62 m=m+1; 63 if(m==1) 64 head=p2=p1; //判断结点个数以添加首地址 65 else 66 { 67 p2->next=p1; 68 p2=p1; 69 } 70 }while(!feof(fp)); 71 p2->next=NULL; 72 fclose(fp); 73 n=m; 74 return(head); //返回读取信息的首地址供其他函数使用 75 } 76 77 //读取账户文件 78 stu_accout *Acount_Load(stu_accout* head) //定义函数 79 { 80 stu_accout *p1, *p2 ; 81 int m=0; 82 FILE *fp; 83 84 if((fp=fopen("F:\Account.txt", "rb"))==NULL) //地址为空,则输出提示 85 { 86 printf("不能打开文件"); 87 return 0; 88 } 89 head=NULL; 90 do 91 { 92 p1=(stu_accout *)malloc(sizeof(stu_accout));//每读取一次创建一个新的结点 93 fscanf(fp, "%s %s %d ", p1->name, p1->passwd,&p1->role); //读入内存 94 m=m+1; 95 if(m==1) 96 head=p2=p1; //判断结点个数以添加首地址 97 else 98 { 99 p2->next=p1; 100 p2=p1; 101 } 102 }while(!feof(fp)); 103 x=m; 104 p2->next=NULL; 105 fclose(fp); 106 return(head); //返回读取信息的首地址供其他函数使用 107 } 108 109 110 111 //******************************************************读取文件模块 112 113 114 115 116 //******************************************************保存文件模块 117 //存 学生 118 int Save_Stu(student *p1) //定义保存函数 119 { 120 FILE *fp; //定义fp指针 121 if((fp=fopen("F:\Student.txt", "wb"))==NULL) 122 { 123 printf("不能打开文件 "); 124 return 0; 125 } 126 while(p1!=NULL) 127 { 128 fprintf(fp, "%ld %s %s %d %f %f %f %f %f ",p1->num, p1->name,p1->gender, p1->age, p1->math, p1->phy, p1->eng, p1->cpu, p1->sum);//把内存中的文件输入到硬盘中 129 p1=p1->next; 130 } 131 fclose(fp); //关闭文件 132 printf("文件已保存 "); //成功保存,显示提示 133 return 1; 134 } 135 136 137 //另存为 学生 138 int Other_Save_Stu(student *p1) //定义保存函数 139 { 140 FILE *fp; //定义fp指针 141 char fileopen[20]; 142 printf("请输入保存的文件路径及文件名:"); //输入文件名和路径 143 scanf("%s", fileopen); 144 if((fp=fopen(fileopen, "wb"))==NULL) 145 { 146 printf("不能打开文件 "); 147 return 0; 148 } 149 while(p1!=NULL) 150 { 151 fprintf(fp, "%ld %s %s %d %f %f %f %f %f ",p1->num, p1->name,p1->gender, p1->age, p1->math, p1->phy, p1->eng, p1->cpu, p1->sum);//把内存中的文件输入到硬盘中 152 p1=p1->next; 153 } 154 fclose(fp); //关闭文件 155 printf("文件已保存 "); //成功保存,显示提示 156 return 1; 157 } 158 159 160 //保存账户 161 int Save_Account(stu_accout *p1) //定义保存函数 162 { 163 FILE *fp; //定义fp指针 164 if((fp=fopen("F:\Account.txt", "wb"))==NULL) 165 { 166 printf("不能打开文件 "); 167 return 0; 168 } 169 while(p1!=NULL) 170 { 171 fprintf(fp, "%s %s %d ",p1->name, p1->passwd, p1->role);//把内存中的文件输入到硬盘中 172 p1=p1->next; 173 } 174 fclose(fp); //关闭文件 175 printf("文件已保存 "); //成功保存,显示提示 176 return 1; 177 } 178 179 180 181 //******************************************************保存文件模块 182 183 184 185 186 187 188 189 190 //***************************************************登入模块 191 //登入函数 192 int login(int Login_Change,int& type,stu_accout* Account) 193 { 194 195 system("cls"); 196 197 stu_accout *p=Account; 198 printf(" "); 199 printf("请输入用户名:"); 200 201 scanf("%s",UserName); 202 203 while(p!=NULL) 204 { 205 if(strcmp(p->name,UserName)==0) 206 break; 207 p=p->next; 208 } 209 210 211 if(p==NULL) 212 { 213 printf("无该用户名!! "); 214 printf("您还有%d次机会 ",Login_Change); 215 getchar(); 216 getchar(); 217 return 0; 218 } 219 else 220 { 221 printf("请输入密码:"); 222 char c=0; 223 int i=0; 224 char password[PWD_LEN]; 225 while(i<PWD_LEN-1&&c!=' ') 226 { c=getch(); 227 228 if(c!=' ') 229 { 230 putchar('*'); 231 password[i]=c; 232 ++i; 233 } 234 } 235 if(i==PWD_LEN-1&&c!=' ') 236 { 237 printf(" 密码错误!! "); 238 printf("您还有%d次机会 ",Login_Change); 239 getchar(); 240 getchar(); 241 return 0; 242 } 243 password[i]='