给大三的学长做的小东西了,呵呵,说是一个系统我自己都惭愧,基本上除了添加删除排序以外没有其他的功能,而且还没有进行异常处理。
根本没有任何技术性的东西,只是练习了下单链表的应用,基本的冒泡排序而已。
简单介绍下程序吧
程序功能:
实现简单的学生成绩表的建立,插入,索引,排序,输出,存储,读入功能;
基本每个模块都有对应的函数,排序和索引使用冒泡排序。
制表使用制表符。程序注释已经比较完备,请大家自己参阅下吧。
//
C语言 学生信息管理系统 v1.0.0 beta版

//
东北大学秦皇岛分校

//
2007-9-6

//
版权没有 盗版不究

//
环境:Dev C++ Windows XP SP2 调试通过


//
包含的头文件部分
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
malloc.h
>
#include
<
string
.h
>

//
定义学生数据结构体
struct
sctStudent

...
{
int iNum;//学号

char cName[10];//姓名

float fLine;//线代成绩

float fEnglish;//英语成绩

float fMaths;//高数成绩

float fComputer;//计导成绩

float fScore;//总成绩

struct sctStudent *next;
}
;

//
函数声明部分
void
vNewPause(
void
);
void
vOutCaption(
void
);
void
vOutSpace(
int
);
void
vOutTableLineA(
int
);
void
vOutTableHead(
void
);
void
vShowLink(
struct
sctStudent
*
);

int
iFindLink(
struct
sctStudent
*
);
int
iMainMenu(
void
);
int
iSaveLink(
struct
sctStudent
*
);

struct
sctStudent
*
psctDeleteLink(
struct
sctStudent
*
);
struct
sctStudent
*
psctGreatLink(
void
);
struct
sctStudent
*
psctIndexLink(
struct
sctStudent
*
,
int
);
struct
sctStudent
*
psctLoadLink(
void
);
struct
sctStudent
*
psctSortLink(
struct
sctStudent
*
);

//
清空输入流并无回显暂停等待回车
void
vNewPause(
void
)

...
{
int c;
while ( (c = getchar()) != ' ' && c != EOF )
clearerr(stdin);
getchar();
}

//
该函数用于输出n个空格
void
vOutSpace(
int
n)

...
{
int i;
for(i=1;i<=n;i++)printf(" ");
}

//
输出首行或尾行制表符
void
vOutTableLineA(
int
Judge)

...
{
int i;
if(Judge==0)printf(" ┏");
else printf(" ┗");
for(i=1;i<=37;i++)

...{
if((i%5==0)&&(i!=35))

...{
if(Judge==0)printf("┳");
if(Judge==1)printf("┻");
}
else printf("━");
};
if(Judge==0)printf("┓ ");
else printf("┛ ");
}

//
该函数用于输出格式头
void
vOutTableHead(
void
)

...
{
vOutTableLineA(0);
printf(" ┃学号 ┃姓名 ┃线代 ┃英语 ┃高数 ┃计导 ┃总分 ┃");
}
//
该函数用于输出程序标题
void
vOutCaption(
void
)

...
{
int i;
//以下用制表符制表

printf("┏");
for(i=1;i<=38;i++)printf("━");
printf("┓┃");
vOutSpace(26);
printf("学 生 成 绩 管 理 系 统 v1.0.0");
vOutSpace(20);
printf("┃");
printf("┗");
for(i=1;i<=38;i++)printf("━");
printf("┛");
}

//
该函数用于绘制主界面
int
iMainMenu(
void
)

...
{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(32);
printf("1.输入学生数据 ");
vOutSpace(32);
printf("2.查询学生数据 ");
vOutSpace(32);
printf("3.统计学生数据 ");
vOutSpace(32);
printf("4.排序学生数据 ");
vOutSpace(32);
printf("5.删除学生数据 ");
vOutSpace(32);
printf("6.存储学生数据 ");
vOutSpace(32);
printf("7.读取学生数据 ");
vOutSpace(32);
printf("0.退出本次实例 ");
printf(" ");
vOutSpace(31);
printf("请输入您的选项:");
int temp;
fflush(stdin);
scanf("%d",&temp);
return (temp);
}

//
该函数用于建立单向链表并赋值
struct
sctStudent
*
psctGreatLink(
void
)

