c提高第五次作業

2022-09-03 23:54:13 字數 2606 閱讀 7932

重寫結構體巢狀一級指標老師和二級指標學生的**

//結構體型別,每個導師有三個學生

typedef struct teacher

teacher;

//在createteacher中分配空間

int createteacher(teacher **p/*out*/, int n1, int n2);

//給成員賦值

void initteacher(teacher *p, int n1, int n2);

//列印結構體成員資訊

void printteacher(teacher *p, int n1, int n2);

//根據導師名字排序, 降序

void sortteacher(teacher *p, int n);

//釋放空間,在函式內部把p賦值為null

void freeteacher(teacher **p, int n1, int n2);

int main(void)

initteacher(p, n1, n2); //給成員賦值

//列印成員,排序前

printf("排序前:\n");

printteacher(p, n1, n2);

//根據導師名字排序, 降序

sortteacher(p, n1);

//列印成員,排序後

printf("\n排序後:\n");

printteacher(p, n1, n2);

//釋放空間,在函式內部把p賦值為null

freeteacher(&p, n1, n2);

return 0;

}

#pragma warning(disable:4996)#include

#include

#include

typedef

struct

teacher

teacher;

//在createteacher中分配空間

int createteacher(teacher **p/*

out*/, int n1, int

n2)

int i = 0

;

int j = 0

; teacher *tmp = null;//

teacher tmp[3]

tmp =(teacher*)malloc(n1 * sizeof

(teacher));

if (tmp ==null)

for (i = 0; i < n1; i++)

//直接賦值

tmp[i].stu =s;

}//間接賦值是指標存在的最大意義

*p =tmp;

return0;

}//給成員賦值

void initteacher(teacher *p, int n1, int

n2)

int i = 0

;

int j = 0

;

char buf[30

];

for (i = 0; i < n1; i++)

}}//

列印結構體成員資訊

void printteacher(teacher *p, int n1, int

n2)

printf("\n

");}}//

根據導師名字排序, 降序

void sortteacher(teacher *p, int

n)

int i = 0

;

int j = 0

; teacher tmp;

for (i = 0; i < n-1; ++i)

}}}//

釋放空間,在函式內部把p賦值為null

void freeteacher(teacher **p, int n1, int

n2)

teacher *tmp = *p;

int i = 0

;

int j = 0

;

for (i = 0; i < n1; i++)

//學生

for (j = 0; j < n2; j++)

}if (tmp[i].stu !=null)

}if (tmp !=null)

}int main(void

) initteacher(p, n1, n2);

//給成員賦值

//列印成員,排序前

printf("

排序前:\n");

printteacher(p, n1, n2);

//根據導師名字排序, 降序

sortteacher(p, n1);

//列印成員,排序後

printf("

\n排序後:\n");

printteacher(p, n1, n2);

//釋放空間,在函式內部把p賦值為null

freeteacher(&p, n1, n2);

return0;

}

view code

C 第五次作業

專案一 陣列分離 includeusing namespace std int main for i 0 i 9 i else cout 偶數為 for i 0 i 專案二 陣列選擇 includeusing namespace std int main for i 0 i 10 i if k 9 ...

c 第五次作業

專案1 陣列分離 從鍵盤中輸入10個數放在陣列a中,將該陣列中奇數放到陣列b,偶數放到陣列c中後分別輸出 include using namespace std intmain for i 0 i 10 i else for n 0 n for n 0 n return 0 專案4 字串 參考上面的...

C 第五次作業

二陣列選擇 include using namespace std int main for int i 0 i 10 i if count 1 count 0 cout 不重複的數字有 for int i 0 i 三成績 includeint main int score 10 int score...