日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言第11课

發(fā)布時間:2024/1/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言第11课 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

主要內(nèi)容:函數(shù)指針

一、函數(shù)指針定義

? ? ? ? int ?maxValue(int ?a,int ?b)

? ? ? ? {

? ? ? ? ? ? ? ? ? return ? a > b ?

a : b;

? ? ? ? }

? ? ? ? 函數(shù)名和數(shù)組名一樣是地址,存在在代碼區(qū)


? ? ? ? int ?maxValue(int ?a。int ?b)

? ? ? ? int ?(*p)(int。int)= ?NULL

? ? ? ? 函數(shù)指針定義。p是變量,其它是類型(通常沒有形參a。b)

? ? ? ? p = maxValue(函數(shù)指針的使用:賦值函數(shù)名)

? ? ? ? int ?m = p(3,5)(指針可當(dāng)函數(shù)用)


? ? ? ?練習(xí):定義兩個函數(shù),一個求最大值,一個求和。用戶從控制臺輸入兩個整數(shù),在從控制臺輸入max或sum分別求

? ? ? ? ? ? ? ? ? ?3和5的最大值或和,(提示:定義一個函數(shù)指針,依據(jù)輸入內(nèi)容指向不同的函數(shù)。最后一次調(diào)用完畢)

? ? ? ? int ?maxValue ( int a, int ?b ) ? ?/ / 求最大值的函數(shù)

? ? ? ? {?

? ? ? ? ? ? ? ? ?return ?a > b ? a : b;

? ? ? ? }

? ? ? ? int ?sumValue ( int a, int b ) ? ? / / 求和的函數(shù)

? ? ? ? {

? ? ? ? ? ? ? ? return ?a + b;

? ? ? ? }


? ? ? ? int ?main( int argc, const char *argv[])

? ? ? ? {

? ? ? ? ? ? ? ? void (*p) (int , int) = NULL; ? ? ? ? ? ? ? ? ? ? ? ? ? / / 定義一個指針變量

? ? ? ? ? ? ? ? int ? a ?, b; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 定義兩個須要用的整型變量

? ? ? ? ? ? ? ? printf(" 請輸入兩個整數(shù): "); ? ? ? ? ? ? ? ? ? ? ? ?/ / 讓用戶輸入兩個整數(shù)

? ? ? ? ? ? ? ? scanf(" %d%d ",&a,&b); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 標(biāo)準的輸入函數(shù)

? ? ? ? ? ? ? ? printf(" 請輸入sum或者max: "); ? ? ? ? ? ? ? ? / / ?讓用戶輸入sum或者max

? ? ? ? ? ? ? ? char ?*name = malloc(sizeof(char) * 10); ? ? / / ?申請char類型的乘以10的內(nèi)存空間

? ? ? ? ? ? ? ? scanf(" %s ", name);

? ? ? ? ? ? ? ? / / 推斷用戶輸入的是sum還是max

? ? ? ? ? ? ? ? if (strcmp(name, " sum ") == 0){

? ? ? ? ? ? ? ? p = sumValue;

? ? ? ? ? ? ? ? }else if (strcmp(name, " max ") == 0){

? ? ? ? ? ? ? ? p = maxValue;

? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? printf(" 輸入錯誤,失敗?");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? free (name); ? ? ? ?/ / ?釋放內(nèi)存,一定不能忘


? ? ? ? ? ? ? ? int ?result = p(a, b);/ / ?定義result接收結(jié)果

? ? ? ? ? ? ? ? printf(" %d ", result);/ / ?輸出結(jié)果

? ? ? ? ?}


二、回調(diào)函數(shù)

? ? ? ?函數(shù)指針做參數(shù)

? ? ? ?int ?getValue(int ?a, int ?b, int ?(*p)(int , int));

? ? ? ?getValue: 函數(shù)名

? ? ? ?int (*p)(int ,int): 函數(shù)指針做getValue函數(shù)的參數(shù)

? ? ? ?int ?value = getValue(3, 5, maxValue);(函數(shù)調(diào)用:getValue函數(shù)運行過程中再調(diào)用(回調(diào))maxValue)

? ? ? ?如圖所看到的:



? ? ? ? 練習(xí):寫一函數(shù)查找成績在90分以上的學(xué)生,是用回調(diào)函數(shù)在姓名后面加“高富帥”

? ? ? ? ? ? ? ? typedef ?struct { ? ? ? ? ? / /??定義一個結(jié)構(gòu)體

? ? ? ? ? ? ? ? char name[20];

? ? ? ? ? ? ? ? int ?age;

? ? ? ? ? ? ? ? float ?score;

? ? ? ? ? ? ? ? } Student;

? ? ? ? ? ? ? ? void ?printfStudent(Student ?*stu, int ?count)。

? ? ? ? ? ? ? ? void ?printfStudent(Student ?*stu, int ?count) ? / / ?聲明并編寫一個打印結(jié)構(gòu)體數(shù)組的函數(shù)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? for(int ?i = 0; i < count ; i++){

? ? ? ? ? ? ? ? ? ? ? ? printf("%s %d %.2f", (stu + i) ->name, (stu + i) ->age, (stu + i) ->score);

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }


/ / 聲明并編寫查找90分以上的學(xué)生并在姓名后面加“高富帥”

? ? ? ? ? ? ? ?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 = {/ / 定義結(jié)構(gòu)體變量

? ? ? ? ? ? ? ? ? ? ? ? {" 方世玉 ", 26, ?92},

? ? ? ? ? ? ? ? ? ? ? ? {" 令狐沖 ", 30, ?89},

? ? ? ? ? ? ? ? ? ? ? ? {" 韋小寶 ", 27, ?99},

? ? ? ? ? ? ? ? ? ? ? ? {" 花仙子 ", 20, ?80},

? ? ? ? ? ? ? ? ? ? ? ? {" 大教主 ", 24, ?80}

? ? ? ? ? ? ? ? };

? ? ? ? ? ? ? ? int ?count ?= sizeof(student) / sizeof(Student); ?/ / 求結(jié)構(gòu)體數(shù)組的長度


? ? ? ? ? ? ? ? printfStudent(stuent, count);?/ / 打印沒查找前的結(jié)構(gòu)體數(shù)組


? ? ? ? ? ? ? ? changeStudent(student, count, changeName); / / 推斷是否符合條件90分以上的進行改變


? ? ? ? ? ? ? ? ?printfStudent(student, ?count) ?/ / 打印查找后的結(jié)構(gòu)體數(shù)組

? ? ? ? ? }

