C语言学生成绩管理系统

发表时间:2018-04-16 01:46:06 -0400

 
(code:C)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#define SUMGRA(a) (a->english+a->math+a->c)//计算总分 eg.:SUMGRA(pcur)
#define KF 1//开发状态开关


/*******************************
结构体:存放用户及学生的档案信息
*******************************/
typedef struct
{
char name[20];//姓名
char group[20];//班级
char sex[4];//性别
char pass[20];//密码
} USER;


/***************************
结构体:存放学生成绩信息
***************************/
typedef struct grade
{
char id[20];//学号
int english;//英语成绩
int math;//数学成绩
int c;//C语言成绩
struct grade *next;//链表下一节点
} GRA;


/**************
函数声明
**************/
int tipOpen();//提示打开成绩单
char getcheln();//getcheln()
int login();//用户登录
int logOut();//用户退出
void mainMenu();//主菜单
void graSortMenu();//成绩排序菜单
void tapToContinue(char *);//实现点击后继续
void inputPass(char *);//输入密码
int creatList();//创建成绩单
int screenGra(char command);//筛选成绩
int insertGra();//插入成绩
void modifyGra();//修改成绩
int deleteGra();//删除成绩
int printList();//打印列表
void exitSystem();//结束程序
char *strSub(int,int,char *);//字符串截取
char *strConnect(char *,char *);//字符串链接
int getList(char *);//导入成绩单
int putList();//写入成绩单
int sortList(char);//排序成绩
int openRecent();//询问是否打开最近成绩单
void getRecent();//获取最近成绩单名称
int manageUser();//管理用户档案
int changePass();//修改本用户密码
void gotoxy(int x,int y);//移动光标



/******************
全局变量声明
******************/
USER *user=NULL;//存放登录者信息
GRA *head=NULL;//存放成绩单头节点地址
char userId[20],recentFile[20];//userId为登录者id,recentFile为最后一次打开的成绩单名称
int isAdmin=0,isSaved=1,askToOpen=0;;//isAdmin登录者是否为管理员,isSaved成绩单是否修改后未保存(1是0否)


/**********
函数算法
**********/

/*****************
函数:main()
功能:主函数
输入:无
输出:1(非正常结束) 或 0(正常结束)
**************/

int main()
{
do
{
if(userId[0]!=0)//判断用户是否登录
{
if(!askToOpen && head==NULL)//判断成绩单是否打开
{
openRecent();//询问是否打开最近成绩单
}
mainMenu();//显示主菜单
}
else
{
login();//用户登录
}
}while(1);
return 0;
}


/*****************
函数:mainMenu()
功能:主菜单
形参:无
返回值:无
**************/
void mainMenu()
{
char command;
FILE *fp;
system("cls");
printf("┏━━━━━━━━━┓\n");
printf("┃ KX学生管理系统 ┃\n");
printf("┣━━━━━━━━━┫\n");
printf("┃用户:%13s┃\n",strSub(0,13,userId));
printf("┣━━━━━━━━━┫\n");
if(head==NULL)
{
printf("┃ 暂未打开列表 ┃\n");
}
else
{
printf("┃列表:%12s%c┃\n",strSub(0,8,recentFile),(isAdmin&&!isSaved?'*':' '));
}
printf("┣━━━━━━━━━┫\n");
printf("┃ 请输入命令 ┃\n");
printf("┣━━━━━━━━━┫\n");
if(head!=NULL)
{
printf("┃ 1.%8s ┃\n",isAdmin?"添加成绩 ":"查询我的成绩");
printf("┃ 2.筛选成绩 ┃\n");
isAdmin&&printf("┃ 3.修改成绩 ┃\n");
isAdmin&&printf("┃ 4.删除成绩 ┃\n");
printf("┃ 5.打印成绩单 ┃\n");
isAdmin&&printf("┃ 6.保存成绩单 ┃\n");
}
isAdmin&&printf("┃ 8.新建成绩单 ┃\n");
printf("┃ 9.读取成绩单 ┃\n");
printf("┃ a.修改密码 ┃\n");
isAdmin&&printf("┃ b.增改学生 ┃\n");
printf("┃ c.退出登录 ┃\n");
printf("┃ 0.退出系统 ┃\n");
printf("┗━━━━━━━━━┛\n");
command=getch();
switch(command)
{
case'1'://插入成绩
if(tipOpen())
{
system("cls");
if(isAdmin)
{
insertGra();
}
else
{
printf("正在查询,请稍等...\n");
sortList('2');
screenGra('A');
tapToContinue("");
}
}
break;
case'2'://筛选成绩
if(tipOpen())
{
screenGra(' ');
}
break;
case'3'://修改成绩
if(isAdmin&&tipOpen())
{
modifyGra();
}
break;
case'4'://删除成绩
if(isAdmin&&tipOpen())
{
system("cls");
screenGra('A');
deleteGra();
}
break;
case'5'://查看列表
if(tipOpen(head))
{
graSortMenu();
}
break;
case'6'://保存列表
if(isAdmin&&tipOpen())
{
putList();
}
break;
case'8'://创建列表
if(isAdmin)
{
system("cls");
creatList();
}
break;
case'9'://读取列表
system("cls");
getList("");
break;
case'a'://修改密码
system("cls");
changePass();
break;
case'b'://添加修改用户
if(isAdmin)
{
system("cls");
manageUser();
}
break;
case'c'://退出登录
system("cls");
logOut();
break;
case'0'://退出系统
exitSystem(0);
break;
}
return;
}


