You are on page 1of 120

C 語言程式設計

講師:曾冠諭
教學流程
 語法→範例→練習
 C 語言易懂不易熟
 大部分時間是練習
 凡事起頭難,紥實練習才有收穫
 理解後背下來才能熟練
 上課方式
下載
http://esnips.com/web/mycallmax/
http://esnips.com/web/Nothing/
教學內容
 C 語言基本概念
 基本資料型態
 輸出入函數
 運算子
 流程控制
 陣列
 函數
 結構
 相關應用 ( 矩陣運算,解聯立方程組 , 大數加法 )
C 語言的基本概念
 C 語言執行過程 ?
 原始程式 經由編譯器 變成目的檔 經由連結器 變成執行檔
SOURCE.C  COMPILER  SOURCE.OBJ  LINKER  SOURCE.EXE

 C 語言的特性
 (1) 可以像組合語言一樣進行硬體之直接存取
.
(2) 為語法規則簡單 , 清楚 , 容易使用之結構
化語言 .
(3) 必要時可以與組合語言連結 .
( 4 ) 可 攜 性 ( Portability ) 極 佳 , 跨 平 台 的 能 力 強
.
編譯式語言
程式庫
library

原始程式 目的碼 可執行碼 執行


source code object code executable
code

編譯程式 連結程式
compiler linker
直譯式語言

原始程式
執行
source code

直譯程式
interpreter
C 語言的程式架構
//Program name:01-03-01.c
#include <stdio.h> 宣告區
#include <stdlib.h>
int sum(int n);
int main()
{
printf("The sum of 1 to 100 is %d", sum(100));
system("PAUSE"); 主程式區
return 0;
}

int sum(int n)
{
int s=0,i;
for (i=1;i<=n;i++) 凾式區
s=s+i;
return s;
}
初體驗
#include <stdio.h> // 這是註解
int main()
{
/* 這也是註解,
而且可以多行
*/
printf(“Hello world!This is my first time!\n”);
return 0;
}
關鍵字 v.s. 識別字
 C 語言中有大小寫之分
 關鍵字 (Keyword) :
如: #include, int, void, main, return
 識別字 (Identifier) :
如: stdio.h, sum, printf, i, s
練習
 將剛剛的關鍵字背下來
 練習寫出自己的第一個程式
變數命名的原則
 開頭必須是字母 ( letter ) 或底線
( underscore )
 字元的大寫小寫所代表的意義不同
 不得使用關鍵字 ( keyword )
 常數 & 變數
變數的資料型態
類別 符號位元 位元長 (bits) 表示法 數值範圍

16 int(short int) -32768(-215)~32767(215-1)


32 long(long int) -2147483648(-231)~2147483647(231-1)

整數
16 unsigned int 0->65535 (216)

無 16 unsigned short 0->65535 (216)

32 unsigned long 0->4294967295(232)

32 float 10-38~1038
浮點數 有
64 double 10-308~10308

字元 無 8 char 0->255
變數的宣告方法
(1) 整數部份 :
int a;
int b,c=45;
short int name;
long int var_name;
unsigned int argu1=30;

(2) 浮點數部份 :
float a=12.3456; /* 單精準度 */
float b=0.12345e2;
double score=4.987654322e-7; /* 雙精準度 */

(3) 字元部份 :
char c='c';
char bell=7; /*7 為 ASCII 之響聲字元 */
範例
#include <stdio.h>
int main()
{
int a,b,c;
long d=2147483647;
float e=5.1146;
double f=123.465458;
char g='A';
a=3;
a=4;
b=5;
c=6;
printf(“If you want to write this statement in two lines.\
You can add \”\\\” between two lines\n”);
// If you want to write this statement in two lines.You can add “\” between two lines.
printf("a=%d\n",a); //a=4;
printf("a=%d,b=%d,c=%d\n",a,b,c); //a=4,b=5,c=6
printf("e=%f\n",e); //e=5.114600
printf("f=%lf\n",f); //f=123.465458
printf("g=%c\n",g); //g=A;
return 0;
}
轉換字元的種類
種類 表示法 功能敘述