...
{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("请输入学生成绩,以学号0结束 ");
vOutSpace(32);
struct sctStudent *head,*temp1,*temp2,*temp3;
head=NULL;
temp1=(struct sctStudent *)malloc(sizeof(struct sctStudent));
printf("请输入学生学号:");
fflush(stdin);
scanf("%d",&temp1->iNum);
if(temp1->iNum!=0)

...{
vOutSpace(32);
printf("请输入学生姓名:");
scanf("%s",temp1->cName);
vOutSpace(32);
printf("请输入线代成绩:");
scanf("%f",&temp1->fLine);
vOutSpace(32);
printf("请输入英语成绩:");
scanf("%f",&temp1->fEnglish);
vOutSpace(32);
printf("请输入高数成绩:");
scanf("%f",&temp1->fMaths);
vOutSpace(32);
printf("请输入计导成绩:");
scanf("%f",&temp1->fComputer);
temp1->fScore=temp1->fLine+temp1->fEnglish+temp1->fMaths+temp1->fComputer;
while(temp1->iNum!=0)

...{
if(head==NULL)head=temp1;
temp2=(struct sctStudent *)malloc(sizeof(struct sctStudent));
temp1->next=temp2;//前一节点的后继指针指向新开辟的节点

temp3=temp1;//保留住前一节点的指针

temp1=temp2;//方便后面的循环

system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("请输入学生成绩,以学号0结束 ");
vOutSpace(32);
printf("请输入学生学号:");
scanf("%d",&temp1->iNum);
if(temp1->iNum!=0)

...{
vOutSpace(32);
printf("请输入学生姓名:");
scanf("%s",temp1->cName);
vOutSpace(32);
printf("请输入线代成绩:");
scanf("%f",&temp1->fLine);
vOutSpace(32);
printf("请输入英语成绩:");
scanf("%f",&temp1->fEnglish);
vOutSpace(32);
printf("请输入高数成绩:");
scanf("%f",&temp1->fMaths);
vOutSpace(32);
printf("请输入计导成绩:");
scanf("%f",&temp1->fComputer);
temp1->fScore=temp1->fLine+temp1->fEnglish+temp1->fMaths+temp1->fComputer;
}
else

...{
temp3->next=NULL;//前节点的后继指针为空 ,链表结束于前节点

}
}
head=psctIndexLink(head,0);//直接对输入链表进行排序

}
free(temp1);//释放新开辟的未用节点,节省内存开销

return head;
}

//
该函数用于查找指定学号的学生成绩
int
iFindLink(
struct
sctStudent
*
temp)

