鱼C论坛

 找回密码
 立即注册
查看: 1661|回复: 0

[技术交流] 结构指针的强化

[复制链接]
发表于 2018-6-10 00:41:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 02:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表