C语言基础实现简易学生管理系统(无链表)
文件存储,无链表,使用100数组实现简易学生管理系统
main.c
1 #include <stdio.h> 2 #include "menu.h" 3 #include "student.h" 4 Student_t stu[100]; 5 int Stu_size = 0; 6 int main() 7 { 8 read_student(stu); 9 Studetn_Print_all(stu); 10 Menu_1(); 11 12 return 0; 13 }
menu.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include "menu.h" 5 #include "student.h" 6 extern int Stu_size; 7 extern Student_t stu[]; 8 void Menu_1() 9 { 10 int num = 0; 11 12 system("clear"); 13 printf("****************************\n"); 14 printf("****** 1.管理登录 *********\n"); 15 printf("****** 2.学生登录 *********\n"); 16 printf("****************************\n"); 17 printf("请输入选项:"); 18 scanf("%d", &num); 19 switch (num) 20 { 21 case 1: 22 //管理员登录界面 23 Menu_Admin(); 24 break; 25 case 2: 26 //学生登录界面 27 Menu_Student(); 28 break; 29 default: 30 printf("输入有误,程序即将结束\n"); 31 } 32 } 33 34 void Menu_Admin() 35 { 36 system("clear"); 37 char user[20] = ""; 38 char pass[20] = ""; 39 printf("请输入管理员账号:"); 40 scanf("%s", user); 41 printf("请输入管理员密码:"); 42 scanf("%s", pass); 43 44 if (strcmp(user, "admin") == 0 && strcmp(pass, "admin") == 0) 45 { 46 system("clear"); 47 printf("欢迎管理员大大\n"); 48 printf("输入任意键继续\n"); 49 getchar(); 50 getchar(); 51 Menu_Admin_Menu(); 52 } 53 } 54 55 void Menu_Student() 56 { 57 system("clear"); 58 char user[20] = ""; 59 char pass[20] = ""; 60 printf("请输入学生账号:"); 61 scanf("%s", user); 62 printf("请输入学生密码:"); 63 scanf("%s", pass); 64 65 if (strcmp(user, "admin") == 0 && strcmp(pass, "admin") == 0) 66 { 67 system("clear"); 68 printf("欢迎学生某某某\n"); 69 } 70 } 71 72 void Menu_Admin_Menu() 73 { 74 int num = 0; 75 while (1) 76 { 77 system("clear"); 78 printf("****************************\n"); 79 printf("****** 1.添加学生 *********\n"); 80 printf("****** 2.修改学生 *********\n"); 81 printf("****** 3.排序学生 *********\n"); 82 printf("****** 4.显示全部 *********\n"); 83 printf("****** 0.显示全部 *********\n"); 84 printf("****************************\n"); 85 printf("请输入选项:"); 86 scanf("%d", &num); 87 if (num == 0) 88 { 89 break; 90 } 91 switch (num) 92 { 93 case 1: //1.添加学生 94 add_Student(); 95 break; 96 case 2: //2.修改学生 97 98 break; 99 case 3: //排序学生 100 101 break; 102 case 4: //4.显示全部 103 Studetn_Print_all(stu); 104 break; 105 default: 106 printf("输入有误,程序即将结束\n"); 107 getchar();getchar(); 108 } 109 getchar();getchar(); 110 } 111 system("clear"); 112 printf("欢迎下次光临\n"); 113 getchar();getchar(); 114 } 115 116 void add_Student() 117 { 118 Student_t stu_temp; 119 memset(&stu_temp,0,sizeof(Student_t)); 120 //账号操作 121 sprintf(stu_temp.Iduser,"%d",1000+Stu_size); 122 printf("请输入密码:"); 123 scanf("%s",stu_temp.Idpass); 124 printf("请输入姓名:"); 125 scanf("%s",stu_temp.name); 126 printf("请输入年龄:"); 127 scanf("%d",&stu_temp.age); 128 printf("请输入身高:"); 129 scanf("%d",&stu_temp.height); 130 printf("请输入语文:"); 131 scanf("%d",&stu_temp.language); 132 printf("请输入数学:"); 133 scanf("%d",&stu_temp.math); 134 printf("请输入英语:"); 135 scanf("%d",&stu_temp.english); 136 getchar(); 137 printf("是否写入(y/n):"); 138 char ch; 139 scanf("%c",&ch); 140 if(ch == 'y') 141 { 142 write_student(&stu_temp); 143 stu[Stu_size++] = stu_temp; 144 } 145 }
menu.h
1 #ifndef MENU_H 2 #define MENU_H 3 4 void Menu_1(); 5 void Menu_Admin(); 6 void Menu_Admin_Menu(); 7 void Menu_Student(); 8 void add_Student(); //添加学生 9 10 #endif
student.c
1 #include "student.h" 2 #include <stdio.h> 3 extern int Stu_size; 4 void read_student(Student_t *stu) 5 { 6 FILE *fp = NULL; 7 fp = fopen(Filename,"r"); 8 if(fp == NULL) 9 { 10 return ; 11 } 12 13 while( fread(&stu[Stu_size],sizeof(Student_t),1,fp) > 0 ) 14 { 15 Stu_size++; 16 ; 17 } 18 fclose(fp); 19 } 20 21 void write_student(Student_t *stu) 22 { 23 FILE *fp = NULL; 24 fp = fopen(Filename,"a"); 25 fwrite(stu,sizeof(Student_t),1,fp); 26 fclose(fp); 27 } 28 29 void Studetn_Print(Student_t *stu) 30 { 31 printf("%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n",stu->Iduser,stu->Idpass,stu->name,stu->age,stu->height,stu->language,stu->math,stu->english); 32 } 33 34 void Studetn_Print_all(Student_t *stu) 35 { 36 printf("账号\t密码\t姓名\t年龄\t身高\t语文\t数学\t英语\n"); 37 for(int i = 0;i < Stu_size;i++) 38 { 39 Studetn_Print(&stu[i]); 40 } 41 }
student.h
1 #ifndef STUDENT_H 2 #define STUDENT_H 3 4 #define Filename "student_data.txt" 5 6 struct Student 7 { 8 char Iduser[20]; 9 char Idpass[20]; 10 char name[20]; 11 int age; 12 int height; 13 int language; 14 int math; 15 int english; 16 }; 17 18 typedef struct Student Student_t; 19 void read_student(Student_t *stu); 20 void write_student(Student_t *stu); 21 void Studetn_Print(Student_t *stu); 22 void Studetn_Print_all(Student_t *stu); 26 #endif