d 以十進位方式印出。

o 以八進位方式印出。

整 數 (integer) x 以十六進位方式印出。

u 以不帶符號的十進位方式印出。

ld 以長整數 (long) 方式印出。

f 以 xxx.xxxxxx 方式印出。 ( 預設為六位小數 )


浮點數 (float)
e 以指數的方式印出。

c 以字元方式印出。
字元 (char)
s 以字串方式印出。

- 向左邊靠齊印出 。

dd 指定欄位寬。
其他 (other)
. 分隔欄寬。

*.* 指定浮點數之精確度。
範例
{ Result :
int x=42;
float y=12.345;
char c='A',c1[4]="ABC";
printf("/%d/\n",x); /42/
printf("/%15d/\n”,x); / 42/
printf(“/%-15d/\n",x); /42 /
printf("/%f %4.3f/\n",y,y); /12.3450000 12.345/
printf("/%08.4f/\n",y); /012.3450/
printf("/%0*.*f/\n",8,4,y); /012.3450/
printf("/%c %s/\n",c,c1); /A ABC/
x=32768;
printf(“%d\n”,x); -32768
}
特殊控制碼
種類 功能敘述

\n newline 新行。

\r carriage return 回歸鍵。

\t tab 跳格。

\b backspace 退位。

\f form feed 跳頁。

\\ backslash 反斜線。

\' single quote 單引號。

\" double quote 雙引號。


scanf 範例
int main( ) Result
{
int x;
float y;
char z; 24 12.45 G  此處為鍵盤輸入之資料
scanf("%d %f %c“ ,&x,&y,&z); 24 12.4500001 G
printf("%d %f %c\n",x,y,z); 123 4567890  此處為鍵盤輸入之資料
scanf("%3d %4f",&x,&y); 123 4567.000000
printf("%d %f\n",x,y);
return 0;
}
範例
目的:讓使用者輸入他的年齡,並在螢幕上顯示出他的年齡
// Program name: 01-04-03.c
#include <stdio.h>
int main()
{
int age; // 變數 age 用來儲存使用者輸入的年齡

printf("Please input your age:");


scanf("%d", &age); // 讀取使用者輸入資料
printf("You are %d years old.", age);
return 0;
}
練習 1
題目:在螢幕上顯示
I am 18 years old, 174 cm tall, and 68.5
weight. My lucky character is J.
其中 18, 174, 68.5, J 請建立適當的變數在用
printf 顯示出來
解答
// Program name:02-01-01A.c
#include <stdio.h>
int main()
{
int age=18,height=174;
float weight=68.5;
char c=‘J’;
printf("I am %d years old, %d cm tall, and %f kg
weight.\n",age,height,weight);
printf("My lucky character is %c.\n",c);
return 0;
}
練習 2
將剛剛的程式改成可以輸入資料
如:
輸出: Please enter your age, height,
weight, and lucky character. Separate
each of them with a blank.
輸入: 18 174 68.5 J
輸出: I am 18 years old, 174 cm tall, and
68.5 weight. My lucky character is J.
解答
// Program name:02-01-01A.c
#include <stdio.h>
int main()
{
long age,height;
float weight;
char c;
printf(“Please enter your age, height, weight, and lucky character.
Separate each of them with a blank.\n”);
scanf(“%ld %ld %f %c”,&age,&height,&weight,&c);
printf("I am %d years old, %d cm tall, and %f kg
weight.\n",age,height,weight);
printf("My lucky character is %c.\n",c);
return 0;
}
運算元 v.s 運算子
 運算元 (operand)ex. 如常數,變數
 運算子 (operator)
可分為
一元運算子 ex.++,--,-,+
二元運算子 ex.+,-,*,/
三元運算子 ex.a>b?a:b
最好是同資料型態再運算
指定運算元 (=)
 將右邊運算的結果指定給左邊的變數
 a=13;
 b=15;
 a=a+b;
 a=b=c=d=15;
 由右至左
運算子的優先權
負號 (-) 高優先順序

乘 ( * ) 、除 ( / ) 、餘數 ( % ) 中優先順序

加 ( +) 、減 ( - ) 低優先順序
練習
請設計一個程式,輸入兩個數
計算出他們的加減乘除及模數 ( 負數別拿來取模數 )
Ex.
Input:10 5
Output:
10+5=15
10-5=5
10*5=50;
10/5=2;
10%5=0;
解答
#include <stdio.h>
int main()
{
long a,b;
scanf("%ld %ld",&a,&b);
printf("%ld+%ld=%ld\n",a,b,a+b);
printf("%ld-%ld=%ld\n",a,b,a-b);
printf("%ld*%ld=%ld\n",a,b,a*b);
printf("%ld/%ld=%ld\n",a,b,a/b);
return 0;
}
特殊運算式 ( 由右至左 )
特殊運算式 基本運算式
a += b a=a+b
a -= b a=a-b
a *= b a=a*b
a /= b a=a/b
a %= b a=a%b

Int a=1,b=3;
a=b+=3;
a=?
b=?
練習
 輸入一個三位整數,將此整數的三個位數分離
Ex.
Input:
784
Output:
4
8
7
解答
//Program name:03-02-02A.c
#include <stdio.h>

int main()
{
int n;
scanf("%d",&n);
printf("%d\n",n%10);
n=n/10;
printf("%d\n",n%10);
n=n/10;
printf("%d\n",n%10);
return 0;
}
遞增與遞減
 遞 增與 遞 減 又 分 為 前 置 模 式 ( prefix )
與 後 置 模 式 ( postfix ) ;
 前 置 模 式:是 將 ++ 或 - - 置 於 變 數
之 前,其運算是在使用此運算元之前先進
行加一或減一之動作。
 後 置 模 式: 是 將 ++ 或 - - 置 於 變 數
之 後,其運算是在使用此運算元之後才進
行加一或減一之動作 。
範例
int main() Result: 
{ 2 1
int a=1,b=1,c=1,d=1;
int aplus,bplus;
2 2
int cminus,dminus; 0 1
aplus=++a; 0 0
bplus=b++;
cminus=--c;
dminus=d--;
printf("%d %d\n",aplus,bplus);
printf("%d %d\n",a,b);
printf("%d
%d\n",cminus,dminus);
printf("%d %d\n",c,d);
return 0;
}
範例
//Program name:03-03-02A.c
#include <stdio.h>
int main()
{
int a,b,c,d,e,f;
a=b=c=d=e=f=3;
a+=++b-c;
d-=e*f--;
f%=--a/c++;
c*=a--+f++*b++;
printf("a=%d, b=%d, c=%d\n",a,b,c);
printf("d=%d, e=%d, f=%d\n",d,e,f);
system("PAUSE");
return 0;
}
解說
運算式 a b c d e f

a=b=c=d=e=f=3; 3 3 3 3 3 3

a+=++b-c; 4 4 3 3 3 3

d-=e*f--; 4 4 3 -6 3 2

f%=--a/c++; 3 4 4 -6 3 0

c*=a--+f++*b++; 2 5 12-6 3 1
練習
int a=2,b=3,c=4,d=5,e=9,f=2;
a+=b+--c*(d+e)/f++;
解答
 a=26,c=3,f=3,b,d,e 不變
資料型態之轉換
ma in ( ) Re su lt :
{ 4
in t x1; 9.200000
flo at y1,y2; 5.000000
x1=(in t)4.123;// 無條件捨

y1=fl oat (46)/5;
y2=(in t)5.3456;
printf("%d\n%f\n%f\n",x1,y1,y2
);
}
範例
#include <stdio.h>
Result:
int main()
{ 3
int a; 3.250000
float b;
a=13;
b=a/4;
printf("%f\n",b);
b=(float)a/4;
printf("%f\n",b);
return 0;
}
練習
 輸入攝氏溫度,輸出華氏溫度
 輸入之攝式溫度為整數
 輸出之華氏溫度請四捨五入到整數位
解答
#include <stdio.h>
int main()
Origin:87.000000
{ Origin:87.555557
float origin,temp;
int roc,celsius; Modified:88.055557
scanf("%d",&celsius);
origin=celsius*5/9+32; Result1:88
printf("Origin:%f\n",origin);
origin=(float)celsius*5/9+32;
Result2:88
printf("Origin:%f\n",origin);
temp=origin+0.5;
printf("Modified:%f\n",temp);
roc=(int)temp;
printf("Result1:%d\n",roc);
printf("Result2:%.0f\n",origin);
return 0;
}
邏輯運算子 (Logical operators)

運算子 功能敘述

&& AND( 且 )

|| OR( 或 )

! NOT( 反向 )
關係運算子 (Relational
operators)
運算子 功能敘述 運算子 功能敘述

< 小於 >= 大於等於

<= 小於等於 == 等於

> 大於 != 不等於
範例
int a=3,b=9,c=5;
printf(“%d\n”,a>b); //0
printf(“%d\n”,a<b); //1
printf(“%d\n”,a<b>c); //1 a<b || b>c
printf(“%d\n”,b>a && b>c); //1
printf(“%d\n”,a>b || b>c); //1
printf(“%d\n”,!(a>b)); //1
printf(“%d\n”,a==b); //0
printf(“%d\n”,b!=c); //1
基本運算子優先順序表
!, 負號 (-), ++, --

乘 ( * ) 、除 ( / ) 、餘數 ( % )

加 ( +) 、減 ( - )

<, <=, >, >=

==, !=

&&

||
控制結構 (control structure)
 結構化程式設計的基礎建立在下列三個結
構:
 (1) 循序 (sequential): 以一個接著一個方式
進 行作業 。
 (2) 條件執行 (conditional) : if_then_else 。
 (3) 迴路 (looping) :for, while
If 敘述
1. 最簡單型式
if( 條件表示式 )
{
敘述列 ;
}
If 敘述
2.if-else
if( 條件表示式 )
{
敘述 1;
}
else
{
敘述 2;
}
If 敘述
3.if-else if-else
if( 條件表示式 )
{
敘述 1;
}
else if
{
敘述 2;
}
….
else if
{
敘述 n;
}
Else
{
敘述 ;
}
範例
輸入一個數,請判斷其是否為偶數
#include <stdio.h>
int main()
{
long a;
scanf("%ld",&a);
if(a%2==0)
printf("Even.\n");
else printf("Odd.\n");
return 0;
}
15
Odd.
16
Even.
範例
輸入三個整數,判斷是否能夠構成一個三角

解答
#include <stdio.h> 5 10 10
int main() It isn't a triangle.
{ 10 10 5
long a,b,c; It isn't a triangle.
scanf("%ld %ld 5 12 13
%ld",&a,&b,&c); It’s a triangle.
if(a+b>c && b+c>a &&
a+c>b) printf("It's a triangle.\n");
else printf("It isn't a
triangle.\n");
return 0;
}
使用 if-else if 的解法
#include <stdio.h>
int main()
{
long a,b,c;
scanf("%ld %ld %ld",&a,&b,&c);
if(a+b<=c) printf("It isn't a triangle.\n");
else if(a+c<=b) printf("It isn't a triangle.\n");
else if(b+c<=a) printf("It isn't a triangle.\n");
else printf("It's a triangle.\n");
return 0;
}
範例
 輸入學生的成績判斷其為那個等級
 A:90~100
 B:80~89
 C:70~79
 D:60~69
 E:59 以下
解答
#include <stdio.h> 100
int main() A
{ 95
long score;
A
scanf("%ld",&score);
84
if(90<=score && score<=100)
printf("A\n"); B
else if(80<=score && 79
score<=89) printf("B\n"); C
else if(70<=score && 69
score<=79) printf("C\n");
D
else if(60<=score &&
score<=69) printf("D\n"); 55
else printf("E\n"); E
return 0;
}
巢狀 if-else
if(…)
{
if(…)
{

}
else
{

}
}
else
{

}
範例
 輸入年齡與性別 ( 男生 =1, 女生 =0)
 判斷此人是 18 歲以上或以下的男生或女生
解答 1
#include <stdio.h>
20 1
int main()
{ You are a man.
long age,gender;
scanf("%ld
18 0
%ld",&age,&gender); You are a woman.
if(age>=18)
if(gender) 15 1
printf("You are a man.\n");
else printf("You are
You are a boy.
a woman.\n"); 14 0
else
if(gender) You are a girl.
printf("You are a boy.\n");
else printf("You are
a girl.\n");
解答二
#include <stdio.h>
int main()
{
long age,gender;
scanf("%ld %ld",&age,&gender);
if(age>=20) printf("You are over 18 years old.\n");
else printf("You can't watch adult movies.\n");
if(gender) printf("You are male.\n");
else printf("You are female.\n");
return 0;
}
多重分支結構式 (switch…case)
switch( 條件表示式 )
{
case 值 1: 敘述式 1;
case 值 2: 敘述式 2;
case 值 3: 敘述式 3;
case 值 4: 敘述式 4;
default: 敘述式 5;
}
範例
 輸入年齡 , 輸出可看的電影級別
解答
#include <stdio.h> 50
int main() You can watch restricted movies.
You can watch parental guidence movies.
{
You can watch protected movies.
long age;
You can watch general movies.
switch(age/6) 18
{ You can watch restricted movies.
default: You can watch parental guidence movies.
case 3: printf("You can You can watch protected movies.
watch restricted movies.\n"); You can watch general movies.
case 2: printf("You can 15
watch parental guidence movies.\n"); You can watch parental guidence movies.
case 1: printf("You can You can watch protected movies.
watch protected movies.\n"); You can watch general movies.
11
case 0: printf("You can
watch general movies.\n"); You can watch protected movies.
You can watch general movies.
}
5
return 0;
You can watch general movies.
}
範例
 將剛剛分數等級用 switch-case 改寫
解答
#include <stdio.h>
int main()
{
long score;
scanf("%ld",&score);
switch(score/10)
{
case 10:
case 9: printf("A\n");
break;
case 8: printf("B\n");
break;
case 7: printf("C\n");
break;
case 6: printf("D\n");
break;
default:printf("E\n");
}
return 0;
}
For loop
for([ 初始表示式 ];[ 條件表示式 ];[ 迴路表示式 ])
{
敘述列 ;
}
Example:
for(i=0;i<5;i++)
printf(“i=%d\n”,i);
執行多少次 ?
結果 ?
流程圖
初始表示式


條件表示式
是否為真

敘述式

迴路表示式
練習
 計算 1 加到 100 的和
 計算 1 加到 n 的和
解答 1
#include <stdio.h>
int main()
{
int i,sum=0;
for (i=1;i<=100;i++)
sum+=i;
printf(“The sum is %d\n",sum);
return 0;
}
Output:
The sum is 5050
解答 2
#include <stdio.h> Input:
int main()
{ 100
int i,sum=0,input; Output:
scanf(“%d”,&input);
The sum is 5050
for (i=1;i<=input;i++)
sum+=i;
printf(“The sum is %d\n",sum);
return 0;
}
練習
 輸入底數 a 與次方 n
 輸出 an
解答
#include <stdio.h>
The answer is 27
#include <math.h>
int main() The answer is
{ 27.000000 in
long a,n,i,answer=1;
mathematic function
freopen("power.txt","w",stdout);
scanf("%ld %ld",&a,&n);
for(i=0;i<n;i++)
answer*=a;
printf("The answer is
%ld\n",answer);
printf("The answer Is %f in
mathematic function\n",pow(a,n));
return 0;
}
練習
 輸出 99 乘法表
 修改成四個表一列
解答 1
#include <stdio.h>
int main()
{
long i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
printf("%ld*%ld=%ld\n",i,j,i*j);
printf("\n");
}
}
結果
1*1=1
1*2=2
…..

2*1=2
….

9*9=81;
解答 2
#include <stdio.h>
int main()
{
long i,j,k;
for(i=2;i<=9;i+=4)
{
for(j=1;j<=9;j++)
{
for(k=0;k<4;k++)
printf("%ld*%ld=%ld\t",i+k,j,(i+k)*j);
printf("\n");
}
printf("\n");
}
}
結果
2*1=2 3*1=3 4*1=4 5*1=5
2*2=4 3*2=6 4*2=8 5*2=10
2*3=6 3*3=9 4*3=12 5*3=15
2*4=8 3*4=12 4*4=16 5*4=20
2*5=10 3*5=15 4*5=20 5*5=25
2*6=12 3*6=18 4*6=24 5*6=30
2*7=14 3*7=21 4*7=28 5*7=35
2*8=16 3*8=24 4*8=32 5*8=40
2*9=18 3*9=27 4*9=36 5*9=45

6*1=6 7*1=7 8*1=8 9*1=9


6*2=12 7*2=14 8*2=16 9*2=18
6*3=18 7*3=21 8*3=24 9*3=27
6*4=24 7*4=28 8*4=32 9*4=36
6*5=30 7*5=35 8*5=40 9*5=45
6*6=36 7*6=42 8*6=48 9*6=54
6*7=42 7*7=49 8*7=56 9*7=63
6*8=48 7*8=56 8*8=64 9*8=72
6*9=54 7*9=63 8*9=72 9*9=81
while loop
While( 條件表示式 )
{
敘述式 ;
}
條件式成立的條件
True=1 或 0 以外的其他數
False=0
練習
用 while 迴圈計算 1+…+100
解答
#include <stdio.h>
int main()
{
long i=1,sum=0;
while(i<=100)
{
sum+=i;
i++;
}
printf("The sum is %ld\n",sum);
return 0;
}
Output:
The sum is 5050
簡化
#include <stdio.h>
int main()
{
long i=1,sum=0;
while(i<=100)
sum+=i++;
printf("The sum is %ld\n",sum);
return 0;
}
再簡化
#include <stdio.h>
int main()
{
long i=1,sum=0;
while(++i<=100)
sum+=i;
printf("The sum is %ld\n",sum);
return 0;
}
練習
輸入一個整數
將整數反向輸出
Ex.
123456
654321
解答
#include <stdio.h> 123456
int main()
{ 654321
long n;
scanf("%ld",&n);
while(n!=0)
{

printf("%ld",n%10);
n/=10;
}
printf("\n");
return 0;
}
練習
 輸入 a,b
 輸出 a,b 的最大公因數 (a>=b)
解答
#include <stdio.h>
20 12
int main()
{ G.C.D is 4
long a,b,temp;
scanf("%ld %ld",&a,&b);
169 13
while(a%b!=0) G.C.D is 13
{
a%=b;
13 169
temp=b; G.C.D is 13
b=a;
a=temp; Why?
}
printf("G.C.D is %ld\n",b);
return 0;
}
練習
用 do-while
輸入四位數字
檢查其是否為 1234
若不是的話要求重新輸入
解答
#include <stdio.h> Enter a number with four digits
int main() 5678
{
Enter a number with four digits
long
pass=0,answer=1234; 1234
do You get it.
{
printf("Enter a
number with four digits\n");

scanf("%d",&pass);
}while(pass!=answer);
printf("You get it.\n");
return 0;
do-while loop
do
{
敘述列 ;
}while( 條件表示式 ); 別忘了有分號
i=1;
sum=0;
do
{
sum+=i++;
}while(i<=100);
範例
 用 do-while 計算 12+22+32+…+102
解答
#include <stdio.h>
int main()
{
long i=1,sum=0;
do
{
sum+=i*i;
i++;

}while(i<=10);
printf("%ld\n",sum);
return 0;
}
break
//Program name:04-04-01A.c
#include <stdio.h>
int main()
{
int i,s=0;
for (i=1;i<=100;i++)
{
if (i>5) break;
s+=i;
}
printf("i=%d\n",i); // 輸出 i 結果
printf("s=%d\n",s); // 輸出 s 內容
return 0;
}
i=6
s=15
範例
 模擬使用者輸入四個數字的密碼
 提供三次輸入機會
 正確則顯示歡迎畫面
 錯誤則再顯示一次輸入畫面
 錯誤三次則拒絕登入
解答
#include <stdio.h> Please enter your 4-digit password
int main()
{ 4567
long i,input,password=1234; Wrong.
for(i=0;i<3;i++)
{
Please enter your 4-digit password
printf("Please enter your 4847
4-digit password\n");
scanf("%ld",&input);
Wrong.
if(input==password) Please enter your 4-digit password
{
3333
printf("Correct.\n"); Wrong.
break;
}
You can't log in.
else printf("Wrong.\n");
}
if(i==3) printf("You can't log in.\n");
return 0;
}
continue
//Program name:04-04-01A.c
#include <stdio.h>
int main()
{
int i,s=0;
for (i=1;i<=100;i++)
{
if (i>5) continue;
s+=i;
}
printf("i=%d\n",i); // 輸出 i 結果
printf("s=%d\n",s); // 輸出 s 內容
return 0;
}
i=101
s=15
範例
 計算 1~100 之間所有 2 或 3 的倍數但不是
6 的倍數的和
解答
#include <stdio.h>
int main()
{
long i,sum=0;
for(i=1;i<=100;i++)
{
if(i%6==0) continue;
else if(i%2==0 || i%3==0) sum+=i;
}
printf("%ld\n",sum);
return 0;
}
2601
goto( 不建議使用 )
//Program name:04-04-01A.c
#include <stdio.h>
int main()
{
int i,s=0;
for (i=1;i<=100;i++)
{
if (i>5) goto TheEnd;
s+=i;
}
TheEnd:
printf("i=%d\n",i); // 輸出 i 結果
printf("s=%d\n",s); // 輸出 s 內容
return 0;
}
i=6
s=15
一維陣列
 資料型態 陣列名稱 [ 陣列大小 ];
 Ex.
 int a[10]; //a[0]~a[9]
 float f[20];
 Char str[40];
初值設定
int a[5];
a[0]=2;
a[1]=4;
a[2]=6;
a[3]=8;
a[4]=10;
OR
int a[5]={2,4,6,8,10}; // 只有初值設定時才可用
int a[]={2,4,6,8,10};
範例
 寫一個程式可以輸入五個數值,找出其最
大值及最小值
解答
#include <stdio.h>
int main()
54321
{ MAX=5,MIN=1
long i,input[5],max,min;
freopen("maxmin.txt","w",stdout);
for(i=0;i<5;i++)
scanf("%ld",&input[i]);
max=min=input[0];
for(i=1;i<5;i++)
{
if(input[i]>max)
max=input[i];
if(input[i]<min)
min=input[i];
}

printf("MAX=%ld,MIN=%ld\n",max,min);
return 0;
範例
 費伯納西數列 f(n)=f(n-1)+f(n-2) n>=2
 f(0)=1,f(1)=1
 輸入 n 請算出 f(n)n 不要太大會溢位
(overflow)
解答
#include <stdio.h> 2input
int main() 2output
{
3 input
long a[100]={1,1},i,n;
scanf("%ld",&n);
3 output
for(i=2;i<100;i++) 4 input
a[i]=a[i-1]+a[i- 5 output
2]; 5 input
printf("%ld\n",a[n]);
8 output
return 0;
}
字元一維陣列的初值設定
char a[6];
a[0]=‘A’;
a[1]=‘P’;
a[2]=‘P’;
a[3]=‘L’;
a[4]=‘E’;
a[5]=‘\0’;
OR
char a[6]={‘A’,’P’,’P’,’L’,’E’,’\0’}; // 僅限於初值設定
char a[]={‘A’,’P’,’P’,’L’,’E’,’\0’}; // 僅限於初值設定
OR
char a[6]=“APPLE”; // 僅限於初值設定 , 系統自動在 a[5] 補’ \0’
char a[]=“APPLE”; // 僅限於初值設定
讀取與輸出
char a[50];
scanf(“%c”,&a[0]); // 讀一個字元 , 不忽略空白 , 亦不忽
略’ \n’
scanf(“ %c”,&a[0]); // 忽略空白及分行符號
scanf(“%s”,a); // 讀一個字串以空白 ,Tab,Enter 隔開
// 注意沒有 &
範例
 輸入一字串
 設計可以計算字串長度的程式
解答
#include <stdio.h> abcdefghi
int main() 9
{
long len;
char input[100];
scanf("%s",input);

for(len=0;input[len]!='\0';len+
+);

printf("Length:%ld\n",len);
return 0;
範例
 輸入兩字串以空白隔開
 設計一程式判斷兩個字串是否相同
解答
#include <stdio.h>
int main() abc abc
{
long i,yes; Equal
char a[50],b[50];
scanf("%s %s",a,b); abcde abc
yes=1;
for(i=0;i<50;i++) Unequal
{
if(a[i]=='\0' && b[i]=='\0') abc abcde
break;
if(a[i]!=b[i]) Unequal
{
yes=0;
break;
}
}
if(yes) printf("Equal\n");
else printf("Unequal\n");
return 0;
多維陣列
int a[2][4];
float f[5][10][5];
char strs[10][50];
int a[2][4]={{1,3,5,7},{2,4,6,8}};
int a[][4]={{1,3,5,7},{2,4,6,8}};// 第一維不可省
, 其餘可省
輸入
 scanf(“%ld %s”,&a[1][3],strs[9]);
範例
 設計可以計算兩個 3*3 矩陣相加 , 轉置
 設計可以計算 2*3 及 3*2 矩陣相乘
相加
#include <stdio.h>
int main()
{
long matrix[2][3][3];
long i,j,k,temp;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
scanf("%ld",&matrix[i][j][k]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
matrix[0][i][j]+=matrix[1][i][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%ld ",matrix[0][i][j]);
printf("\n");
}
return 0;
}
結果
123
456
789

987
654
321

10 10 10
10 10 10
10 10 10
#include <stdio.h>
轉置
int main()
{
long matrix[2][3][3];
long i,j,k,temp;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
scanf("%ld",&matrix[i][j][k]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<j;k++)
{
temp=matrix[i][j][k];
matrix[i][j][k]=matrix[i][k][j];
matrix[i][k][j]=temp;
}
for(k=0;k<2;k++)
{
printf("%ld\n",k);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%ld ",matrix[k][i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}
結果
123
456
789

987
654
321

147
258
369

963
852
741
矩陣相乘
#include <stdio.h>
int main()
{
long m1[3][2],m2[2][3],answer[3][3];
long i,j,k;
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf("%ld",&m1[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%ld",&m2[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
answer[i][j]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<2;k++)
answer[i][j]+=m1[i][k]*m2[k][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3ld ",answer[i][j]);
printf("\n");
}
return 0;
}
結果
12
34
56
123
456
9 19 15
19 26 33
29 40 51
質因數分解
 差和級數
 猜數字遊戲
 等比級數

You might also like