Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main 的參數 無參數時 void main(void) { … } 有參數時 void main(int argc, char **argv) { … } 參數哪裡來? Console 下,例: dir /? 兩個參數 argc=2 參數字串 argv[0]= “ dir ” argv[1]= “

Similar presentations


Presentation on theme: "Main 的參數 無參數時 void main(void) { … } 有參數時 void main(int argc, char **argv) { … } 參數哪裡來? Console 下,例: dir /? 兩個參數 argc=2 參數字串 argv[0]= “ dir ” argv[1]= “"— Presentation transcript:

1 main 的參數 無參數時 void main(void) { … } 有參數時 void main(int argc, char **argv) { … } 參數哪裡來? Console 下,例: dir /? 兩個參數 argc=2 參數字串 argv[0]= “ dir ” argv[1]= “ /? ” Argument count Argument value

2 argc, argv Ex.: dir /h Ex. Prog8-17 argv[0] argv argv[1] *argv[0] *argv[1] “ dir ” “ /h ” #include void main(int argc, char **argv) { int n; for(n=0; n<argc; n++) printf("argv[%d]= \"%s\"\n", n, argv[n]); }

3 Set arguments in VC

4 main 參數皆為字串形式 #include #include // atoi 所在 void main(int argc, char **argv) { int n1, n2, sum; if(argc == 3){ n1=atoi(argv[1]); // ASCII to integer n2=atoi(argv[2]); printf("%d + %d = %d\n", n1, n2, n1+n2); }

5 將程式分成多個檔案及標頭檔 (header file) compiler powermain.c.obj compiler.obj power.h + link test.exe power.c

6 #include int power(int m, int n); main() { int i; for(i=0; i<10; ++i) printf("%d %d\n", i, power(2,i)); } int power(int base, int n) { int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p; }

7

8

9

10 Recursion Jen-Chang Liu, Fall 2004

11 Revisit of algorithms Algorithms have 3 basic control structures Sequencing Selection Depend on conditions If … else Repetition Loops: while, for, do … while Recursive 遞迴

12 Albert Baker Sequential algorithm: sort Sorting: sort the cards from top to down in alphabetical order Charlie

13 Algorithm: sort Input: 3 cards in arbitrary order 1.Compare the names on the first and second cards. Exchange them if they are out of order. 2. Compare the names on the second and third cards. Exchange them if they are out of order. 3. Compare the names on the first and second cards. Exchange them if they are out of order. Output: 3 cards in alphabetical order

14 Algorithm: sort (cont.) Albert Baker Charlie Albert Baker Charlie input After Step 1 Charlie Baker Albert After Step 2 Charlie Albert Baker After Step 3

15 Iterative algorithm: merge sort

16

17 Recursive: 函數呼叫自己 #include void func(void); main() { func(); } void func(void) { printf("This is a non_stop program\n"); func(); }

18 Example: factorial 階乘 n! = n x (n-1)! = n x (n-1) x (n-2)! = … = n x (n-1) x (n-2) x … x 1!

19

20 Recursive algorithm: Hanoi tower 河內塔 Move the disks from A to C One disk a time Smaller disks over the larger disks

21 Subgoals and Subproblems

22 6 disks Hanoi tower problem: Hanoi(6, A, C) Hanoi(5, A, B) Move disk#6 from A to C Hanoi(5, B, C) Hanoi(4, B, A) Move disk#5 from B to C Hanoi(4, A, C) … … Move disk#5 from A to B Hanoi(4, C, B) … …

23 Try the smaller problems 1 disk and 2 disks are trivial 3 disks

24

25

26 Notes for 2nd midterm Programming exam. 2 parts: pencil exam. and programming exam. 五題至少對三題 五題中有兩題是看過的範例


Download ppt "Main 的參數 無參數時 void main(void) { … } 有參數時 void main(int argc, char **argv) { … } 參數哪裡來? Console 下,例: dir /? 兩個參數 argc=2 參數字串 argv[0]= “ dir ” argv[1]= “"

Similar presentations


Ads by Google