/*****************
函数:graSortMenu()
功能:成绩排序菜单
形参:无
返回值:无
**************/

void graSortMenu()
{
if(!tipOpen(head))
{
return;
}
while(1)
{
system("cls");
screenGra('A');
char command;
printf("┏━━━━━━━┓\n");
printf("┃1.按学号排序 ┃\n");
printf("┃2.按总分排序 ┃\n");
printf("┃3.按英语排序 ┃\n");
printf("┃4.按高数排序 ┃\n");
printf("┃5.按C语言排序 ┃\n");
printf("┃6.筛选成绩 ┃\n");
isAdmin&&printf("┃7.添加成绩 ┃\n");
isAdmin&&printf("┃8.修改成绩 ┃\n");
isAdmin&&printf("┃9.删除成绩 ┃\n");
printf("┃0.返回菜单 ┃\n");
printf("┗━━━━━━━┛\n");
command=getch();
switch(command)
{
case'1'://重新排序
case'2'://重新排序
case'3'://重新排序
case'4'://重新排序
case'5'://重新排序
sortList(command);
break;
case'6'://调用筛选菜单
screenGra(' ');
break;
case'7'://添加成绩
if(isAdmin)
{
insertGra();
}
break;
case'8'://修改成绩
if(isAdmin)
{
modifyGra();
}
break;
case'9'://删除成绩
deleteGra();
break;
case'0'://返回
return;
}
}
return;
}


/*******************************
函数:getcheln()
功能:getche()并换行
形参:无
返回值:读取到的字符
********************************/
char getcheln()
{
char c=getch();
printf("%c\n",c);
return c;
}



/*****************
函数:tipOpen()
功能:判断并提示打开成绩单
输入:无
输出: 1(已打开成绩单)或0(未打开成绩单)
**************/
int tipOpen()
{
if(head==NULL)
{
printf("还没有打开或创建成绩单!\n");
tapToContinue("");
return 0;
}
return 1;
}


/*****************
函数:getUserMess()
功能:获取账户为id的用户信息
输入:char *id
输出:User*
**************/
USER *getUserMess(char *id)
{
FILE *fp;
USER *cUser;
//char fileName[30]="user/";
//strcat(fileName,userId);
if((fp=fopen(strConnect("user/",id),"r"))==NULL)
{
return NULL;
}
else
{
cUser=(USER*)malloc(sizeof(USER));
fread(cUser,sizeof(USER),1,fp);
fclose(fp);
return cUser;
}
}


/*****************
函数:login()
功能:用户登录
输入:无
输出:1(正常登陆)或0(未正常登陆)
**************/
int login()
{
int result=0;
char id[20],getPass[20],truePass[20];
FILE *fp;
//USER *user=NULL;
system("cls");
printf("┏━━━━━━━━━┓\n");
printf("┃ KX学生管理系统 ┃\n");
printf("┣━━━━━━━━━┫\n");
printf("┃请输入账号信息登录┃\n");
printf("┣━━━━━━━━━┫\n");
printf("┃账号: ┃\n");
printf("┃密码: ┃\n");
printf("┗━━━━━━━━━┛\n");
while(1)
{
gotoxy(8,5);
scanf("%s",id);

if((user=getUserMess(id))==NULL && !KF)
{
gotoxy(0,7);
printf("┣━━━━━━━━━┫\n");
printf("┃提示:用户不存在 ┃\n");
printf("┗━━━━━━━━━┛\n\n\n");
tapToContinue("");
gotoxy(0,8);
printf("┃%18s┃","");
gotoxy(0,5);
printf("┃账号: ┃");
continue;
}
else
{
break;
}
}
while(1)
{
gotoxy(8,6);
inputPass(getPass);
if(strcmp(getPass,user->pass)==0 || KF)
{
strcpy(userId,id);
result=1;
printf("%s,",user->name);
gotoxy(0,7);
printf("┣━━━━━━━━━┫\n");
printf("┃提示:登录成功! ┃\n");
printf("┗━━━━━━━━━┛\n");
tapToContinue("");
system("cls");
isAdmin=strcmp(user->group,"admin")==0?1:0;
askToOpen=0;
return 1;
}
else if(getPass[0])
{
gotoxy(0,7);
printf("┣━━━━━━━━━┫\n");
printf("┃提示:密码错误 ┃\n");
printf("┗━━━━━━━━━┛\n");
tapToContinue("");
gotoxy(0,6);
printf("┃密码: ┃");
gotoxy(0,8);
printf("┃%18s┃","");
continue;
}
}
return 0;
}


