/* Question: // declare a struct type, struct _ AdvTeacher {char * name; char * tile; int age; char * addr; char * p1; // system reserved member domains char ** p2; // system reserved member domains}; a struct array (6 elements) must be defined, and data must be input from the keyboard, sort by name and print the output. 1. Print the struct array and encapsulate it as a function. 10 2. Sort the struct array and encapsulate it as a function (sort by name ); 50 3. Compile the Business Test Model in the main function; 40 2014-04-22 19:59:31 wirting by zhangshichuan. */# include <iostream> using namespace std; # define StructArrarySize 3 // number of instructors # define StudentNum 1 // Number of students per instructor typedef struct _ AdvTeacher {char * name; char * tile; int age; char * addr; char ** student;} AdvTeacher; int CreateStructArray (AdvTeacher **, int, int); // The array in which the client initializes the struct T FreeStructArray (AdvTeacher **, int, int); // The client releases the struct Array Memory int PrintStructArray (AdvTeacher *, int, int ); // The client prints the struct array element int SortStructArray (AdvTeacher *, int); // The client sorts the struct array elements int main (void) {int rv = 0; advTeacher * t = NULL; rv = CreateStructArray (& t, StructArrarySize, StudentNum); // The called function allocates memory and throws it out if (rv! = 0) {printf ("func: CreateStructArray () _ % d_error _ \ n", rv); goto End ;}for (int I = 0; I <StructArrarySize; ++ I) // client initialization value {printf ("enter the name of the % d INSTRUCTOR:", I + 1); scanf ("% s ", t [I]. name); printf ("Enter the age of the % d INSTRUCTOR:", I + 1); scanf ("% d", & (t [I]. age); printf ("Enter the title of the % d INSTRUCTOR:", I + 1); scanf ("% s", t [I]. tile); printf ("Enter the address of the % d INSTRUCTOR:", I + 1); scanf ("% s", t [I]. addr); for (int j = 0; j <StudentNum; ++ j) {printf ("please Enter the name of the % d student of the % d INSTRUCTOR: ", I + 1, j + 1); scanf (" % s ", t [I]. student [j]) ;}} printf ("Before sorting: \ n"); rv = PrintStructArray (t, StructArrarySize, StudentNum); // print if (rv! = 0) {printf ("func: PrintStructArray () _ % d_error _ \ n", rv); goto End;} rv = SortStructArray (t, StructArrarySize ); // sort if (rv! = 0) {printf ("func: SortStructArray () _ % d_error _ \ n", rv); goto End;} printf ("sorted: \ n "); rv = PrintStructArray (t, StructArrarySize, StudentNum); // print if (rv! = 0) {printf ("func: PrintStructArray () _ % d_error _ \ n", rv); goto End;} End: rv = FreeStructArray (& t, StructArrarySize, studentNum); if (rv! = 0) {printf ("Fatal error: FreeStructArray () execution failed! \ N _ % d_error _ \ n ", rv);} system (" pause "); return rv;} // create struct array 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) // create a student memory block {(temp [I]. stud Ent) [j] = (char *) malloc (256 * sizeof (char); if (NULL = (temp-> student) [j]) {rv =-5; return rv ;}}* t = temp; return rv ;}// destroy struct array 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) // destroy the student memory block {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; // indirect value assignment through * (real parameter address), indirectly modify the value of real parameter to null} return rv ;} // print the struct array 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 ("The Name Of The % d instructor is: % s \ n ", I + 1, temp [I]. name); printf ("the age of the % d teacher is % d \ n", I + 1, (temp [I]. age); printf ("Number % d: % s \ n", I + 1, temp [I]. tile); printf ("the address of the % d instructor is % s \ n", I + 1, temp [I]. addr); for (int j = 0; j <studentNum; ++ j) {printf ("The Name Of The % d student of the % d instructor is: % s \ n ", I + 1, j + 1, temp [I]. student [j]) ;}} return rv;} // sorting struct array 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 ;}