# 運算思維與程式設計範例 ## 01-C語言入門 ## 02-資料型態、格式化輸入輸出 ## 03-運算子與運算式 ## 04-程式流程控制 ### P.42 - do while迴圈範例一 ```c= /* do while迴圈的應用 */ #include <stdio.h> #include <stdlib.h> int main(void) { int n,i=1,sum=0; // 設定迴圈初值 do { printf("請輸入n值 (n>0): "); scanf("%d",&n); } while (n<=0); // 當n<=0時重複輸入n的值 do { // sum+=i++; // 計算sum=sum+i之後,i的值再加1 sum = sum + i; i++; } while (i <= n); // 當i<=n時執行累加的動作 printf("1+2+...+%d=%d\n",n,sum); system("pause"); return 0; } ``` ### P.43 - do while迴圈範例二 ```c= /* 利用do while迴圈求n! */ #include <stdio.h> #include <stdlib.h> int main(void) { int n,i=1,fact=1; // 設定迴圈初值 do { printf("請輸入n值 (n>0): "); scanf("%d",&n); } while (n<=0); // 當n<=0時重複輸入n的值 do { // fact*=i++; fact = fact * i; i++; } while (i <= n); // 當i<=n時執行累乘的動作 printf("%d!=%d\n",n,fact); system("pause"); return 0; } ``` ### P.47 - 巢狀迴圈的範例-九九乘法表(1/2) ```c= /* 巢狀for迴圈印出九九乘法表 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i,j; for (i=1;i<=9;i++) // 外層迴圈 { for (j=1;j<=9;j++) // 內層迴圈 { printf("%d*%d=%2d ",i,j,i*j); } printf("\n"); } system("pause"); return 0; } ``` ### P.49 - 以巢狀while迴圈改寫九九乘法表 ```c= /* 巢狀while迴圈求9*9乘法表 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i=1, j=1; //設定迴圈控制變數的初值 while (i<=9) //外層迴圈 { while (j<=9) //內層迴圈 { printf("%d*%d=%2d ",i,j,i*j); j++; } printf("\n"); i++; j=1; } system("pause"); return 0; } ``` ### P.50 - 以巢狀迴圈印出幾何圖形 ```c= /* 利用巢狀迴圈印出三角形 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i,j,n=6; // 設定迴圈初值 for (i=1;i<=n;i++) // 外層迴圈決定哪一列要印星號 { for (j=1;j<=i;j++) // 內層迴圈印出*星號 { printf("*"); } printf("\n"); } system("pause"); return 0; } ``` ### P.51 - 以巢狀迴圈反印數字(1/2) ```c= /* 巢狀迴圈,將整數反過來列印 */ #include <stdio.h> #include <stdlib.h> int main(void) { int a,r; while(1) { do { printf("Input an integer:"); scanf("%d",&a); } while (a<=0); // 必須輸入大於0的正整數 printf("The reverse is "); // a = 1234 while (a!=0) // 將正整數倒過來輸出 { r=a%10; // 計算a/10的餘數 // a = 1234 r = 4 // a = 123 r = 3 // a = 12 r = 2 // a = 1 r = 1 a = a/10; // 計算a/10,再把結果設回給a // a = 123 // a = 12 // a = 1 // a = 0 printf("%d",r); } printf("\n\n"); } system("pause"); return 0; } ``` ### P.53 - break 敘述 ```c= /* break敘述的使用 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i; for(i=1;i<=10;i++) { if(i%3==0) // 判斷i%3是否為0 break; // 跳離迴圈 printf("i=%d\n",i); // 印出i的值 } printf("跳離迴圈時, i=%d\n",i); system("pause"); return 0; } ``` ### P.55 - continue 敘述 ```c= /* continue敘述的使用 */ #include <stdio.h> #include <stdlib.h> int main(void) { int i; for(i=1;i<=10;i++) { if(i%3==0) // 判斷i%3是否為0 continue; // 回到迴圈的起始處繼續執行 printf("i=%d\n",i); // 印出i的值 } printf("跳離迴圈時, i=%d\n",i); system("pause"); return 0; } ``` ## 05-函數 ### P.5 - 簡單的函數範例 ```c= /* 簡單的函數範例 */ #include <stdio.h> #include <stdlib.h> void star(void); // star()函數的原型 int main(void) { star(); // 呼叫star函數 printf("歡迎使用C語言\n"); star(); // 呼叫star函數 system("pause"); return 0; } void star(void) { printf("*************\n"); // 印出13個星號 return; } ``` ### P.11 - 於程式裡呼叫函數 ```c= /* 使用add()函數 */ #include <stdio.h> #include <stdlib.h> int add(int,int); // add()函數的原型 int main(void) { int sum, a=5, b=3; sum=add(a,b); // 呼叫add()函數,並把傳回值設給sum printf("%d+%d=%d\n",a,b,sum); system("pause"); return 0; } int add(int num1, int num2) // add()函數的定義 { int a; // 於add()函數裡宣告變數a a=num1+num2; return a; // 傳回num1+num2 的值 } ``` ### P.13 - 函數練習-display() (1/2) ```c= /* display()的練習 */ #include <stdio.h> #include <stdlib.h> void display(char,int); // display()函數的原型 int main(void) { int n; char ch; printf("請輸入欲列印的字元:"); scanf("%c",&ch); printf("請問要印出幾個字元:"); scanf("%d",&n); display(ch,n); // 呼叫自訂的函數,印出n個ch字元 system("pause"); return 0; } void display(char ch,int n) // 自訂的函數display() { int i; for(i=1;i<=n;i++) // for迴圈,可印出n個ch字元 { printf("%c",ch); // 印出ch字元 } printf("\n"); return; } ``` ### P.14 - 絕對值函數 abs() ```c= /* 求絕對值函數abs() */ #include <stdio.h> #include <stdlib.h> int abs(int); // 宣告函數abs()的原型 int main(void) { int i; printf("Input an integer:"); // 輸入整數 scanf("%d",&i); printf("abs(%d)=%d\n",i,abs(i)); // 印出絕對值 system("pause"); return 0; } int abs(int n) // 自訂的函數abs(),傳回絕對值 { if (n<0) return -n; else return n; } ``` ### P.15 - 次方函數power(x,n) -xn (1/2) ```c= /*算x的n次方 */ #include <stdio.h> #include <stdlib.h> double power(double, int); // 宣告函數power()的原型 int main(void) { double x; // x為底數 int n; // n是次方 printf("請輸入底數與次方:"); scanf("%lf,%d",&x,&n); // 輸入底數與次方 printf("%lf的%d次方=%lf\n",x,n,power(x,n)); system("pause"); return 0; } double power(double base, int n) // power()函數的定義 { int i; double pow=1.0; for(i=1;i<=n;i++) // for() 迴圈,用來將底數連乘n次 pow=pow*base; return pow; } ``` ### P.17 - 練習題 ### #7 學號 姓名 CH5P7-1練習 ``` 試撰寫int cub(int x) 函數,可用來傳回x的3次方,並利用此函數來計算cub(2),即計算 。 ``` ### #8 學號 姓名 CH5P7-2練習 ``` 試撰寫double square(double x) 函數,可用來傳回x的平方,並利用此函數來計算square(4.0),即計算 。 ``` ### P.18 - 同時使用多個函數 ```c= /* 同時呼叫多個函數 */ #include <stdio.h> #include <stdlib.h> void sum(int), fac(int); // 定義函數的原型 int main(void) { fac(5); // 呼叫fac()函數 sum(5); // 呼叫sum()函數 system("pause"); return 0; } void fac(int a) // 自訂函數fac(),計算a! { int i,total=1; for(i=1;i<=a;i++) total*=i; printf("1*2*...*%d=%d\n",a,total); // 印出a!的結果 } void sum(int a) // 自訂函數sum(),計算1+2+...+a的結果 { int i,total=0; for(i=1;i<=a;i++) total+=i; printf("1+2+...+%d=%d\n",a,total); // 印出加總的結果 } ``` ### P.19 - 函數之間的相互呼叫 ```c= /* 用萊布尼茲方法估算π */ #include <stdio.h> #include <stdlib.h> double Leibniz(int); // 宣告函數Leibniz()的原型 double power(double, int); // 宣告函數power()的原型 int main(void) { int i; for(i=1;i<=10000;i++) // 找出前10000個π的估算值 printf("Leibniz(%d)=%12.10f\n",i,Leibniz(i)); system("pause"); return 0; } double Leibniz(int n) // Leibniz()函數,可估算π值到第n項 { int k; double sum=0.; for(k=1;k<=n;k++) sum=sum+power(-1.0,k-1)/(2*k-1); // 萊布尼茲公式 return 4*sum; } double power(double base, int n) // power()函數,可計算base的n次方 { int i; double pow=1.0; for(i=1;i<=n;i++) pow=pow*base; return pow; } ``` ### P.22 - 遞迴-階乘函數 (2/2) ```c= /* 遞迴函數,計算階乘 */ #include <stdio.h> #include <stdlib.h> int fac(int); // 遞迴函數fac()的原型 int main(void) { printf("fac(4)=%d\n", fac(4));// 呼叫遞迴函數fac() system("pause"); return 0; } int fac(int n) // 自訂函數fac(),計算n! { if(n>0) return (n*fac(n-1)); else return 1; } ``` ### P.24 - 遞迴-次方函數 (2/2) ```c= /* 遞迴函數power(b,n),計算b的n次方 */ #include <stdio.h> #include <stdlib.h> int power(int,int); // 迴函數power()的原型 int main(void) { printf("power(2,3)=%d\n", power(2,3)); system("pause"); return 0; } int power(int b,int n) // 自訂函數power(),計算b的n次方 { if(n==0) return 1; else return (b*power(b,n-1)); } ``` ### P.26 - 區域變數的範例 (1/2) ```c= /* 區域變數的範例(一)*/ #include <stdio.h> #include <stdlib.h> int fac(int); // fac()函數的原型 int main(void) { int ans; ans=fac(5); printf("fac(5)=%d\n",ans); system("pause"); return 0; } int fac(int n) { int i, total=1; for(i=1; i<=n; i++) total=total*i; return total; } ``` ### P.27 - 區域變數的範例 (2/2) ```c= /* 區域變數的範例(二)*/ #include <stdio.h> #include <stdlib.h> void func(void); int main(void) { int a=100; //宣告main()函數裡的區域變數a printf("呼叫func()之前,a=%d\n",a); // 印出main()中a的值 func(); // 呼叫自訂的函數 printf("呼叫func()之後,a=%d\n",a); // 印出a的值 system("pause"); return 0; } void func(void) // 函數func() { int a=300; // 宣告func()函數裡的區域變數a printf("於func()函數裡,a=%d\n",a); // 印出func函數中a的值 } ``` ### P.28 - 全域變數 (1/3) ```c= /* prog8_15, 全域變數的範例(一) */ #include <stdio.h> #include <stdlib.h> void func(void); /* 函數func()的原型 */ int a; /* 宣告全域變數a */ int main(void) { a=100; /* 設定全域變數a的值為100 */ printf("呼叫func()之前,a=%d\n",a); func(); /* 呼叫自訂的函數 */ printf("呼叫func()之後,a=%d\n",a); system("pause"); return 0; } void func(void) /* 自訂的函數func() */ { a=300; /* 設定全域變數a的值為300 */ printf("於func()函數裡,a=%d\n",a); } ``` ### P.29 ```c= #include <stdio.h> #include <stdlib.h> int i = 5; void p(void); int main() { printf("%d\n", i); int i = 10; i = i + 1; p(); printf("%d\n", i); system("pause"); return 0; } void p() { i = i + 1; printf("%d\n", i); return; } ``` ### P.30 ```c= #include <stdio.h> #include <stdlib.h> int main() { int i = 3; printf("%d\n", i); if (i == 3) { i = i + 1; int i = 6; printf("%d\n", i); i = i + 1; } if (i == 3) { printf("%d\n", i); } system("pause"); return 0; } ``` ### P.31 - 全域變數 (2/3) ```c= /* 全域變數的範例(二) */ #include <stdio.h> #include <stdlib.h> void func(void); int a=50; // 定義全域變數a int main(void) { int a=100; // 定義區域變數a printf("呼叫func()之前,a=%d\n",a); func(); // 呼叫自訂的函數 printf("呼叫func()之後,a=%d\n",a); system("pause"); return 0; } void func(void) { a=a+300; // 這是全域變數a printf("於func()函數裡,a=%d\n",a); } ``` ### P.32 - 全域變數 (3/3) ```c= /* prog8_17, 全域變數的使用範例(三) */ #include <stdio.h> #include <stdlib.h> double pi=3.14; /* 宣告全域變數pi */ void peri(double); void area(double); int main(void) { double r=1.0; printf("pi=%.2f\n",pi); /* 於main()裡使用全域變數 pi*/ printf("radius=%.2f\n",r); peri(r); /* 呼叫自訂的函數 */ area(r); system("pause"); return 0; } void peri(double r) /* 自訂的函數peri(),印出圓周 */ { printf("圓周長=%.2f\n",2*pi*r); /* 於peri()裡使用全域變數pi */ } void area(double r) /* 自訂的函數area(),印出圓面積 */ { printf("圓面積=%.2f\n",pi*r*r); /* 於area()裡使用全域變數pi */ } ``` ### P.33 - 靜態變數 ```c= /* 區域靜態變數使用的範例 */ #include <stdio.h> #include <stdlib.h> void func(void); // 宣告func()函數的原型 int main(void) { func(); // 呼叫函數func() func(); // 呼叫函數func() func(); // 呼叫函數func() system("pause"); return 0; } void func(void) { static int a=100; // 宣告靜態變數 printf("In func(),a=%d\n",a); // 印出func()函數中a的值 a+=200; } ``` ### P.35 - #define的使用範例 (1/3) ```c= /* 使用#define */ #include <stdio.h> #include <stdlib.h> #define BEGIN { // 定義識別名稱BEGIN為左大括號{ #define END } // 定義識別名稱END為右大括號} int main(void) BEGIN // 此行的BEGIN相當於左大括號 { int i,j; for(i=1;i<=5;i++) BEGIN // 此行的BEGIN相當於左大括號 { for(j=1;j<=i;j++) printf("*"); printf("\n"); END // 此行的END相當於右大括號 } system("pause"); return 0; END // 此行的END相當於右大括號 } ``` ### P.36 - #define的使用範例 (2/3) ```c= /* 使用#define */ #include <stdio.h> #include <stdlib.h> #define WORD "Think of all the things \ we've shared and seen.\n" int main(void) { printf(WORD); system("pause"); return 0; } ``` ### P.37 - #define的使用範例 (3/3) ```c= /* 使用前置處理器來定義數學常數 */ #include <stdio.h> #include <stdlib.h> #define PI 3.14 // 定義PI為3.14 */ double area(double); int main(void) { printf("PI=%4.2f, area()=%6.2f\n",PI,area(2.0)); system("pause"); return 0; } double area(double r) { return PI*r*r; } ``` ### P.38 - 利用 #define 取代簡單的函數 ```c= /* 使用巨集的範例 */ #include <stdio.h> #include <stdlib.h> #define SQUARE n*n // 定義巨集SQUARE為n*n int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n,n,SQUARE); // 計算並印出n的平方 system("pause"); return 0; } ``` ### P.39 - 利用 #define 取代簡單的函數 ```c= /* 帶有引數的巨集 */ #include <stdio.h> #include <stdlib.h> #define SQUARE(X) X*X // 定義巨集SQUARE(X)為X*X/ int main(void) { int n; printf("Input an integer:"); scanf("%d",&n); printf("%d*%d=%d\n",n,n,SQUARE(n)); // 計算並印出n的平方 system("pause"); return 0; } ``` ### P.42 - 使用 const 關鍵字 ```c= /* const關鍵字使用的範例 */ #include <stdio.h> #include <stdlib.h> const double pi=3.14; // 宣告pi為double型態的常數 double area(double); int main(void) { //若在此處設定pi=3.1416,則編譯時會發生錯誤 printf("pi=%4.2f, area()=%6.2f\n",pi,area(2.0)); system("pause"); return 0; } double area(double r) { return pi*r*r; } ``` ## 06-陣列與字串 ## 07-指標