在编译器上编写一个可以对文件进行读取和存储功能的一系列代码函数;代码要求对学生的成绩信息进行管理,可以实现登录,注册,密码修改,密码找回,以及教师端,学生端,管理员端的功能操作,主要包括对链表的增删查改四大基础功能,和文件的应用。
typedef struct a {
char nameword[20]; //账号
char password[20]; //密码
int status; //身份,1.学生 2,教师 3,管理员
} users;
users s[2] = {
{"hhh", "hhh", 3},
{"jjj", "jjj", 3}
};//管理员信息
typedef struct c {
char nameword[20];
char password[20];
int status;
struct c* next;
} userNode;
typedef struct b {
char name[20]; //学生姓名
char st_number[20]; //学号
char st_class [20]; //班级
int grade_1; //第一门成绩
int grade_2; //第二门成绩
int grade_3; //第三门成绩
struct b* next;
} st_Node;
char nameword_1[20];
char password_1[20];
char password_2[20];
int cnt = 0;//账号密码输错的次数
int m;//身份
userNode* head;
void ID (); //系统主菜单
void login ();//登录界面
void logon ();//注册界面
void revise ();//修改密码界面
void retrieve ();//密码找回界面
void ad_login ();//管理员登录界面
void se_login ();//教师或学生登陆界面
void ad_menu ();//管理员菜单
void st_menu (char* num);//学生菜单
void te_menu ();//教师菜单
void look_list ();//查看代办选项
void add_name ();//增加账号和密码
void delete_name();//删除账号和密码
void change_name ();//修改账号和密码
void search_name ();//查找账号和密码
void entry_name ();//录入所有账号和密码
void print_name ();//输出所有账号和密码
void reentry_name ();//手动重新输入账号和密码
void add_te ();
void delete_te ();
void change_te ();
void search_te ();
void print_te ();
void analysis_te ();
void remind_te ();
void appeal_st (char* num);
void search_st (char* num);
void print_st (char* num);
void analysis_st (char* num);
userNode* read_name ();
void write_name (userNode* head);
void free_name (userNode* head);
st_Node* read_st1 ();
st_Node* read_st2 ();
st_Node* read_st3 ();
st_Node* read_st4 ();
st_Node* read_st5 ();
void write_st1 (st_Node* head);
void write_st2 (st_Node* head);
void write_st3 (st_Node* head);
void write_st4 (st_Node* head);
void write_st5 (st_Node* head);
void free_st (st_Node* head);
此处可以实现四种功能,登录,注册,密码修改,密码找回;但要注意的是,为了防止系统崩溃,我们在选择时不能使用int型变量,而应该使用char型的字符串作为输入。同时为了明确系统的层次,我们可以报证函数的进出都在系统中,如下
void ID () {
system ("cls");
printf("注:身份只能是数字\n");
printf("\t\t\t\t\t1.登录\n");
printf("\t\t\t\t\t2.注册\n");
printf("\t\t\t\t\t3.密码修改\n");
printf("\t\t\t\t\t4.密码找回\n");
printf("\t\t\t\t\t5.退出系统\n");
printf("\t\t\t\t\t请选择\n");
char n[20];
scanf("%s",n);
if (n[0] == '1' && n[1] == '\0' ) {
login ();
} else if (n[0] == '2' && n [1] == '\0') {
logon ();
} else if (n[0] == '3' && n [1] == '\0') {
revise ();
} else if (n[0] == '4' && n [1] == '\0') {
retrieve ();
} else if (n [0] == '5' && n [1] == '\0') {
exit (0);
} else {
ID ();
}
}
上面的函数报证了系统只会在这层循环,或者进入下一层;
分为管理员登录和教师学生登录,方便区分进入不同的端
具体可以看一下源代码
void login () { //登录
system ("cls");
printf("\t\t\t\t\t1.管理员登录\n");
printf("\t\t\t\t\t2.教师和学生登录\n");
printf("\t\t\t\t\t3.返回上一级\n");
printf("\t\t\t\t\t4.退出系统\n");
printf("\t\t\t\t\t请选择\n");
char n[20];
scanf("%s",n);
if (n[0] == '1' && n [1] == '\0') {
ad_login ();
} else if (n[0] == '2' && n [1] == '\0') {
se_login ();
} else if (n[0] == '3' && n [1] == '\0') {
ID ();
} else if (n[0] == '4' && n [1] == '\0') {
exit (0);
} else {
printf("没有该选项\n");
system ("pause");
login ();
}
login ();
}
void ad_login () { //管理员登录
system ("cls");
printf("\t\t\t\t\t账号:");
scanf("%s",nameword_1);
printf("\t\t\t\t\t密码:");
int i;
//scanf("%s",password_1);
for (i = 0; i < 20; i++) {
password_1 [i] = getch ();
if (password_1 [i] == '\r') {
break;
} else if (password_1 [i] == '\b' && i > 0) {
password_1 [--i] = '\0';
i--;
} else if (password_1 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_1 [i] = '\0';
printf("\n");
for (i =0; i < 2; i++) {
if ((strcmp (nameword_1, s[i].nameword) == 0) && (strcmp (password_1,s[i].password) == 0)) {
printf("登录成功\n");
cnt =0;
system ("pause");
ad_menu ();
break;
}
}
if (i == 2) {
printf("账号或密码输入错误,请重新输入/n");
cnt++;
if (cnt == 3) {
printf("你已输错三次,即将返回上一层/n");
cnt =0;
system ("pause");
login ();
}
ad_login ();
}
}
void se_login () { //学生或教师登录
system ("cls");
printf("\t\t\t\t\t账号:");
scanf("%s",nameword_1);
printf("\t\t\t\t\t密码:");
int i;
for (i = 0; i < 20; i++) {
password_1 [i] = getch ();
if (password_1 [i] == '\r') {
break;
} else if (password_1 [i] == '\b' && i > 0) {
password_1 [--i] = '\0';
i--;
} else if (password_1 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_1 [i] = '\0';
printf("\n");
users t;
int flag = 0;
FILE* fp;
fp = fopen ("账号密码内容.txt", "r");
while (!feof (fp)) {
fscanf (fp, "%s %s %d\n", t.nameword, t.password, &t.status);
if ((strcmp (nameword_1, t.nameword) == 0) && (strcmp (password_1, t.password) == 0)) {
flag = 1;
break;
}
}
fclose (fp);
if (flag == 1) {
printf("登陆成功\n");
system ("pause");
if (t.status == 1) {
char num [20];
strcpy (num, nameword_1);
st_menu (num);
} else {
te_menu ();
}
} else {
printf("账号或密码错误,请重新输入\n");
cnt++;
if (cnt == 3) {
printf("输入错误次数过多,返回上一级/n");
system ("pause");
cnt = 0;
login ();
}
system ("pause");
se_login ();
}
}
在密码输入时我们可以用getch()函数来进行隐式输入,同时设置输入错误返回上一层的计数器,可以使用全局变量,减少讨论。
同时由于学生账号是自生的学号,我们要打开学生信息文件进行查找;
同时登录也是一种对账号密码的查找对比,我们也要对文件进行操作。
void logon () { //注册
system ("cls");
printf("如果你是学生输入1 教师输入2\n");
int n;
scanf("%d",&n);
if (n!= 1 && n!= 2) {
printf("没有该身份\n");
system ("pause");
logon ();
}
if (n == 2) {
printf("\t\t\t\t\t输入新账号:");
scanf("%s",nameword_1);
userNode* head_1 = read_name ();
userNode* x ;
int flag_1 = 0;
for (x = head_1->next; x; x = x->next) {
if (strcmp (nameword_1, x->nameword) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该账号密码已存在\n");
system ("pause");
ID ();
}
printf("\t\t\t\t\t输入密码: ");
int i;
for (i = 0; i < 20; i++) {
password_1 [i] = getch ();
if (password_1 [i] == '\r') {
break;
} else if (password_1 [i] == '\b' && i > 0) {
password_1 [--i] = '\0';
i--;
} else if (password_1 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_1 [i] = '\0';
printf("\n");
printf("\t\t\t\t\t再次输入密码:");
for (i = 0; i < 20; i++) {
password_2 [i] = getch ();
if (password_2 [i] == '\r') {
break;
} else if (password_2 [i] == '\b' && i > 0) {
password_2 [--i] = '\0';
i--;
} else if (password_2 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_2 [i] = '\0';
printf("\n");
if (strcmp (password_1,password_2) == 0) {
users t;
strcpy (t.nameword, nameword_1);
strcpy (t.password, password_1);
t.status = n;
FILE* fp;
fp = fopen ("账号密码内容.txt", "ab");
fprintf(fp, "%s %s %d\n", t.nameword, t.password, t.status);
printf("注册成功!\n");
system ("pause");
fclose (fp);
fp = fopen ("密码注册.txt", "a");
fprintf(fp, "有人注册了一个账号\n" );
fclose (fp);
ID ();
} else {
printf("两次密码不相同,请重新输入账号密码\n");
system ("pause");
ID ();
}
} else if (n == 1) {
printf("请输入你的班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
logon ();
}
printf("\t\t\t\t\t输入新账号:");
scanf("%s",nameword_1);
userNode* head_1 = read_name ();
userNode* x ;
int flag_1 = 0;
for (x = head_1->next; x; x = x->next) {
if (strcmp (nameword_1, x->nameword) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该账号密码已存在\n");
system ("pause");
ID ();
}
st_Node* p;
int flag = 0;
for ( p = head->next; p ;p = p->next) {
if (strcmp (nameword_1, p->st_number) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("\t\t\t\t\t输入密码: ");
int i;
for (i = 0; i < 20; i++) {
password_1 [i] = getch ();
if (password_1 [i] == '\r') {
break;
} else if (password_1 [i] == '\b' && i > 0) {
password_1 [--i] = '\0';
i--;
} else if (password_1 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_1 [i] = '\0';
printf("\n");
printf("\t\t\t\t\t再次输入密码:");
for (i = 0; i < 20; i++) {
password_2 [i] = getch ();
if (password_2 [i] == '\r') {
break;
} else if (password_2 [i] == '\b' && i > 0) {
password_2 [--i] = '\0';
i--;
} else if (password_2 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_2 [i] = '\0';
printf("\n");
if (strcmp (password_1,password_2) == 0) {
users t;
strcpy (t.nameword, nameword_1);
strcpy (t.password, password_1);
t.status = n;
FILE* fp;
fp = fopen ("账号密码内容.txt", "ab");
fprintf(fp, "%s %s %d\n", t.nameword, t.password, t.status);
printf("注册成功!\n");
system ("pause");
fclose (fp);
fp = fopen ("密码注册.txt", "a");
fprintf(fp, "有人注册了一个账号\n" );
fclose (fp);
ID ();
} else {
printf("两次密码不相同,请重新输入账号密码\n");
system ("pause");
ID ();
}
} else {
printf("不存在该学生\n");
system ("pause");
ID ();
}
} else {
printf("没有该选项\n");
system ("pause");
ID ();
}
}
此处的注册其实就是对账号密码信息的写入,但学生注册时要求账号为它的学号,所以会有一个和登录相像的查找过程。
同理,和上面两部一样,大致都是文件打开,读取信息后,组成链表,修改后再写入,在以链表为操作方式的系统中,这是通用的
void retrieve () { //密码找回
system ("cls");
printf("\t\t\t\t\t输入要找回的账号: ");
scanf("%s",nameword_1);
FILE* fp;
fp = fopen ("账号密码内容.txt", "rb");
int flag = 0;
users t;
while (!feof (fp)) {
fscanf(fp, "%s %s %d\n", t.nameword, t.password, &t.status);
if (strcmp (nameword_1, t.nameword) == 0) {
flag = 1;
break;
}
}
fclose (fp);
if (flag == 1) {
printf("密码为:%s",t.password);
system ("pause");
fp = fopen ("密码找回.txt", "a");
fprintf(fp, "有人找回了一个账号\n" );
fclose (fp);
ID ();
} else {
printf("账号不存在\n");
system ("pause");
ID ();
}
}
void revise () { //密码修改
system ("cls");
userNode* head = (userNode*) malloc (sizeof (userNode));
head->next = NULL;
userNode* p = head;
FILE* fp;
fp = fopen ("账号密码内容.txt", "r");
while (!feof (fp)) {
userNode* s = (userNode*) malloc (sizeof (userNode));
fscanf(fp, "%s %s %d\n", s->nameword,s->password,&s->status);
s->next = NULL;
p->next = s;
p = s;
}
fclose (fp);
printf("\t\t\t\t\t请输入你的账号: ");
scanf("%s",nameword_1);
printf("\t\t\t\t\t请输入原密码: ");
int i;
for (i = 0; i < 20; i++) {
password_1 [i] = getch ();
if (password_1 [i] == '\r') {
break;
} else if (password_1 [i] == '\b' && i > 0) {
password_1 [--i] = '\0';
i--;
} else if (password_1 [i] == '\b' && i == 0) {
i = -1;
} else {
printf("*");
}
}
password_1 [i] = '\0';
printf("\n");
int flag = 0;
for (p = head->next; p; p = p->next) {
if ((strcmp (nameword_1, p->nameword) == 0) && (strcmp (password_1, p->password) == 0)) {
flag = 1;
break;
}
}
if (flag == 1) {
fp = fopen ("账号密码内容.txt", "w");
printf("\t\t\t\t\t请输入新密码: ");
scanf("%s",p->password);
for (p = head->next; p; p = p->next) {
fprintf(fp, "%s %s %d\n", p->nameword, p->password, p->status);
}
printf("\t\t\t\t\t密码修改成功!");
system ("pause");
cnt = 0;
fclose (fp);
fp = fopen ("密码修改.txt", "a");
fprintf(fp, "有人修改了一个账号\n" );
fclose (fp);
ID ();
} else {
printf("\t\t\t\t\t账号或密码错误请重输\n");
system ("pause");
cnt++;
if (cnt == 3) {
printf("\t\t\t\t\t已输错三次,返回上一级\n");
system ("pause");
cnt = 0;
ID ();
}
revise ();
}
}
void add_te () {//增加学生信息
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system("pause");
add_te ();
}
st_Node* s = (st_Node*) malloc (sizeof (st_Node));
printf("输入学生信息\n");
scanf("%s %s %s %d %d %d",s->name,s->st_number,s->st_class,&s->grade_1,&s->grade_2,&s->grade_3);
if (m[0] != s->st_number[1] || m[0] != s->st_class[2]) {
printf("信息格式不对\n");
system ("pause");
te_menu ();
}
st_Node* x;
int flag_1 = 0;
for (x = head->next; x;x = x->next) {
if ((strcmp (x->name, s->name) == 0) ||( strcmp (x->st_number,s->st_number) == 0)) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该学生的名字或学号已存在\n");
system ("pause");
te_menu ();
}
s->next = NULL;
printf("\t\t\t\t\t1.插入尾部\n");
printf("\t\t\t\t\t2.插入任意位置\n");
char t[20];
scanf("%s",t);
if (t[0] == '1' && t[1] == '\0') {
st_Node* p = head;
while (p->next) {
p= p->next;
}
p->next = s;
printf("插入成功!\n");
system ("pause");
if (m[0] == '1' && m[1] == '\0') {
write_st1 (head);
} else if (m[0] == '2' && m[1] == '\0') {
write_st2 (head);
} else if (m[0] == '3' && m[1] == '\0') {
write_st3 (head);
} else if (m[0] == '4' && m[1] == '\0') {
write_st4 (head);
} else if (m[0] == '5' && m[1] == '\0') {
write_st5 (head);
} else {
add_te ();
}
free_st (head);
te_menu ();
} else if (t[0] == '2' && t[1] == '\0') {
int tem ;
printf("要插入的位置是\n");
scanf("%d",&tem);
st_Node* q = head;
st_Node* p = head->next;
int i;
int flag = 1;
for (i = 0; i < tem -1 ; i++) {
if (p == NULL) {
flag = 0;
break;
}
q = p;
p = p->next;
}
if (flag == 1) {
s->next = p;
q->next = s;
printf("插入成功!\n");
system ("pause");
if (m[0] == '1' && m[1] == '\0') {
write_st1 (head);
} else if (m[0] == '2' && m[1] == '\0') {
write_st2 (head);
} else if (m[0] == '3' && m[1] == '\0') {
write_st3 (head);
} else if (m[0] == '4' && m[1] == '\0') {
write_st4 (head);
} else if (m[0] == '5' && m[1] == '\0') {
write_st5 (head);
} else {
add_te ();
}
free_st (head);
te_menu ();
} else {
printf("没有该位置!\n");
system ("pause");
free_st (head);
te_menu ();
}
}
}
此处要注意的是对学生信息格式的检查判断和信息查重,可以打开文件进行查找和if判断,不符合则返回上一层
对文件打开输出
void print_te () {//打印
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
print_te ();
}
st_Node* p = head->next;
if(p == NULL) {
printf("No");
system ("pause");
}
while (p!=NULL) {
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
p = p->next;
}
system ("pause");
free_st (head);
te_menu ();
}
本质上是对链表的四大基础操作,但这里要把文件中的信息读入链表中,同时注意对学生信息的判断限制
void delete_te () {//删除的信息
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
delete_te ();
}
printf("要删除第几个学生的信息:\n");
int t;
scanf("%d",&t);
st_Node* p = head->next;
st_Node* q = head;
int i;
for (i = 0; i < t - 1; i++) {
if (p == NULL) {
printf("没有该学生信息\n");
system ("pause");
te_menu ();
}
q = p;
p = p->next;
}
q->next = p->next;
free (p);
printf("删除成功!\n");
system ("pause");
if (m[0] == '1' && m[1] == '\0') {
write_st1 (head);
} else if (m[0] == '2' && m[1] == '\0') {
write_st2 (head);
} else if (m[0] == '3' && m[1] == '\0') {
write_st3 (head);
} else if (m[0] == '4' && m[1] == '\0') {
write_st4 (head);
} else if (m[0] == '5' && m[1] == '\0') {
write_st5 (head);
} else {
delete_te ();
}
free_st(head);
te_menu ();
}
void change_te () {//修改学生信息
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
change_te ();
}
printf("要修改第几个学生的信息:\n");
int t;
scanf("%d",&t);
st_Node* p = head;
int i;
for (i = 0; i < t ; i++) {
p = p->next;
if (p == NULL) {
printf("该账号和密码不存在\n");
system ("pause");
free_st (head);
te_menu ();
}
}
printf("输入修改后的名字:");
scanf("%s",p->name);
st_Node* x;
int flag_1 = 0;
for (x = head->next; x;x = x->next) {
if (strcmp (x->name, p->name) == 0 ) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该学生的名字已存在\n");
system ("pause");
te_menu ();
}
printf("学号:");
scanf("%s",p->st_number);
if (m[0] != p->st_number[1]) {
printf("学号格式有误\n");
system ("pause");
te_menu();
}
flag_1 = 0;
for (x = head->next; x;x = x->next) {
if (strcmp (x->st_number,p->st_number) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该学生学号已存在\n");
system ("pause");
te_menu ();
}
printf("班级:");
scanf("%s",p->st_class);
if (m[0] != p->st_class[2] ) {
printf("班级格式有误\n");
system ("pause");
te_menu();
}
printf("三门成绩:");
scanf("%d %d %d",&p->grade_1, &p->grade_2, &p->grade_3);
printf("修改成功!");
system ("pause");
if (m[0] == '1' && m[1] == '\0') {
write_st1 (head);
} else if (m[0] == '2' && m[1] == '\0') {
write_st2 (head);
} else if (m[0] == '3' && m[1] == '\0') {
write_st3 (head);
} else if (m[0] == '4' && m[1] == '\0') {
write_st4 (head);
} else if (m[0] == '5' && m[1] == '\0') {
write_st5 (head);
} else {
change_te ();
}
free_st (head);
te_menu ();
}
void search_te () {//查找学生信息
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
search_te ();
}
printf("\t\t\t\t\t1.学号查找\n");
printf("\t\t\t\t\t2.姓名查找\n");
printf("\t\t\t\t\t3.姓名学号组合查找\n");
char tem[20] ;
scanf("%s",tem);
if (tem[0] == '1' && tem[1] == '\0') {
printf("输入要查找的学生学号: ");
char s[20];
scanf("%s",s);
st_Node* p = head->next;
int flag = 0;
while (p) {
if (strcmp (s, p->st_number) == 0) {
flag = 1;
break;
}
p = p->next;
}
if (flag == 1) {
printf("查找成功!\n");
printf("该学生的信息为:\n");
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
system ("pause");
free_st (head);
te_menu ();
} else {
printf("没有该学生的信息\n");
system ("pause");
free_st (head);
te_menu ();
}
} else if (tem[0] == '2' && tem[1] == '\0') {
printf("输入要查找的学生姓名: ");
char s[20];
scanf("%s",s);
st_Node* p = head->next;
int flag = 0;
while (p) {
if (strcmp (s, p->name) == 0) {
flag = 1;
break;
}
p = p->next;
}
if (flag == 1) {
printf("查找成功!\n");
printf("该学生的信息为:\n");
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
system ("pause");
free_st (head);
te_menu ();
} else {
printf("没有该学生的信息\n");
system ("pause");
free_st (head);
te_menu ();
}
} else if (tem[0] == '3' && tem[1] == '\0') {
printf("输入要查找的学生学号和姓名: ");
char s[20];
char q [20];
scanf("%s",s);
scanf("%s",q);
st_Node* p = head->next;
int flag = 0;
while (p) {
if ((strcmp (s, p->st_number) == 0) && (strcmp (q, p->name) == 0)) {
flag = 1;
break;
}
p = p->next;
}
if (flag == 1) {
printf("查找成功!\n");
printf("该学生的信息为:\n");
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
system ("pause");
free_st (head);
te_menu ();
} else {
printf("没有该学生的信息\n");
system ("pause");
free_st (head);
te_menu ();
}
} else {
printf("没有该选项\n");
system ("pause");
search_te ();
}
}
对链表中的信息进行统计处理
void analysis_te () {//分析
system ("cls");
printf("\t\t\t\t\t输入班级:\n");
printf("\t\t\t\t\t1.网络1班\n");
printf("\t\t\t\t\t2.网路2班\n");
printf("\t\t\t\t\t3.网络3班\n");
printf("\t\t\t\t\t4.网路4班\n");
printf("\t\t\t\t\t5.网络5班\n");
char m[20] ;
scanf("%s", m);
st_Node* head;
if (m[0] == '1' && m[1] == '\0') {
head = read_st1 ();
} else if (m[0] == '2' && m[1] == '\0') {
head = read_st2 ();
} else if (m[0] == '3' && m[1] == '\0') {
head = read_st3 ();
} else if (m[0] == '4' && m[1] == '\0') {
head = read_st4 ();
} else if (m[0] == '5' && m[1] == '\0') {
head = read_st5 ();
} else {
printf("没有该选项\n");
system ("pause");
analysis_te ();
}
st_Node* p = head->next;
int sum1 = 0, sum2 = 0, sum3 = 0;
int max1 = p->grade_1;
int max2 = p->grade_2;
int max3 = p->grade_3;
int min1 = p->grade_1;
int min2 = p->grade_2;
int min3 = p->grade_3;
int cnt = 0;
while (p) {
sum1 += p->grade_1;
sum2 += p->grade_2;
sum3 += p->grade_3;
if (p->grade_1 > max1) {
max1 = p->grade_1;
}
if (p->grade_2 > max2) {
max2 = p->grade_2;
}
if (p->grade_3 > max3) {
max3 = p->grade_3;
}
if (p->grade_1 < min1) {
min1 = p->grade_1;
}
if (p->grade_2 < min2) {
min2 = p->grade_2;
}
if (p->grade_3 < min3) {
min3 = p->grade_3;
}
cnt++;
p = p->next;
}
double average1 = 1.0* sum1 / cnt;
double average2 = 1.0* sum2 / cnt;
double average3 = 1.0* sum3 / cnt;
printf("三门科目平均分,最大值,最小值:\n");
printf("%f %f %f\n", average1, average2,average3);
printf("%d %d %d\n", max1, max2, max3);
printf("%d %d %d\n", min1, min2, min3);
system ("pause");
te_menu ();
}
其实就是向管理员的文件里写入一句提示语;
void remind_te () {//提醒成绩库更新
system ("cls");
FILE* fp = fopen ("更新成绩库.txt", "a");
fprintf(fp, "有人提醒您更新成绩库\n");
fclose (fp);
printf("提醒成功!\n");
system ("pause");
te_menu ();
}
绝大部分操作与教师端类似,但我将管理员的信息作为全局变量便于访问;
其次就时方问的文件对象不同
源代码如下
void ad_menu () { //管理员菜单
system ("cls");
printf("\t\t\t\t\tHello,这里是学生信息管理系统——管理员端\n");
printf("\t\t\t\t\t1.查看代办选项\n");
printf("\t\t\t\t\t2.进入教师端进行信息修改\n");
printf("\t\t\t\t\t3.增加账号及密码\n");
printf("\t\t\t\t\t4.删除账号和密码\n");
printf("\t\t\t\t\t5.修改账号及密码\n");
printf("\t\t\t\t\t6.查找账号及密码\n");
printf("\t\t\t\t\t7.录入所有账号及密码\n");
printf("\t\t\t\t\t8.输出所有账号及密码\n");
printf("\t\t\t\t\t9.返回上一层\n");
printf("\t\t\t\t\t0.退出\n");
printf("\t\t\t\t\t请选择\n");
char n[20];
scanf("%s",n);
if (n[0] == '1' && n[1] == '\0') {
look_list ();
} else if (n[0] == '2' && n[1] == '\0') {
te_menu ();
} else if (n[0] == '3' && n[1] == '\0') {
add_name ();
} else if (n[0] == '4' && n[1] == '\0') {
delete_name ();
} else if (n[0] == '5' && n[1] == '\0') {
change_name ();
} else if (n[0] == '6' && n[1] == '\0') {
search_name ();
} else if (n[0] == '7' && n[1] == '\0') {
entry_name ();
} else if (n[0] == '8' && n[1] == '\0') {
print_name ();
} else if (n[0] == '9' && n[1] == '\0') {
ID ();
} else if (n[0] == '0' && n[1] == '\0') {
exit (0);
} else {
printf("没有该选项\n");
system ("pause");
ad_menu ();
}
}
void look_list () { //查看代办选项
system ("cls");
printf("\t\t\t\t\t1.密码注册\n");
printf("\t\t\t\t\t2.密码修改\n");
printf("\t\t\t\t\t3.密码找回\n");
printf("\t\t\t\t\t4.成绩申诉\n");
printf("\t\t\t\t\t5.更新成绩库\n");
printf("\t\t\t\t\t请选择\n");
char n[20];
scanf("%s",n);
FILE* fp;
if (n[0] == '1' && n[1] == '\0') {
fp = fopen ("密码注册.txt", "r");
if (fp == NULL) {
printf("无消息/n");
system ("pause");
fclose (fp);
ad_menu ();
} else {
char s[20];
while (!feof (fp)) {
fscanf(fp, "%s\n", s);
printf("%s\n",s);
}
fclose (fp);
system ("pause");
ad_menu ();
}
} else if (n[0] == '2' && n[1] == '\0') {
fp = fopen ("密码修改.txt", "r");
if (fp == NULL) {
printf("无消息/n");
system ("pause");
fclose (fp);
ad_menu ();
} else {
char s[20];
while (!feof (fp)) {
fscanf(fp, "%s\n", s);
printf("%s\n",s);
}
fclose (fp);
system ("pause");
ad_menu ();
}
} else if (n[0] == '3' && n[1] == '\0') {
fp = fopen ("密码找回.txt", "r");
if (fp == NULL) {
printf("无消息/n");
system ("pause");
fclose (fp);
ad_menu ();
} else {
char s[20];
while (!feof (fp)) {
fscanf(fp, "%s\n", s);
printf("%s\n",s);
}
fclose (fp);
system ("pause");
ad_menu ();
}
} else if (n[0] == '4' && n[1] == '\0') {
fp = fopen ("成绩申诉.txt", "r");
if (fp == NULL) {
printf("无消息/n");
system ("pause");
fclose (fp);
ad_menu ();
} else {
char s[20];
while (!feof (fp)) {
fscanf(fp, "%s\n", s);
printf("%s\n",s);
}
fclose (fp);
system ("pause");
ad_menu ();
}
} else if (n[0] == '5' && n[1] == '\0') {
fp = fopen ("更新成绩库.txt", "r");
if (fp == NULL) {
printf("无消息/n");
system ("pause");
fclose (fp);
ad_menu ();
} else {
char s[20];
while (!feof (fp)) {
fscanf(fp, "%s\n", s);
printf("%s\n",s);
}
fclose (fp);
system ("pause");
ad_menu ();
}
} else {
look_list ();
}
}
userNode* read_name () {//读文件得链表
userNode* head = (userNode*) malloc (sizeof (userNode));
head->next = NULL;
userNode* p = head;
FILE* fp = fopen ("账号密码内容.txt", "r");
while (!feof (fp)) {
userNode* s =(userNode*) malloc (sizeof (userNode));
fscanf(fp, "%s %s %d\n",s->nameword, s->password, &s->status);
s->next = NULL;
p->next = s;
p = s;
}
fclose (fp);
return head;
}
void write_name (userNode* head) {//写链表入文件
userNode* p = head->next;
FILE* fp = fopen ("账号密码内容.txt", "w+");
while (p) {
fprintf(fp,"%s %s %d\n",p->nameword, p->password, p->status );
p = p->next;
}
fclose (fp);
}
void free_name (userNode* head) {//释放链表
userNode* p = head;
while (p) {
userNode* q = p->next;
free (p);
p = q;
}
}
void add_name () {//增加账号和密码,可以插入指定位置
system ("cls");
userNode* s = (userNode*) malloc (sizeof (userNode));
printf("增加的账号和密码:\n");
scanf("%s %s",s->nameword, s->password);
strcpy (nameword_1, s->nameword);
userNode* head_1 = read_name ();
userNode* x ;
int flag_1 = 0;
for (x = head_1->next; x; x = x->next) {
if (strcmp (nameword_1, x->nameword) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该账号密码已存在\n");
system ("pause");
ad_menu ();
}
printf("身份为:\n");
printf("\t\t\t\t\t1.学生\n");
printf("\t\t\t\t\t2.老师\n");
scanf("%d",&s->status);
if (s->status != 1 && s->status != 2) {
printf("没有该身份\n");
system ("pause");
add_name ();
}
s->next = NULL;
printf("\t\t\t\t\t1.插入尾部\n");
printf("\t\t\t\t\t2.插入任意位置\n");
char t [20];
scanf("%s",t);
if (t[0] == '1' && t[1] == '\0') {
userNode* head = read_name ();
userNode* p =head;
while (p->next) {
p= p->next;
}
p->next = s;
printf("插入成功!\n");
system ("pause");
write_name (head);
free_name (head);
ad_menu ();
} else if (t[0] == '2' && t[1] == '\0') {
userNode* head = read_name ();
int tem ;
printf("要插入的位置是\n");
scanf("%d",&tem);
userNode* q = head;
userNode* p = head->next;
int i;
int flag = 1;
for (i = 0; i < tem -1 ; i++) {
if (p == NULL) {
flag = 0;
break;
}
q = p;
p = p->next;
}
if (flag == 1) {
s->next = p;
q->next = s;
printf("插入成功!\n");
system ("pause");
write_name (head);
free_name (head);
ad_menu ();
} else {
printf("没有该位置!\n");
system ("pause");
free_name (head);
ad_menu ();
}
} else {
printf("没有该选项\n");
system ("pause");
add_name ();
}
}
void delete_name () {//删除账号和密码
system ("cls");
printf("要删除第几个账号和密码:\n");
int t;
scanf("%d",&t);
userNode* head = read_name ();
userNode* p = head->next;
userNode* q = head;
int i;
for (i = 0; i < t - 1; i++) {
if (p == NULL) {
printf("不存在该账号\n");
system ("pause");
ad_menu ();
}
q = p;
p = p->next;
}
q->next = p->next;
free (p);
printf("删除成功!\n");
system ("pause");
write_name (head);
free_name(head);
ad_menu ();
}
void change_name () {//修改账号和密码
system ("cls");
printf("要修改第几个账号和密码:\n");
int t;
scanf("%d",&t);
userNode* head = read_name ();
userNode* p = head;
int i;
for (i = 0; i < t ; i++) {
p = p->next;
if (p == NULL) {
printf("该账号和密码不存在\n");
system ("pause");
free_name (head);
ad_menu ();
}
}
printf("输入修改后的账号:");
scanf("%s",p->nameword);
strcpy (nameword_1, p->nameword);
userNode* head_1 = read_name ();
userNode* x ;
int flag_1 = 0;
for (x = head_1->next; x; x = x->next) {
if (strcmp (nameword_1, x->nameword) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该账号密码已存在\n");
system ("pause");
ad_menu ();
}
printf("密码:");
scanf("%s",p->password);
printf("身份:");
scanf("%d",&p->status);
printf("修改成功!");
system ("pause");
write_name (head);
free_name (head);
ad_menu ();
}
void search_name () {//查找账号密码
system ("cls");
printf("输入要查找的账号: ");
scanf("%s",nameword_1);
userNode* head = read_name ();
userNode* p = head->next;
int flag = 0;
while (p) {
if (strcmp (nameword_1, p->nameword) == 0) {
flag = 1;
break;
}
p = p->next;
}
if (flag == 1) {
printf("查找成功!\n");
printf("其密码为%s\n",p->password);
system ("pause");
free_name (head);
ad_menu ();
} else {
printf("没有该账号和密码\n");
system ("pause");
free_name (head);
ad_menu ();
}
}
void print_name () {//打印
system ("cls");
userNode* head = read_name ();
userNode* p = head->next;
if(p == NULL) {
printf("No");
}
while (p) {
printf("%s %s %d\n",p->nameword, p->password,p->status);
p = p->next;
}
free_name (head);
system ("pause");
ad_menu ();
}
void entry_name () {//录入账号密码
while (1) {
system ("cls");
char i[20];
printf("\t\t\t\t\t1.继续执行录入\n");
printf("\t\t\t\t\t2.结束录入\n");
scanf("%s",i);
if (i[0] == '1' && i[1] == '\0') {
userNode* s = (userNode*) malloc (sizeof (userNode));
printf("增加的账号和密码:\n");
scanf("%s %s",s->nameword, s->password);
strcpy (nameword_1, s->nameword);
userNode* head_1 = read_name ();
userNode* x ;
int flag_1 = 0;
for (x = head_1->next; x; x = x->next) {
if (strcmp (nameword_1, x->nameword) == 0) {
flag_1 = 1;
break;
}
}
if (flag_1 == 1) {
printf("该账号密码已存在\n");
system ("pause");
ad_menu();
}
printf("身份为:\n");
printf("\t\t\t\t\t1.学生\n");
printf("\t\t\t\t\t2.老师\n");
scanf("%d",&s->status);
s->next = NULL;
userNode* head = read_name ();
userNode* p =head;
while (p->next) {
p= p->next;
}
p->next = s;
printf("录入成功!\n");
system ("pause");
write_name (head);
free_name (head);
} else if (i[0] == '2' && i[1] == '\0') {
ad_menu ();
} else {
printf("没有该选项\n");
system ("pause");
entry_name ();
}
}
}
可以对在不改变学生信息的同时访问;但要注意账号与学号相关,所以要把账号作为参数传下去
void st_menu ( char* num) {//学生界面
system ("cls");
printf("\t\t\t\t\t1.成绩查询\n");
printf("\t\t\t\t\t2.查询本班成绩\n");
printf("\t\t\t\t\t3.成绩分析\n");
printf("\t\t\t\t\t4.成绩申诉\n");
printf("\t\t\t\t\t5.返回上一层\n");
printf("\t\t\t\t\t0.退出\n");
printf("\t\t\t\t\t请选择:\n");
char n[20];
scanf("%s", n);
if (n[0] == '1' && n[1] == '\0') {
search_st (num);
} else if (n[0] == '2' && n[1] == '\0') {
print_st (num);
} else if (n[0] == '3' && n[1] == '\0') {
analysis_st (num);
} else if (n[0] == '4' && n[1] == '\0') {
appeal_st (num);
} else if (n[0] == '5' && n[1] == '\0' ){
ID ();
} else if (n[0] == '0' && n[1] == '\0') {
exit (0);
} else {
printf("没有该选项\n");
system ("pause");
st_menu (num);
}
}
void appeal_st (char* num) {//成绩申诉
system ("cls");
FILE* fp = fopen ("成绩申诉.txt", "a");
fprintf(fp, "有同学对成绩提出了申诉\n");
fclose (fp);
printf("申诉成功!\n");
system ("pause");
st_menu (num);
}
void search_st (char* num) {//查找学生信息
system ("cls");
st_Node* head;
if (num[1] == '1') {
head = read_st1 ();
} else if (num[1] == '2') {
head = read_st2 ();
} else if (num[1] == '3') {
head = read_st3 ();
} else if (num[1] == '4') {
head = read_st4 ();
} else if (num[1] == '5') {
head = read_st5 ();
}
st_Node* p = head->next;
int flag = 0;
while (p) {
if (strcmp (num, p->st_number) == 0) {
flag = 1;
break;
}
p = p->next;
}
if (flag == 1) {
printf("查找成功!\n");
printf("该学生的信息为:\n");
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
system ("pause");
free_st (head);
st_menu (num);
} else {
printf("没有该学生的信息\n");
system ("pause");
free_st (head);
st_menu (num);
}
}
void print_st (char* num) {//打印
system ("cls");
st_Node* head;
if (num[1] == '1') {
head = read_st1 ();
} else if (num[1] == '2') {
head = read_st2 ();
} else if (num[1] == '3') {
head = read_st3 ();
} else if (num[1] == '4') {
head = read_st4 ();
} else if (num[1] == '5') {
head = read_st5 ();
}
st_Node* p = head->next;
if(p == NULL) {
printf("No");
system ("pause");
}
while (p!=NULL) {
printf("%s %s %s %d %d %d\n",p->name, p->st_number, p->st_class, p->grade_1, p->grade_2, p->grade_3);
p = p->next;
}
system ("pause");
free_st (head);
st_menu (num);
}
void analysis_st (char* num) {//分析
system ("cls");
st_Node* head;
if (num[1] == '1') {
head = read_st1 ();
} else if (num[1] == '2') {
head = read_st2 ();
} else if (num[1] == '3') {
head = read_st3 ();
} else if (num[1] == '4') {
head = read_st4 ();
} else if (num[1] == '5') {
head = read_st5 ();
}
st_Node* p = head->next;
for (p; p; p = p->next) {
if (strcmp (num, p->st_number) == 0) {
break;
}
}
double average = (p->grade_1 + p->grade_2 + p->grade_3) / 3;
int min = p->grade_1;
int max = p->grade_1;
if (p->grade_2 > max) {
max = p->grade_2;
}
if (p->grade_2 < min) {
min = p->grade_2;
}
if (p->grade_3 > max) {
max = p->grade_3;
}
if (p->grade_3 < min) {
min = p->grade_3;
}
printf("平均分为%f\n",average);
printf("最高成绩为%d,最低成绩为%d",max,min);
system ("pause");
st_menu (num);
}