/*****************
函数:logOut()
功能:用户退出
输入:无
输出:1(正常退出)或0(非正常退出)
**************/
int logOut()
{
user=NULL;
userId[0]=0;
isAdmin=0;
printf("┏━━━┓\n");
printf("┃已退出┃\n");
printf("┗━━━┛\n");
return 1;
}



/*****************
函数:tapToContinue()
功能:显示提示(tip)实现点击后继续效果
(如果tip[0]==0则不显示)
输入:char *tip
输出:无
**************/
void tapToContinue(char *tip)
{
if(tip[0])
{
printf("%s",tip);
}
getch();
}


/*****************
函数:inputPass()
功能:实现输入一系列字符显示为*并将其拼接成字符串
输入:char *str
输出:无
**************/
void inputPass(char *str)
{
char c;
int i;
for(i=0;i<19;)
{
//printf("%s\n",str);
c=getch();
if(c=='\r')
{
break;
}
else if(c==8){
if(i>0)
{
str[i-1]=0;
i=i==0?0:(i-1);
printf("%c%c%c",8,' ',8);
}
}
else if((c>='0' && c<='9') || (c>='a' && c<='z'))
{
str[i]=c;
str[++i]=0;
putchar('*');
}
}
putchar('\n');
}


/*****************
函数:creatList()
功能:创建一个新成绩单
输入:无
输出: 1(正常创建)或0(非正常创建)
**************/
int creatList()
{
GRA *pcur,*newHead;
char command;
newHead=(GRA*)malloc(sizeof(GRA));
newHead->next=NULL;
if(!isSaved)
{
printf("┏━━━━━━━━━━━━━━━┓\n");
printf("┃创建新的成绩单将舍弃当前成绩单┃\n");
printf("┣━━━━━━━┳━━━━━━━┫\n");
printf("┃  1.是   ┃  2.否   ┃\n");
printf("┗━━━━━━━┻━━━━━━━┛\n");
command=getch();
if(command!='1')
{
return 0;
}
}
head=newHead;
strcpy(recentFile,"Untitle");
insertGra();
printf("新的成绩单创建完成\n");
isSaved=0;
return 1;
}


/*****************
函数:screenGra()
功能:筛选并显示成绩单
输入:char command
输出:成绩单中成员的个数
**************/
int screenGra(char command)
{
GRA *pcur;
int screenInt1,screenInt2,count=0;
char screenStr[50],userName[20],userGroup[20],userSex[4];
while(1)
{
system("cls");
if(command!='A' && command!='0')
{
while(!(command>='1' && command<='7'))
{
system("cls");
printf("┏━━━━━━━━━┓\n");
printf("┃  筛选成绩   ┃\n");
printf("┣━━━━━━━━━┫\n");
printf("┃1.按姓名筛选   ┃\n");
printf("┃2.按学号筛选   ┃\n");
printf("┃3.按班级筛选   ┃\n");
printf("┃4.按总成绩筛选  ┃\n");
printf("┃5.按英语成绩筛选 ┃\n");
printf("┃6.按高数成绩筛选 ┃\n");
printf("┃7.按C语言成绩筛选┃\n");
printf("┃0.返回上一层   ┃\n");
printf("┗━━━━━━━━━┛\n");
command=getcheln();
if(command=='0')
{
return 0;
}
}
switch(command)
{
case'1':
case'2':
case'3':
printf("┏━━━━━━━━━┓\n");
printf("┃请输入筛选关键字:┃\n");
printf("┃%18s┃\n","");
printf("┗━━━━━━━━━┛\n");
gotoxy(2,15);
scanf("%s",screenStr);
gotoxy(0,17);
break;
case'4':
case'5':
case'6':
case'7':
printf("┏━━━━━━━━━┓\n");
printf("┃请输入成绩区间: ┃\n");
printf("┃[   ,   ]┃\n","");
printf("┗━━━━━━━━━┛\n");
gotoxy(4,15);
scanf("%d",&screenInt1);
gotoxy(12,15);
scanf("%d",&screenInt2);
gotoxy(0,17);
break;
default:
printf("error\n");
}
}
pcur=head;
printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━┳━━┳━━┳━━┳━━┓\n");
printf("┃%6s┃%10s┃%10s┃%10s┃%4s┃%4s┃%4s┃%4s┃%4s┃\n","序号","学号","姓名","班级","性别","英语","高数","C","总分");
while(pcur->next!=NULL)
{
pcur=pcur->next;
user=getUserMess(pcur->id);
if(user!=NULL)
{
strcpy(userName,user->name);
strcpy(userGroup,user->group);
strcpy(userSex,user->sex);
}
else
{
userName[0]=userGroup[0]=userSex[0]=0;
}
if (command=='A'
|| (command=='1' && strstr(userName,screenStr)!=NULL)
|| (command=='2' && strstr(pcur->id,screenStr)!=NULL)
|| (command=='3' && strstr(userGroup,screenStr)!=NULL)
|| (command=='4' && screenInt1<=SUMGRA(pcur) && screenInt2>=SUMGRA(pcur))
|| (command=='5' && screenInt1<=pcur->english && screenInt2>=pcur->english)
|| (command=='6' && screenInt1<=pcur->math && screenInt2>=pcur->math)
|| (command=='7' && screenInt1<=pcur->c && screenInt2>=pcur->c))
{
count++;
printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━╋━━╋━━╋━━╋━━┫\n");
printf("┃%s%5d┃%10s┃%10s┃%10s┃%4s┃%4d┃%4d┃%4d┃%4d┃\n",strcmp(pcur->id,userId)==0?"*":" ",count,pcur->id,userName,userGroup,userSex,pcur->english,pcur->math,pcur->c,SUMGRA(pcur));
}
}
printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━┻━━┻━━┻━━┻━━┛\n");
if(command=='A')
break;
else
{
tapToContinue("点击任意键返回...\n");
command=' ';
}
}
return count;
}