三、動態(tài)排序

? ? ? ? 排序需求不定,無法預(yù)測的需求變更

? ? ? ? void ?sortArray(int ?*array, ?int ?count)

? ? ? ? {

? ? ? ? ? ? ? ? ? for (int i = 0; i < count - 1; i++){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?for (int j = 0; j < count - 1 - i; j++){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (條件(需求)) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?交換

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }?

? ? ? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ? ? ? 決定排序方式的重要語句封裝成函數(shù)在此回調(diào)


? ? ? ? ? int 數(shù)組動態(tài)排序

? ? ? ? ? typedef ?BOOL(* PFUNC)(int , int); ? / / 為函數(shù)指針類型起名為PFUNC

? ? ? ? ? void ?sortArray(int ?*array, int count , PFUNC ?p); ?/ / 動態(tài)函數(shù)排序聲明

? ? ? ? ? 演示樣例:

/ / 1:創(chuàng)建一個結(jié)構(gòu)體

? ? ? ? ? ? ? ? ?typedef ?struct {

? ? ? ? ? ? ? ? ?char ?name[20];

? ? ? ? ? ? ? ? ?int ?age;

? ? ? ? ? ? ? ? ?float ?score;

? ? ? ? ? ? ? ? ?} Student;


? ? ? ? ? ? ? ? ?/ / 3:打印學(xué)生結(jié)構(gòu)體數(shù)組的函數(shù)

? ? ? ? ? ? ? ? ?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:寫一個函數(shù)依據(jù)分數(shù)從小到大排序

? ? ? ? ? ? ? ? ? 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:定義一個按分數(shù)比較大小的函數(shù)

? ? ? ? ? ? BOOL? biJiaoScore(Student ?*stu1, ?Student ?*stu2);

? ? ? ? ? ? BOOL??biJiaoScore(Student ?*stu1, ?Student ?*stu2)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? return ?stu1 ->score ?> ?stuff ->score;

? ? ? ? ? ? ?}


? ? ? ? ? ? / / 定義一個按年齡排序的函數(shù)

? ? ? ? ? ?BOOL ?compareAge(Student ?*stu1, Student ?*stu2);

? ? ? ? ? ?BOOL ?compareAge(Student ?*stu1, Student ?*stu2)

? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ? ? ? return ?stu1 ->age ?> ?stuff ->age

? ? ? ? ? ?}


? ? ? ? ? ?int ?main(int ?argc, char ?* argv[]){

? ? ? ? ? ? ? ? ? ? ? ? ? ? / / 2:創(chuàng)建學(xué)生結(jié)構(gòu)體數(shù)組

? ? ? ? ? ? ? ? ? ? ? ? ? ? Student ?*student = {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"索隆", ?22, ?93},

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"香吉士", ?21, ?97},

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"犬夜叉", ?20, ?80},

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"路飛", ?19, ?70}

? ? ? ? ? ? ? ? ? ? ? ? ? ? };

? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 4:排序前調(diào)用打印函數(shù)打印結(jié)構(gòu)體數(shù)組

? ? ? ? ? ? ? ? ? ? ? ? ? ?int ?count = sizeof(student) / sizeof(Student);

? ? ? ? ? ? ? ? ? ? ? ? ? ?printfStudent(student, count);

? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 7:調(diào)用函數(shù)

? ? ? ? ? ? ? ? ? ? ? ? ? ?paiXuStudent(student, ?count , biJiaoScore);

? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / paiXuStudent(student, ?count , compareAge);按年齡排序(僅僅需傳入函數(shù)就可)

? ? ? ? ? ? ? ? ? ? ? ? ? ?/ / 8:打印排序后的結(jié)構(gòu)體數(shù)組

? ? ? ? ? ? ? ? ? ? ? ? ? ?printfStudent(student, ?count);

? ? ? ? ? ? ? ? ?}


版權(quán)聲明:本文博主原創(chuàng)文章,博客,未經(jīng)同意不得轉(zhuǎn)載。

轉(zhuǎn)載于:https://www.cnblogs.com/mengfanrong/p/4844743.html

總結(jié)

以上是生活随笔為你收集整理的C语言第11课的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。