|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct Teacher
{
char *tname;
char **stu;
int age;
}Teacher;
int creatTeacher(Teacher **Tp,int nt,int nstu)
{
int ret=0;
if (NULL==Tp || 0==nt || 0==nstu)
{
ret=-1;
return ret;
}
{
Teacher *temp=NULL;
int i,j;
temp=(Teacher *)malloc(sizeof(Teacher)*nt);
if (!temp)
{
ret=-2;
goto END;
}
for(i=0;i<nt;i++)
{
temp[i].tname=(char *)malloc(sizeof(char)*20);
if (!temp[i].tname)
{
ret=-3;
goto END;
}
}
for (i=0;i<nt;i++)
{
temp[i].stu=(char **)malloc(sizeof(char *)*nt);
if (!temp[i].stu)
{
ret=-4;
goto END;
}
for (j=0;j<nstu;j++)
{
temp[i].stu[j]=(char *)malloc(sizeof(char)*20);
if (!temp[i].stu[j])
{
ret=-5;
goto END;
}
}
}
END:
if (ret!=0)
{
if (temp)
{
for (i=0;i<nt;i++)
{
if (temp[i].tname)
{
free(temp[i].tname);
temp[i].tname=NULL;
}
for (j=0;j<nstu;j++)
{
if (temp[i].stu[j])
{
free(temp[i].stu[j]);
temp[i].stu[j];
}
}
free(temp);
}
}
}
*Tp=temp;
return ret;
}
}
void initTeacher(Teacher *Tp,int nt,int nstu)
{
if (NULL==Tp || 0==nt || 0==nstu)
{
return;
}
{
int i,j;
for (i=0;i<nt;i++)
{
printf("请输入第%d个导师的名字:",i+1);
scanf("%s",Tp[i].tname);
for(j=0;j<nstu;j++)
{
printf("请输入第%d个导师下的第%d个学生:",i+1,j+1);
scanf("%s",Tp[i].stu[j]);
}
printf("请输入第%d个导师的年龄:",i+1);
scanf("%d",&Tp[i].age);
}
return;
}
}
void sortTeacher(Teacher *Tp,int nt)
{
if (NULL==Tp || 0==nt)
{
return;
}
{
Teacher temp;
int i,j;
for (i=0;i<nt;i++)
{
for (j=i+1;j<nt;j++)
{
if (strcmp(Tp[i].tname,Tp[j].tname)<0)
{
temp=Tp[i];
Tp[i]=Tp[j];
Tp[j]=temp;
}
}
}
return;
}
}
void freeTeacher(Teacher **Tp,int nt,int nstu)
{
if (NULL==Tp || 0==nt || 0==nstu)
{
return;
}
{
Teacher *p=*Tp;
int i,j;
for (i=0;i<nt;i++)
{
if (p[i].tname)
{
free(p[i].tname);
p[i].tname=NULL;
}
for(j=0;j<nstu;j++)
{
if (p[i].stu[j])
{
free(p[i].stu[j]);
p[i].stu[j]=NULL;
}
}
if(p[i].stu)
{
free(p[i].stu);
p[i].stu=NULL;
}
}
if (p)
{
free(p);
*Tp=NULL;
}
return;
}
}
void printTeacher(Teacher *Tp,int nt,int nstu)
{
if (NULL==Tp || 0==nt || 0==nstu)
{
return;
}
{
int i,j;
for (i=0;i<nt;i++)
{
printf("第%d个导师的姓名:%s\n",i+1,Tp[i].tname);
for(j=0;j<nstu;j++)
{
printf("第%d个导师下面的第%d个学生姓名:%s\n",i+1,j+1,Tp[i].stu[j]);
}
printf("第%d个导师的年龄:%d\n",i+1,Tp[i].age);
}
return;
}
}
void main()
{
Teacher *Tp=NULL;
int nt=3;
int nstu=3;
int ret =0;
ret=creatTeacher(&Tp,nt,nstu);
if (ret!=0)
{
printf("The Function creatTeacher() is Error:%d",ret);
return;
}
initTeacher(Tp,nt,nstu);
printTeacher(Tp,nt,nstu);
sortTeacher(Tp,nt);
printTeacher(Tp,nt,nstu);
freeTeacher(&Tp,nt,nstu);
system("pause");
return;
} |
|