/*
题目:
//声明一个结构体类型
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 = ;
AdvTeacher * t = NULL; rv = CreateStructArray(&t, StructArrarySize, StudentNum); //被调函数分配内存,甩出来
if (rv != )
{
printf("func: CreateStructArray() _%d_error_\n ", rv);
goto End;
} for (int i = ; i < StructArrarySize; ++i) // 客户端初始化赋值
{
printf("请输入第%d位老师的姓名: ", i+);
scanf("%s", t[i].name);
printf("请输入第%d位老师的年龄: ", i+);
scanf("%d", &(t[i].age));
printf("请输入第%d位老师的职务: ", i+);
scanf("%s", t[i].tile);
printf("请输入第%d位老师的地址: ", i+);
scanf("%s", t[i].addr);
for (int j = ; j < StudentNum; ++j)
{
printf("请输入第%d位老师的第%d位学生的姓名: ", i+, j+);
scanf("%s", t[i].student[j]);
}
} printf("排序前:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != )
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
} rv = SortStructArray(t, StructArrarySize); // 排序
if (rv != )
{
printf("func: SortStructArray() _%d_error_\n ", rv);
goto End;
} printf("排序后:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != )
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
} End:
rv = FreeStructArray(&t, StructArrarySize, StudentNum);
if (rv != )
{
printf("致命错误: FreeStructArray()执行失败!\n _%d_error_\n", rv);
} system("pause");
return rv;
} // 创建结构体数组
int CreateStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
} AdvTeacher * temp = NULL; temp = (AdvTeacher *)malloc(structArrarySize * sizeof(AdvTeacher));
if (NULL == temp)
{
rv = -;
return rv;
} for (int i = ; i < structArrarySize; ++i)
{
temp[i].name = (char *)malloc( * sizeof(char));
temp[i].addr = (char *)malloc( * sizeof(char));
temp[i].tile = (char *)malloc( * sizeof(char)); if (NULL == temp[i].name || NULL ==temp[i].addr || NULL == temp[i].tile)
{
rv = -;
return rv;
} temp[i].student = (char **)malloc(studentNum * sizeof(char *));
if (NULL == temp[i].student)
{
rv = -;
return rv;
}
for (int j = ; j < studentNum; ++j) //创建学生内存块
{
(temp[i].student)[j] = (char *)malloc( * sizeof(char));
if (NULL == (temp->student)[j])
{
rv = -;
return rv;
}
}
} *t = temp; return rv;
} // 销毁结构体数组
int FreeStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
AdvTeacher *temp = *t; for (int i = ; i < structArrarySize; ++i)
{
for (int j = ; 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 = ;
if (NULL == t)
{
rv = -;
return rv;
} AdvTeacher *temp = t; for (int i = ; i < structArrarySize; ++i)
{
printf("第%d位老师的姓名为:%s \n", i + , temp[i].name);
printf("第%d位老师的年龄为:%d \n", i + , (temp[i].age));
printf("第%d位老师的职务为:%s \n", i + , temp[i].tile);
printf("第%d位老师的地址为:%s \n", i + , temp[i].addr);
for (int j = ; j < studentNum; ++j)
{
printf("第%d位老师的第%d位学生的姓名为:%s\n", i + , j + , temp[i].student[j]);
}
} return rv;
}
// 排序结构体数组
int SortStructArray(AdvTeacher*t, int structArrarySize)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
}
AdvTeacher *temp = t; for (int i = ; i < structArrarySize; ++i)
{
for (int j = i + ; j < structArrarySize; ++j)
{
if ( > strcmp(temp[i].name, temp[j].name))
{
AdvTeacher tmp = temp[i];
temp[i] = temp[j];
temp[j] = tmp;
}
}
} return rv;
}