/*****************
函数:insertGra()
功能:插入成绩
输入:无
输出:插入的成绩个数
**************/
int insertGra()
{
GRA *pnew,*pcur=head;
int i=0,count=1;
char id[10],fileName[30];
FILE *fp;
system("cls");
printf("┏━━━━┳━━━━━━━━┳━━━━┳━━━━┳━━━━━┓\n");
printf("┃%8s┃%16s┃%8s┃%8s┃%10s┃\n","序号","学号","英语成绩","高数成绩","C语言成绩");
printf("┣━━━━╋━━━━━━━━╋━━━━╋━━━━╋━━━━━┫\n");
printf("┃%8d┃%16s┃%8s┃%8s┃%10s┃\n",1,"","","","");
printf("┗━━━━┻━━━━━━━━┻━━━━┻━━━━┻━━━━━┛\n");
while(1)
{
gotoxy(12,1+count*2);
scanf("%s",id);
if(id[0]=='0' && id[1]==0)
{
break;
}
pnew=(GRA*)malloc(sizeof(GRA));
pnew->next=pcur->next;
pcur->next=pnew;
pcur=pnew;
strcpy(pcur->id,id);
gotoxy(30,1+count*2);
scanf("%d",&pcur->english);
gotoxy(40,1+count*2);
scanf("%d",&pcur->math);
gotoxy(50,1+count*2);
scanf("%d",&pcur->c);
i++;
isSaved=0;
gotoxy(0,2+count*2);
count++;
printf("┣━━━━╋━━━━━━━━╋━━━━╋━━━━╋━━━━━┫\n");
printf("┃%8d┃%16s┃%8s┃%8s┃%10s┃\n",count,"","","","");
printf("┗━━━━┻━━━━━━━━┻━━━━┻━━━━┻━━━━━┛\n");
}
return i;
}


/*****************
函数:deleteGra()
功能:删除成绩
输入:无
输出:删除的成绩个数
**************/
int deleteGra()
{
char c;
GRA *pcur,*ptemp;
int i=0,j,delId[100],id,sum,min;
printf("┏━━━━━━━━━━━━┓\n");
printf("┃ 请输入要删除成绩的编号 ┃\n");
printf("┃多个用空格隔开,输入0结束┃\n");
printf("┗━━━━━━━━━━━━┛\n");
while(scanf("%d",&id),id!=0)
{
delId[i]=id;
i++;
}
sum=i;
for(i=0;i<sum-1;i++)
{
min=i;
for(j=i;j<sum-1;j++)
{
if(delId[j]<delId[min])
{
min=j;
}
}
j=delId[i];
delId[i]=delId[min];
delId[min]=j;
}
pcur=head;
j=1;
i=0;
system("cls");
printf("┏━━━━━━━━━━━━┓\n");
while(pcur->next!=NULL)
{
ptemp=pcur;
pcur=pcur->next;
if(j==delId[i])
{
printf("┃删除成功 成绩编号:%6d!┃\n",delId[i]);
ptemp->next=pcur->next;
free(pcur);
pcur=ptemp;
i++;
isSaved=0;
}
j++;
}
printf("┗━━━━━━━━━━━┛\n");
tapToContinue("");
return i;
}


