1 / 32

授課: ANT 日期: 2011/3/28

函數. 授課: ANT 日期: 2011/3/28. 函數 - argc, argv[] 函數呼叫方式 call_by_value call_by_reference (( 基本指標 )) 數學函數 字元、字串函數 巨集. 自訂函數. 使用函數的目的:可以利用模組化來簡化主程式。 要自訂函數需要注意的地方:函數的宣告、引數的使用、函數的主題以及傳回值。. 宣告方式. void functionName(variable1, variable2 ). 函數的屬性. 函數傳入的值. 函數的名稱.

gianna
Download Presentation

授課: ANT 日期: 2011/3/28

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 函數 授課:ANT 日期:2011/3/28

  2. 函數-argc, argv[] 函數呼叫方式 call_by_value call_by_reference((基本指標)) 數學函數 字元、字串函數 巨集

  3. 自訂函數 • 使用函數的目的:可以利用模組化來簡化主程式。 • 要自訂函數需要注意的地方:函數的宣告、引數的使用、函數的主題以及傳回值。

  4. 宣告方式 voidfunctionName(variable1, variable2) 函數的屬性 函數傳入的值 函數的名稱

  5. #include<stdio.h> #include<stdlib.h> void line(); void line2(); //先前宣告有哪些副函式 int main(){ line(); printf(“我剛呼叫了什麼\n"); line2(); system("pause"); return 0; } void line(){ printf("----分隔線----\n"); } void line2(){ printf(“\n\n---!!!-分隔線-!!!---\n\n"); }

  6. 程式的執行流程圖 int main(){ line(); printf(“我剛呼叫了什麼\n"); line(); system("pause"); return 0; } 第一步 第二步 第三步 void line(void){ printf("----分隔線----\n"); return; } 第四步

  7. argc, argv 解析用法 argc 是指命令行輸入參數的個數 argv存儲了所有的命令行參數 #include<stdio.h> int main(int argc, char* argv[]) { printf("剛剛輸入了%d個指令\n",argc); printf("剛剛輸入的指令為 : \n%s\n",argv[0]); return 0 ; } 如果在命令行運行一程式,運行命令為:hello.exe hello world 那麼,argc的值是 3,argv[0]是"hello.exe",argv[1]是"hello",argv[2]是"world"

  8. argc, argv 小應用 Q : 想要設計一個小程式,程式目的為”輸入一段文字到一個檔案裡” 命令列格式 Demo

  9. 下面為Argc.c的程式碼

  10. 函數的參數呼叫方式

  11. 基本指標 宣告 一變數 為 一指標, 該變數的值為一記憶体的地址,例如int *ip; ip 為 一變數,如果ip的值為1000,那麼我們可以說 ip指向記憶体其地址為1000。 "&" 是可當做取址運算子,用來表示(或取得)一變數的記憶体地址,例如: int *ip; int x; scanf("%d", &x); ip = &x; &x 即變數 x的記憶体地址,於 scanf("%d ", &x);中,我們欲以輸入方式改變 x的值,或以 x存輸入值。 ip = &x; 即指標變數 ip指向變數 x,或 ip的值為 x的記憶体地址。

  12. #include<stdio.h> #include<stdlib.h> void call_by_value(int b) { b = 200; } void call_by_reference(int *b) //b是一個指標 { *b = 200;//* 有”指向”的意思 } int main() { int a = 100; call_by_value(a); printf("call_by_value : %d\n",a); call_by_reference(&a); //將a的位址傳給call_by_reference printf("call_by_reference : %d\n",a); }

  13. C程式過程 Call by value Call by reference 100 100 a 的記憶體位址 100 a a 數值 200 記憶體位址 b 指標變數b

  14. 回傳值到主程式 #include<stdio.h> #include<stdlib.h> int divide(int a , int b ) { return(a/b); } int main() { int var1 , var2 , result; printf("輸入被除數 : "); scanf("%d",&var1); printf("輸入除數 : "); scanf("%d",&var2); result = divide(var1 , var2); printf("相除結果是 %d \n",result); return 0 ; }

  15. 常用到的數學函數 • #include <math.h> • double pow(double x, double y) x的y次方 • double sqrt(double x) 求x的平方根 • double fabs(double x) 求實數x的絕對值 • int abs(int x) 求整數x的絕對值 • int rand() 產生一個亂數 • sin(x) x以弧度解釋 • cos(x) x以弧度解釋 • tan(x) x以弧度解釋

  16. Sin(x)使用範例

  17. 常用的字串函數 • #include <string.h> • int strcmp(char *s1,*s2) 字串比較 • char *strcpy(char *dest, char *src) 字串拷貝 • int strlen(char *s1) 字串長度 • int atoi(char *s) 字串轉整數 • gets(char *s) 讀取一字串 • puts(char *s) 輸出一字串

  18. 常見的字元函數 • tolower(ch) ch轉成小寫 • toupper(ch) ch轉成大寫 • islower(ch) ch 為小寫? • isspace(ch) ch 為空白? • isdigit(ch) ch 為數字? • isalpnum(ch) ch 為文數字? • getchar() 讀入一字元

  19. 字串函式使用範例

  20. 巨集指令 • C前置處理器的#define指令除了定義常數,事實上,#define指令是巨集指令,可以定義新關鍵字或使用參數建立巨集函數。 • #define巨集指令的格式,如下所示: #define 名稱 替換的內容 #include <stdio.h> #define SQUARE(x) x*x //自行宣告的巨集 int main() { printf("Square of 10 is %d\n", SQUARE(10)); return 0 ; }

  21. Lab5 寫一個程式,可以讓user去選擇要計算的東西。分三種:1. 面積 2. 數學式 3.畫sin圖 計算面積的是矩形(使用巨集指令)。 計算數學式的有階層(計算在副程式,結果在主程式印出)。 畫圖型的動作(在副程式執行)。 限制- 使用switch來做選擇,計算過程必須在副函式執行。

  22. 說明 Switch(choice){ case 1: printf(“radius : ”); scanf(“%d”,input); circle(); break; case 2: … case 3: … }

  23. Lab5 PS:矩形-> 長寬,階層->一個N值,sin圖參考前面的投影片

  24. HW05 • 寫一個會員查詢系統。 • 一開始程式執行會先作會員登入,在判定是否為會員後,如果不是就做會員新增的動作,如果是就進入會員系統。 • 其中登入者的身份需要分為、查詢會員、離開系統,並可做重新登入的動作。一般使用者及管理者,管理者登入後會有四種功能。分別為新增會員、刪除會員 • 一般使用者則只有兩種功能即查詢以及離開系統,離開後可做重新登入。 • 以上動作需要將變更後的會員資料,就是其名字以及相對應的密碼輸出至檔案。 • 作業限制 – 所有功能需(新增、刪除、查詢)要寫在副程式裡。

  25. 資料格式 一開始就有兩個檔案,分 別為root.txt以及user.txt作為 資料比對用。內容請依照右 圖。。 利用兩個檔案做資料比對以 及新增、 刪除。 Root只能改密碼。

  26. 模擬結果 可自行增加功能酌量加分,須註解 此為查詢

  27. 模擬結果 此為新增與刪除

  28. 作業提示 • 可利用以下函式幫助程式撰寫 • 字串比較 • 字串轉整數 • 讀取/輸出一字串 • 開寫讀檔 • 作業繳交需要將root.txt以及user.txt一起放到壓縮檔裡。

More Related