您的位置:首页 > 其它

简单易懂带注释学生管理系统

2016-12-21 12:07 232 查看
在大一下学期,我们的实习是做一个学生管理系统。

系统啊……

崩溃啊……

不过还好我做出来了

(电脑上存的是没做完的版本,有几个函数没实现,懒得写了^_^)

附上代码:

这是主函数的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "paixu.h"
int flag=0;
int main()
{
int nn;
tishi();
stu *head=(stu*)malloc(sizeof(stu));
head->next=NULL;
int saveTag=0;
while(scanf("%d",&nn)!=-1)
{        if(nn==10){printf("即将退出\n");system("pause");break;}
if(nn>10||nn<1){printf("输入错误,请重新输入\n");system("pause");}
switch(nn)
{
case 1:
{   int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
head=zengjia(head);
stu *p=head;
while(1)//找到链表的尾部,若名次是0,则排序赋名次
{
p=p->next;
if(p->next==NULL)break;
}
if(p->mingci==0)
{
sortjiang(head);
fumingci(head);
}
break;
}

case 2:
{
break;
}

case 3:
{   stu *p=head;

output(head);
break;
}

case 4:
{
break;
}

case 5:
{
printf("请输入删除方式:\n0表示按学号,1表示按姓名,2表示按名次\n");
int n;
scanf("%d",&n);
if(n==0)
{
int mm;
scanf("%d",&mm);
head=shanchunum(head,mm);
if(flag==0)printf("删除失败!\n");
if(flag==1){printf("删除成功!\n");sortjiang(head);fumingci(head);}
}
else if(n==1)
{   getchar();
char mm[20];
gets(mm);
head=shanchuxingming(head,mm);
if(flag==0)printf("删除失败!\n");
if(flag==1){printf("删除成功!\n");sortjiang(head);fumingci(head);}
}
else if(n==2)
{
int mm;
scanf("%d",&mm);
head=shanchumingci(head,mm);
if(flag==0)printf("删除失败!\n");
if(flag==1){printf("删除成功!\n");sortjiang(head);fumingci(head);}
}
if(head->next==NULL)printf("链表以空!\n");
break;
}

case 6:
{

printf("请输入要排序的方式:\n(0表示升序,1表示降序)\n");
int n,m;
scanf("%d",&n);
printf("请输入排序的标准:\n1表示按学号排序\n2表示按名字排序\n3表示按名次排序\n");
scanf("%d",&m);
if(n==1)
switch(m)
{
case 1:{jiangxuxuehao(head);break;}
case 2:{jiangxuxingming(head);break;}
case 3:{jiangxumingci(head);break;}
}
else if(n==0)
switch(m)
{
case 1:{shengxuxuehao(head);break;}
case 2:{shengxuxingming(head);break;}
case 3:{shengxumingci(head);break;}
}

else if(n!=1&&n!=0)break;
break;
}

case 7:
{   int t;
t=savefile(head);
if(t==0){printf("存储成功!!!\n"); saveTag=1;}
if(t==-1){printf("存储失败!!!\n");saveTag=0;}

break;
}

case 8:
{
break;
}

case 9:
{

FILE *fp;
fp=fopen("E:\\计一高鹏专用\\学生管理系统\\student.txt","w");
fclose(fp);
printf("创建成功!\n");
break;
}

}

tishi();
}

return 0;
}


这是排序函数的(不光排序,啥都有):

如何建立?codeblocks:File->new->Files->c/c++source然后起一个名字(这里叫paixu)

#include <stdio.
#include <stdlib.h>
#include "paixu.h"
#include <string.h>

int flag;
void tishi()
{
printf("******************************************\n");
printf("*  请输入操作类型:                      *\n");
printf("*      1表示增加学生信息;                *\n");
printf("*      2表示修改学生信息;                *\n");
printf("*      3表示显示学生信息;                *\n");
printf("*      4表示查询学生信息;                *\n");
printf("*      5表示删除学生信息;                *\n");
printf("*      6表示对学生信息进行排序;          *\n");
printf("*      7表示保存学生信息至记录文件;      *\n");
printf("*      8表示从记录文件读取学生信息;      *\n");
printf("*      9表示新建学生信息文件;            *\n");
printf("*     10表示运行结束;                    *\n");
printf("******************************************\n");

}

void sortjiang(stu *head)//降序排列
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->sum>max->sum)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
}
}
}

void jiangxumingci(stu *head)//降序排列名次
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->mingci>max->mingci)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void jiangxuxuehao(stu *head)//降序排列学号
{
stu temp;

4000
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->num>max->num)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void jiangxuxingming(stu *head)//降序排列姓名
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(strcmp(q->name,max->name)>0)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void shengxumingci(stu *head)//升序排列名次
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->mingci<max->mingci)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void shengxuxuehao(stu *head)//升序排列学号
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->num<max->num)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void shengxuxingming(stu *head)//升序排列姓名
{
stu temp;
stu *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(strcmp(q->name,max->name)<0)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.num=p->num;
strcpy(temp.name,p->name);
temp.sex=p->sex;
temp.s[0]=p->s[0];
temp.s[1]=p->s[1];
temp.s[2]=p->s[2];
temp.s[3]=p->s[3];
temp.s[4]=p->s[4];
temp.aver=p->aver;
temp.sum=p->sum;
temp.mingci=p->mingci;

p->num=max->num;
strcpy(p->name,max->name);
p->sex=max->sex;
p->s[0]=max->s[0];
p->s[1]=max->s[1];
p->s[2]=max->s[2];
p->s[3]=max->s[3];
p->s[4]=max->s[4];
p->aver=max->aver;
p->sum=max->sum;
p->mingci=max->mingci;

max->num=temp.num;
strcpy(max->name,temp.name);
max->sex=temp.sex;
max->s[0]=temp.s[0];
max->s[1]=temp.s[1];
max->s[2]=temp.s[2];
max->s[3]=temp.s[3];
max->s[4]=temp.s[4];
max->aver=temp.aver;
max->sum=temp.sum;
max->mingci=temp.mingci;
}
}
}