/*****************
函数:modifyGra()
功能:修改成绩
输入:无
输出:无
**************/
void modifyGra()
{
GRA *pcur;
int i,modId,count;
system("cls");
count=screenGra('A');
while(1)
{
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃%50s┃\n","");
printf("┃%50s┃\n","");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
gotoxy(2,4+2*count);
printf("%50s","");
gotoxy(2,4+2*count);
printf("请输入要修改成绩的编号(输入0返回) \n");
gotoxy(2,5+2*count);
printf("%50s","");
gotoxy(2,5+2*count);
scanf("%d",&modId);
if(modId==0)
{
break;
}
pcur=head;
for(i=0;i<modId;i++)
{
if(pcur->next==NULL)
{
gotoxy(2,4+2*count);
printf("%50s","");
gotoxy(2,4+2*count);
printf("未找到序号:%5d",modId);
tapToContinue("");
return;
}
pcur=pcur->next;
}
gotoxy(2,1+2*i);
putchar('');
gotoxy(2,4+2*count);
printf("%50s","");
gotoxy(2,4+2*count);
printf("输入新的英语成绩、高数成绩、C语言成绩(用空格隔开)\n");
gotoxy(2,5+2*count);
printf("%50s","");
gotoxy(2,5+2*count);
isSaved=0;
scanf("%d%d%d",&pcur->english,&pcur->math,&pcur->c);
gotoxy(52,1+2*i);
printf("%4d",pcur->english);
gotoxy(58,1+2*i);
printf("%4d",pcur->math);
gotoxy(64,1+2*i);
printf("%4d",pcur->c);
gotoxy(70,1+2*i);
printf("%4d",SUMGRA(pcur));
gotoxy(2,1+2*i);
putchar('*');
gotoxy(0,3+2*count);
}
}


/*********************
函数:strSub()
功能:字符串截取
将str从第start个字符截取到第end个字符
输入:int start,int end,char *str
输出:截取后字符串的地址
********************/
char *strSub(int start,int end,char *str)
{
int lenth=strlen(str);
if(start>end || start>=lenth)
{
return 0;
}
char *str2=(char *)calloc(lenth+1,sizeof(char));
strcpy(str2,str);
if(end<lenth)
{
str2[end]=0;
}
return str2+start;
}



/*****************
函数:exitSystem()
功能:退出系统,结束程序
输入:无
输出:无
**************/
void exitSystem()
{
char c;
system("cls");
if(isSaved)
{
printf("┏━━━━━━━┓\n");
printf("┃确定退出系统?┃\n");
printf("┣━━━┳━━━┫\n");
printf("┃1.是 ┃ 2.否┃\n");
printf("┗━━━┻━━━┛\n");
c=getch();
if(c=='1')
{
gotoxy(0,4);
printf("┣━━━┻━━━┫\n");
printf("┃ 已退出 ┃\n");
printf("┗━━━━━━━┛\n");
exit(0);
}
}
else
{
if(isAdmin)
{
printf("┏━━━━━━━━━━━━━━━━━━┓\n");
printf("┃当前成绩单未保存,是否保存后再退出?┃\n");
printf("┣━━━━━━┳━━━━━┳━━━━━┫\n");
printf("┃1.保存并退出┃2.直接退出┃0.返回菜单┃\n");
printf("┗━━━━━━┻━━━━━┻━━━━━┛\n");
c=getch();
switch(c)
{
case'1':
while(!putList());
case'2':
exit(0);
break;
default:
return;
}
}
else
{
exit(0);
}
}
}