...
{
int iSearchNum;
system("cls");
vOutCaption();
printf(" ");
if(temp!=NULL)

...{
vOutSpace(23);
printf("请在下面输入学生学号,输入0返回 ");
vOutSpace(32);
printf("请输入查询学号:");
fflush(stdin);
scanf("%d",&iSearchNum);
while((temp->iNum!=iSearchNum)&&(temp->next!=NULL))

...{
temp=(temp->next);
}
if(temp->iNum==iSearchNum)

...{
system("cls");
vOutCaption();
printf(" ");
vOutTableHead();
printf(" ");
printf("┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf(" ");
vOutTableLineA(1);
printf(" ");
vOutSpace(34);
printf("按回车键返回");
vNewPause();
return 0;
}
else

...{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(23);
printf("未能找到该笔数据,按回车键返回 ");
vOutSpace(23);
vNewPause();
return 1;
}
}
else

...{
vOutSpace(23);
printf("尚未创建或读取列表,按回车键返回");
vNewPause();
return 1;
}
}

//
用于向屏幕输出显示链表
void
vShowLink(
struct
sctStudent
*
temp)

...
{
system("cls");
vOutCaption();
printf(" ");
vOutTableHead();
printf(" ");
while(temp!=NULL)

...{
printf(" ┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf(" ");
temp=temp->next;
}
vOutTableLineA(1);
printf(" ");
vOutSpace(34);
printf("按回车键返回");
vNewPause();
}

//
该函数用于统计学生成绩并显示之
struct
sctStudent
*
psctIndexLink(
struct
sctStudent
*
h,
int
iJudge)

...
{
struct sctStudent *endpt,*u,*v,*p;
u=(struct sctStudent *)malloc(sizeof(struct sctStudent));
u->next=h;
h=u;
for(endpt=NULL;endpt!=h;endpt=p)
for(p=u=h;u->next->next!=endpt;u=u->next)
if(u->next->iNum > u->next->next->iNum)

...{
v= u->next->next;
u->next->next=v->next;
v->next=u->next;
u->next=v;
p=u->next->next;
}
u=h;
h=h->next;
free(u);
if(iJudge==1)vShowLink(h) ;
return (h);
}
//
该函数用于排序学生成绩 根据单向链表的特点 采用冒泡法
struct
sctStudent
*
psctSortLink(
struct
sctStudent
*
h)

...
{
struct sctStudent *endpt,*u,*v,*p;
u =(struct sctStudent*)malloc(sizeof(struct sctStudent));
u->next=h;
h = u;
for(endpt=NULL;endpt!=h;endpt=p)
for(p=u=h;u->next->next!=endpt;u=u->next)
if(u->next->fScore < u->next->next->fScore)

...{ /**//* 两相邻结点比较 */
v = u->next->next;
u->next->next = v->next;
v->next = u->next;
u->next = v;
p = u->next->next;
}
u = h;
h = h->next;
free(u);
vShowLink(h);
return h;
}
//
该函数用于删除学生成绩
struct
sctStudent
*
psctDeleteLink(
struct
sctStudent
*
temp)

...
{
struct sctStudent *psctSaveHead=temp,*psctNewTemp;
int iSearchNum;
char cJudge;
system("cls");
vOutCaption();
printf(" ");
if(temp!=NULL)

...{
vOutSpace(23);
printf("请在下面输入学生学号,输入0返回 ");
vOutSpace(32);
printf("请输入删除的学号:");
fflush(stdin);
scanf("%d",&iSearchNum);
if(iSearchNum==0)return (temp);
while((temp->iNum!=iSearchNum)&&(temp->next!=NULL))

...{
psctNewTemp=temp;
temp=(temp->next);
}
if(temp->iNum==iSearchNum)

...{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(4);
printf("您要删除的数据是: ");
vOutTableHead();
printf(" ");
printf("┃%-4d ┃",temp->iNum);
printf("%-6.6s ┃",temp->cName);
printf("%-4.1f ┃",temp->fLine);
printf("%-4.1f ┃",temp->fEnglish);
printf("%-4.1f ┃",temp->fMaths);
printf("%-4.1f ┃",temp->fComputer);
printf("%-3.2f ┃",temp->fScore);
printf(" ");
vOutTableLineA(1);
printf(" ");
vOutSpace(34);
printf("确定删除(y/n)?");
fflush(stdin);
scanf("%c",&cJudge);
if(cJudge=='y'||cJudge=='Y')

...{
if(temp==psctSaveHead)

...{
temp=psctSaveHead;
psctSaveHead=(psctSaveHead->next);
free(temp);
}
else
if(temp->next==NULL)

...{
psctNewTemp->next=NULL;
free(temp);
}
else

...{
psctNewTemp->next=temp->next;
free(temp);
}
return (psctSaveHead);
}
else
return (psctSaveHead);
}
else

...{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(23);
printf("未能找到该笔数据,按回车键返回 ");
vOutSpace(23);
vNewPause();
return (psctSaveHead);
}
}
else

...{
vOutSpace(23);
printf("尚未创建或读取列表,按回车键返回");
vNewPause();
return (psctSaveHead);
}
}
//
该函数用于存储学生成绩到文件
int
iSaveLink(
struct
sctStudent
*
temp)

...
{
FILE *pfSaveFile;
char cDir[100];
system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("请输入保存文件的路径: ");
vOutSpace(26);
scanf("%s",cDir);
if(strcmp(cDir,"0")==0)return (0);
if((pfSaveFile=fopen(cDir,"wt"))==NULL)

...{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("无法打开该文件! ");
}
else

...{
while(temp!=NULL)

...{
fprintf(pfSaveFile,"%d %s %f %f %f %f %f ",
temp->iNum,
temp->cName,
temp->fLine,
temp->fEnglish,
temp->fMaths,
temp->fComputer,
temp->fScore);
temp=temp->next;
}
fclose(pfSaveFile);
}
}
//
该函数用于从文件读取学生成绩
struct
sctStudent
*
psctLoadLink(
void
)

...
{
FILE *pfLoadFile;
char cDir[100];
system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("请输入需要读取文件的路径: ");
vOutSpace(26);
scanf("%s",cDir);
if(strcmp(cDir,"0")==0)return (0);
if((pfLoadFile=fopen(cDir,"rt"))==NULL)

...{
system("cls");
vOutCaption();
printf(" ");
vOutSpace(26);
printf("无法打开该文件! ");
vOutSpace(26);
printf("请按回车键返回!");
vNewPause();
}
else

...{
struct sctStudent *head,*temp1,*temp2;
head=NULL;
if(!feof(pfLoadFile))

...{
temp1=(struct sctStudent *)malloc(sizeof(struct sctStudent));
fscanf(pfLoadFile,"%d %s %f %f %f %f %f ",
&(temp1->iNum),
(temp1->cName),
&(temp1->fLine),
&(temp1->fEnglish),
&(temp1->fMaths),
&(temp1->fComputer),
&(temp1->fScore));
while(!feof(pfLoadFile))

...{
if(head==NULL)head=temp1;
temp2=(struct sctStudent *)malloc(sizeof(struct sctStudent));
temp1->next=temp2;//前一节点的后继指针指向新开辟的节点

temp1=temp2;//方便后面的循环

fscanf(pfLoadFile,"%d %s %f %f %f %f %f ",
&(temp1->iNum),
(temp1->cName),
&(temp1->fLine),
&(temp1->fEnglish),
&(temp1->fMaths),
&(temp1->fComputer),
&(temp1->fScore));
}
temp1->next=NULL;
fclose(pfLoadFile);
return head;
}
else

...{
fclose(pfLoadFile);
return NULL;
}
}
}
//
程序总入口点
int
main(
void
)

...
{
int iJudge=1,t=1;
struct sctStudent *temp;
temp=NULL;
while(1)

...{
switch(iMainMenu())

...{
case 1:
temp=psctGreatLink();break;
case 2:
iFindLink(temp);break;
case 3:
temp=psctIndexLink(temp,1);break;
case 4:
temp=psctSortLink(temp);break;
case 5:
temp=psctDeleteLink(temp);break;
case 6:
iSaveLink(temp);break;
case 7:
temp=psctLoadLink();break;
case 0:
exit(0);
default:
break;
}
}
system("pause");
return 0;
}