大一下学期写的C语言程序设计实验报告——学生成绩管理系统课程设计与实现
此篇为“实现篇”o~
目录
一、界面效果
1.登录账号
2.注册账号
3.登录成功选择用户端
4.进入教师端
5.录入信息
6.浏览信息
7.删除信息
8.修改信息
9.查找信息
10.排序成绩
11.进入学生端,根据学号查找信息
12.学生端,浏览班级成绩排名
二、代码实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[20];
char num[20];
int math;
int Cgrade;
int English;
int total;
};
struct teacher
{
char ac[30];//账号
char pw[30];//密码
};
struct teacher tea[50];
struct Node
{
struct student data;
struct Node *next;
};
struct Node *createList()
{
struct Node *listHeadNode=(struct Node*)malloc(sizeof(struct Node));
listHeadNode->next=NULL;
return listHeadNode;
};
struct Node* list=NULL;
struct Node *createNode(struct student data)
{
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
};
//插入链表 录入信息
void insertNodeByHead(struct Node* listHeadNode,struct student data)
{
struct Node *newNode=createNode(data);
newNode->next=listHeadNode->next;
listHeadNode->next=newNode;
}
//删除信息
void deleteNodeByAppoinNum(struct Node* listHeadNode,char* num)
{
struct Node* posFrontNode=listHeadNode;
struct Node* posNode=listHeadNode->next;
if(posNode==NULL)
{
printf("无相关内容,无法删除!\n");
}
else
{
while(strcmp(posNode->data.num,num))
{
posFrontNode=posNode;
posNode=posFrontNode->next;
if(posNode==NULL)
{
printf("无相关内容,无法删除!\n");
}
}
posFrontNode->next=posNode->next;
free(posNode);
printf("信息删除成功!\n");
}
}
// 按总分从高到低排序学生成绩
struct Node *sortStudentListByTotal(struct Node *listHeadNode)
{
if(listHeadNode != NULL)
{
struct Node *p1, *p2, *max;
struct student s;
p1 = listHeadNode;
// 简单选择排序
// 直接交换信息,不改变结点
while((p1=p1->next) != NULL)
{
max = p1;
p2 = p1;
while((p2=p2->next) != NULL)
{
if((p2->data).total > (max->data).total)
max = p2;
}
if(max != p1)
{
s = p1->data;
p1->data = max->data;
max->data = s;
}
}
}
return listHeadNode;
}
//根据学号查找信息
struct Node* searchNodeByAppoinNum(struct Node* listHeadNode,char* num)
{
struct Node* pMove=listHeadNode->next;
if(pMove==NULL)
{
return pMove;
}
else
{
while(strcmp(pMove->data.num,num))
{
pMove=pMove->next;
if(pMove==NULL)
{
break;
}
}
return pMove;
}
};
//打印当前信息
void printNode(struct Node* curNode)
{
printf("学号 姓名 数学 C语言 英语 总成绩\n");
printf(" %s %s %d %d %d %d\n",curNode->data.num,curNode->data.name,curNode->data.math,curNode->data.Cgrade,curNode->data.English,curNode->data.total);
}
//打印 浏览信息
void printList(struct Node* listHeadNode)
{
struct Node* pMove=listHeadNode->next;
printf("学号 姓名 数学 C语言 英语 总成绩\n");
while (pMove)
{
printf(" %s %s %d %d %d %d\n",pMove->data.num,pMove->data.name,pMove->data.math,pMove->data.Cgrade,pMove->data.English,pMove->data.total);
pMove=pMove->next;
}
printf("\n");
}
//从文件中读取信息
void readInfoFromFile(char *fileName,struct Node* listHeadNode)
{
FILE* fp=fopen(fileName,"r");
if(fp==NULL)
{
fp=fopen(fileName,"w");
}
struct student tempData;
while(fscanf(fp,"%s\t%s\t%d\t%d\t%d\t%d",tempData.num,tempData.name,&tempData.math,&tempData.Cgrade,&tempData.English,&tempData.total)!=EOF)
{
insertNodeByHead(listHeadNode,tempData);
memset(&tempData,0,sizeof(tempData));
}
fclose(fp);
}
//保存到文件
void saveInfoToFile(char *fileName,struct Node* listHeadNode)
{
FILE* fp=fopen(fileName,"w");
struct Node* pMove=listHeadNode->next;
while (pMove)
{
fprintf(fp,"%s\t%s\t%d\t%d\t%d\t%d\n",pMove->data.num,pMove->data.name,pMove->data.math,pMove->data.Cgrade,pMove->data.English,pMove->data.total);
pMove=pMove->next;
}
fclose(fp);
}
//系统菜单
void Menu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.教师端\n");
printf("\t\t2.学生端\n");
printf("\t\t3.退出登录\n");
printf("----------------------------------------------\n");
printf("请选择身份(请输入0-3):");
}
//系统菜单功能
void Key()
{
int userKey;
struct student tempData;
scanf("%d",&userKey);
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
while(1)
{
system("cls");
systemMenu();
keyDown();
system("pause");
}
break;
case 2:
while(1)
{
system("cls");
stuMenu();
stuKeyDown();
system("pause");
}
break;
case 3:
while(1)
{
system("cls");
Hello();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
//学生菜单
void stuMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【学生端】------------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.查询成绩\n");
printf("\t\t2.浏览成绩\n");
printf("\t\t3.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-3):");
}
//学生菜单功能
void stuKeyDown()
{
int userKey;
struct student tempData;
scanf("%d",&userKey);
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
printf("\t\t【查找信息】\n");
printf("请输入查找的学号:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
{
printf("未找到相关信息!\n");
}
else
{
printNode(searchNodeByAppoinNum(list,tempData.num));
}
break;
case 2:
printf("\t\t【浏览成绩】\n");
printf("总成绩由高到低排序为:\n");
sortStudentListByTotal(list);
printList(list);
break;
case 3:
//printf("\t\t【返回主菜单】\n");
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
//教师菜单
void systemMenu()
{
printf("---------------【学生管理系统】---------------\n\n");
printf("------------------【教师端】------------------\n\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.删除信息\n");
printf("\t\t4.修改信息\n");
printf("\t\t5.查找信息\n");
printf("\t\t6.排序成绩\n");
printf("\t\t7.返回主菜单\n");
printf("----------------------------------------------\n");
printf("请输入(0-7):");
}
//教师菜单功能
void keyDown()
{
int userKey;
struct student tempData;
scanf("%d",&userKey);
switch(userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
printf("\t\t【录入信息】\n");
printf("请输入学号 姓名 数学成绩 C成绩 英语成绩:\n");
scanf("%s%s%d%d%d",tempData.num,tempData.name,&tempData.math,&tempData.Cgrade,&tempData.English);
tempData.total=tempData.math+tempData.Cgrade+tempData.English;
//readInfoFromFile("student.txt",list);
if(searchNodeByAppoinNum(list,tempData.num)!=NULL)
{
printf("学号已存在,请重新输入!\n");
}
else
{
insertNodeByHead(list,tempData);
saveInfoToFile("student.txt",list);
printf("学号%s的学生信息添加成功!\n",tempData.num);
}
break;
case 2:
printf("\t\t【浏览信息】\n");
/*if(searchNodeByAppoinNum(list,tempData.num)==NULL)
{
printf("学生信息为空!\n");
}*/
// else
//{
printList(list);
//}
break;
case 3:
printf("\t\t【删除信息】\n");
printf("请输入要删除的学生学号:\n");
scanf("%s",tempData.num);
deleteNodeByAppoinNum(list,tempData.num);
saveInfoToFile("student.txt",list);
break;
case 4:
printf("\t\t【修改信息】\n");
printf("请输入要修改的学生学号:\n");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
{
printf("未找到相关信息!\n");
}
else
{
struct Node* curNode=searchNodeByAppoinNum(list,tempData.num);
printf("请输入新的学号 姓名 数学成绩 C成绩 英语成绩:\n");
scanf("%s%s%d%d%d",curNode->data.num,curNode->data.name,&curNode->data.math,&curNode->data.Cgrade,&curNode->data.English);
curNode->data.total=curNode->data.math+curNode->data.Cgrade+curNode->data.English;
saveInfoToFile("student.txt",list);
printf("学号%s的学生信息修改成功!\n",curNode->data.num);
}
break;
case 5:
printf("\t\t【查找信息】\n");
printf("请输入查找的学号:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
{
printf("未找到相关信息!\n");
}
else
{
printNode(searchNodeByAppoinNum(list,tempData.num));
}
break;
case 6:
printf("\t\t【排序成绩】\n");
printf("总成绩由高到低排序为:\n");
sortStudentListByTotal(list);
printList(list);
break;
case 7:
while(1)
{
system("cls");
Menu();
Key();
system("pause");
}
break;
default:
printf("输入错误!请重新输入:");
break;
}
}
//登录界面
void Hello()
{
int se;
printf("---------------【学生管理系统】---------------\n\n");
printf("-----------------【登录界面】-----------------\n\n");
printf("\t\t0.-退出系统-\n");
printf("\t\t1.-登录账号-\n");
printf("\t\t2.-注册账号-\n\n");
printf("-----------------【欢迎使用】-----------------\n\n");
printf("请输入(0-2):");
scanf("%d", &se);
switch (se)
{
case 1:
login();//登录
break;
case 2:
registeruser();//注册
break;
case 0:
printf("\t\t【退出系统】\n");
exit(0);
system("pause");
break;
default:
printf("输入错误!请重新输入:\n");
break;
}
}
// 注册账号功能
void registeruser()
{
char ac[30];
char pw[6],pd[6];
int i, a = 0;
printf("\t\t【注册帐号】\n");
FILE *fp = fopen("账号信息档案.txt", "ab");
printf("请输入用户账号和密码(账号、密码可以为数字,中文字母,英文字母,符号,密码必须6位)\n\n");
printf("请输入账号:");
scanf("%s", ac);
strcpy(tea[a].ac, ac);
printf("请输入密码:");
for (i = 0; i<6; i++)
{
pw[i] = _getch();
printf("*");
tea[a].pw[i] = pw[i];
if (pw[i] == '\r')
{
printf("注册失败,请重新注册账号\n");
return;
}
}
printf("\n再次确认密码:");
for (i = 0; i<6; i++)
{
pd[i] = _getch();
printf("*");
if (tea[a].pw[i] != pd[i])
{
printf("\n密码不对,请重新注册账号");
return;
}
}
fprintf(fp, "账号\t密码\n");
fprintf(fp, "%s\t%s\n", tea[a].ac, tea[a].pw);
printf("\n注册成功!!!\n");
fclose(fp);
system("pause");
system("cls");
}
//登录账号功能
void login()
{
char ac[30];
char ad[20], pw;
int i, j, a = 0;
int flag = 0;
printf("\t\t【登录账号】\n");
for (i = 0; i<3; i++)
{
printf("请输入帐号:");
scanf("%s", ac);
printf("请输入密码:");
for (j = 0; j<6; j++)
{
pw = _getch();
printf("*");
ad[j] = pw;
}
FILE *fp;
fp = fopen("账号信息档案.txt", "r");//读取文件操作
while (!feof(fp))
{
fscanf(fp, "%s %sn", tea[a].ac, tea[a].pw);
if ((strcmp(ac, tea[a].ac) == 0) && (strcmp(ad, tea[a].pw) == 0))
{
flag = 1;
fclose(fp);
break;
}
a++;
}
if (flag == 1)
{
printf("\n登录成功!\n");
system("pause");
system("cls");
while (1)
{
Menu();
Key();
system("pause");
system("cls");
}
}
else
{
printf("\n账号或者密码输入错误,你还有%d次机会,请重新输入:\n", 2 - i);
}
}
if (i == 3)
{
printf("登录失败!\n");
return;
}
}
int main()
{
//system("color 5");
list=createList();
readInfoFromFile("student.txt",list);
while(1)
{
Hello();
system("pause");
system("cls");
}
return 0;
}