/*****************
函数:putList()
功能:保存成绩单
输入:无
输出:1(成功保存)或0(未成功保存)
**************/
int putList()
{
GRA *pcur=head->next;
char fileName[30],list[20],command=0;
FILE *fp;
int haveName=strcmp(recentFile,"Untitle")==0?0:1;
system("cls");
if(haveName)
{
printf("┏━━━━━━━━━━━━━━━━┓\n");
printf("┃ 请选择保存方式 ┃\n");
printf("┣━━━━━━━┳━━━━┳━━━┫\n");
printf("┃1.保存到原文件┃2.另存为┃3.返回┃\n");
printf("┗━━━━━━━┻━━━━┻━━━┛\n");
command=getcheln();
if(command=='1')
{
strcpy(list,recentFile);
}
else if(command=='2')
{

}
else
{
return 1;
}
}
while(1)
{
if(!haveName || command=='2')
{
system("cls");
printf("┏━━━━━━━━━━━━━━━━┓\n");
printf("┃请输入要保存的文件名(输入0取消):┃\n");
printf("┃%32s┃\n","");
printf("┗━━━━━━━━━━━━━━━━┛\n");
gotoxy(2,2);
scanf("%s",list);
gotoxy(0,4);
if(list[1]==0)
{
if(list[0]=='0')
{
return 0;
}
}
}
strcpy(fileName,strConnect("list/",list));
if((fp=fopen(fileName,"r"))!=NULL)
{
fclose(fp);
if(strcmp(list,recentFile)!=0)
{
printf("┏━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃列表%20s已存在,确定要覆盖吗?┃\n",list);
printf("┣━━━━━━━━━━━┳━━━━━━━━━━┫\n");
printf("┃ 1.是 ┃ 2.否 ┃\n");
printf("┗━━━━━━━━━━━┻━━━━━━━━━━┛\n");
if(getch()!='1')
{
continue;
}
}
}
remove(fileName);
system("cls");
printf("┏━━━━━┓\n");
printf("┃正在保存..┃\n");
printf("┗━━━━━┛\n");
if((fp=fopen(fileName,"a"))==NULL)
{
gotoxy(2,1);
printf("打开失败!\n");
return 0;
}
while(pcur!=NULL)
{
fwrite(pcur,sizeof(GRA),1,fp);
pcur=pcur->next;
}
fclose(fp);
putRecent(list);
gotoxy(2,1);
printf("保存成功!\n",list);
isSaved=1;
tapToContinue("");
return 1;
}
}



/*****************
函数:getList()
功能:读取名为ilist的成绩单
ilist[0]==0的话则由用户输入并赋值给ilist
输入:char *ilist
输出:1(正常读取)或0(非正常读取)
**************/
int getList(char *ilist)
{
char command;
if(!isSaved && isAdmin)
{
printf("┏━━━━━━━━━━━━━━━━━━┓\n");
printf("┃当前成绩单未保存,是否先进行保存? ┃\n");
printf("┣━━━━━━┳━━━━━┳━━━━━┫\n");
printf("┃1.保存成绩单┃2.放弃保存┃3.返回菜单┃\n");
printf("┗━━━━━━┻━━━━━┻━━━━━┛\n");
command=getch();
switch(command)
{
case'1':
while(!putList());
case'2':
break;
case'3':
return;
}
}
GRA *pcur,*pnew,*newHead;
newHead=(GRA *)malloc(sizeof(GRA));
FILE *fp;
char list[20],clearStr[5]="%22s";
while(1)
{
newHead->next=NULL;
pcur=newHead;
if(!ilist[0])
{
system("cls");
strcpy(clearStr,"%30s");
printf("┏━━━━━━━━━━━━━━━┓\n");
printf("┃请输入导入的成绩单(输入0取消):┃\n");
printf("┃%30s┃\n","");
printf("┗━━━━━━━━━━━━━━━┛\n");
gotoxy(2,2);
scanf("%s",list);
if(list[0]=='0' && list[1]==0)
{
return 0;
}
}
else
{
strcpy(list,ilist);
}
gotoxy(2,1);
printf(clearStr,"");
gotoxy(2,1);
printf("打开:%s...\n",list);
if((fp=fopen(strConnect("list/",list),"r"))==NULL)
{
gotoxy(2,1);
printf(clearStr,"");
gotoxy(2,1);
printf("打开失败!\n");
free(list);
continue;
}
putRecent(list);
while(1)
{
pnew=(GRA *)malloc(sizeof(GRA));
fread(pnew,sizeof(GRA),1,fp);
pcur->next=pnew;
if(pnew->next==NULL)
{
break;
}
pcur=pcur->next;
}
fclose(fp);
gotoxy(2,1);
printf(clearStr,"");
gotoxy(2,1);
printf("读取成绩单完成!\n");
tapToContinue("");
isSaved=1;
head=newHead;
return 1;

}
}




/*****************
函数:sortList()
功能:按方式command排序成绩单
command==' '则由用户输入command
输入:char command
输出:1(正常排序)或0(非正常排序)
**************/
int sortList(char command)
{
GRA *pcur,*ptemp,*pmax,*pswap,*pcurNext,*pmaxNext;
pswap=(GRA *)malloc(sizeof(GRA));
pcur=head->next;
while(pcur!=NULL)
{
pmax=pcur;
for(ptemp=pcur;ptemp!=NULL;ptemp=ptemp->next)
{
if((command=='1' && strcmp(ptemp->id,pmax->id)<0)
|| (command=='2' && SUMGRA(ptemp)>SUMGRA(pmax))
|| (command=='3' && ptemp->english>pmax->english)
|| (command=='4' && ptemp->math>pmax->math)
|| (command=='5' && ptemp->c>pmax->c))
{
pmax=ptemp;
}
}
pcurNext=pcur->next;
pmaxNext=pmax->next;
memcpy(pswap,pcur,sizeof(GRA));
memcpy(pcur,pmax,sizeof(GRA));
memcpy(pmax,pswap,sizeof(GRA));
pcur->next=pcurNext;
pmax->next=pmaxNext;
pcur=pcur->next;
}
system("cls");
isSaved=0;
return 1;
}





