zoukankan      html  css  js  c++  java
  • c提高第五次作业

    重写结构体嵌套一级指针老师和二级指针学生的代码

    //结构体类型,每个导师有三个学生
    typedef struct Teacher
    {
    char *tName; //导师
    char **stu; //三个学生
    int age;
    }Teacher;

    //在createTeacher中分配空间
    int createTeacher(Teacher **p/*out*/, int n1, int n2);

    //给成员赋值
    void initTeacher(Teacher *p, int n1, int n2);

    //打印结构体成员信息
    void printTeacher(Teacher *p, int n1, int n2);


    //根据导师名字排序, 降序
    void sortTeacher(Teacher *p, int n);


    //释放空间,在函数内部把p赋值为NULL
    void freeTeacher(Teacher **p, int n1, int n2);

    int main(void)
    {
    int ret = 0;
    int n1 = 3; //导师个数
    int n2 = 3; //学生
    Teacher *p = NULL;

    ret = createTeacher(&p, n1, n2);
    if (ret != 0)
    {
    printf("createTeacher err:%d ", ret);
    return ret;
    }

    initTeacher(p, n1, n2); //给成员赋值

    //打印成员,排序前
    printf("排序前: ");
    printTeacher(p, n1, n2);


    //根据导师名字排序, 降序
    sortTeacher(p, n1);

    //打印成员,排序后
    printf(" 排序后: ");
    printTeacher(p, n1, n2);

    //释放空间,在函数内部把p赋值为NULL
    freeTeacher(&p, n1, n2);

    return 0;
    }

    #pragma warning(disable:4996)
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
    typedef struct Teacher
    {
        char *tName; //导师
        char **stu;  //三个学生
        int age;
    }Teacher;
    
    //在createTeacher中分配空间
    int createTeacher(Teacher **p/*out*/, int n1, int n2) {
        if (p == NULL) {
    
            return -1;
        }
        int i = 0;
        int j = 0;
        Teacher *tmp = NULL;//Teacher tmp[3]
        tmp =(Teacher*)malloc(n1 * sizeof(Teacher));
        if (tmp == NULL) {
            return -2;
        }
        for (i = 0; i < n1; i++) {
            //导师
            tmp[i].tName = (char *)malloc(30);
    
    
            //学生
            char **s = (char **)malloc(n2 * sizeof(char *));
            for (j = 0; j < n2; j++) {
                s[j] = (char *)malloc(30);
    
            }
            //直接赋值
            tmp[i].stu = s;
        }
    
        //间接赋值是指针存在的最大意义
        *p = tmp;
    
        return 0;
    }
    
    //给成员赋值
    void initTeacher(Teacher *p, int n1, int n2) {
        if (p == NULL) {
            return;
        }
        int i = 0;
        int j = 0;
        char buf[30];
        for (i = 0; i < n1; i++) {
            //导师名字
            sprintf(buf, "teacher%d%d%d", i, i, i);
            strcpy(p[i].tName, buf);
    
            //年龄
            p[i].age = 30 + 2 * i;
    
            //n2学生
            for (j = 0; j < n2; j++) {
                sprintf(buf, "stu%d%d%d%d", i, i, j, j);
                strcpy(p[i].stu[j], buf);
            }
        }
    }
    
    //打印结构体成员信息
    void printTeacher(Teacher *p, int n1, int n2) {
        if (p == NULL)
            return;
        int i = 0;
        int j = 0;
    
        for (i = 0; i < n1; i++) {
            //导师名字
            //年龄
            printf("%s[%d]
    ", p[i].tName, p[i].age);
            //学生
            for (j = 0; j < n2; j++) {
                printf("	%s", p[i].stu[j]);
            }
            printf("
    ");
        }
    }
    
    
    //根据导师名字排序, 降序
    void sortTeacher(Teacher *p, int n) {
        if (p == NULL) {
            return;
        }
        int i = 0;
        int j = 0;
        Teacher tmp;
    
        for (i = 0; i < n-1; ++i) {
            for (j = i + 1; j < n; j++) {
                if (strcmp(p[i].tName, p[j].tName) < 0) {
                    tmp = p[i];
                    p[i] = p[j];
                    p[j] = tmp;
                }
            }
        }
    }
    
    
    //释放空间,在函数内部把p赋值为NULL
    void freeTeacher(Teacher **p, int n1, int n2) {
        if (p == NULL) {
            return;
        }
        Teacher *tmp = *p;
    
        int i = 0;
        int j = 0;
        for (i = 0; i < n1; i++) {
            //可以释放导师
            if (tmp[i].tName != NULL) {
                free(tmp[i].tName);
                tmp[i].tName = NULL;
            }
    
            //学生
            for (j = 0; j < n2; j++) {
                if (tmp[i].stu[j] != NULL) {
                    free(tmp[i].stu[j]);
                    tmp[i].stu[j] = NULL;
                }
            }
            if (tmp[i].stu != NULL) {
                free(tmp[i].stu);
            }
        }
        if (tmp != NULL) {
            free(tmp);
            *p = NULL;
        }
    }
    
    int main(void)
    {
        int ret = 0;
        int n1 = 3; //导师个数
        int n2 = 3; //学生
        Teacher *p = NULL;
    
        ret = createTeacher(&p, n1, n2);
        if (ret != 0)
        {
            printf("createTeacher err:%d
    ", ret);
            return ret;
        }
    
        initTeacher(p, n1, n2); //给成员赋值
    
                                //打印成员,排序前
        printf("排序前:
    ");
        printTeacher(p, n1, n2);
    
    
        //根据导师名字排序, 降序
        sortTeacher(p, n1);
    
        //打印成员,排序后
        printf("
    排序后:
    ");
        printTeacher(p, n1, n2);
    
        //释放空间,在函数内部把p赋值为NULL
        freeTeacher(&p, n1, n2);
    
        return 0;
    }
    View Code
  • 相关阅读:
    202020211 20209326 《Linux内核原理与分析》第一周作业
    指针入门,以及利用指针简单的数组逆置
    函数入门
    C语言关于处理数组元素的插入、删除、排序
    flag用法之一
    css3中的transform值matrix
    HTML5CANVAS做的打砖块游戏。。。
    用curl抓取网站数据,仿造IP、防屏蔽终极强悍解决方式
    javascript中最好用的extend继承方法
    使用CSS3中Media Queries兼容不同设备
  • 原文地址:https://www.cnblogs.com/zyqy/p/9459597.html
Copyright © 2011-2022 走看看