/*
題目:
//聲明一個結構體類型
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_\n ", 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("排序前:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != 0)
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
}
rv = SortStructArray(t, StructArrarySize); // 排序
if (rv != 0)
{
printf("func: SortStructArray() _%d_error_\n ", rv);
goto End;
}
printf("排序後:\n");
rv = PrintStructArray(t, StructArrarySize, StudentNum); // 打印
if (rv != 0)
{
printf("func: PrintStructArray() _%d_error_\n ", rv);
goto End;
}
End:
rv = FreeStructArray(&t, StructArrarySize, StudentNum);
if (rv != 0)
{
printf("致命錯誤: FreeStructArray()執行失敗!\n _%d_error_\n", 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 \n", i + 1, temp[i].name);
printf("第%d位老師的年齡為:%d \n", i + 1, (temp[i].age));
printf("第%d位老師的職務為:%s \n", i + 1, temp[i].tile);
printf("第%d位老師的地址為:%s \n", i + 1, temp[i].addr);
for (int j = 0; j < studentNum; ++j)
{
printf("第%d位老師的第%d位學生的姓名為:%s\n", 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;
}