📄 link.c
字号:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <malloc.h>
#include "FuncDeclare.h"
#define LEN sizeof(student)
extern inforsave;
extern Sno_count;
extern student *head,*last;
extern sign[100][2];
//插入最后
int InsertLinkByLast(student **temp,student *data)
{
student *p=(student *)malloc(LEN);
if(p==NULL)//没开辟成功
return 0;
memcpy(p,data,LEN);
p->next=NULL;
if(*temp==NULL)
*temp=p;
else
last->next=p;
last=p;
return 1;
}
//得到链表的长度
int GetLinkLen(student *link)
{
int i=0;
student *temp=link;
while(temp)
{
i++;
temp=temp->next;
}
return i;
}
//初始化链表
int InitLink(student data[],int num)
{
int i;
for(i=0;i<num;i++)
{
InsertLinkByLast(&head,&data[i]);
}
return num;
}
//释放整个链表
int DeletLink(student *temp)
{
student *pt;
while(pt)
{
pt=temp;
temp=temp->next;
free(pt);
}
return 0;
}
//增加学生记录
student *InsertStuInfo(student *head,student *stu)
{
student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while((strcmp(p0->sno,p1->sno) > 0) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p0->sno,p1->sno)<=0)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
//按学号删除
int DeleteLinkBySno(student **pt,char sno[])
{
student *temp=NULL,*p=NULL;
if(*pt==NULL)
return 0;
temp=*pt;
while( strcmp(temp->sno,sno)!=0 && temp->next!=NULL)
{
p=temp;
temp=temp->next;
}
if(temp==*pt)
*pt=temp->next;
else
p->next=temp->next;
free(temp);
return 1;
}
//找学号,返回目标学号指针位置
student *FindSno(student *pt,char sno[])
{
student *temp=NULL;
temp=pt;
while(temp)
{
if(strcmp(temp->sno,sno)==0)
return temp;
temp=temp->next;
}
return NULL;
}
//验证学号合法性
void CheckSnoRight(int xcursor,int ycursor,char sno[])
{
int select;
CleaseMessage();//清除信息框内容
SetXY(xcursor,ycursor);
printf("please input Sno:");
do
{
SetXY(xcursor+18,ycursor);
input(sno,3);
select=SnoErrorTip(sno,xcursor+18,ycursor);
if(select==0)
{
break;
}
}while(1);
return;
}
//判断学号是否存在,1是直接查找学号
int IsSnoExist(student *pt,char sno[],int flag,int xcursor,int ycursor)
{
if(pt==NULL)
{
CleaseMessage();//清除信息框内容
if(flag==1)
{
SetXY(1,19);
printf("Sno %s don't Exist!...Enter any key to continue",sno);
getch();
return -1;
}
else if(flag==2)
{
SetXY(1,19);
printf("Sno %s don't Exist!...Enter any key to continue",sno);
getch();
ClearScreen(xcursor,ycursor,78,ycursor);//清除输入的
SetXY(xcursor,ycursor);
return 0;
}
}
return 1;
}
//打印学号查询结果
int PrintSearchSno(student *pt,int count)
{
int value;
CleanMenuInfo();
value=PrintStu(&pt,count,1,"Search Result",3);
return value;
}
//删除某个学号对应的结点
int DealtDel(student **pt,char sno[],int flag)
{
int makesno;
if(flag==2)
DeleteLinkBySno(pt,sno);
DeleteLinkBySno(&head,sno);
Sno_count--;
makesno=atoi(sno);
sign[makesno-1][1]=0;
CleaseMessage();//清除信息框内容
SetXY(1,19);
if(flag==1)
{
printf("Delete %s Successfully! Enter any key to back...",sno);
getch();
return 0;
}
else if(flag==2)
{
return 1;
}
return 2;
}
//封装查询学号函数
int SearchSnoResult(student **pt,int xcursor,int ycursor,int flag)
{
int del;
char sno[25];//存放姓名
student *temp=NULL;
int searchsno;
int snoexist;
CheckSnoRight(xcursor,ycursor,sno);//验证学号合法性
temp=FindSno(*pt,sno);//没找到返回NULL
snoexist=IsSnoExist(temp,sno,flag,xcursor,ycursor);
if(snoexist==-1)
return 1;//学号不存在
searchsno=PrintSearchSno(temp,1);
if(searchsno==6)
{
del=DealtDel(pt,sno,flag);
if(del==0)
return 1;
}
if(searchsno==0)
return 1;
return 0;
}
//学号查询最终调用
int SearchSno_Result(student **pt,int xcursor,int ycursor,int flag)
{
int value;
value=SearchSnoResult(pt,xcursor, ycursor,flag);
if(value==1)
{
CleanMenuInfo();//清除菜单名称,菜单项,信息框
//SearchStuInfo();
return 1;
}
return 0;
}
//按姓名查找
student *FindName(char name[])
{
student *temp=NULL;//新的链表
student *pt;
student *phead=head;
if(head==NULL)
{
temp=NULL;
return temp;
}
else
{
do
{
if(strstr(strlwr(phead->name),strlwr(name))!=NULL)
{
pt=(student *)malloc(LEN);
memcpy(pt,phead,LEN);
temp=InsertStuInfo(temp,pt);
}
phead=phead->next;
}while(phead!=NULL);
}
return temp;
}
//验证姓名输入是否合法
int CheckSearchName(char name[])
{
int select;
SetXY(24,9);
printf("please input Name:");
SetXY(42,9);
//合法验证
do
{
SetXY(42,9);
input(name,25);
select=NameErrorTip(name,42,9,2);
if(select==0)
break;
}while(1);
return 0;
}
//判断姓名是否存在
int IsNameExist(student *temp,char name[])
{
//执行查找
if(temp==NULL)//没找到处理
{
CleaseMessage();//清除信息框内容
SetXY(1,19);
printf("Name: %s don't Exist!...Enter any key to continue",name);
getch();
return 0;
}
else
return 1;
}
//打印满足查询的姓名
int PrintSearchSName(student *pt,int count)
{
int value;
CleanMenuInfo();
value=PrintStu(&pt,count,1,"Search Result",3);
return value;
}
//封装查询姓名函数
int SearchNameResult(student **pt,int xcursor,int ycursor,int flag)
{
student *temp=NULL;
char name[25];//存放姓名
char sno[5];
int searchname;
int linklen;//求链表temp长度
int snoexist;
int nameexist;
int del;//判断是否删除成功
CheckSearchName(name);//判断姓名是否存在
temp=FindName(name);//没找到返回NULL
nameexist=IsNameExist(temp,name);
if(nameexist==0)
return 0;//跳转到SearchStuInfo()
linklen=GetLinkLen(temp);//得到链表的长度
do
{
searchname=PrintSearchSno(temp,linklen);
if(searchname==6)
{
CheckSnoRight(40,17,sno);//验证学号合法性
//判断学号是否存在,1是直接查找学号
snoexist=IsSnoExist(FindSno(temp,sno),sno,2,40,17);
if(snoexist==1)
{
del=DealtDel(&temp,sno,2);
linklen--;
if(linklen==0)
{
printf("Del %s successfully!...",sno);
getch();
CleanMenuInfo();
break;
}
if(del==1)
{
printf("Del %s successfully!...",sno);
getch();
}
}
}
else if(searchname==0)
{
return 0;
}
}while(1);
return 1;
}
//按姓名查询最终处理
int SearchName_Result(student **pt,int xcursor,int ycursor,int flag)
{
int value;
value=SearchNameResult(pt,xcursor,ycursor,flag);
if(value==0)
{
CleanMenuInfo();
//SearchStuInfo();//查询学生信息界面
return 1;
}
return 0;
}
/*求各科目平均分
subject科目代号,比如0代表语文
count为学生人数
*/
float SubjecAverage(student *temp,int subject,int count)
{
float subjectavg=0;
float subjecttotal=0;
int i=0;
if(temp==NULL)
{
subjectavg=0;
}
else
{
while(i<count)
{
subjecttotal+=temp->score[subject];
temp=temp->next;
i++;
}
subjectavg=subjecttotal/count;
}
return subjectavg;
}
/*按科目降序排列
count表示人数
*/
void SubjectDesc(student *temp,int subject,int count)
{
int i=0;
int j;
student *p1,*p2;
char sno[5];
char name[25];
int age;
float score;//临时变量
p1=temp;
if(p1->next!=NULL)
{
for(i=0;i<count;i++)
{
p1=head;
do
{
p2=p1->next;//p2是后一个结点
if(p2->score[subject]>p1->score[subject])
{
strcpy(sno,p1->sno);
strcpy(p1->sno,p2->sno);
strcpy(p2->sno,sno);
strcpy(name,p1->name);
strcpy(p1->name,p2->name);
strcpy(p2->name,name);
age=p1->age;
p1->age=p2->age;
p2->age=age;
for(j=0;j<5;j++)
{
score=0;
score=p1->score[j];
p1->score[j]=p2->score[j];
p2->score[j]=score;
}
}
p1=p2;
}while(p2->next!=NULL);
}
}
}
//求科目前三甲
int SubjectTopThree(student *temp,int subject,int count,char str[])
{
int linklen;//pnew链表长度
int value;
int i=0;
student *pnew=NULL;
student *px=NULL,*py=NULL,*pz=NULL,*pt=NULL;
px=temp;
SubjectDesc(temp,subject,count);
if(count<=3)
{
while(i<count)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
}
else
{
while(i<3)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
i=0;
px=NULL;
px=temp;
//px指向第三个结点
while(i<2)
{
px=px->next;
i++;
}
py=px->next;
while(py)
{
if(py->score[subject]==px->score[subject])
{
InsertLinkByLast(&pnew,py);
px=py;
}
else
break;
py=px->next;
}
}
linklen=GetLinkLen(pnew);
value=PrintStu(&pnew,linklen,2,str,0);
if(value==0)
return 0;
if(value==6)
return 1;
return 2;
}
//科目前三甲
int TopThree_subject(student *temp,int subject,int count,char str[])
{
int value;
value=SubjectTopThree(temp,subject,count,str);
if(value==0)
{
return 0;
}
else if(value==1)
{
return 1;
}
return 2;
}
//求总分前三甲
int ScoreTopThree(student *temp,int count,char str[])
{
int linklen;
int value;
int i=0;
float score1;
float score2;
student *pnew=NULL;
student *px=NULL,*py=NULL,*pz=NULL,*pt=NULL;
px=temp;
ScoreSortDesc(temp, count,2);//总分降序
if(count<=3)
{
while(i<count)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
}
else
{
while(i<3)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
i=0;
px=NULL;
px=temp;
//px指向第三个结点
while(i<2)
{
px=px->next;
i++;
}
py=px->next;
while(py)
{
score1=0;
score2=0;
score1=Totalscore(px,5);
score2=Totalscore(py,5);
if(score2==score1)
{
InsertLinkByLast(&pnew,py);
px=py;
}
else
break;
py=px->next;
}
}
linklen=GetLinkLen(pnew);
value=PrintStu(&pnew,linklen,2,str,0);
if(value==0)
return 0;
if(value==6)
return 1;
return 2;
}
//总分前三甲
int TopThree_score(student *temp,int count,char str[])
{
int value;
value=ScoreTopThree(temp,count,str);
if(value==0)
{
return 0;
}
else if(value==1)
return 1;
return 2;
}
//求平均分前三甲
int AvgTopThree(student *temp,int count,char str[])
{
int linklen;
int value;
int i=0;
float score1;
float score2;
student *pnew=NULL;
student *px=NULL,*py=NULL,*pz=NULL,*pt=NULL;
px=temp;
AvgSortDesc(temp,count,2);//总分降序
if(count<=3)
{
while(i<count)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
}
else
{
while(i<3)
{
InsertLinkByLast(&pnew,px);
px=px->next;
i++;
}
i=0;
px=NULL;
px=temp;
//px指向第三个结点
while(i<2)
{
px=px->next;
i++;
}
py=px->next;
while(py)
{
score1=0;
score2=0;
score1=Totalscore(px,5);
score2=Totalscore(py,5);
if(score2==score1)
{
InsertLinkByLast(&pnew,py);
px=py;
}
else
break;
py=px->next;
}
}
linklen=GetLinkLen(pnew);
value=PrintStu(&pnew,linklen,2,str,0);
if(value==0)
return 0;
if(value==6)
return 1;
return 2;
}
//平均分前三甲
int TopThree_avg(student *temp,int count,char str[])
{
int value;
value=AvgTopThree(temp,count, str);
if(value==0)
{
return 0;
}
else if(value==1)
return 1;
return 2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -