zoukankan      html  css  js  c++  java
  • [学习记录]接口的封装和设计练习一

    /*
            题目:
            //声明一个结构体类型
            struct _AdvTeacher
            {
                char    *name;
                char    *tile;
                int        age;
                char    *addr;
                char    *p1; //系统预留成员域
                char    **p2;//系统预留成员域
            };
            要求定义一个结构体数组(6个元素),要求从键盘输入数据,并按照名称大小进行排序;打印输出。
                1、    打印结构体数组,需要单独封装成函数;10
                2、    排序结构体数组,需要单独封装成函数(按照名称进行排序);50
                3、    main函数中编写业务测试模型;40
    
                2014-04-22 19:59:31
                wirting by zhangshichuan.
    */
    #include <iostream>
    
    using namespace std;
    
    #define  StructArrarySize 3               // 老师数量                         
    #define  StudentNum 1                     // 每位老师的学生的数量             
    
    typedef struct _AdvTeacher
    {
        char    *name;
        char    *tile;
        int        age;
        char    *addr;
        char    **student;
    }AdvTeacher;
                                                              
    int CreateStructArray(AdvTeacher **, int, int);            //客户端初始化结构体数组
    int FreeStructArray(AdvTeacher **, int, int);              //客户端释放结构体数组内存
    int PrintStructArray(AdvTeacher*, int, int);              //客户端打印结构体数组元素
    int SortStructArray(AdvTeacher*, int);                    //客户端对结构体数组元素排序
    
    int main(void)
    {
        int rv = 0;
        AdvTeacher * t = NULL;
        
        rv = CreateStructArray(&t, StructArrarySize, StudentNum);    //被调函数分配内存,甩出来
        if (rv != 0)
        {
            printf("func: CreateStructArray() _%d_error_
     ", rv);
            goto End;
        }
    
        for (int i = 0; i < StructArrarySize; ++i)                   // 客户端初始化赋值
        {
            printf("请输入第%d位老师的姓名: ", i+1);
            scanf("%s", t[i].name);
            printf("请输入第%d位老师的年龄: ", i+1);
            scanf("%d", &(t[i].age));
            printf("请输入第%d位老师的职务: ", i+1);
            scanf("%s", t[i].tile);
            printf("请输入第%d位老师的地址: ", i+1);
            scanf("%s", t[i].addr);
            for (int j = 0; j < StudentNum; ++j)
            {
                printf("请输入第%d位老师的第%d位学生的姓名: ", i+1, j+1);
                scanf("%s", t[i].student[j]);
            }
        }
    
        printf("排序前:
    ");
        rv = PrintStructArray(t, StructArrarySize, StudentNum);     // 打印
        if (rv != 0)
        {
            printf("func: PrintStructArray() _%d_error_
     ", rv);
            goto End;
        }
    
        rv = SortStructArray(t, StructArrarySize);      // 排序
        if (rv != 0)
        {
            printf("func: SortStructArray() _%d_error_
     ", rv);
            goto End;
        }
    
        printf("排序后:
    ");
        rv = PrintStructArray(t, StructArrarySize, StudentNum);     // 打印
        if (rv != 0)
        {
            printf("func: PrintStructArray() _%d_error_
     ", rv);
            goto End;
        }
    
    End:
        rv = FreeStructArray(&t, StructArrarySize, StudentNum);
        if (rv != 0)
        {
            printf("致命错误: FreeStructArray()执行失败!
     _%d_error_
    ", rv);
        }
    
    
        system("pause");
        return rv;
    }
    
    // 创建结构体数组
    int CreateStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
    {
        int rv = 0;
        if (NULL == t)
        {
            rv = -1;
            return rv;
        }
        
        AdvTeacher * temp = NULL;
    
        temp = (AdvTeacher *)malloc(structArrarySize * sizeof(AdvTeacher));
        if (NULL == temp)
        {
            rv = -2;
            return rv;
        }
    
        for (int i = 0; i < structArrarySize; ++i)
        {
            temp[i].name = (char *)malloc(256 * sizeof(char));
            temp[i].addr = (char *)malloc(256 * sizeof(char));
            temp[i].tile = (char *)malloc(256 * sizeof(char));
    
            if (NULL == temp[i].name || NULL ==temp[i].addr || NULL == temp[i].tile)
            {
                rv = -3;
                return rv;
            }
    
            temp[i].student = (char **)malloc(studentNum * sizeof(char *));
            if (NULL == temp[i].student)
            {
                rv = -4;
                return rv;
            }
            for (int j = 0; j < studentNum; ++j)                                  //创建学生内存块
            {
                (temp[i].student)[j] = (char *)malloc(256 * sizeof(char));
                if (NULL == (temp->student)[j])
                {
                    rv = -5;
                    return rv;
                }
            }
        }
    
        *t = temp;
    
        return rv;
    }
    
    // 销毁结构体数组
    int FreeStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
    {
        int rv = 0;
        AdvTeacher *temp = *t;
    
        for (int i = 0; i < structArrarySize; ++i)
        {
            for (int j = 0; j < studentNum; ++j)                               // 销毁学生内存块
            {
                if (NULL != temp[i].student[j])
                {
                    free(temp[i].student[j]);
                }
            }
    
            if (NULL != temp[i].addr && NULL != temp[i].name && NULL != temp[i].tile && NULL != temp[i].student)
            {
                free(temp[i].addr);
                free(temp[i].name);
                free(temp[i].tile);
                free(temp[i].student);
            }
        }
    
        if (NULL != temp)
        {
            free(temp);
            *t = NULL; //间接赋值  通过*(实参的地址), 去间接修改实参的值 为null
        }
        
        return rv;
    }
    
    // 打印结构体数组
    int PrintStructArray(AdvTeacher*t, int structArrarySize, int studentNum)
    {
        int rv = 0;
        if (NULL == t)
        {
            rv = -1;
            return rv;
        }
    
        AdvTeacher *temp = t;
    
        for (int i = 0; i < structArrarySize; ++i)
        {
            printf("第%d位老师的姓名为:%s 
    ", i + 1, temp[i].name);
            printf("第%d位老师的年龄为:%d 
    ", i + 1, (temp[i].age));
            printf("第%d位老师的职务为:%s 
    ", i + 1, temp[i].tile);
            printf("第%d位老师的地址为:%s 
    ", i + 1, temp[i].addr);
            for (int j = 0; j < studentNum; ++j)
            {
                printf("第%d位老师的第%d位学生的姓名为:%s
    ", i + 1, j + 1, temp[i].student[j]);
            }
        }
    
        return rv;
    }
    // 排序结构体数组
    int SortStructArray(AdvTeacher*t, int structArrarySize)
    {
        int rv = 0;
        if (NULL == t)
        {
            rv = -1;
            return rv;
        }
        AdvTeacher *temp = t;
    
        for (int i = 0; i < structArrarySize; ++i)
        {
            for (int j = i + 1; j < structArrarySize; ++j)
            {
                if (0 > strcmp(temp[i].name, temp[j].name))
                {
                    AdvTeacher tmp = temp[i];
                    temp[i] = temp[j];
                     temp[j] = tmp;
                }
            }
        }
    
        return rv;
    }
  • 相关阅读:
    JavaScript对原始数据类型的拆装箱操作
    Javascript继承(原始写法,非es6 class)
    动态作用域与词法作用域
    自行车的保养
    探索JS引擎工作原理 (转)
    C语言提高 (7) 第七天 回调函数 预处理函数DEBUG 动态链接库
    C语言提高 (6) 第六天 文件(续) 链表的操作
    C语言提高 (5) 第五天 结构体,结构体对齐 文件
    C语言提高 (4) 第四天 数组与数组作为参数时的数组指针
    C语言提高 (3) 第三天 二级指针的三种模型 栈上指针数组、栈上二维数组、堆上开辟空间
  • 原文地址:https://www.cnblogs.com/shichuan/p/4428337.html
Copyright © 2011-2022 走看看