結構體基礎02

#include <stdio.h>
#include <stdlib.h>

typedef struct _Teacher {
    char name[100];
    char *title;
    int age;
    char **p_stu;   // 每個老師帶的學生
} Teacher;

// 在堆區分配內存
Teacher *create_mem(int count)
{
    Teacher *pt = NULL;
    int i = 0, j = 0;

    pt = (Teacher *)malloc(count*sizeof(Teacher)); 

    // 爲結構體的成員title(指針變量)開闢內存空間
    for (i=0; i<count; i++){
        pt[i].title = (char *)malloc(100);

        // 爲結構體的成員p_stu分配內存(二維數組)
        char **tmp = (char **)malloc(3*sizeof(char *));
        for (j=0; j<count; j++){
            tmp[j] = (char *)malloc(100);
        }
        pt[i].p_stu = tmp;
    }
    return pt;
}

// 堆內存釋放
int free_mem(Teacher *p, int count)
{
    int i = 0, j = 0;

    if (p == NULL)
        return -1;
    for (i=0; i<count; i++){
        char **tmp = p[i].p_stu;
        if (tmp == NULL){
            continue;
        }
        for (j=0; j<count; j++){
            if (tmp[j] != NULL){
                free(tmp[j]);
            }
        }
        free(tmp);
    }

    for (i=0; i<count; i++){
        if (p[i].title != NULL)
            free(p[i].title);
    }
    free(p);

}

// 打印結構體成員age
int print_t(Teacher *p, int count)
{
    int i = 0, j = 0;

    for (i=0; i<count; i++){
        printf("%d: %d  name: %s  title: %s\n", i+1, p[i].age, p[i].name, p[i].title);
        for (j=0; j<count; j++){
            printf("%s  ", p[i].p_stu[j]);
        }
        printf("\n");
    }

    return 0;
}

// 按照年齡對結構體進行排序
int sort_t(Teacher *p, int count)
{
    int i = 0, j = 0;
    Teacher tmp;

    for (i=0; i<count; i++){
        for (j=i+1; j<count; j++){
            if (p[i].age > p[j].age){
                tmp = p[i];
                p[i] = p[j];
                p[j] = tmp;
            }
        }
    }

    return 0;
}


int main_1(void)
{
    Teacher t_arr[3];
    int i = 0, count = 3, j = 0;

    for (i=0; i<3; i++){
        printf("請輸入第%d位老師的年齡:", i+1);
        scanf("%d", &t_arr[i].age);
        printf("請輸入第%d位老師的姓名:", i+1);
        scanf("%s", t_arr[i].name);

        for (j=0; j<3; j++){
            printf("請輸入第%d位老師的姓名:", j+1);
            scanf("%s", t_arr[i].p_stu[j]);
        }
    }
    printf("-----排序之前-----\n");
    print_t(t_arr, count);
    printf("-----排序之後-----\n");
    sort_t(t_arr, count);
    print_t(t_arr, count);

    return 0;
}


int main(void)
{
    Teacher *pt = NULL;
    int i = 0, count = 3, j = 0;

    pt = create_mem(count);

    // 從鍵盤的輸入爲結構體賦值
    for (i=0; i<3; i++){
        printf("請輸入第%d位老師的年齡:", i+1);
        scanf("%d", &pt[i].age);
        printf("請輸入第%d位老師的姓名:", i+1);
        scanf("%s", pt[i].name);
        printf("請輸入第%d位老師的職稱:", i+1);
        scanf("%s", pt[i].title);

        for (j=0; j<3; j++){
            printf("請輸入第%d位學生的姓名", j+1);
            scanf("%s", pt[i].p_stu[j]);
        }
    }
    // 打印結構體
    printf("-----排序之前-----\n");
    print_t(pt, count);
    printf("-----排序之後-----\n");
    sort_t(pt, count);
    print_t(pt, count);

    free_mem(pt, count);

    pt = NULL;

    return 0;
}
發佈了39 篇原創文章 · 獲贊 22 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章