/*****************
函数:putRecent()
功能:记录最后操作的成绩单的名称(fileName)
输入:char *fileName
输出:1(成功记录)或0(未记录)
**************/
int putRecent(char *fileName)
{
if(!fileName)
{
return 0;
}
strcpy(recentFile,fileName);
FILE *recent;
recent=fopen("data/recentList","w");
fputs(fileName,recent);
fclose(recent);
return 1;
}



/*****************
函数:getRecent()
功能:获取最后操作的成绩单的名称并赋值给rencentFile
输入:无
输出:无
**************/
void getRecent()
{
char command;
FILE *recent;
if((recent=fopen("data/recentList","r"))==NULL)
{
return;
}
fgets(recentFile,20,recent);
fclose(recent);
}



/*****************
函数:openRecent()
功能:询问用户是否打开最后操作的成绩单
输入:无
输出:1(正常打开)或0(未打开)
**************/
int openRecent()
{
char command;
FILE *recent;
char recentFileTemp[20];
askToOpen=1;
getRecent();
strcpy(recentFileTemp,strSub(0,13,recentFile));
recentFileTemp[10]=recentFileTemp[11]=recentFileTemp[12]='.';
printf("┏━━━━━━━━━━━┓\n");
printf("┃是否打开上次的成绩单?┃\n");
printf("┣━━━━━━━━━━━┫\n");
printf("┃上次打开:%13s┃\n",recentFileTemp);
printf("┣━━━━━━┳━━━━┫\n");
printf("┃ 1.是  ┃ 2.否 ┃\n");
printf("┗━━━━━━┻━━━━┛\n");
command=getch();
if(command!='1')
{
return 0;
}
getList(recentFile);
return 1;
}