void input(stu *p)//输入函数,只输入一组数据
{
char tmp[20];
printf("请输入学生的学号:\n");
scanf("%s",tmp);
getchar();
p->num=atoi(tmp);
printf("请输入学生的姓名:\n");
gets(p->name);
printf("请输入学生的性别:\n");
scanf("%c",&p->sex);
printf("请输入学生五门课的成绩:\n");
scanf("%lf%lf%lf%lf%lf",&p->s[0],&p->s[1],&p->s[2],&p->s[3],&p->s[4]);
getchar();
p->sum=p->s[0]+p->s[1]+p->s[2]+p->s[3]+p->s[4];
p->aver=p->sum/5;
p->mingci=0;
}

void output(stu *head)//输出函数
{
stu *p=head;
printf("学号   姓名   性别  成绩1  成绩2  成绩3  成绩4  成绩5  平均分  总分  名次\n");
while(1)
{   p=p->next;
printf("%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",p->num,p->name,p->sex,p->s[0],p->s[1],p->s[2],p->s[3],p->s[4],p->aver,p->sum,p->mingci);

if(p->next==NULL)
break;
}
}

int savefile(stu *head)
{
int ans=-1,t;
FILE *fp;
fp=fopen("E:\\计一高鹏专用\\学生管理系统\\student.txt","w+");
stu *p=head->next;
while(p!=NULL)
{
t=fprintf(fp,"%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",p->num,p->name,p->sex,p->s[0],p->s[1],p->s[2],p->s[3],p->s[4],p->aver,p->sum,p->mingci);
ans+=t;
p=p->next;
}
fclose(fp);
if(ans>0)ans=0;
return ans;
}
void fumingci(stu *head)//赋名次函数,使用之前按降序排列
{
stu *p=head;
int i=1;
while(1)
{
p=p->next;
p->mingci=i++;
if(p->next==NULL)break;
}
}

stu * zengjia(stu *head)//增加函数,在链表的尾部增加数据
{
stu *p=head,*w=head;
while(1)
{
if(p->next==NULL)break;
p=p->next;
}
stu *q=(stu *)malloc(sizeof(stu));
p->next=q;
input(q);
q->next=NULL;
return w;
}

stu *shanchunum(stu *head,int t)//删除函数按学号
{   flag=0;
stu *p=head->next,*q,*ans=head;
if(head->next->num==t)
{ans=p;flag=1;}
else
for(p=head->next;p->next!=NULL;p=p->next)
{
if(p->next->num==t)
{   flag=1;
q=p->next->next;
p->next=q;
break;

}

}return ans;
}

stu *shanchumingci(stu *head,int t)//删除函数按学号
{   flag=0;
stu *p=head->next,*q,*ans=head;
if(head->next->mingci==t)
{ans=p;flag=1;}
else
for(p=head->next;p->next!=NULL;p=p->next)
{
if(p->next->mingci==t)
{   flag=1;
q=p->next->next;
p->next=q;
break;

}

}return ans;
}

stu *shanchuxingming(stu *head,char s[])//删除函数按学号
{   flag=0;
stu *p=head->next,*q,*ans=head;
if(strcmp(head->next->name,s)==0)
{ans=p;flag=1;}
else
for(p=head->next;p->next!=NULL;p=p->next)
{
if(strcmp(head->next->name,s)==0)
{   flag=1;
q=p->next->next;
p->next=q;
break;
}

}return ans;
}


这是headers里声明paixu.h 的:

如何建立?codeblocks:File->new->Files->c/c++header起个名字(paixu.h)

#ifndef PAIXU_H_INCLUDED
#define PAIXU_H_INCLUDED

typedef struct su
{
int num;
char name[20];
char sex;
double s[5];
double aver;
double sum;
int mingci;
struct su *next;
}stu;
void tishi();
void sortjiang(stu *head);
void jiangxusum(stu *head);
void jiangxumingci(stu *head);
void jiangxuxuehao(stu *head);
void jiangxuxingming(stu *head);
void shengxumingci(stu *head);
void shengxuxuehao(stu *head);
void shengxuxingming(stu *head);
void input(stu *p);
void output(stu *head);
stu * zengjia(stu *head);
void fumingci(stu *head);
int savefile(stu *head);
stu *shanchunum(stu *head,int t);
stu *shanchumingci(stu *head,int t);
stu *shanchuxingming(stu *head,char s[]);

#endif // PAIXU_H_INCLUDED
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: