C语言第11课
主要內容:函數指針
一、函數指針定義
? ? ? ? int ?maxValue(int ?a,int ?b)
? ? ? ? {
? ? ? ? ? ? ? ? ? return ? a > b ?
a : b;
? ? ? ? }
? ? ? ? 函數名和數組名一樣是地址,存在在代碼區
? ? ? ? int ?maxValue(int ?a。int ?b)
? ? ? ? int ?(*p)(int。int)= ?NULL
? ? ? ? 函數指針定義。p是變量,其它是類型(通常沒有形參a。b)
? ? ? ? p = maxValue(函數指針的使用:賦值函數名)
? ? ? ? int ?m = p(3,5)(指針可當函數用)
? ? ? ?練習:定義兩個函數,一個求最大值,一個求和。用戶從控制臺輸入兩個整數,在從控制臺輸入max或sum分別求
? ? ? ? ? ? ? ? ? ?3和5的最大值或和,(提示:定義一個函數指針,依據輸入內容指向不同的函數。最后一次調用完畢)
? ? ? ? int ?maxValue ( int a, int ?b ) ? ?/ / 求最大值的函數
? ? ? ? {?
? ? ? ? ? ? ? ? ?return ?a > b ? a : b;
? ? ? ? }
? ? ? ? int ?sumValue ( int a, int b ) ? ? / / 求和的函數
? ? ? ? {
? ? ? ? ? ? ? ? return ?a + b;
? ? ? ? }
? ? ? ? int ?main( int argc, const char *argv[])
? ? ? ? {
? ? ? ? ? ? ? ? void (*p) (int , int) = NULL; ? ? ? ? ? ? ? ? ? ? ? ? ? / / 定義一個指針變量
? ? ? ? ? ? ? ? int ? a ?, b; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 定義兩個須要用的整型變量
? ? ? ? ? ? ? ? printf(" 請輸入兩個整數: "); ? ? ? ? ? ? ? ? ? ? ? ?/ / 讓用戶輸入兩個整數
? ? ? ? ? ? ? ? scanf(" %d%d ",&a,&b); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 標準的輸入函數
? ? ? ? ? ? ? ? printf(" 請輸入sum或者max: "); ? ? ? ? ? ? ? ? / / ?讓用戶輸入sum或者max
? ? ? ? ? ? ? ? char ?*name = malloc(sizeof(char) * 10); ? ? / / ?申請char類型的乘以10的內存空間
? ? ? ? ? ? ? ? scanf(" %s ", name);
? ? ? ? ? ? ? ? / / 推斷用戶輸入的是sum還是max
? ? ? ? ? ? ? ? if (strcmp(name, " sum ") == 0){
? ? ? ? ? ? ? ? p = sumValue;
? ? ? ? ? ? ? ? }else if (strcmp(name, " max ") == 0){
? ? ? ? ? ? ? ? p = maxValue;
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? printf(" 輸入錯誤,失敗?");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? free (name); ? ? ? ?/ / ?釋放內存,一定不能忘
? ? ? ? ? ? ? ? int ?result = p(a, b);/ / ?定義result接收結果
? ? ? ? ? ? ? ? printf(" %d ", result);/ / ?輸出結果
? ? ? ? ?}
二、回調函數
? ? ? ?函數指針做參數
? ? ? ?int ?getValue(int ?a, int ?b, int ?(*p)(int , int));
? ? ? ?getValue: 函數名
? ? ? ?int (*p)(int ,int): 函數指針做getValue函數的參數
? ? ? ?int ?value = getValue(3, 5, maxValue);(函數調用:getValue函數運行過程中再調用(回調)maxValue)
? ? ? ?如圖所看到的:
? ? ? ? 練習:寫一函數查找成績在90分以上的學生,是用回調函數在姓名后面加“高富帥”
? ? ? ? ? ? ? ? typedef ?struct { ? ? ? ? ? / /??定義一個結構體
? ? ? ? ? ? ? ? char name[20];
? ? ? ? ? ? ? ? int ?age;
? ? ? ? ? ? ? ? float ?score;
? ? ? ? ? ? ? ? } Student;
? ? ? ? ? ? ? ? void ?printfStudent(Student ?*stu, int ?count)。
? ? ? ? ? ? ? ? void ?printfStudent(Student ?*stu, int ?count) ? / / ?聲明并編寫一個打印結構體數組的函數
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? for(int ?i = 0; i < count ; i++){
? ? ? ? ? ? ? ? ? ? ? ? printf("%s %d %.2f", (stu + i) ->name, (stu + i) ->age, (stu + i) ->score);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
/ / 聲明并編寫查找90分以上的學生并在姓名后面加“高富帥”
? ? ? ? ? ? ? ?void ?changeStudent(Student ?*stu, int count, ?void (*p)(Student ?*));
? ? ? ? ? ? ? ?void ?changeStudent(Student ?*stu, int count, ?void(*p)(Student ?*)) ?
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?for (int ?i = 0; i < count ; i++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if ((stu + i) ->score >= 90){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?p(stu + i);?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?void ?changeName(Student *stu);
? ? ? ? ? ? ? ?void ?changeName(Student *stu)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?strcat (stu ->name, " 高富帥 ");
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?int ?main(int argc, const char*argv[]){
? ? ? ? ? ? ? ?Student ?student = {/ / 定義結構體變量
? ? ? ? ? ? ? ? ? ? ? ? {" 方世玉 ", 26, ?92},
? ? ? ? ? ? ? ? ? ? ? ? {" 令狐沖 ", 30, ?89},
? ? ? ? ? ? ? ? ? ? ? ? {" 韋小寶 ", 27, ?99},
? ? ? ? ? ? ? ? ? ? ? ? {" 花仙子 ", 20, ?80},
? ? ? ? ? ? ? ? ? ? ? ? {" 大教主 ", 24, ?80}
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? int ?count ?= sizeof(student) / sizeof(Student); ?/ / 求結構體數組的長度
? ? ? ? ? ? ? ? printfStudent(stuent, count);?/ / 打印沒查找前的結構體數組
? ? ? ? ? ? ? ? changeStudent(student, count, changeName); / / 推斷是否符合條件90分以上的進行改變
? ? ? ? ? ? ? ? ?printfStudent(student, ?count) ?/ / 打印查找后的結構體數組
? ? ? ? ? }
三、動態排序
? ? ? ? 排序需求不定,無法預測的需求變更
? ? ? ? void ?sortArray(int ?*array, ?int ?count)
? ? ? ? {
? ? ? ? ? ? ? ? ? for (int i = 0; i < count - 1; i++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?for (int j = 0; j < count - 1 - i; j++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (條件(需求)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?交換
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? 決定排序方式的重要語句封裝成函數在此回調
? ? ? ? ? int 數組動態排序
? ? ? ? ? typedef ?BOOL(* PFUNC)(int , int); ? / / 為函數指針類型起名為PFUNC
? ? ? ? ? void ?sortArray(int ?*array, int count , PFUNC ?p); ?/ / 動態函數排序聲明
? ? ? ? ? 演示樣例:
/ / 1:創建一個結構體
? ? ? ? ? ? ? ? ?typedef ?struct {
? ? ? ? ? ? ? ? ?char ?name[20];
? ? ? ? ? ? ? ? ?int ?age;
? ? ? ? ? ? ? ? ?float ?score;
? ? ? ? ? ? ? ? ?} Student;
? ? ? ? ? ? ? ? ?/ / 3:打印學生結構體數組的函數
? ? ? ? ? ? ? ? ?void ?printfStudent(Student ?*stu, ?int ?count);
? ? ? ? ? ? ? ? ?void ?printfStudent(Student ?*stu, ?int ?count)
? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ?for (int ?i = 0; i < count; i++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("%s %d %.2f",(stu + i) ->name, (stu + i) ->age, ?(stu + i) ->score);
? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ?printf("\n");
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? / / 5:寫一個函數依據分數從小到大排序
? ? ? ? ? ? ? ? ? void ? paiXuStudent(Student ?*stu, ?int ?count, ?BOOL (*PFUNC)(Student ?*stu1, ?Student ?*stu2));
? ? ? ? ? ? ? ? ? void ? paiXuStudent(Student ?*stu, ?int ?count, ?BOOL (*PFUNC)(Student ?*stu1, ?Student ?*stu2))
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int ?i = 0; i < count - 1; i++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?for (int ?j = 0; j < count - 1 - i; j++){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (PFUNC((stu + j), (stu + j + 1))){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Student ?temp;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? temp = *(stu + j);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *(stu + j) = *(stu + j + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *(stu + j + 1) = temp;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? / / 6:定義一個按分數比較大小的函數
? ? ? ? ? ? BOOL? biJiaoScore(Student ?*stu1, ?Student ?*stu2);
? ? ? ? ? ? BOOL??biJiaoScore(Student ?*stu1, ?Student ?*stu2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? return ?stu1 ->score ?> ?stuff ->score;
? ? ? ? ? ? ?}
? ? ? ? ? ? / / 定義一個按年齡排序的函數
? ? ? ? ? ?BOOL ?compareAge(Student ?*stu1, Student ?*stu2);
? ? ? ? ? ?BOOL ?compareAge(Student ?*stu1, Student ?*stu2)
? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? return ?stu1 ->age ?> ?stuff ->age
? ? ? ? ? ?}
? ? ? ? ? ?int ?main(int ?argc, char ?* argv[]){
? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 2:創建學生結構體數組
? ? ? ? ? ? ? ? ? ? ? ? ? ? Student ?*student = {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"索隆", ?22, ?93},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"香吉士", ?21, ?97},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"犬夜叉", ?20, ?80},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"路飛", ?19, ?70}
? ? ? ? ? ? ? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 4:排序前調用打印函數打印結構體數組
? ? ? ? ? ? ? ? ? ? ? ? ? ?int ?count = sizeof(student) / sizeof(Student);
? ? ? ? ? ? ? ? ? ? ? ? ? ?printfStudent(student, count);
? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 7:調用函數
? ? ? ? ? ? ? ? ? ? ? ? ? ?paiXuStudent(student, ?count , biJiaoScore);
? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / paiXuStudent(student, ?count , compareAge);按年齡排序(僅僅需傳入函數就可)
? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 8:打印排序后的結構體數組
? ? ? ? ? ? ? ? ? ? ? ? ? ?printfStudent(student, ?count);
? ? ? ? ? ? ? ? ?}
版權聲明:本文博主原創文章,博客,未經同意不得轉載。
轉載于:https://www.cnblogs.com/mengfanrong/p/4844743.html
總結
- 上一篇: 201509280825_《css3——
- 下一篇: 06-图2 Saving James B