/*****************
函数:manageUser()
功能:管理学生用户
输入:无
输出:
**************/
int manageUser()
{
USER *user;
int isCreat,count=0;
char id[20],newId[20],command;
FILE *fp;
int existUser;
while(1)
{
system("cls");
isCreat=1;
printf("┏━━━━━━━━━━━━┓\n");
printf("┃ 学生档案信息管理 ┃\n");
printf("┣━━━━━━━━━━━━┫\n");
printf("┃输入学生ID(输入0取消): ┃\n");
printf("┣━━━━━━━━━━━━┫\n");
printf("┃ ┃\n");
printf("┗━━━━━━━━━━━━┛\n");
gotoxy(2,5);
scanf("%s",id);
gotoxy(0,7);
if(id[0]=='0' && id[1]==0)
{
return count;
}
while(1)
{
system("cls");
existUser=(fp=fopen(strConnect("user/",id),"r"))!=NULL?1:0;
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ %24s的用户%s存在 ┃\n",strConnect("ID为",id),existUser?"已":"不");
printf("┣━━━━━━━━┳━━━━━┳━━━━━┳━━┳━━━━━━━━━━┫\n");
printf("┃%16s┃%10s┃%10s┃%4s┃%20s┃\n","学号","姓名","班级","性别","登录密码");
if(existUser)
{

user=getUserMess(id);
printf("┃%16s┃%10s┃%10s┃%4s┃%20s┃\n",id,user->name,user->group,user->sex,user->pass);
printf("┣━━━━━━━━┻━━━━━┻━━━━━┻━━┻━━━━━━━━━━┫\n");
printf("┃ 1.修改Ta的学号          ┃\n");
printf("┃ 2.修改Ta的姓名         ┃\n");
printf("┃ 3.修改Ta的班级        ┃\n");
printf("┃ 4.修改Ta的性别        ┃\n");
printf("┃ 5.修改登录密码        ┃\n");
printf("┃ 6.删除学生信息       ┃\n");
printf("┃ 7.管理其它学生       ┃\n");
printf("┃ 0.返回菜单      ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
fclose(fp);
command=getch();
if(command=='0')
{
return count;
}
else if(command=='1')
{
gotoxy(2,4);
printf("%16s","");
gotoxy(2,4);
scanf("%16s",newId);
if(rename(strConnect("user/",id),strConnect("user/",newId))==0)
{
strcpy(id,newId);
}
else
{
gotoxy(2,4);
printf("%16s",id);
}

}
else if((command>='2' && command<='5'))
{
user=getUserMess(id);
switch(command)
{
case'2':
gotoxy(20,4);
printf("%10s","");
gotoxy(20,4);
scanf("%s",user->name);
break;
case'3':
gotoxy(32,4);
printf("%10s","");
gotoxy(32,4);
scanf("%s",user->group);
break;
case'4':
strcpy(user->sex,strcmp("女",user->sex)==0?"男":"女");
break;
case'5':
gotoxy(50,4);
printf("%20s","");
gotoxy(50,4);
scanf("%s",user->pass);
break;
default:
break;
}
fp=fopen(strConnect("user/",id),"w");
fwrite(user,sizeof(USER),1,fp);
fclose(fp);
count++;
}
else if(command=='6')
{
if(remove(strConnect("user/",id))==0)
{
printf("删除成功!\n");
break;
}
else
{
printf("删除失败!\n");
}
}
else if(command=='7')
{
break;
}
}
else
{
user=(USER*)malloc(sizeof(USER));
printf("┃%16s┃%10s┃%10s┃%4s┃%20s┃\n",id,"","","","");
printf("┗━━━━━━━━┻━━━━━┻━━━━━┻━━┻━━━━━━━━━━┛\n");
gotoxy(20,4);
scanf("%s",user->name);
gotoxy(32,4);
scanf("%s",user->group);
gotoxy(44,4);
scanf("%s",user->sex);
gotoxy(50,4);
scanf("%s",user->pass);
//scanf("%s%s%s%s",user->name,user->group,user->sex,user->pass);
FILE *fp;
fp=fopen(strConnect("user/",id),"w");
fwrite(user,sizeof(USER),1,fp);
fclose(fp);
printf("新建学生信息完成!\n");
}

}
tapToContinue("");
}
}


/*****************
函数:changePass()
功能:修改密码
输入:无
输出:1(成功修改)或0(未成功修改)
**************/
int changePass()
{
char getPass[20],truePass[20],putPass[2][20],fileName[30];
FILE *fp;
int result=0;
do
{
system("cls");
printf("┏━━━━━━━━━━━━━━━━┓\n");
printf("┃ 修改登录密码 ┃\n");
printf("┣━━━━━━━━━━━━━━━━┫\n");
printf("┃原密码: ┃\n");
printf("┃新密码: ┃\n");
printf("┃新密码: ┃\n");
printf("┣━━━━━━━━━━━━━━━━┫\n");
printf("┃提示: ┃\n");
printf("┗━━━━━━━━━━━━━━━━┛\n");
gotoxy(8,7);
printf("请输入原密码(输入0取消)");
gotoxy(10,3);
printf("%12s","");
gotoxy(10,3);
inputPass(getPass);
if(getPass[0]=='0' && getPass[1]==0)
{
break;
}
if(strcmp(user->pass,getPass)!=0)
{
gotoxy(8,7);
printf("%26s","");
gotoxy(8,7);
tapToContinue("原密码错误!");
continue;
}
do
{
gotoxy(8,7);
printf("请输入新密码(输入0取消)");
gotoxy(10,4);
printf("%12s","");
gotoxy(10,4);
inputPass(putPass[0]);
if(putPass[0][0]=='0' && putPass[0][1]==0)
{
break;
}
if(strlen(putPass[0])<6)
{
gotoxy(8,7);
printf("%26s","");
gotoxy(8,7);
tapToContinue("新密码至少要六位!");
continue;
}
gotoxy(8,7);
printf("%26s","");
gotoxy(8,7);
printf("请再次输入新密码");
gotoxy(10,5);
printf("%12s","");
gotoxy(10,5);
inputPass(putPass[1]);
if(strcmp(putPass[0],putPass[1])!=0)
{
gotoxy(8,7);
printf("%26s","");
gotoxy(8,7);
tapToContinue("两次输入的密码不一致!");
continue;
}
strcpy(user->pass,putPass[0]);
fp=fopen(strConnect("user/",userId),"w");
fwrite(user,sizeof(USER),1,fp);
gotoxy(8,7);
printf("%26s","");
gotoxy(8,7);
tapToContinue("密码修改成功!");
fclose(fp);
result=1;
break;
}while(!(putPass[0][0]=='0' && putPass[0][1]==0));
}while((getPass[0]!='0' && getPass[1]!=0) && result!=1);
return result;
}


/*****************
函数:strConnect()
功能:将字符串str1和str2连接
输入:char *str1,char *str2
输出:连接后的字符串地址
**************/
char *strConnect(char *str1,char *str2)
{
char *str=(char*)malloc(strlen(str1)+strlen(str2)+1);
strcpy(str,str1);
strcat(str,str2);
return str;
}


/*****************
函数:gotoxy()
功能:光标移动到第y-1行第x个字节
输入:int x,int y
输出:无
**************/
void gotoxy(int x,int y)
{
int xx=0x0b;
HANDLE hOutput;
COORD loc;
loc.X= x;
loc.Y=y;
hOutput= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, loc);
return;
}
(/code)
Comment:
Name:

返回上级 | 返回首页