📄 mdw.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
//---------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct AllProject
{
char name[15];
int xuefen;
int num;
int max;
struct AllProject *pnext;
};
struct StdPass
{
char sID[9];
char sPass[17];
int xuefen;
int num;
struct StdPass *pnext;
struct StdProMes *snext;
};
struct StdProMes
{
char name[11];
int xuefen;
struct StdProMes *pnext;
};
void TeacherLand(struct StdPass **shead,struct AllProject **ahead);
void CreatProject(struct AllProject **ahead);
struct AllProject *CreatP();
int InputP(struct AllProject * p);
void DeleteProject(struct AllProject **ahead);
void AddProject(struct AllProject **ahead);
void AddStd(struct StdPass **shead);
struct StdPass * CreatStd();
int InputStd(struct StdPass *p);
void Student(struct StdPass **shead,struct AllProject **ahead);
void StudentLand(struct StdPass **shead,struct AllProject **ahead,struct StdPass *p);
void ChangePassword(struct StdPass *p);
void LookMessage(struct AllProject **ahead);
void ShowProjectMessage(struct AllProject *p);
void LookUseMessage(struct StdPass *p);
void XuanKe(struct StdPass **shead,struct AllProject **ahead,struct StdPass *p);
void LookList(struct StdPass **shead);
void freeAllLinker(struct AllProject *h);
void freeStdLinker(struct StdPass *h);
void SaveAllProject(struct AllProject *h);
struct AllProject *ReadAllProject();
struct StdPass * ReadStd();
FILE *OpenFile(char *mode);
void SaveStd(struct StdPass *h);
void Teacher(struct StdPass **shead,struct AllProject **ahead);
int main()
{
char ch;
struct StdPass *shead=NULL;
struct AllProject *ahead=NULL;
while(1)
{
clrscr();
printf("This is a System!\n");
printf("1 to land teacher .\n");
printf("2 to land student . \n");
// printf("3 to save project message .\n");
// printf("4 to save student message .\n");
printf("0 to quit \n");
ch=getchar();
switch(ch)
{
case '1':
Teacher(&shead,&ahead);
break;
case '2':
Student(&shead,&ahead);
break;
case '0':
printf("\nSave project message .");
getch();
SaveAllProject(ahead);
printf("\nSave studnt message .");
getch();
SaveStd(shead);;
return 0;
default :
printf("Input it again!");
getch();
break;
}
}
freeAllLinker(ahead);
freeStdLinker(shead);
return 0;
}
void Teacher(struct StdPass **shead,struct AllProject **ahead)
{
char pass[]="t",temp2[15];
clrscr();
getchar();
printf("Input password : \n ");
gets(temp2);
if(!strcmp(pass,temp2)) TeacherLand(shead,ahead);
else
{
printf("\nYou input is not right!");
getch();
}
}
void TeacherLand(struct StdPass **shead,struct AllProject **ahead)
{
char ch;
struct StdPass *stemp;
struct AllProject *atemp;
while(1)
{
clrscr();
printf("1 to add project\n");
printf("2 to delete project\n");
printf("3 to add student\n");
printf("4 to add project\n");
printf("5 to read project message from file\n");
printf("6 to read message from file\n");
printf("Input other key to return... \n");
ch=getchar();
switch(ch)
{
case '1':
freeAllLinker(*ahead);
CreatProject(ahead);
break;
case '2':
DeleteProject(ahead);
break;
case '3':
AddStd(shead);
break;
case '4':
AddProject(ahead);
break;
case '5':
*ahead=ReadAllProject();
break;
case '6':
*shead=ReadStd();
break;
default :
return ;
/* break;*/
}
}
}
void CreatProject(struct AllProject **ahead)
{
struct AllProject *atemp,*p;
if(!*ahead) *ahead=CreatP();
else
{
atemp=CreatP();
for(p=(*ahead)->pnext;p->pnext&&p;p=p->pnext);
p->pnext=atemp->pnext;
free(atemp);
}
}
struct AllProject *CreatP()
{
struct AllProject *phead,*p1,*p2;
p1=phead=(struct AllProject *)malloc(sizeof(struct AllProject));
while(1)
{
clrscr();
p2=(struct AllProject *)malloc(sizeof(struct AllProject));
if(!InputP(p2))
{
free(p2);
break;
}
p1->pnext=p2;
p1=p2;
}
p1->pnext=NULL;
return phead;
}
int InputP(struct AllProject * p)
{
printf("Input project'name (0 to quit):\n ");
getchar();
gets(p->name);
if(p->name[0]=='0') return 0;
printf("Input project'xue fen :\n");
scanf("%d",&(p->xuefen));
printf("Input the max student number :\n");
scanf("%d",&(p->max));
p->num=0;
return 1;
}
void DeleteProject(struct AllProject **ahead)
{
char name[11];
struct AllProject *p=*ahead,*temp;
clrscr();
if(!p)
{
printf("You have input nothing!\n");
getch();
return ;
}
else if(!p->pnext)
{
printf("You have input nothing!\n");
getch();
return ;
}
printf("Which project you want to delete?\n");
gets(name);
while(1)
{
if(!strcmp(p->pnext->name,name)||p->pnext)
break;
p=p->pnext;
}
if(!(p->pnext))
{
printf("no exit!"); getch(); return ;
}
temp=p->pnext;
p->pnext=p->pnext->pnext;
free(temp);
}
void AddProject(struct AllProject **ahead)
{
CreatProject(ahead);
}
void AddStd(struct StdPass **shead)
{
struct StdPass *p;
if(!*shead) *shead=CreatStd();
else
{
p=CreatStd();
*shead=p->pnext;
free(p);
}
}
struct StdPass * CreatStd()
{
struct StdPass *shead,*p1,*p2 ;
p1=shead=(struct StdPass *)malloc(sizeof(struct StdPass));
while(1)
{
clrscr();
p2=(struct StdPass *)malloc(sizeof(struct StdPass));
if(!InputStd(p2)) { free(p2); break; }
p1->pnext=p2;
p1=p2;
}
return shead;
}
int InputStd(struct StdPass *p)
{
getchar();
printf("Input student'ID:(0 to quit)\n");
gets(p->sID);
if(p->sID[0]=='0') return 0;
printf("Input student'PassWord:\n");
gets(p->sPass);
p->xuefen=0;
p->snext=NULL;
p->num=0;
return 1;
}
void Student(struct StdPass **shead,struct AllProject **ahead)
{
char ID[10],PW[19];
struct StdPass *p;
clrscr();
getchar();
printf("Input you ID:\n");
gets(ID);
printf("Input you PassWard:\n");
gets(PW);
if(!*shead) { printf("You ID no exit!\n"); getch(); return; }
for(p=(*shead)->pnext;p;p=p->pnext)
if(!strcmp(p->sID,ID))
break;
if(!p) { printf("You ID no exit!");getch(); return; }
if(!strcmp(p->sPass,PW)) StudentLand(shead,ahead,p);
else
{ printf("PassWord wrong!"); getch(); }
}
void StudentLand(struct StdPass **shead,struct AllProject **ahead,struct StdPass *p)
{
char ch;
while(1)
{
clrscr();
printf("1 to change PassWord\n");
printf("2 to look ke xuan ke cheng\n");
printf("3 to look yi yuan ke cheng \n");
printf("4 to xuan ke\n");
printf("5 to look xuan ke xin xi\n");
printf("0 to return\n");
ch=getchar();
switch(ch)
{
case '1':
ChangePassword(p);
break;
case '2':
LookMessage(ahead);
break;
case '3':
LookUseMessage(p);
break;
case '4':
XuanKe(shead,ahead,p);
break;
case '5':
LookList(shead);
getch();
break;
case '0':
return;
}
}
}
void ChangePassword(struct StdPass *p)
{
char temp1[17],temp2[17];
int i=0;
while(1)
{
clrscr();
printf("Input new password : ");
gets(temp1);
printf("Input it again : ");
gets(temp2);
if(!strcmp(temp1,temp2))
break;
}
strcpy(p->sPass,temp1);
}
void LookMessage(struct AllProject **ahead)
{
struct AllProject *p;
if(!(*ahead)||!(*ahead)->pnext)
{
printf("There is no message to show!");
getch();
return ;
}
for(p=(*ahead)->pnext;p;p=p->pnext)
{
clrscr();
ShowProjectMessage(p);
getch();
}
}
void ShowProjectMessage(struct AllProject *p)
{
printf("Project name : %s\n",p->name);
printf("Project xuefen : %d\n",p->xuefen);
printf("The number of neng xuan : %d \n",p->max-p->num);
}
void LookUseMessage(struct StdPass *p)
{
struct StdProMes *sp;
sp=p->snext;
if(!sp) { printf("You have xuan nothing!"); getch(); return; }
for(;sp;sp=sp->pnext)
{
clrscr();
printf("Project name : %s \n",sp->name);
printf("Project xue fen : %d \n",sp->xuefen);
getch();
}
printf("You have xuan %d ge xuefen ",p->xuefen);
getch();
}
void XuanKe(struct StdPass **shead,struct AllProject **ahead,struct StdPass *p)
{
char name[15];
struct StdPass *sp;
struct AllProject *ap;
struct StdProMes *temp;
ap=*ahead;
clrscr();
getchar();
printf("Input you xuan ke de name : \n");
gets(name);
if(!ap) { printf("You Input not exit!"); getch(); return ; }
for(;ap;ap=ap->pnext)
if(!strcmp(ap->name,name)) break;
if(!ap) { printf("You Input not exit!"); getch(); return ; }
else if((ap->xuefen+p->xuefen)>=28)
printf("You can't xuan this project because you xue fen can't > 28 !");
else
{
temp=(struct StdProMes *)malloc(sizeof(struct StdProMes));
strcpy(temp->name,ap->name);
temp->xuefen=ap->xuefen;
temp->pnext=p->snext;
p->snext=temp;
p->num++;
p->xuefen+=temp->xuefen;
}
}
void LookList(struct StdPass **shead)
{
char name[15];
struct StdPass *sp=*shead;
struct StdProMes *p;
clrscr();
printf("Input you want look'project'name : ");
gets(name);
gets(name);
for(sp=sp->pnext;sp;sp=sp->pnext)
for(p=sp->snext;p;p=p->pnext)
if(!strcmp(name,p->name))
printf("%s\n",sp->sID);
}
void freeAllLinker(struct AllProject *h)
{
struct AllProject *p1,*p2;
for(p1=h;p1;p1=p2)
{
p2=p1->pnext;
free(p1);
}
}
void freeStdLinker(struct StdPass *h)
{
struct StdPass *sp1,*sp2;
struct StdProMes *mp1,*mp2;
for(sp1=h;sp1;sp1=sp2)
{
for(mp1=sp1->snext;mp1;mp1=mp2)
{
mp2=mp1->pnext;
free(mp1);
}
sp2=sp1->pnext;
free(sp1);
}
}
void SaveAllProject(struct AllProject *h)
{
struct AllProject *p;
FILE *fp;
fp=OpenFile("w");
if(!fp) { printf("Open file failed!"); getch(); return; }
if(!h) return ;
for(p=h->pnext;p;p=p->pnext)
fwrite(p,sizeof(struct AllProject),1,fp);
fclose(fp);
}
struct AllProject * ReadAllProject()
{
struct AllProject *p1,*h,*p2;
FILE *fp;
fp=OpenFile("r");
if(!fp) { printf("Open file failed!"); getch(); return NULL;}
p1=h=(struct AllProject *)malloc(sizeof(struct AllProject));
while(1)
{
p2=(struct AllProject *)malloc(sizeof(struct AllProject));
if(fread(p2,sizeof(struct AllProject),1,fp)!=1)
{
free(p2);
break;
}
p1->pnext=p2;
p1=p2;
}
p1->pnext=NULL;
fclose(fp);
return h;
}
struct StdPass * ReadStd()
{
struct StdPass *sp1,*sp2,*sh;
struct StdProMes *mp1,*mp2;
int i;
FILE *fp;
sp1=sh=(struct StdPass *)malloc(sizeof(struct StdPass));
fp=OpenFile("r");
if(!fp) { printf("Open file failed!"); getch(); return NULL; }
while(1)
{
sp2=(struct StdPass *)malloc(sizeof(struct StdPass));
if(fread(sp2,sizeof(struct StdPass),1,fp)!=1) break;
printf("%s\n",sp2->sID);
for(i=0;i<sp2->num;i++)
{
mp2=(struct StdProMes *)malloc(sizeof(struct StdProMes));
if(fread(mp2,sizeof(struct StdProMes),1,fp)!=1)
{
free(mp2);
break;
}
if(i==0) { sp2->snext=mp2; mp1=mp2; continue ;}
mp1->pnext=mp2;
mp1=mp2;
}
mp1->pnext=NULL;
if(!i) sp2->snext=NULL;
sp1->pnext=sp2;
sp1=sp2;
}
sp1->pnext=NULL;
fclose(fp);
getch();
return sh;
}
FILE *OpenFile(char *mode)
{
char route[19];
FILE *fp;
getchar();
clrscr();
printf("Which file you want to read?\n");
gets(route);
fp=fopen(route,mode);
return fp;
}
void SaveStd(struct StdPass *h)
{
struct StdPass *sp;
struct StdProMes *mp;
FILE *fp;
fp=OpenFile("w");
if(!fp) { printf("open file failed !"); getch(); return; }
if(!h) return;
for(sp=h->pnext;sp;sp=sp->pnext)
{
fwrite(sp,sizeof(struct StdPass),1,fp);
for(mp=sp->snext;mp;mp=mp->pnext)
fwrite(mp,sizeof(struct StdProMes),1,fp);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -