History of C 1960 Algo 60 International Committe

  • Slides: 90
Download presentation
History of C ß ß ß 西 元 1960 年 Algo 60 語 言

History of C ß ß ß 西 元 1960 年 Algo 60 語 言 ( International Committe ) 西 元 1963 年 CPL 語 言 ( 劍 橋 與 倫 敦 大 學 ) 西 元 1966 年 BCPL 語 言 下 ( 劍橋大學 Martin Richards ) 西 元 1970 年 B 語 言 ( AT&T Ken Thompson ) 西 元 1972 年 C 語 言 ( AT&T Dennis Ritchie ) 1

2

2

Program I : The happiness is… 執行結果 #include <stdio. h> #include <stdlib. h> int

Program I : The happiness is… 執行結果 #include <stdio. h> #include <stdlib. h> int main( ) { printf("「幸福」就是… "); printf("每天晚上能一覺到天亮!n "); system("PAUSE"); return 0; } 3

Program II : 5 + 7 = ?  執行結果 #include <stdio. h> #include <stdlib.

Program II : 5 + 7 = ?  執行結果 #include <stdio. h> #include <stdlib. h> int main() a b c { 5 7 12 int a, b, c; a = 5; b = 7; 5+7 c = a + b; printf("%d + %d = %dn", a, b, c); system("PAUSE"); return 0; } 4

Program III : How old are you ? 執行結果 #include <stdio. h> #include <stdlib.

Program III : How old are you ? 執行結果 #include <stdio. h> #include <stdlib. h> int main( ) { int myage; myage = 16; printf("I am %d years old. n", myage); printf("Next year, I will be %d years old. n", myage+1); system("PAUSE"); return 0; } 5

How printf( ) work? ß ß ß printf("Hello"); printf("Hellon"); printf("%d", b); printf("The temperature is

How printf( ) work? ß ß ß printf("Hello"); printf("Hellon"); printf("%d", b); printf("The temperature is "); printf("%d", b); printf(" degreesn"); printf("The temperature is %d degreesn", b); printf("%d + %d = %dn", a, b, c); 6

資料型態 (character, 字元) char (integer, 整數) int (long integer, 長整數) long (short integer, 短整數)

資料型態 (character, 字元) char (integer, 整數) int (long integer, 長整數) long (short integer, 短整數) short (浮點數) float (倍精度浮點數) double 7 佔 1 byte 佔 4 bytes 佔 2 bytes 佔 4 bytes 佔 8 bytes

How printf( ) work? ß ß int (整數) uses %d float (浮點數) uses %f

How printf( ) work? ß ß int (整數) uses %d float (浮點數) uses %f char (字元) uses %c character strings(字串)use %s 8

Program IV : a + b = ? 執行結果 #include <stdio. h> #include <stdlib.

Program IV : a + b = ? 執行結果 #include <stdio. h> #include <stdlib. h> int main() { int a, b, c; printf("Enter the first value: "); scanf("%d", &a); printf("Enter the second value: "); scanf("%d", &b); c = a + b; printf("%d + %d = %dn", a, b, c); system("PAUSE"); return 0; } 11

Program V : How old are you ? #include <stdio. h> 執行結果 #include <stdlib.

Program V : How old are you ? #include <stdio. h> 執行結果 #include <stdlib. h> int main() { int yourage; printf("How old are you? "); scanf("%d", &yourage); printf("Next Year, you will be %d years old. n", yourage+1); system("PAUSE"); return 0; } 12

How scanf() work? ß scanf("%d", &b); Þ Þ ß ß ß int uses %d

How scanf() work? ß scanf("%d", &b); Þ Þ ß ß ß int uses %d float uses %f char uses %c character strings use %s scanf("%d %d", &a, &b); scanf("%d, %d", &a, &b); 範例程式、執行結果 13

程式碼 #include <stdio. h> #include <stdlib. h> #include <math. h> int main() { int

程式碼 #include <stdio. h> #include <stdlib. h> #include <math. h> int main() { int a, b, c; float D, x 1, x 2; printf("請輸入一元二次方程式的三個係數a, b, c:"); scanf("…………", …………); D = pow(b, 2)– 4*a*c; x 1 = …………; x 2 = …………; printf(" ax 2+bx+c=0 的兩個根為:……… ", …………); } system("PAUSE"); return 0; 31

程式的結構設計 循序性結構 sequence structure 選擇性結構 selection structure 重複性結構 iteration structure 32

程式的結構設計 循序性結構 sequence structure 選擇性結構 selection structure 重複性結構 iteration structure 32

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… 36 do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… 36 do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… 37 do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… 37 do-while…

if 敘述 ß Syntax 1: if (關係運算元) statement ; ß if (關係運算元) { statement

if 敘述 ß Syntax 1: if (關係運算元) statement ; ß if (關係運算元) { statement 1 ; statement 2 ; . . . statement n ; } Syntax 2: 38

if -- else 敘述 ß Syntax 3: if { if (關係運算元) statement ; else

if -- else 敘述 ß Syntax 3: if { if (關係運算元) statement ; else statement ; ß (關係運算元) statement 1 ; statement 2 ; … Syntax 4: 39 } else { statement 1 ; statement 2 ; … }

40

40

if -- else 範例 #include <stdio. h> #include <stdlib. h> int main() { int

if -- else 範例 #include <stdio. h> #include <stdlib. h> int main() { int b; printf("Enter a value: "); scanf("%d", &b); if (b < 0) printf("The value is negative. "); else printf("The value is not negative"); system("PAUSE"); 41 return 0;

if - else #include <stdio. h> #include <stdlib. h> 若 b = 5…… int

if - else #include <stdio. h> #include <stdlib. h> 若 b = 5…… int main() { int b; printf("Enter a value: "); 喔哦! FALSE! scanf(“%d”, b); if (b < 0) 喔哦! FALSE! printf("The value is negative. n"); YA! TRUE!! else if (b = = 0) printf("The value is zero. n"); else printf("The 42 value is

if 其他範例一 main( ) 執行結果: { 2 th step. int x=1, y=2; if( x

if 其他範例一 main( ) 執行結果: { 2 th step. int x=1, y=2; if( x == 1 && y == 3 ) printf("1 th step. n"); else if( x == 1 || y ==3 ) printf("2 th step. n"); } 43

switch--case(可多重選擇 ) switch (expression) { case const_exp 1: statements 1; break; case const_exp 2:

switch--case(可多重選擇 ) switch (expression) { case const_exp 1: statements 1; break; case const_exp 2: statements 2; break; default: statements; 47

switch--case 範例一 char grade; printf("請輸入你現在就讀的年級: "); scanf(" %c", &grade); switch ( grade ) {

switch--case 範例一 char grade; printf("請輸入你現在就讀的年級: "); scanf(" %c", &grade); switch ( grade ) { case ‘ 1’: printf(“喔!你是小星星! "); break; case ‘ 2’: printf(“嗯!你是中星星呢! "); break; case ‘ 3’: printf(“哇!你是大星星耶! "); 48 break;

switch--case 範例二 int month; printf(“請輸入現在的月份: ”); scanf(“%d”, &month); switch (month) { case 3 :

switch--case 範例二 int month; printf(“請輸入現在的月份: ”); scanf(“%d”, &month); switch (month) { case 3 : case 4 : case 5 : printf("現在是春天!n"); break; case 6 : case 7 : case 8 : printf("現在是夏天!n"); break; case 9 : case 10: case 11: printf("現在是秋天!n"); break; case 12: case 1 : case 2 : printf("現在是冬天!n"); break; 49 default : printf("月份不存在!n");

控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for… 50

控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for… 50

控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for… 51

控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for… 51

while 迴圈 ß syntax 1: while ( 關係運算 元) statement ; ¡ 52 syntax

while 迴圈 ß syntax 1: while ( 關係運算 元) statement ; ¡ 52 syntax 2: while ( 關係運算元 ) { statement 1 ; . . … statement n ; }

while 範例(一) main( ) { int i=0; while ( i < 5 ) printf("%dn",

while 範例(一) main( ) { int i=0; while ( i < 5 ) printf("%dn", i++); printf("We're out of the loop. n"); 執行結果: 0 } 53 1 2 3 4 We're out of the loop

while 範例(二) 執行結果: main( ) 01 { 12 int i=0, c=0; 23 while (

while 範例(二) 執行結果: main( ) 01 { 12 int i=0, c=0; 23 while ( i < 5 ) 34 45 { We're out of the printf("%d", i++); loop. printf("%dn", ++c); } printf("We're out of the loop. n"); } 54

do -- while 迴圈 ß syntax 1: do statement ; while (關係運算元 ); 55

do -- while 迴圈 ß syntax 1: do statement ; while (關係運算元 ); 55 ¡ syntax 2: do { statement 1 ; statement 2 ; . . . statement n ; } while (關係運算元) ;

do -- while 的範例 執行結果: main( ) 01 { 12 int i=0, c=0; 23

do -- while 的範例 執行結果: main( ) 01 { 12 int i=0, c=0; 23 34 do { 45 printf("%d", i++); We're out of the printf("%dn", ++c); loop. }while( i < 5 ); printf("We're out of the loop. n"); } 56

for 迴圈範例(二) 想一想: 印出來的結果為? main( ) 01 { 12 int i, c ; 23

for 迴圈範例(二) 想一想: 印出來的結果為? main( ) 01 { 12 int i, c ; 23 for ( i=0, c=1 ; i < 5 ; ++i, ++c )34 45 { We're out of the printf("%d", i); printf("%dn", c); loop. } printf("We're out of the loop. n"); } 59

隨堂練習三 試利用 for 迴圈撰寫如下九九乘法表。 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5

隨堂練習三 試利用 for 迴圈撰寫如下九九乘法表。 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*1= 2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1= 3 3*2= 6 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1= 4 4*2= 8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 62

break 的用途 int main( ) { int i= 0 ; while(i <= 5) {

break 的用途 int main( ) { int i= 0 ; while(i <= 5) { ++i; if ( i == 3 ) break ; printf(“i=%dn”, i); } system("PAUSE"); return 0; } 63 結果: i=1 i=2

continue 的用途 int main( ) { int i= 0; while(i <= 5) { ++i;

continue 的用途 int main( ) { int i= 0; while(i <= 5) { ++i; if ( i == 3 ) continue ; printf(“i=%dn”, i); } system("PAUSE"); return } 0; 64 結果: i=1 i=2 i=4 i=5 i=6

產生亂數的方法 #include <stdio. h> #include <stdlib. h> #include <time. h> int main() { int

產生亂數的方法 #include <stdio. h> #include <stdlib. h> #include <time. h> int main() { int rand_num; int ……… srand((unsigned)time(NULL)); rand_num = rand() % 10 + 1; 的亂數� ……… 65 //下亂數種子 //� 生介於 1~10

Array 的宣告 ß 整數陣列的宣告 Þ int student[5]={0}; 意義:宣告了5個int大小的連續空間,名稱 為student, 且裏面的值皆預設為 0。 student 0 0

Array 的宣告 ß 整數陣列的宣告 Þ int student[5]={0}; 意義:宣告了5個int大小的連續空間,名稱 為student, 且裏面的值皆預設為 0。 student 0 0 student[0] student[1] 0 0 0 student[2] student[3] student[4] 67

Array 的宣告 ß 整數陣列的宣告 Þ int student[5]={1, 3, 4, 6, 7}; 意義:宣告了5個int大小的連續空間,名稱 為student, 其預設值如下:

Array 的宣告 ß 整數陣列的宣告 Þ int student[5]={1, 3, 4, 6, 7}; 意義:宣告了5個int大小的連續空間,名稱 為student, 其預設值如下: student 1 3 student[0] student[1] 4 6 7 student[2] student[3] student[4] 68

Array 使用範例一:比較大小 int #include <stdio. h> 74 A #include <stdlib. h> int main() A[0]

Array 使用範例一:比較大小 int #include <stdio. h> 74 A #include <stdlib. h> int main() A[0] { int A[5]={74, 48, 30, 17, 62}; int i, min, max; min=max=A[0]; printf("elements in array A are "); for(i=0; i<5; i++) { printf("%d ", A[i]); if(A[i]>max) /* 判斷最大值 */ i max=A[i]; if(A[i]<min) /* 判斷最小值 */ min=A[i]; 0 } printf("n. Maximum is %d", max); 1 printf("n. Minimum is %dn", min); system(“PAUSE”); 2 return 0; } 71 int 48 30 A[1] int 17 A[2] 62 A[3] A[4] int int 1 2 0 74 48 30 17 74 i max min A[i]>max A[i]<min max min false true 48 false true 30 3 false true 17 4 false

Array 的運用 ß #include <stdio. h> #include <stdlib. h> int main() { int f[10];

Array 的運用 ß #include <stdio. h> #include <stdlib. h> int main() { int f[10]; int i; f[0]=1, f[1]=1; for (i=2; i<10; i++){ f[i]=f[i-1]+f[i-2]; } return 0; } 72

函數的用法 /* 傳回較大值 */ #include <iostream> using namespace std; a int max(int, int); /*

函數的用法 /* 傳回較大值 */ #include <iostream> using namespace std; a int max(int, int); /* 宣告函數max() */ int main(void) { int a, b; cout << "First number: "; cin >> a; cout << "Second number: "; cin >> b; cout << "The larger number is " << return 0; b 7 9 /*輸入兩個整數*/ max(a, b) << endl; /*印出較大值*/ } int max(int i, int j)/* 自訂函數max(), 傳回較大值 */ { if (i>j) return i; else return j; } return i or j 78 max( a, b ) 7 9 i j 9

函數的寫法 /* 傳回較大值 */ #include <iostream> using namespace std; int max(int, int); /*宣告函數max()*/ int

函數的寫法 /* 傳回較大值 */ #include <iostream> using namespace std; int max(int, int); /*宣告函數max()*/ int main(void) { int a, b; cout << "First number: "; /*輸入兩個整數*/ cin >> a; cout << "Second number: "; cin >> b; cout << "The larger number is " << max(a, b) << endl; /*印出較大值*/ return 0; } int max(int i, int j) /*自訂函數max(), 傳回較大值*/ { if (i>j) return i; else return j; } 79

函數的宣告 /* 傳回較大值 */ #include <iostream> using namespace std; int max(int, int); /*宣告函數max()*/ int

函數的宣告 /* 傳回較大值 */ #include <iostream> using namespace std; int max(int, int); /*宣告函數max()*/ int main(void) { int a, b; cout << "First number: "; /*輸入兩個整數*/ cin >> a; cout << "Second number: "; cin >> b; cout << "The larger number is " << max(a, b) << endl; /*印出較大值*/ return 0; } int max(int i, int j) { if (i > j) return i; else return j; } /* 自訂函數max(), 傳回較大值 */ 80

其他範例—求兩數相加 /* 兩數相加 */ #include <iostream> using namespace std; (2) int add(int, int); /*

其他範例—求兩數相加 /* 兩數相加 */ #include <iostream> using namespace std; (2) int add(int, int); /* 宣告函數add() */ int main(void) { int a, b; (1) cout << "First number : "; /* 輸入兩個整數 */ cin >> a; cout << "Second number : "; cin >> b; cout << "The sum of " << a << " and " << b << " is " << sum值*/ return 0; add(a, b) << endl; /*印出 } (3) int add(int num 1, int num 2) { int sum; sum = num 1 + num 2; return sum; } 81 /* 自訂函數add(), 傳回兩數的和 */

其他範例—求最大公因數 /* 最大公因數 */ #include <iostream> using namespace std; int gcd(int, int); /* 宣告函數gcd()

其他範例—求最大公因數 /* 最大公因數 */ #include <iostream> using namespace std; int gcd(int, int); /* 宣告函數gcd() */ int main(void) { int a, b; cout << "First number : "; /* 輸入兩個整數 */ cin >> a; cout << "Second number : "; cin >> b; cout << "The GCD of " << a << " and " << b << " is " << gcd(a, b) <<endl; /*印出gcd值*/ return 0; } int gcd(int i, int j) { int g; while(j!=0) { g=i%j; i=j; j=g; } return i; } /* 自訂的函數gcd(), 傳回最大公因數 */ 82

#include <iostream> using namespace std; int fib(int); //宣告fib函數的型態 (2) int main(void) //主程式 { int

#include <iostream> using namespace std; int fib(int); //宣告fib函數的型態 (2) int main(void) //主程式 { int N, i; (1) cout << "N = ? "; cin >> N; for (i=1; i<=N; i++) cout << fib(i) << endl ; return 0; } (3) //循序印出f(1) 到 f(n) int fib(int n) { if (n==1 || n==2) return 1; else return (fib(n-1) + fib(n-2)); } 86 //定義fib(n) //函數的Ending 值設定 //函數的一般值設定

F(6) F(4) F(5) F(4) F(3) F(2) 1 F(1) F(3) F(2) F(1) F(2) 1 1

F(6) F(4) F(5) F(4) F(3) F(2) 1 F(1) F(3) F(2) F(1) F(2) 1 1 F(2) F(1) 1 看f(6)的執行序 87 1 1

看f(6)的執行序 n if(n==1 || n==2) Return 結果 6 false F(5)+f(4) 8 5 false F(4)+f(3)

看f(6)的執行序 n if(n==1 || n==2) Return 結果 6 false F(5)+f(4) 8 5 false F(4)+f(3) 5 4 false F(3)+f(2) 3 3 false F(2)+f(1) 2 2 true 1 1 1 true 1 1 88