92
ASCII Art 1 -------------------------------------------------------- | 時間 | A | B | C | D | E | F | -------------------------------------------------------- 1 | 08:30-09:05 | 中文 | 英文 | 數學 | 電腦 | 物理 | 生物 | 2 | 09:05-09:40 | ... -------------------------------------------------------- 3 | 10:00-10:35 | 化學 | 中史 | 世史 | 地理 | 美術 | 體育 | 4 | 10:35-11:10 | ... -------------------------------------------------------- 5 | 11:25-12:00 | 音樂 | 家政 | 宗教 | 經濟 | 通識 | 會計 | 6 | 12:00-12:35 | ... -------------------------------------------------------- 7 | 02:00-02:35 | 週會 ... 8 | 02:35-03:10 | 週會 ... -------------------------------------------------------- 1. TimeTable.c (時間表) ASCII Art 2 2. ascii-art.c (利用printf("...");輸出以下圖案) 如何正確輸出特別字符 % " \ 試試看: printf("試試看:%% \t \\ \r \" \n"); int x = 10; printf("x = %i \n", x);

ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

ASCII Art 1

--------------------------------------------------------| 時間 | A | B | C | D | E | F |

--------------------------------------------------------1 | 0830-0905 | 中文 | 英文 | 數學 | 電腦 | 物理 | 生物 |2 | 0905-0940 | --------------------------------------------------------3 | 1000-1035 | 化學 | 中史 | 世史 | 地理 | 美術 | 體育 |4 | 1035-1110 | --------------------------------------------------------5 | 1125-1200 | 音樂 | 家政 | 宗教 | 經濟 | 通識 | 會計 |6 | 1200-1235 | --------------------------------------------------------7 | 0200-0235 | 週會 8 | 0235-0310 | 週會 --------------------------------------------------------

1 TimeTablec (時間表)

ASCII Art 2

2 ascii-artc (利用printf()輸出以下圖案)

如何正確輸出特別字符

試試看printf(試試看 t r n)

int x = 10printf(x = i n x)

ASCII Art 3

1 ----2 ___ `3 --()()---4 5 6 `-__- _-7 `_ `8 `9 0 Snoopy 1 `---------2 ---` | ---3 (______|______)

1 o2_____ o3 ]|_n_n__][4_|__|________)lt5 OO OOOO- OO_

1 2 =0=3 |

1 2 _3 |4 5

ASCII Art 4

1 --2 ---------------+---------------3 --- ^^[---- -4 |^+----+ |----------- 5 (- + |----| ------------+ 6 ==----------- 7 ~-|---|--

1 2 gt|lt3 4 v

1 o2 |3 -=-4 ||

scanfgets 1

F4 ICT C Programming (L3)

姓名 性別 班別 號數 YOB dd-mmID 電郵 體重kg 身高m 地址 電話

Name 陳大文Class 4A (01)DOB 93-12-25Age 16BMI 224Wt 1105 lbHt 6 ft 12 in

參考C-revision-f3ppt

1 kg = 22 lb1 m = 33 ft

陳大文M4A1199325-12A1234567chantmhotmailcom6517Apleichau2874-3121

namegenderclassnoyyddmmidnoemailwthtaddrphone

問題 輸入變數

scanfgets 2

printf(姓名sn name) 輸出文字printf(性別cn sex) 輸出單字printf(年齡in age) 輸出整數printf(體重fn wt) 輸出小數

include ltstdiohgt

char name[20] 宣告姓名 (文字)char sex 宣告性別 (單字MF)int age 宣告年齡 (整數)float wt 宣告體重 (小數)

main()printf(姓名) gets(name) 輸入文字fflush(stdin)printf(性別) scanf(c ampsex) 輸入單字printf(年齡) scanf(i ampage) 輸入整數printf(體重) scanf(f ampwt) 輸入小數

scanfgets 3

6

5

fput()put()printf() 輸出

4

3

fget()get()scanf() 輸入

2

1

printf() 輸出scanf() 輸入宣告

char x[10]

char x

float x

int x

scanf(fampx)

scanf(iampx)

printf(f x)

printf(Number = i n x)

x=fgetc(stdin)x=fgetchar()------

x=getc(stdin)x=getchar()x=getche()x=getch()

fflush(stdin)scanf(campx)fscanf(stdincampx)

---fgets(xsizeof(x)stdin)

gets(x)---

scanf(sx)fscanf(stdinsx)

---fputs(xstdout)

puts(x)---

printf(sx)fprintf(stdoutsx)

char x[10]

fputc(xstdout)------

putc(xstdout)putch(x)putchar(x)

printf(cx)fprintf(stdoutcx)

char x

scanfgets 4

include ltstdiohgtmain()

printf(姓名 )printf(性別 )printf(班別 )printf(號數 )printf(YOB )printf(dd-mm)printf(ID )printf(電郵 )printf(體重kg )printf(身高m )printf(地址 )printf(電話 )

int noyyddmmfloat wthtchar name[10]genderclass[3]

idno[10]email[20]addr[20]phone[10]

gets(name)

gets(class)

gets(idno)gets(email)

gets(addr)gets(phone)

scanf(c ampgender)

scanf(i ampno)scanf(i ampyy)scanf(i-i ampdd ampmm)

scanf(f ampwt)scanf(f ampht)

scanfgets 5

其他練習心臟病機會

利用 gotoxy setrgb 重寫程式一次過出現所有問題再逐一輸入

例include consolec

setrgb(1) printf(班)setrgb(4) printf(別 )hellipgotoxy(150) gets(name)hellipgotoxy(152) gets(class)hellip

參考consoleppt amp dow2009ppt

功課

Name ChanGender mYOB 1990

A Chan is a 19-year-old boy

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 2: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

ASCII Art 3

1 ----2 ___ `3 --()()---4 5 6 `-__- _-7 `_ `8 `9 0 Snoopy 1 `---------2 ---` | ---3 (______|______)

1 o2_____ o3 ]|_n_n__][4_|__|________)lt5 OO OOOO- OO_

1 2 =0=3 |

1 2 _3 |4 5

ASCII Art 4

1 --2 ---------------+---------------3 --- ^^[---- -4 |^+----+ |----------- 5 (- + |----| ------------+ 6 ==----------- 7 ~-|---|--

1 2 gt|lt3 4 v

1 o2 |3 -=-4 ||

scanfgets 1

F4 ICT C Programming (L3)

姓名 性別 班別 號數 YOB dd-mmID 電郵 體重kg 身高m 地址 電話

Name 陳大文Class 4A (01)DOB 93-12-25Age 16BMI 224Wt 1105 lbHt 6 ft 12 in

參考C-revision-f3ppt

1 kg = 22 lb1 m = 33 ft

陳大文M4A1199325-12A1234567chantmhotmailcom6517Apleichau2874-3121

namegenderclassnoyyddmmidnoemailwthtaddrphone

問題 輸入變數

scanfgets 2

printf(姓名sn name) 輸出文字printf(性別cn sex) 輸出單字printf(年齡in age) 輸出整數printf(體重fn wt) 輸出小數

include ltstdiohgt

char name[20] 宣告姓名 (文字)char sex 宣告性別 (單字MF)int age 宣告年齡 (整數)float wt 宣告體重 (小數)

main()printf(姓名) gets(name) 輸入文字fflush(stdin)printf(性別) scanf(c ampsex) 輸入單字printf(年齡) scanf(i ampage) 輸入整數printf(體重) scanf(f ampwt) 輸入小數

scanfgets 3

6

5

fput()put()printf() 輸出

4

3

fget()get()scanf() 輸入

2

1

printf() 輸出scanf() 輸入宣告

char x[10]

char x

float x

int x

scanf(fampx)

scanf(iampx)

printf(f x)

printf(Number = i n x)

x=fgetc(stdin)x=fgetchar()------

x=getc(stdin)x=getchar()x=getche()x=getch()

fflush(stdin)scanf(campx)fscanf(stdincampx)

---fgets(xsizeof(x)stdin)

gets(x)---

scanf(sx)fscanf(stdinsx)

---fputs(xstdout)

puts(x)---

printf(sx)fprintf(stdoutsx)

char x[10]

fputc(xstdout)------

putc(xstdout)putch(x)putchar(x)

printf(cx)fprintf(stdoutcx)

char x

scanfgets 4

include ltstdiohgtmain()

printf(姓名 )printf(性別 )printf(班別 )printf(號數 )printf(YOB )printf(dd-mm)printf(ID )printf(電郵 )printf(體重kg )printf(身高m )printf(地址 )printf(電話 )

int noyyddmmfloat wthtchar name[10]genderclass[3]

idno[10]email[20]addr[20]phone[10]

gets(name)

gets(class)

gets(idno)gets(email)

gets(addr)gets(phone)

scanf(c ampgender)

scanf(i ampno)scanf(i ampyy)scanf(i-i ampdd ampmm)

scanf(f ampwt)scanf(f ampht)

scanfgets 5

其他練習心臟病機會

利用 gotoxy setrgb 重寫程式一次過出現所有問題再逐一輸入

例include consolec

setrgb(1) printf(班)setrgb(4) printf(別 )hellipgotoxy(150) gets(name)hellipgotoxy(152) gets(class)hellip

參考consoleppt amp dow2009ppt

功課

Name ChanGender mYOB 1990

A Chan is a 19-year-old boy

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 3: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

scanfgets 1

F4 ICT C Programming (L3)

姓名 性別 班別 號數 YOB dd-mmID 電郵 體重kg 身高m 地址 電話

Name 陳大文Class 4A (01)DOB 93-12-25Age 16BMI 224Wt 1105 lbHt 6 ft 12 in

參考C-revision-f3ppt

1 kg = 22 lb1 m = 33 ft

陳大文M4A1199325-12A1234567chantmhotmailcom6517Apleichau2874-3121

namegenderclassnoyyddmmidnoemailwthtaddrphone

問題 輸入變數

scanfgets 2

printf(姓名sn name) 輸出文字printf(性別cn sex) 輸出單字printf(年齡in age) 輸出整數printf(體重fn wt) 輸出小數

include ltstdiohgt

char name[20] 宣告姓名 (文字)char sex 宣告性別 (單字MF)int age 宣告年齡 (整數)float wt 宣告體重 (小數)

main()printf(姓名) gets(name) 輸入文字fflush(stdin)printf(性別) scanf(c ampsex) 輸入單字printf(年齡) scanf(i ampage) 輸入整數printf(體重) scanf(f ampwt) 輸入小數

scanfgets 3

6

5

fput()put()printf() 輸出

4

3

fget()get()scanf() 輸入

2

1

printf() 輸出scanf() 輸入宣告

char x[10]

char x

float x

int x

scanf(fampx)

scanf(iampx)

printf(f x)

printf(Number = i n x)

x=fgetc(stdin)x=fgetchar()------

x=getc(stdin)x=getchar()x=getche()x=getch()

fflush(stdin)scanf(campx)fscanf(stdincampx)

---fgets(xsizeof(x)stdin)

gets(x)---

scanf(sx)fscanf(stdinsx)

---fputs(xstdout)

puts(x)---

printf(sx)fprintf(stdoutsx)

char x[10]

fputc(xstdout)------

putc(xstdout)putch(x)putchar(x)

printf(cx)fprintf(stdoutcx)

char x

scanfgets 4

include ltstdiohgtmain()

printf(姓名 )printf(性別 )printf(班別 )printf(號數 )printf(YOB )printf(dd-mm)printf(ID )printf(電郵 )printf(體重kg )printf(身高m )printf(地址 )printf(電話 )

int noyyddmmfloat wthtchar name[10]genderclass[3]

idno[10]email[20]addr[20]phone[10]

gets(name)

gets(class)

gets(idno)gets(email)

gets(addr)gets(phone)

scanf(c ampgender)

scanf(i ampno)scanf(i ampyy)scanf(i-i ampdd ampmm)

scanf(f ampwt)scanf(f ampht)

scanfgets 5

其他練習心臟病機會

利用 gotoxy setrgb 重寫程式一次過出現所有問題再逐一輸入

例include consolec

setrgb(1) printf(班)setrgb(4) printf(別 )hellipgotoxy(150) gets(name)hellipgotoxy(152) gets(class)hellip

參考consoleppt amp dow2009ppt

功課

Name ChanGender mYOB 1990

A Chan is a 19-year-old boy

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 4: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

scanfgets 3

6

5

fput()put()printf() 輸出

4

3

fget()get()scanf() 輸入

2

1

printf() 輸出scanf() 輸入宣告

char x[10]

char x

float x

int x

scanf(fampx)

scanf(iampx)

printf(f x)

printf(Number = i n x)

x=fgetc(stdin)x=fgetchar()------

x=getc(stdin)x=getchar()x=getche()x=getch()

fflush(stdin)scanf(campx)fscanf(stdincampx)

---fgets(xsizeof(x)stdin)

gets(x)---

scanf(sx)fscanf(stdinsx)

---fputs(xstdout)

puts(x)---

printf(sx)fprintf(stdoutsx)

char x[10]

fputc(xstdout)------

putc(xstdout)putch(x)putchar(x)

printf(cx)fprintf(stdoutcx)

char x

scanfgets 4

include ltstdiohgtmain()

printf(姓名 )printf(性別 )printf(班別 )printf(號數 )printf(YOB )printf(dd-mm)printf(ID )printf(電郵 )printf(體重kg )printf(身高m )printf(地址 )printf(電話 )

int noyyddmmfloat wthtchar name[10]genderclass[3]

idno[10]email[20]addr[20]phone[10]

gets(name)

gets(class)

gets(idno)gets(email)

gets(addr)gets(phone)

scanf(c ampgender)

scanf(i ampno)scanf(i ampyy)scanf(i-i ampdd ampmm)

scanf(f ampwt)scanf(f ampht)

scanfgets 5

其他練習心臟病機會

利用 gotoxy setrgb 重寫程式一次過出現所有問題再逐一輸入

例include consolec

setrgb(1) printf(班)setrgb(4) printf(別 )hellipgotoxy(150) gets(name)hellipgotoxy(152) gets(class)hellip

參考consoleppt amp dow2009ppt

功課

Name ChanGender mYOB 1990

A Chan is a 19-year-old boy

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 5: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

scanfgets 5

其他練習心臟病機會

利用 gotoxy setrgb 重寫程式一次過出現所有問題再逐一輸入

例include consolec

setrgb(1) printf(班)setrgb(4) printf(別 )hellipgotoxy(150) gets(name)hellipgotoxy(152) gets(class)hellip

參考consoleppt amp dow2009ppt

功課

Name ChanGender mYOB 1990

A Chan is a 19-year-old boy

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 6: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

1

Story-telling 故事一則

Q 姓名name 陳大文年齡Age 16身高ht(m) 17體重wt(kg) 60朋友friend 陳七喜歡顏色 紅

陳大文是個樵夫一天他走到森林

變數 int float charnameagehtwtfriendcolor

忽然遇到一條身長17米的噩魚牠的名字叫做陳七

張口要把陳大文吃掉剛巧獵人經過hellip

噩魚陳七身中16槍即時倒地

2

Q 全班人數 40男生人數 24

A 女生人數 = 16

Q 西瓜總數 50吃去多少 20

A 餘下西瓜 30 個

Q 橙每個價錢 5小明買了 20

A 應付款 $100

Guessing game Cuboid長方體

Q L長 = 3 W闊 = 4 H高 = 5Area = Volume = Total Surface Area =

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 7: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 1

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123123

-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

123400000123123412340

輸出句子 printf (adv)輸出

printf (adv) 2

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 8: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 3

21 printf(dn 73)22 printf(dn 10)23 printf(dn 010)24 printf(dn 0x10)

25 printf(041n)26 printf(x41n)27 printf(0 n)

110816

A

輸出

16進制

8進制

28 printf(on 20)29 printf(xn 20)

2414

16進制 (x)

8進制 (o)

printf (adv) 4

n

r

t

v

f

b

a

字元

語言控制字元表

換行new-linex0A012

無換行的歸位returnx0D015

Tab定位(水平)x09011

Tab定位(垂直)x0B013

印出反斜線 字元x5c134

印出單引號 字元x27047

印出雙引號 字元x22042

跳頁form-feedx0c014

退位backspacex08010

發出一聲嗶的聲音beepx07007

功能敘述16進位8進位

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 9: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 5

1 printf(We dont have enough timen)2 printf(Everybody say

Later is better than nevern)

3 printf(Computer is powerfukbln)4 printf(who can make itrYoun)5 printf(Failure 151163 the mother

x6fx66 successn)

We dont have enough timeEverybody say Later is better than never

Computer is powerfulYou can make itFailure is the mother of success

輸出

printf (adv) 6

輸出以小數點表示的浮點數f

輸出字串s

輸出字元c

輸出十進位的整數id功能敘述字元

函數資料轉換型態

輸出十六進位的整數x

輸出八進位的整數o

自動選擇以小數點表示或指數表示的浮點數g

輸出以指數表示的浮點數e

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 10: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 7

八進位十六進位

The ASCII Code of 69 is EThe Octal value of 69 is 105The Hexadecimal value of 69 is 45

將十進位的數值轉換成字元(c)八進位(o)十六進位(x)

1 includeltstdiohgt

2 main()

3 int a = 69

4 printf(The ASCII Code of i is cn a a)

5 printf(The Octal value of i is on a a)

6 printf(The Hexadecimal value of i is xn a a)

7

printf (adv) 8

bull 語法一

bull ni eg 6i 02i

bull 語法二

bull -nd eg -6d

+靠右

-靠左

前面補0

顯示資料的欄寬和精確度

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 11: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 9

修飾語

|1021||1021|| 1021|| +1021||1021 |

includeltstdiohgt

main()

int a = 1021

printf(|i| n a)

printf(|2d| n a)

printf(|6d| n a)

printf(|+6d|n a)

printf(|-6d|n a)

-靠左

printf (adv) 10

bull語法一

bullmnf eg 101f 2f

bull語法二

bull-mnf eg -101f

共10位小數後1位

+靠右-靠左

小數後2位

小數點浮點輸出

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 12: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 11

修飾語變化

|691020996||69102|| 69102|

|69102 |

includeltstdiohgt

main()

float f

f = 691021

f 小數後6位

小數後2位四捨五入

printf(|f| n f)

printf(|22f| n f)

printf(|92f| n f)

printf(|-92f|n f)

printf (adv) 12

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 13: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf-quiz 1

1 printf(|d| n 123)2 printf(|5d| n 123)3 printf(|-5d| n 123)4 printf(|05d| n 123)

5 printf(|+5d| n 123)printf(|+5d| n -123)

6 printf(| 5d| n 123)printf(| 5d| n -123)

Exercise 1

printf-quiz 2

1 printf(|f| n 12345)2 printf(|9f| n 12345)3 printf(|92f| n 12345)4 printf(|-92f|n 12345)

Exercise 2

n d d 5d +5d -5d

0

123

-123

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 14: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf-quiz 3

試寫一句 printf 句子顯示以下字句

Dusercproghtml

comp20soc

座佑銘自古成功在嘗試

printf

printf-quiz 4

資料 格式 結果

12345

10d

+d

-10d

d

010d

123456

72f

0103f

+104f

ICT Quiz

Exercisesabab

Q enter ab 25 12A 2512 = 300Q enter ab 12 215A 12215 = 2580

251225050

300

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 15: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Age Boy Girl Man Woman 1

include ltstdiohgtinclude ltctypehgtmain ()

Q Name ChrisYOB 1980Sex m

A Chris is a 28-year-old man

_____ name gender=M 宣告變數_____ yob age hh

printf(Q Name )printf(YOB )printf(Sex )printf(Time )

輸入資料

c = toupper (c)

2

age

ge21

lt21

Admission Fee

Girl

Boy

00 le time lt 12 le time lt 18 le time lt

Good morningGood afternoonGood evening

age

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 16: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Age Boy Girl Man Woman 3

if (agegt=21)

else

if (hellip)printf (mann)

elseprintf (n)

if (hellip)printf (n)

elseprintf (n)

age =printf (hellip is a -year-old n hellip)

Age Boy Girl Man Woman 4

printf (Good )

if (timehellip)printf (hellip )

else if (hellip)printf (hellip )

elseprintf (hellip )

printf (hellip)

if (hellip)printf (hellip )

elseprintf (hellip )

00 le time lt 1212 le time lt 1818 le time lt 24

Good morningGood afternoonGood evening

Mr Ms

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 17: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Numbers to Digits 1

Modulo Arithmetic xy xdividey 的餘數

10divide4 = 2hellip211divide4 = 2hellip312divide4 = 3hellip013divide4 = 3hellip114divide4 = 3hellip2hellip

104 = 2114 = 2124 = 3134 = 3144 = 3hellip

104 = 2114 = 3124 = 0134 = 1144 = 2hellip

(1) 95 = (2) 950 =

(3) 2000400 = (4) 9012 =

118

06

考考你

商hellip餘數

Numbers to Digits 2

1234 10 rarr1234 100 rarr1234 1000 rarr

1234 10 rarr1234 100 rarr1234 1000 rarr

試利用 及 組合把 4321 分拆為千百十個位4321

123121

434234

千位 =百位 =十位 =個位 =

4321 1000

4321 10

練習1 要求使用者輸入n (4位數字)2 輸出個別數字

例如輸入n (4位數字) 94139413

宣告變數int n d4 d3 d2 d1

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 18: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Numbers to Digits 3

試利用 及 組合把 4321 分拆為千百十個位4321

千位 =百位 =十位 =個位 =

4321 1000

4321 10

4321 1000 1004321 100 10

4321 100 104321 10 10

假若使用者輸入少於4位數字

例如輸入n (4位數字) 5200520

答案

額外要求

Numbers to Digits 4

試把程式改為3位數2位數3位數3位數hellip

AB150 12 = 126

__1212 )150

12030246

要求使用者輸入兩個2位數(ab)

例如輸入a (2位數字) 34輸入b (2位數字) 12

功課AB amp AB

橫式 34 12 = 408直式

34 12-----------

34068

-----------408

ab

c=d=

ab(十)ab(個)

ab

q=ab

積 product 商 quotient 餘 remainder

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 19: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Numbers to Digits 5

試利用 及 組合把 n=7654321 分拆為百萬十萬萬千百十個位

d7 = 百萬位 =d6 = 十萬位 =d5 = 萬 位 =d4 = 千 位 =d3 = 百 位 =d2 = 十 位 =d1 = 個 位 =

考考你

d = n x y d = n x y

d7=n106x=105 y=10x=104 y=10x=103 y=10x=102 y=10x=101 y=10d1=n10

d7=n106x=106 y=105

x=105 y=104

x=104 y=103

x=103 y=102

x=102 y=101

d1=n10

printf (adv) 6

1 printf (d n 123)2 printf (+d n 123)3 printf (+d n -123)4 printf ( d n 123)5 printf ( d n -123)

123+123-123

123-123

6 printf (f n 1234)7 printf (f n 1234)8 printf (1f n 1234)9 printf (82fn 1234)

1234000001231234

12340

輸出句子 printf (adv)輸出

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 20: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

printf (adv) 7

11 printf(dn 205)12 printf(dn 2+3)13 printf(fn 2+3)

14 printf(fn 2050)15 printf(fn 205e2)16 printf(fn 115e-3)

20550000000

2050000002050000000001150

輸出

17 printf(dn 73)18 printf(fn 73)19 printf(dn 703)20 printf(fn 703)

20001150-14316557652333333

printf (adv) 8

char s[6]=abcdeprintf(1234567890n)printf(10s$ n s)printf(-10s$ n s)printf(s n 8s)

printf(f n 102 4567)printf(f n 102 3456)printf(f n 102 2345)

1234567890abcde$

abcde $abcde

xy

4573456

23450

printf(c n 2x)printf(c n 3y)

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 21: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

體重指標 BMI = 體重 身高sup2

Body Mass Index (BMI)= weight (height)sup2 健康狀況g ( g )

Male男 Female女

1 略瘦 under-weight0 20 0 19

2 正常 normal

3 略肥 over-weight

4 過肥 obese

20 25

25 30

30

19 24

24 29

29 4 過肥 obese30 29

體重 (kg) 55 2變數類型輸

身高 (m) 17

BMI 1903 1需要變數多少個

wt ht bmi group健康狀況

變數類型intfloat

wt ht bmi group健康狀況

男 1 略瘦

女 2 正常

charfloat int

輸出

體重指標 BMI 1

女 2 正常

3 步驟

1宣告變數 wt ht bmi group

3 步驟

2要求使用者輸入體重(wt)身高(ht)

3 計算體重指標 bmi wt3計算體重指標 bmi

4顯示 bmi 值 2ht

wtbmi

5計算健康狀況(男) group (1-4)

6 輸出健康狀況體重 (kg) 556輸出健康狀況身高 (m) 17

BMI 1903 7若一切正確可按性別 gender 計算健康狀況 健康狀況

男 1 略瘦

女 2 正常

可按性別 gender 計算健康狀況

體重指標 BMI 2

女 2 正常

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 22: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

步驟

1宣告變數 wt ht bmi group 3計算體重指標 bmi

步驟

bmi =_____ wt ht bmi group

bmi =_____ group

2輸入體重(wt)身高(ht) 4顯示 bmi 值

printf體重

printf (bmi = n )printfscanfprintf

printfprintf (體重 (kg) )scanf (__ ___)printf (身高 (m) )

printf ( bmi = n )

printfscanfprintf ( 身高 (m) )scanf (__ ___)

體重指標 BMI 3

5 計算健康狀況(男) (1 4) BMI 狀況 建議5計算健康狀況(男) group (1-4) BMI 狀況 建議

0 20 1 略瘦

20 25 2 正常if (gender_______)25 30 3 略肥

30 4 過肥

if (bmi________) group=1

6輸出健康狀況else

if (group ) printf(正常n)if (group____) printf(略瘦n)

if (group____) printf( 正常n )if (group____) printf(略肥n)if (group____) printf(過肥n) (g p____) p ( )

7若一切正確可分開計算男女健康狀況

體重指標 BMI 4

可分開計算男女健康狀況

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 23: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

CAL (Flash Action-script)

( )

1 notepad matchingxml cs1xx-mathsxml ( utf8) (5 10 F3 ) 2 (1939-1945) (1840-1842) Opium Cocaine

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

3 ( ) (10 )

4 2011 10 09 (sun) 1200 cs1xx-maths-cycfla cs1xx-maths-cycswf cs1xx-maths-cycxml

5 Yict-f4 httpwwwablmcceduhk~scyCITmatchingfla httpwwwablmcceduhk~scyCITmatchingxml httpwwwablmcceduhk~scys1maths1aswf

1

2

ltxml version=10 encoding=UTF-8 standalone=yesgt ltMatching xmlnsxsi=httpwwww3org2001XMLSchema-instancegt ltQgtltKeygt ltKeygtltMatchgtvolumeltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtoriginltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtsymmetryltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtreflectionltMatchgt ltQgt ltQgtltKeygt ltKeygtltMatchgtrotationltMatchgt ltQgt ltMatchinggt

matchingxml cs1xx-maths-cycxml ( utf8)

volume

5

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 24: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

3

myXMLload(matchingxml)

myXMLload(cs131-eng-llcnxml) myXMLload(cs132-hist-lylxml) myXMLload(cs133-math-cycxml)

2 matchingfla ( cs1xx-maths-cycfla) F9( ) myXMLload(mathsxml) Ctrl-Enter

cs131 4A02

4

httpwwwablmcceduhk~scyhomejavascriptio-ex-anshtm

(1) + (2) printf d scanf (3) if-ex if-1 if-2 if-3 bmi (4) abc xyz ceilfloor (5) array notes array01 array02 for(2) (6) roman money string char-str char-str array rand seq(1) rand seq(2) pair-up (7) strcpy strcpy2 (8) strncpy strncpy2 (9) strcmp file fgets (10) card20 cj-array bubble sort binary search (11) queue queue(c) stack

(3) if amp bmi

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 25: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

clock 1

hhmmss

155950

1需要變數多少個

2變數類型intfloatchar

hh mm ss

include ltstdiohgtmain()

int hh mm ss=50

system (pause)

do

while(1)

printf ( ss)ss++

_sleep(1000)

clock 2

hhmmss

155950

ss==60

Y

N

ss++

ss=0mmhellip

3Program Logic 邏輯

F4include consolecx = rand() __ + __ y = rand() __ + __ gotoxy(xy)setrgb(1)printf(ihh)

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 26: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

clock 3

do

_sleep(500) 停止05秒while (sslt60)

int hh=12mm=59ss=50

if(ss==60)ss=0

printf(02irss)ss++

只要sslt60便重複執行

clock 4

doprintf(02i02i02irhhmmss)ss++

_sleep(1000)while(1)

if(ssgt=60)ss=__ mm__

if(mmgt=__)

if(hhgt=__)hh=__ printf(_____n)

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 27: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

consolec 1

gotoxy (xy) 移動游標printf (x=20 y=10)

include ltstdiohgtinclude consolecmain()

int x=20 y=10

y0123456789012345

getxy (ampx ampy) 目前游標位置printf (x=i y=in x y)

x = whereX()y = whereY()printf (x=i y=in x y)

clrscr() 清除螢幕 Clear Screen

x0123456789012345678901234567890123456789hellip

lt標準gt自訂

httpwwwablmcceduhk~scyhomejavascriptconsolehtm

consolec 2

setrgb(0) 白字黑底setrgb(1) 紅字黑底setrgb(2) 綠字黑底setrgb(3) 黃字黑底setrgb(4) 藍字黑底hellip

setrgb(8) 黑字白底setrgb(9) 紅字白底setrgb(10) 綠字白底setrgb(11) 黃字白底setrgb(12) 藍字白底hellipsetrgb(15) 白字白底

setrgb(9)printf(C)setrgb(12)printf( )setrgb(14)printf(Programmingn)setrgb(7)

C Programming

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 28: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

consolec 3

gotoxy(205)_sleep(1000)clreol() clear to end of line

gets(email)

SetScrnSize(8020) 設定視窗大小(闊高)

togglefullscreen() 全螢幕

consolec 4

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)7)printf(白字黑底n)

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 29: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

比較運算子比較運算子

== equal to

= not equal to

gt greater than greater than

lt less than

gt= greater than or equal to

lt= less than or equal tolt less than or equal to

if-statement 1

條件條件a number

運算子

Operator意義

meaning條件

condition值

valueOperator meaning condition valuelt= 小於或等於 x lt= 0 1(true)lt 小於 power lt MAX_POW 0(false)p _ ( )

gt= 大於或等於 x gt= y 0(false)gt 大於 item gt MIN_ITEM 1(true)

== 等於 mom_or_dad == M 1(true)= 不等於 num = SENTINEL 0(false)

if-statement 2

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 30: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

條件 表示式

1 x 和 y 都大於 z ( )1x 和 y 都大於 z

2x 等於 10 或 30

( )

( )

3x 在 y 和 z 之間 (yltz)

4 x 不在 y 和 z 的範圍以內

( )

( )4x 不在 y 和 z 的範圍以內

已知yltz

( )

if-statement 3

比較字符

9 gt= 0

1(true) 0(false)

1 09 gt= 0

a lt e

1 0

1 0

B lt= A

Z == z

1 0

1 0Z == z

a lt= A

1 0

1 0

chgt=a ampamp chlt=z 1 0

if-statement 4

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 31: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

相反條件相反條件

m==n alt=b

complement 相反

( lt 25) || ( t t S ampamp t t D)(agelt=25) || (status=S ampamp status=D)

( (agelt=25) || (status=S ampamp status=D) )

(agelt=25) ampamp (status=S ampamp status=D)

if-statement 5

bull Syntax 語法y 語法if (條件) 句子1 else 句子2

Ex-1if (heart_rate_atrest gt 56 ) 靜止心跳

printf(keep up your exercise programn)elseprintf(Your heart rate is in excellent healthn)printf( Your heart rate is in excellent healthn )

Ex-2if (x 0)if (x=0)

product = product x

if (x=0) average = total x

product 積

if-statement 6

if (x 0) average total x

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 32: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

流程圖流程圖

bull A diagram that shows the step-by-step execution of aA diagram that shows the step by step execution of acontrol structurendash A diamond-shape box represents a decision

ndash A rectangular box represents an assignment statement or a process

靜止心跳

if-statement 7

嵌套式嵌套式

bull if 中有 if嵌套式

+ve

bull 嵌套式minusve

0

bull Dangling else

0

g gif (x gt 0) if (y gt 0)

a = a + 1a = a + 1else

b = b + 1

else 屬於最接近的 if

if-statement 8

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 33: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

嵌套式嵌套式指示

路面情況安全safe

氣溫gt0

小心小心駕駛

小心雪地

小心地滑地滑

if-statement 9

路面狀況

溫度

前面路滑

停車時間=2倍

路面有雪

停車時間=4倍

小心駕駛 小心駕駛

if-statement 10

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 34: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

語法 if (condition ) if (mm==1)語法Syntax

if (condition1)statement1

else if (condition2)

if (mm==1)maxdays = 31

else if (mm==2)( 2)statement2

else if (condition3)

( )maxdays = 28

else if (mm==3)statement3

hellipelse if (condition )

maxdays = 31hellipelse if (mm==12)else if (conditionn)

statementnelse

else if (mm==12)maxdays = 31

elsedefault statement maxdays = 0

if-statement 11

if (a==b) printf (Equal) if (a=b) printf (Equal)( ) p ( q )

else printf (Not Equal)

( ) p ( q )

else printf (Not Equal)

printf (s (a==b) Equal Not Equal)

b = -10a =

a = (blt0) -b bprintf (a=in a)

a = abs(b)

if-statement 12

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 35: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

聘請保安員條件 (Security Guard)填寫以下偽代碼

犯罪紀錄次數

性 別MF0 0M M

填寫以下偽代碼

年 齡歲

工作經驗年

31-45 18-303+

c1 = (犯罪紀錄 )

身 高米 165+ 170+

(犯罪紀錄______)

c2 = (性別______)

c3 = (年齡______) (年齡______) (身高______) (工作經驗______)

c4 = (年齡______) (年齡______) (身高______)

if( (c1) ampamp (c2) ampamp ((c3) || (c4)))合資格 1

if( (c1) ampamp (c2) )f (( 3) || ( )) 合資格 1合資格=1

else合資格=0

if ((c3) || (c4)) 合資格=1else 合資格=0

else 合資格=0

if-statement 13

1 Compare比較 4 input numbersif ( b ) lif ( a gt b ) x = ___ else x = ___if ( c gt d ) y = ___ else y = ___if ( x gt y ) max = else max = if ( x gt y ) max ___ else max ___

2 Leap Year Test 潤年測試i t i l 0 i tif (_____________ || ________________________)

isleapyear=1

int isleapyear = 0 int yy

isleapyear 1else

isleapyear=0

4 年齡age 星期dow 收費fare

if( agelt || agegt )if( agelt || agegt )fare = fare2

if( dow== || dow== )3 某年2月份有多少日

if( dow || dow )fare = fare2

3 某年2月份有多少日max=28 if ( (mm ) (yy ) )

if-statement 14

if ( (mm_____) (yy__________) )max=29

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 36: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

常見錯誤1 if(12 lt age lt 60) 1 if(12 lt age lt 60) 數學與電腦寫法不同改正 if(age age )

2 if(age lt 12 || gt 60) 欠變數名稱 age改 f( )改正 if(age age )

3 if(agegt60) printf() if中間不要加( )改正 if(agegt60) printf()

4 if(agelt12 ampamp agegt60) 沒可能改正 if(age lt 12 age gt 60) 改正 if(age lt 12 age gt 60)

5 if(age = 12) 賦值(age=12)比較(age 12)

if-statement 15

賦值(age=12)比較(age 12)改正 if(age 12)

6 if(age gt 12 || age lt 60) ( g || g )等同 if(1) 改正 if(agegt12 agelt60)

7 if(ans==y ampamp ans==n) puts(ok) 沒可能改正 if(ans==y ans==n) puts(ok)改正 if(ans== y ans== n ) puts( ok )

8 if(ans=y || ans=n) puts(error) 任何輸入都正確任何輸入都正確改正 if(ans=y ans=n) puts(error)

9 scanf(campch)if(ch==a||b||c||d)

改正 if(ch==a || ch==b || ch==c || ch==d) 改正 if(strchr(abcdch) = NULL)

if-statement 16

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 37: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月09日 Greeting 1

3a boy-girlc

Q Gender MAge(1-100) 22

A Man

char gender=Mint age=23

變數 genderage

Q Gender FAge(1-100) 22

A Woman

Q Gender MAge(1-100) 12

A Boy

Q Gender FAge(1-100) 2

A Girl

2012年09月09日 Greeting 2

while(1)fflush(stdin)printf(Gender (MF) )printf(Age(1-100) )

printf(n)

scanfscanfgender = toupper(gender)

printf(A )if(gender )

else

進階BabyLittle boyLittle girlManWomanOld ManOld Woman

char gender=Mint age=23

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 38: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月09日 Greeting 3

__ is a __-year-old __(gt=21 manwoman otherwise boygirl)

Q Name ChrisYOB 1981Sex m

A Chris is a 28-year-old man

Q Name AngelYOB 2000Sex f

A Angel is a 9-year-old girl

9 years later she will be 18

10 years ago he was 18

3b agec

2009-1981=28

2012年09月09日 Greeting 4

3c greetingc 請安

Q Name LeeGender MMarriedSingle MTime(0-23) 22

A Good Evening MrLee

Q Name ChanGender FMarriedSingle MTime(0-23) 10

A Good Morning MrsChan

Q Name ChengGender FMarriedSingle STime(0-23) 15

A Good Afternoon MsCheng

00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

char name[20]=Leechar gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 39: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月09日 Greeting 5

Q 姓氏(陳) 陳性別 mf F已婚未婚 S時間 0-23 15

A 陳先生午安

3d greetingc 請安 00-11 早晨 Morning12-18 午安 Afternoon19-23 晚安 Evening

Q 姓氏(陳) 李性別 mf M已婚未婚 S時間 0-23 10

A 李先生早晨

Q 姓氏(陳) 高性別 mf M已婚未婚 M時間 0-23 20

A 高太太晚安

char name[20]=陳char gender=Mchar mstatus=Mint time=23

變數 namegendermstatustime

2012年09月09日 Greeting 6

while(1)fflush(stdin)printf(Name )

printf(Gender (MF) )printf(MarriedSingle (MS) )printf(Time(0-23) )

if(strlen(name)==0) break

printf(A Good )if(timegt ampamp timelt )

if(gender M) printf( Mr)

printf( snn name)

A Good Evening MrLee

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 40: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

7

26 Roots of a Quadratic equation Axsup2+Bx+C=0Q enter coefficients (ABC) 0 1 2A Error A must be non-zeroQ enter coefficients (ABC) 1 0 -1A root1=10 root2=-10Q enter coefficients (ABC) 1 2 1A root1=-10Q enter coefficients (ABC) 1 2 4A no real root

Hint calculate d=bsup2-4ac output with 1 dp(小數)if A=0 x= -CB

27 日期轉換 conversion dd-mm-yyyy to wordsQ enter date (dd-mm-yyyy) 25-12-2005A Dec 25 2005

28 星期轉換 DOWQ enter day of the week (0-6) 0A SundayQ enter day of the week (0-6) 6A Saturday

Months1 January2 February3 March4 April5 May6 June7 July8 August9 September10October11November12December

Dow0 Sunday1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday

8

i若c大於或等於a及b之和 (不是三角形)

否則ii若csup2等於asup2及bsup2之和 (直角三角形)Right-anglediii若csup2小於asup2及bsup2之和 (銳角三角形)Acuteiv若csup2大於asup2及bsup2之和 (鈍角三角形)Obtusev若a=b=c (等邊三角形)Isoscelesvi若a=b或b=c或c=a (等腰三角形)Equilateral

int abc minmidmax asup2bsup2csup2

31 三角形測試 triangle-testQ 三角形邊長 (altbltc) 3 4 5A 直角三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 4 5A 等腰三角形

Q 三角形邊長 (altbltc) 5 5 5A 等邊三角形A 等腰三角形A 銳角三角形

Q 三角形邊長 (altbltc) 5 2 2A 不是三角形

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 41: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月09日 星座生肖 Zodiac 1

生肖 Chinese Zodiac

羊馬蛇龍兔虎牛鼠豬狗雞猴

11109876543210

char animals[12][3]= 12個元素猴雞狗豬鼠牛虎兔龍蛇馬羊 文字陣列

scanf (i ampyy) 1994n = yy12printf (sn animals[n]) 狗

1994 12 2

if (n==0) printf(猴n)hellip

do

while(1)

Q 出生年份 1994A 狗年

2012年09月09日 星座生肖 Zodiac 2

星座 Zodiac

1需要變數多少個

mm dd zod

2變數類型intfloatchar

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Q 出生月日 1 20A 水瓶(1)

Q 出生月日 1 19A 山羊(12)

Q 出生月日 10 1A 天秤(9)

mm dd

zod=mm

zod--

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 42: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月09日 星座生肖 Zodiac 3

int mm dd zodscanf

zod =

if ( ) zod--hellip

if (zod==1) printf (水瓶n)if (zod==2) printf (雙魚n)if (zod==3) printf (白羊n)hellip

if(zod==0) hellip

2012年09月09日 星座生肖 Zodiac 4

int mm dd zod day[13]=02019hellipchar zodiac[13][5]=x 水瓶 雙魚 hellip

while (feof(fp))fgets(s100fp)days[i]=atoi(hellip)zodiac[i]

printf (sn zodiac[zod])

山羊

人馬

天蠍

天秤

處女

獅子

巨蟹

雙子

金牛

白羊

雙魚

水瓶

222223232423222121211920

121110987654321

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 43: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Insurance 保險 1

Insurance 保險

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士

宣告變數ms age gender

includeltstdiohgtincludeltctypehgtchar ms=S gender=Mint age=18main()

printf(婚姻狀況(MS) )printf(年齡 )printf(性別(MF) )hellip

Insurance 保險 2

if(ms==M) 已婚M

else 未婚S

printf(n)

if(agegt=20)

elseprintf(受保n)

printf(不受保n)

女年齡gt25

男年齡gt30未婚 S

年齡20已婚 M

受保人士方法一

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 44: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Insurance 保險 3

36 私家車保險 Motor Insurance

1 車價 estimated_value

2 投保項目coverage0 綜合保險1 第三者責任保險

3 汽缸容積 type of engine capacity0 +8600 84 1650cc 或以下1 +9500 55 1650cc 至 3500cc2 +9700 58 3500cc 以上

4 無索償折扣 (No Claim Discount)無索償保險期 續保保費折扣 ncd

一年 20連續 兩年 30連續 三年 40連續 四年 50五年或以上 60

車 價 10000汽 缸 3000無索償(年) 2ncd = 30綜合保險費 = $7571

變數estValuecapacityengTypeyear ncdpremium p

Insurance 保險 4

5 全年保費 premium

綜合保險汽缸容積(0)

p = 車價 times 84 或 1000 (取較多者)保費 = $(p+8600) times (1-ncd) times 103

汽缸容積(1)p = 車價 times 55 或 1000 (取較多者)保費 = $(p+9500) times (1-ncd) times 103

汽缸容積(2)p = 車價 times 58 或 1000 (取較多者)保費 = $(p+9700) times (1-ncd) times 103

第三者責任保險保費 = $5000 times (1-ncd) times 103

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 45: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

while 當只要

int n=0while(1)

int n=0do( )

printf(in n)n++

l (100)

printf(in n)n++

_sleep(100) while(nlt6)

int n=0 int n=0while(nlt6)

printf(in n)n++

doprintf(in n)scanf(i ampn)n++

while(nlt6)scanf( i ampn)

while-for 1

int mm=0範 Rangedo

printf(month )scanf(i ampmm)

範圍檢

gCheck

scanf( i ampmm)if( )

printf(Errorn)

檢測 printf( Errorn )

while( )

char ans=Ydo

fflush(stdin)fflush(stdin)printf(是否繼續 )scanf(c ampans)scanf( c ampans)if( )

printf(Errorn)

while-for 2while( )

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 46: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

i t 0 1 sum = sum+int sum=0 n=1while( )nlt=5

sum = sum+n

sum = sum+sum = sum+sum = sum+

n++

sum sum+n sum sum+sum = sum+sum = sum+

printf(Sum = insum)Sum =

難題1+2+3+hellip+n le 1000n =

int sum=0 n=1while( )

n++

while-for 3

printf(N = in n)

int n=1234while(ngt0)

printf(it ) 10

n

n = n10

int n=1234while(ngt0)

printf(it )n10 10

n = n10

while-for 4

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 47: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

int n=4for( nlt5 )

printf(in)

int n=0f ( 5 )i for( nlt5 )

printf(inn)n++

int nfor(n=0 nlt5 n++)

printf(in n) n++

printf( in n)

int nfor(n=10 nlt=20 n+=2)for(n 10 nlt 20 n+ 2)

printf(inn)

while-for 5

int in Roll a Die 擲骰子int insrand(time(NULL))for (i=0 ilt3 i++)

Roll a Die 擲骰子

printf(it n) Mark VI (1 49)

printf(Enter n (2 100) )nx

Mark VI (1-49)

printf( Enter n (2-100) )scanf(i ampn) n2

n3prime = 1 n是質數for (x=2 x++)

非質數

n4

if( ) prime = 0 n非質數if (prime==1) printf(n是質數n)

while-for 6

if (prime==1) printf( n是質數n )

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 48: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

因數 Factors of N

printf(Enter n (2-100) )scanf(i ampn)

Q n= 24

for (x=2 x++)if( )

QA 1234681224

質因數 P i f t f N

( )printf (i x)

質因數 Prime factors of N

printf(Enter n (2-100) ) Q n= 24scanf(i ampn)

for (x=2 x++)

A 2223

Q 36for (x=2 x++)while( )

printf (i x)

Q n= 36A 2233 = 36

while-for 7

printf ( i x)

練習只利用一句 forwhile 句子顯示以下資料

a) 1 2 3 9 10 b) 1 3 5 19 b) 1 3 5 19 c) 2 4 6 20 d) 31 32 33 39 ) e) 20 18 16 6 4 2 f) 5 10 15 45 50 ) 5 15 25 95 g) 5 15 25 95

h) 1 4 9 16 81 100 i) 1 -2 3 -4 9 -10 i) 1 -2 3 -4 9 -10 j) 1+2 2+3 3+4 9+10 k) -4 -3 -2 -1 0 1 2 3 4 ) l) 4 3 2 1 0 1 2 3 4 m) 1 2 3 4 5 4 3 2 1

while-for 8

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 49: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Range check 範圍檢查 19102009------------int age 1-100int score 0-100int month 1-12char sex M or Fchar house LMCSchar house L M C Schar answer y or n

1a do

printf (Q age(1-100) )scanf (d ampage)

if( )printf (Errorn)

while ( )

1

while ( )

Range Check

1b dodo

printf (Q age(1-100) )scanf (d ampage)

if( )printf(Errorn)

while ( ( ) )

1c while ( )

printf (Q age(1-100) )printf ( Q age(1 100) )scanf (d ampage)

if ( ( ) )if ( ( ) )printf(Errorn)

2Range Check

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 50: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2a 2a do

printf (Q are you hungryltyngt )scanf (c ampans)scanf ( c ampans)ans = toupper(ans)

hil ( )while ( )

2b d do

printf (Q are you hungryltyngt )scanf (campans)ans = toupper(ans)

if (( ) )( ( ) )printf (Errorn)

while (( ) )

3Range Check

3a 3a do

printf (Q month (1-12) )f (d amp )scanf (d ampmm)

printf (Q day (1-31) )scanf (d ampdd)

max = 31

if (( ) )printf (Error - out of rangen)printf ( Error out of rangen )

while ( )

4Range Check

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 51: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

1

20 Admission fee 入場費

dow(0-6) time(10-18) age(1-99) stud(yn)

週末(06) 平日(1-5)

(1) 基本收費 fee = 100

(2) 1400前

否則

(3) 60歲以上

12歲以下或學生

fee times 11= $110

fee times 09

fee times 1

fee times 05fee times 05

fee times 08

Q星期 (0-6) 0時間 (10-18) 10年齡 (1-99) 12學生 (yn) n

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 9學生 (yn) n

A入場費 = HK$500

5588

《 變數》

90100

4550

Admission Fee

2

Q4 試寫C程式根據以下條件計算及列印使用者應付入場費多少

a 宣告變數 fee(收費)dow(星期)time(入場時間)age(年齡)b 要求使用者輸入星期(dow)入場時間(time)年齡(age)c 設基本收費fee 為 $100

Q星期 (0-6) 3時間 (10-18) 13年齡 (1-99) 22

A入場費 = HK$1000

int time age dowfloat fee

fee = 100

Admission Fee

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 52: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

2012年09月13日 Admission Fee 3

例子1 (基本收費) 例子2 (星期日)

Q星期 (0-6) 3時間 (10-18) 15年齡 (1-99) 22

A入場費 = HK$1000

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 22

A入場費 = HK$1100

例子3 (長者+星期日) 例子4 (長者+星期一早上) 例子5 (長者+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$550

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 70

A入場費 = HK$350

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 70

A入場費 = HK$500

例子6 (小童+星期日) 例子7 (小童+星期一早上) 例子8 (小童+星期一下午)

Q星期 (0-6) 0時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$1100

Q星期 (0-6) 1時間 (10-18) 11年齡 (1-99) 11

A入場費 = HK$560

Q星期 (0-6) 1時間 (10-18) 15年齡 (1-99) 11

A入場費 = HK$800

4

星期dow

SatSun

MonFri

+10

timelt1400

X

10

age

gt60

lt12orstudent

50

MonFri 50

SatSun X

d 若在週末(星期六日)進入該公園收費會加10e 若在平日(星期一至五)進入該公園

而進入時間為1400或以前收費會減30

f 調整收費後再計算年齡折扣方法如下60歲以上長者 星期一至日再減5012歲以下小童 星期一至五再減20

g 輸出應付收費 (顯示一位小數)

Admission Fee

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 53: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

1

include lttimehgt

int dow (int yy int mm int dd)struct tm tsructtsructtm_mday = ddtsructtm_mon = mm-1tsructtm_year = yy-1900tsructtm_hour = 0tsructtm_min = 0tsructtm_sec = 1tsructtm_isdst = -1if(mktime(amptsruct)==-1) tsructtm_wday=7return (tsructtm_wday) 星期0-6

呼叫方法n = dow (2009 12 25)

void sysDate (int yy int mm int dd)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

dd = tb-gttm_mdaymm = tb-gttm_mon+1yy = tb-gttm_year+1900

void sysTime (int hh int mm int ss)time_t tstruct tm tbt = time(NULL)tb = localtime(ampt)

hh = tb-gttm_hourmm = tb-gttm_minss = tb-gttm_sec

呼叫方法sysDate (ampy ampm ampd)

dow2010

2

main()int dd=1mm=9yy=2009 n hrminsecsysDate (ampyy ampmm ampdd)n = dow (yymmdd)printf(今天是 i年02i月02i日 yy mm dd)printf(星期in n)

if(n==0) printf(星期日n)if(n==1) printf(星期一n)if(n==2) printf(星期二n)if(n==3) printf(星期三n)if(n==4) printf(星期四n)if(n==5) printf(星期五n)if(n==6) printf(星期六n)

sysTime (amphr ampmin ampsec)printf(02i02i02in hr min sec)

dow2010

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 54: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

3

每月13日是星期幾(0-6)

n = dow (2009 1 13)printf(星期in n)

n = dow (2009 2 13)printf(星期in n)

n = dow (2009 3 13)printf(星期in n)

n = dow (2009 12 13)printf(星期in n)

Q yy 2009mm 10dd 13

A Tue(星期二)

Q Enter year (gt1970) 200913022009 is a Black Friday13032009 is a Black Friday13112009 is a Black Friday

功課黑色星期五 Black Friday

進階試列出十年內所有黑色星期五

dow2010

4

main()int mm yydo

printf (nEnter year (gt1970) )fflush (stdin)scanf (i ___)

while (yylt=1970)

mm=while(mm )

if (hellip)printf (13 is a Black Fridayn)

額外練習試找出以下日子是星期幾元旦日聖誕節國慶生日

dow2010

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 55: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Calendar 1

Calendar月曆S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

printf (it 1)printf (it 2)printf (it 7)printf (n)

int yy=2009 mm=12 dd

12月 n=0

printf (it 29)printf (it 30)printf (it 31)printf (n)

printf (it dd)if(dd ) printf(n)

for (dd )

max=31

Calendar 2

Calendar月曆

dd=1

do

ddwhile( ) dd

可被7整除

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

printf (it dd)if(dd ) printf(n)

n = dow(yymmdd) 星期幾

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

yy=2009 mm=12

12月 n=2

(dd+n)可被7整除

只要dd31

11月 n=0

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 56: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Calendar 3

yy=2009 mm=12 dd=1n = dow(yymmdd)max = 31 2830

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月

doprintf (it dd)if( )

printf(n)

while( )

Calendar 4

for (dd=1 )printf(it ___)if( ) printf(n)

printf(輸入年份 ) scanf(i ampyy)

for (mm=1 )

printf ( )

n = dow(yymm1) 星期max = 31 hellipprintf(i年i月n yymm)

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 57: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Calendar 5

輸出12hellip283031for ( )

printfhelliphellip

for (mm )

n = dow(yymm1)max = 31 hellipprintfhellip

Quiz

S M T W T F S

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

2009年12月

2009年11月hellip2009年11月

輸入年份 2009

Calendar 6

Q3 試寫C句子顯示二九年某月之月曆

靠右對齊

Q月份 99

輸入錯誤

Q月份 5

2009年5月

SUN MON TUE WED THU FRI SAT

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

maxday [mm]

scanf(ldquoirdquoampmm)

t t t t t

n = dow(yymm1)

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 58: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Calendar 7

int maxday[13]=0312831303130313130313031main()

printf(i年i月n yy mm) 2009年5月printf(SUN t MON t TUE t WED t THU t FRI t SATn)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

n = dow(yymm1)for(i=0 ) printf(t) n個空格

for(d=1dlt=maxday[mm]d++) 1-31日printf(it d)if((n+d)7==0) printf(n)

printf(n)

for(d=1 d++) 1-3130日printf(it d)if( ) printf(n)

printf(n)

int yy=2009 mm n i ddo

printf(Q月份 )scanf(i ampmm)if(mmlt1 || mmgt12) printf(輸入錯誤n)

while (mmlt1 || mmgt12)

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 59: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

while 廻路 for 廻路 只要while 廻路 for 廻路 只要ilt10初始化

i=0

i=0 i=0

i 0

每完成1次

i 0do

printf(i i)

i 0for ( ilt10 )

printf(i i)i++

while (ilt10)i++

只要ilt10ilt10

輸出 0123456789

2011年10月22日 for-loop 1

輸出 56789 輸出 13579

for ( )printf(i i)

輸出 56789 輸出 13579

for ( )i tf(i i)printf( i i) printf(i i)

輸出 98765

for ( )( )printf(i i)

輸出i 1 j 9

for ( )printf(i=i j=in i j)

i=1 j=9i=2 j=8i=3 j=7printf( i=i j=in i j) i 3 j 7i=4 j=6i=5 j=5

2011年10月22日 for-loop 2

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 60: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

Q2 j 1Q2int ijfor (i=1 i++)

11 2

i=1

j=1

for (i=1 i++) 1 21 2 3

1 2 3 4printf(s )

for (j=1 j++)printf (i )

1 2 3 41 2 3 4 5i=5

printf (i )

printf( )

for-loop 3

某數x若除以3餘數是a若除以5餘數是b若除以7餘數是c求x的可能值x必須在1999的範圍以內而 abc 是由使用者輸入的

Q3int a b c x

Q abc 1 3 5A x = 103 208 int abcx

printf (Q abc )scanf (iii )

A x 103 208 Q abc 2 3 1A x = 8 113

printf (A x = )

f ( )for (x=1 x++)if

printf (i x)printf ( i x)

for-loop 4

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 61: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

1Taxi Fare 1

市區的士

2km

$18

7km

$15200m $10200m

新界的士

2km

$145

6km

$13200m $10200m

大嶼山

2km

$13

18km

$13200m $12200m

的士收費短加長減

$705

$535

$130

2Taxi Fare 2

市區的士 新界的士 大嶼山的士

fare1 首2公里 $180 $145 $130

fare2 每200m $15 $13 $13

limit 車費多於 $705 $535 $1300

fare3 每200m $10 $10 $12

float dist=0 距離 kmdouble pay 應付 $

do

while (distlt10)

dist += 02

printf (dist=51f pay=51fn )

dist=02 pay=180dist=04 pay=180hellipdist=18 pay=180dist=20 pay=180dist=22 pay=195if (distlt=2) pay = 18

else if ( ) payelse pay

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 62: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

3Taxi Fare 3

dodist += 02

printf (dist=51f pay=51fn dist pay)

while (distlt10)

printf (市區(0)新界(1)大嶼山(2) )

scanf (i ampd)

if (distlt=2) pay =

else if ( ) pay

else pay

0

4Taxi Fare 4

市區 新界 大嶼山

20 km 180 145 130

22 km 195 158 143

78 km 615 522 507

80 km 630 535 520

82 km 645 548 533

88 km 690 578 572

90 km 705 588 585

92 km 715 598 598

98 km 745 628 637

100 km 755 638 650

輸出以下資料

pay1 pay2 pay3

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 63: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

5Taxi Fare 5

main()do

dist += 02printf (dist=51f dist)

printf (n)while (distlt10)

float pay[3] = 000 float fare1[3] = 180 145 130float fare2[3] = 15 13 13float fare3[3] = 10 10 12float limit [3] = 705 535 130

for (d=0 dlt3 d++)

printf (t81f pay[d])

6

ltlt 火柴遊戲1 gtgt

有火柴共21支由兩人(AB)對賽每人輪流抽取1234支取最後一支者敗

尚餘火柴 21

A 1尚餘火柴 20

B 2尚餘火柴 18

hellip

A 1尚餘火柴 0

B 勝

0(A)1(B)turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do輸入火柴數目n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

必須做範圍測試

turn = 0 表示Aturn = 1 表示B

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 64: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

7

ltlt 火柴遊戲2 gtgt

遊戲規則不變但轉由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則以隨機數代抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 2尚餘火柴 18hellip使用者 1尚餘火柴 0

電腦勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入隨機數)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

8

ltlt 火柴遊戲3 gtgt

遊戲規則不變由電腦及使用者輪流對賽使用者抽取1234支火柴而電腦則抽(5n)支使用者必須先抽

尚餘火柴 21

使用者 1尚餘火柴 20

電 腦 4尚餘火柴 16hellip使用者 1尚餘火柴 0

電腦必勝

01turn

輸入火柴數目n

尚餘火柴數目max

意義變數名稱

hellip max=21 n=0 turn=1顯示尚餘火柴數目

do火柴數目(輸入計算)n從max扣除n顯示尚餘火柴數目max轉人(turn=0turn=1)

while(還有火柴)

顯示結果hellip

turn = 0 表示電腦turn = 1 表示使用者

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 65: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

本金(p) 100000年利率(R) 6每月還款(A) 1000

月利率(r) 05月息(I) $500

輸出

輸入

n P0 I=P0r P1=P0+I-A total

(1) Loan 借貸

10000005

100000+5001000

Loan 借貸 Mortgage 按揭 1

期0

本金0

利息1 0

新本金 累積利息

1 100000 500 99500 500

2 99500 498 98998 998

3 98998 495 98492 1493

4 98492 492 97985 1985

hellip

列印每月欠款及還款情況直至全數清還為止

變數 意義 例子 公式

P0 Principal(Loan Amount)

本金 1000000

R Interest Rate () 年利率 12 per annum r = 1 per month

N Number of years(Loan period)

年期 10 n = 120 installments120期

(2) Mortgage 按揭

輸入

Loan 借貸 Mortgage 按揭 2

( p )

1)1(

)1(0

n

n

r

rrPAA Monthly Repayment

Amount每月供款

= $14347

I Interest 利息 1000000 1 = 10000

P1 New Principal 新本金 1000000 + 10000 14347

計算

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 66: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

輸出Sample outputs

n P0本金

Interest=P0r利息

P1=P0+I-A新本金

Total Interestpaid so far

累積利息

1 1000000 10000 995653 10000

2 995 653 9 956 991 262 19 956

n = 120 r = 1 A = $14347

10000001

1000000+1000014374

Loan 借貸 Mortgage 按揭 3

2 995653 9956 991262 19956

3 991262 9912 986827 29868

4 986827 9868 982348 39736

5 982348 9823 977824 49559

120

1)1(

)1(0

n

n

r

rrPA每月

供款

公式 Formulae

Interest = Principal Interest Rate年息 = 本金(Pi) 年利率(R)月息(I) = 本金(Pi) 月利率(r)

New Principal= Principal + Interest Monthly Repayment Amount新本金(Pi+1) = 上月本金(Pi) + 本月利息(I) 每月供款(A)

10 yr 11 yr 12 yr 13 yr 14 yr

200 920 845 782 728 683

225 931 856 793 740 694

250 943 867 805 751 706

275 954 879 816 763 718

3 00 96 6 89 0 82 8 77 5 73 0

(3) Repayment Amount1)1(

)1(0

n

n

r

rrPA每月

供款P0 = 10000

Loan 借貸 Mortgage 按揭 4

300 966 890 828 775 730

325 977 902 840 787 742

350 989 914 851 799 754

375 1001 926 863 811 766

400 1012 938 876 823 778

425 1024 950 888 835 791

450 1036 962 900 848 803

475 1048 974 912 860 816

本金 Loan Amount ($1000000)年利率 Interest Rate ( per annum)年期 Noof Years Months月供 Monthly Repayment利息 Total Interest Paid

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 67: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 1

int abcdefgh aaabzxzyzza = 1 b = 2

一般宣告變數方式 (沒有陣列的日子)

scanf (iiii ampa ampb ampc ampd)d = a+b+c

int a[1000]a[0] = 1 a[1] = 2 a[ ] = 66

以陣列方式宣告變數

scanf (iiii ampa[0]ampa[1]ampa[2]ampa[3])

1000個元素

宣告大量變數

array 陣列 2

int days 宣告句子

賦值句子

輸出句子

輸入句子

days = 28

printf (i days )

scanf (i ampdays )

陣列

days[12]

days[11]

days[10]

days[ 9]

days[ 8]

days[ 7]

days[ 6]

days[ 5]

days[ 4]

days[ 3]28days[ 2]

days[ 1]

days[ 0]

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 68: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 331days[12]31days[11]31days[10]31days[ 9]31days[ 8]31days[ 7]31days[ 6]31days[ 5]31days[ 4]31days[ 3]31days[ 2]31days[ 1]

days[ 0]

days[ 1] = 31days[ 2] = 31days[ 3] = 31days[ 4] = 31days[ 5] = 31days[ 6] = 31days[10] = 31days[11] = 31days[12] = 31

for (i=1 i++)days = 31

array 陣列 4

printf(i days[ 1])printf(i days[ 2])printf(i days[ 3])printf(i days[ 4])printf(i days[ 5])printf(i days[ 6])printf(i days[10])printf(i days[11])printf(i days[12])

for (i=1ilt=12i++)printf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 69: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 5

scanf(i ampdays[ 1])scanf(i ampdays[ 2])scanf(i ampdays[ 3])scanf(i ampdays[ 4])scanf(i ampdays[ 5])scanf(i ampdays[ 6])scanf(i ampdays[10])scanf(i ampdays[11])scanf(i ampdays[12])

for (i=1ilt=12i++)scanf(i )

31days[12]30days[11]31days[10]30days[ 9]31days[ 8]31days[ 7]30days[ 6]31days[ 5]30days[ 4]31days[ 3]28days[ 2]31days[ 1]

days[ 0]

array 陣列 6

for (i=0 ilt1000 i++)a[i]=i

for (i=0 ilt1000 i++)a[i]=2i

for (i=0 ilt1000 i++)a[i]=1000-i

a[999]a[1]a[0]

for (i=0 ilt1000 i++)a[i]=2i+1

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 70: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 7

int score[40] 40個分數char grade[40] 40個等級grade[0] = grade[1] =

for (i=0ilt40i++)if (score[i]lt=20) grade[i]=Eelse if (score[i]lt=40) grade[i]=Delse if (score[i]lt=60) grade[i]=Celse if (score[i]lt=80) grade[i]=Belse _____________

for (i=0 i++)if (score___gt=50) grade___=Pelse grade___ = F

A8839

B7738

66

55

44

33

score[i]

hellip

B3

B2

C1

D0

grade[i]

array 陣列 8

輸出畫面Q 輸入整數 (1-999) 34A thirty four

算法 algorithm1 準備兩個陣列 units[] tens[]2 使用者輸入整數 (n)

literals文字01-10 one two three four five six seven eight nine ten11-15 eleven twelve thirteen fourteen fifteen16-19 sixteen seventeen eighteen nineteen20-90 twenty thirty forty fifty sixty seventy eighty ninety

nineteen19

eighteen18

hellip

ninetynine9

eightyeight8

seventyseven7

sixtysix6

fiftyfive5

four

three

two

one

units[i]

forty4

thirty3

twenty2

1

0

tens[i]

3 若 nlt20 則輸出文字 (units[n])4 否則 (利用及) 把 n 分柝為

個位 unit digit 及十位 tens digit輸出適當文字

if(nlt20) printfhellipelse if(nlt100)

d1 = hellipd10 = hellip

test 192020921120109020

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 71: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 9

1 sum of first n integers 首n個整數的和 1+2+3hellipint i sum=0for (i=1ilt=10i++) sum = _____________

2 factorial of n 首n個整數的積 123hellipint i factorial=1for (i=1ilt=10i++) factorial =____________

3 alphabets 字母char ch

for (ch=___chlt=___ch++)printf (d cn ch ch)

for (ch=___chlt=___ch++)printf (d cn ch ch)

65 A66 Bhellip90 Z

97 a98 bhellip122 z

array 陣列 10

3 shift left 左移上移a[0] =a[1] =a[2] =

4 shift right 右移下移a[1] =a[2] =a[3] =

for (i=__i___i___)a[___] = a[___]

for (i=0ilt9i++) a[___] = a[___]

1098765432

10987654321

9876543210

987654321

10987654321

9876543210

當i=9 a[9]=a[__]當i=8 a[8]=a[__]

當i=1 a[1]=a[__]

a[__]a[__]a[__]

a[__]a[__]a[__]

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 72: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 11

5 swapping xy 互換int x=100 y=50 t

6 swapping a[0]a[9] 互換int a[10] t

7 reverse 倒轉(數值)for (i=0ilt__i++)

8 reverse 反轉倒轉(列印)

t = __x = __y = __

t = a[__]a[__] = a[__]a[__] = t

t = a[i]a[i] = a[___]a[___] = t

for (i=9i____i--)printf (dn a[i])

for (i=0ilt10i++)printf (dn a[___])

9876543210

array 陣列 12

(a)char animals[__][3]=猴雞狗豬鼠牛

虎兔龍蛇馬羊yy=1980printf (sn animals[yy______])

9 陣列的應用

(b)char monthname[__][4]

=JanFebMarAprMayJunJulAugSepOctNovDec

mm=9printf (sn monthname[____])

(c)char cdow[___][4]

=SunMonTueWedThuFriSat

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 73: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 13

(d)char cnum[___][___]

=一二三四五六七八九

n=24x =y =printf (s十sn cnum[__] cnum[__])

(e)int maxday[13] =0

312831303130313130313031

doprintf (Enter mmdd )scanf (ii ampmm ampdd)

while (mmlt1 || mmgt12 || ddlt1 || ddgt31)

array 陣列 14

(f)char zodiac[__][__]=山羊

水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蠍人馬山羊

int zday[__]=0201921212122232423232222

Q mm dd 2 22A 雙魚

Q mm dd 2 10A 水瓶

z = mmif(ddltprintf (zodiac = ___n zodiac[___])

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 74: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

array 陣列 15

int i n count[10]=0

統計(0-9)

09

08

07

06

05

0

0

0

0

0

count [i]

4

3

2

1

0

i

for (i=0ilt10i++) count[__]=0

doscanf(i ampn)

while(1)

if (ngt=0 ampamp _____)

for (i=0ilt10i++)printf(i i n ___ count[__])

count[___]++

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 75: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

質數 1

Prime Number (質數1)Q Enter n 13

A 13 is prime

n j13 2 = 113 3 = 113 4 = 113 5 = 313 6 = 1hellip1312 = 1

Prime Number (質數1)Q Enter n 24

A 24 is not prime

n j24 2 = 024 3 = 024 4 = 024 5 = 424 6 = 0hellip2423 = 1

至少有一個 j可以整除

n

沒有一個 j可以整除

n

for (j=2jlthellipj++)if (nhellipjhellip) break

if (jhellip) printf (hellip primen)else printf (hellip not primen)

質數 2

Prime Factors (質因數2)Q Enter n 70

A 70 = 257

scanf(i ampn)for (j=2jlthellipj++)

if (n2==0)printfhellipn = hellip

n j = r70 2 = 035 3 = 235 4 = 335 5 = 07 6 = 17 7 = 0

A 24 = 2223

n j = r24 2 = 012 2 = 06 2 = 03 3 = 0

Prime Factors (質因數2)Q Enter n 24

scanf(i ampn)j=2while (ngt)

if (n==0)printfhellipn = hellip

else試下一個數

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 76: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

質數 3

Prime Number (質數3)Q Enter max

23571113hellip97

n=24for (j=2jltradicnj++)

if (n==0) break

if(jhellip) printfhellip

for (n=2nlt100n++)

100

A prime numbers are

質數 4

Prime Number (質數4)Q Enter max 100

23571113hellip97

10

j=7

j=6

j=5

j=4

j=3

j=2

hellip18

17

16

15

14

13

12

11

10

98765432njn

凡 j 的倍數都不是質數

Array

A prime numbers are

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 77: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

質數 5

Prime Number (質數1-100)

int prime[101]=0for (i=1ilt101i++) prime[i]=1

k=2prime[4] = 0prime[6] = 0prime[8] = 0prime[10] = 0hellipprime[100] = 0

k=2for (j=2jlthellipj++)

prime[] = 0

k=3prime[6] = 0prime[9] = 0prime[12] = 0prime[15] = 0hellipprime[99] = 0

k=4prime[k2] = 0prime[k3] = 0prime[k4] = 0prime[k5] = 0hellipprime[k25] = 0

k=3for (j=2jlthellipj++)

prime[] = 0

printf(100以內的質數 )for(i=2ilt100i++)

if(prime[i]==1) printf()

假設全部為

質數(1)

非質數(0)

質數 6

HCFGCDQ Enter ab

236

min =printf (A common factors are )

for (j=2 jltmin j++)

if( )printf ( i )hcf =

printf (n最大公因數HCF= in hcf)

18 24

A common factors are公因數

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 78: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

1

num[0] 1

num[1] 1

num[2] 1

num[3] 1

num[4] 1

num[5] 1

num[6] 1

num[7] 0

num[8] 1

hellip

num[51] 1

int card[___] num[___]

for(i=0ilt52i++)

n=rand()52

card[i] = n

card[0] 7

card[1] 11

card[2] 51

card[3] 33

card[4] 22

card[5]

card[6]

card[7]

card[8] 7

hellip

card[51]

for(i=__ilt___i++)do

n=rand()___while(num[__]______)card[__] = ___num[__] = ___

n=rand()____

for(i=__ilt___i++) num[__]=___

n=7

num[7]lt1

num[7] =0

列印出來

2

0 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51

for(i=__ilt___i++) card[__]=___

0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 3 3 3

card[i]____

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

card[i]____

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 79: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

3

第1張 28 (梅花3) 點數 3第2張 25 (紅心K) 點數10第3張 50 (階磚Q) 點數10for(i=0ilt52i++)

printf(2i n card[i])

n = card[__]____ 花 0-3

printf(2i n n)

char suit[4] = 葵扇紅心梅花階磚char alpha[13]= A234567890JQK

printf(點數2i n card[i]______) 1-13

printf(2i n suit___) 葵扇紅心梅花階磚

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 80: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

String

string 1

string 2

String

char s1[10]char s2char s3[] = Chan Tai Manchar s4[20] = Chan Siu Mingchar s5[]=Hello0

1

0 1 2 3 4 5 6 7 8 9 10 11 12

s3 C h a n T a i M a n 0

printf (sn s3)s3[8]=0printf (sn s3)

0

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 81: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 3

(stdout s s)(s stdout)

2

char s[]=Game Overnprintf (s)

printf (Game Overn)printf (Game

Overn)

printf (sn s)

string 4

bull scanf (s s)

bull gets (s)

bull scanf (ss firstname lastname)

bull fgets (s 20 stdin)

bull fscanf (stdin s s)

3

amp

keyboard

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 82: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 5

char s[10]=chan tm t[10]=Chan TM

4

bull if (strcmp(st)==0)

printf (s=sn s t)

bull if (strcmp(st)lt0 )

printf (sltsn s t)

bull if (strcmp(st)gt0 )

printf (sgtsn s t)

bull n = stricmp(st)

ignore cases (A = a)

c-C=99-67

32

48 0

49 1

64

65 A

66 B

67 C

97 a

98 b

99 c 0

string 6

int stringCompare (const char s1 const char s2)while(s1==s2)

if(s1==0 ampamp s2==0) return 0s1++s2++

if(s1gts2) return 1return -1

4

main()char s[10]=chan tm t[10]=Chan TMn = stringCompare (s t)

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 83: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 7

fclose (fp)fclose (fp2)

5

FILE fp fp2

fp = fopen (abctxt r) read

fp2 = fopen (xyztxt w) write

while ( feof(fp) )

file pointers

fgets (s 20 fp) printf (s s)fprintf (fp2 s s)

string 8

fprintf (fp s s)fputs (s fp)

5

fgets (s 10 fp) ( 10char)fscanf (fp s s)

Chan TMChan Tai Manhellip

fscanf (fp i ampn)

50 70 90 6077 88 66 55hellip

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 84: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 9

strncpy (t s 3)

strncpy (t s+1 3)

s = face illegal strcpy (s face) string copy

6

char s[20]=facehellip t[20]

n=strlen (s) string length

s fs+1 as+2 cs+3 es+4 hellip

0

t = s[0]+s[1]+s[2]

t = s[1]+s[2]+s[3]

fac

ace

string 10

if (s==face) illegal if (strcmp (s face)==0 )

if (strstr (stxt)==NULL) s txtstrcat (stxt) string concatenate

6 ( ) char s[20] t[20]

printf(c hellip)s[0]s[1]hellip

printf(i hellip)s[0]s[1]hellip

printf(s hellip)ss+1s+2hellip

char s[]=A123456(7)

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 85: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 11

printf (Q [Y]es [N]o [C]ancel )do

key = toupper (getch())while (_________(YNC key) == NULL)

char p key

p = str____ (abctxt html jpg )printf (s p)

p = strrchr (abctxt html jpg )printf (s p)

if (str_____(abctxt html jpg txt))printf (txt n)

YNC

string 12

char name[40][20]strcpy(name[0]chan tai man) string copy

for (i=0ilt40i++)puts (name____) 40

gets(name[2]) scanf (sname[2])

for (i=0ilt40i++)_____(name[i]) 40

for (i=0ilt40i++)_____(name[i]) 40

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 86: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 13

char dow___[4]=Sun Mon Tue Wed Thu Fri Sat

ABLMCC

char school[7] = ABLMCC

for (i=0 iltstrlen(school) i++)printf (__n school___)

for (i=0ilt7i++)printf (__n dow___)

school[0]

school[5]

SunMonTueWedThuFriSat

dow[0]

dow[6]

____(school)

string 14

char dow[7][4]=Sun Mon Tue Wed Thu Fri Sat

char school[7] = ABLMCC

school[0]

school[5]

dow[0] dow[6]

hellip

6AhellipC

dow[4]

dow[6][0]dow[6][1]dow[6][2]

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 87: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 15

char s[13] =chan tai man

s[0] = toupper (s[0])____ = toupper (____)hellips[10] = toupper (s[10])s[11] = toupper (s[11])

for (i=0 ilt12 i++)____ = toupper (____)

s = toupper (s)

printf (sn s) CHAN TAI MAN

( )

printf (sn _____(s) ) upperprintf (sn _____(s) ) lowerprintf (ldquosnrdquo _____(s) ) reverse

string 16

char cno[5] =1A01 t[3]=99

ok=0if (______(cno)==4)

ok=1

gets (cno)

if ( cno[0]lt1cno[1]cno[2]

) ok=0

if (ok==1)strncpy(t___________)n = atoi(t) if (____________) ok=0

if (ok==0) printf(Errorn)

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 88: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 17

char idno[11] =A123456(7)

ok=0if (___________==10

ampamp idno[0]gt=____ ampamp idno[0]lt=____ )ok=1

gets (idno)

for (i=1 _____ i++)if (______________________ ) ok=0

if (ok==0) printf(Errorn)

if ( ( ||) )

ok=0

string 18

char idno[11] =A123456(7)

sum=589 + (__________)8

gets (idno)

for (i=1 ilt7 i++)sum +=

sum += (idno[1] ) 7sum += (idno[2] ) 6hellipsum += (idno[5] ) 3sum += (idno[6] ) 2

if (idno[8]==A)else

if (sum_______) printf (Validn)else printf (Invalidn)

printf(c hellip)idno[0]idno[1]hellip

printf(i hellip)idno[0]idno[1]hellip

printf(s hellip)idnoidno+1idno+2hellip

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 89: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string 19

7

(b)Q Enter text Chan Tai ManA 12 char 4 vowels 2 spacesQ Enter text A Bye

CHANhellip

CHAN

hellip

ChanTaiManhan TaiMananTaiMannTaiManhellip

(c)Q Enter text Chan Tai ManA ChanTaiManQ Enter text A Bye

(a)Q Enter text Chan Tai ManQ n= 2A s[2] = a Ch

s[2]=0

string 20

(a)Q Enter Name ChanA You are ChanQ Enter Name QUITA Bye

8

(b)Q Enter password 1314A IncorrectQ Enter password axeA correct

(c)Q Enter alphabet D

Noof letters 5A DEFGH

(d)n = posn (banana ana)isSym = symmetric (ana)isSym = symmetric (bana)palindrome (abc s)palindrome (cba s)n = anagram (act cat)

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 90: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

(1) 文字 String 測驗 httpwwwablmcceduhk~scyhomejavascriptstrncpyswf

(0) char name[30]=Chan_Tai_Man 來源 source

strcpy (name Chan Tai Man) name = Chan Tai Man

文字複製 錯誤

(1) int p = 5 n = 3 起點(p)字元數目(n)

(2) char ch = name [p] 抽取第p個字元(字符)

(3) char dst[30] 1 strcpy (dst name) 2 strncpy (dst name n) 3 strncpy (dst name+p n)

目的地 destination 文字複製 string copy 部分複製 dst[] larr

dst[] larr

(4) char str[ ]=520 strcat (strdst) x = strlen (str)

文字合併 concatenate (str str+dst)larr

str[] larr x larr

(2) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] target char s[20] = a1b3c5d7e90p2q4r6s8t 先選取 上面 uarr 部分字串再按 確定

(3) 複製部分字串 strncpy(target目標 source來源 length長度)

char t[20] = xyzopqr char s[11] = a1b3c5d7e9 strncpy (t+1 s+4 4) t larr

(4) Relational Operators amp String Comparison 測驗 strcmpswf

string 文字 compare 比較 0=48 A=65 B=66 a=97 b=98

n = strcmp (abbc) rarr n = strcmp (abab) rarr n = strcmp (baab) rarr

(5) input absAbsabstabcab

char input[10] string文字 char grade 單字字符

等於 == if(strcmp(inputabs) ==0) if(grade == A) 不等於 = if(strcmp(inputabs) =0) if(grade = U) 大於 gt if(strcmp(inputabs) gt0) if(grade gt C) 小於 lt if(strcmp(inputabs) lt0) if(grade lt F) 請填上 (===ltgt)

abs Abs abst ab cab

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13
Page 91: ASCII Art 1scy/s4/01-cprogram/00-pgm-ex-01-20.pdf · 2016-09-05 · 1 Story-telling 故事一則 Q: 姓名name: 陳大文 年齡Age: 16 身高ht(m): 1.7 體重wt(kg): 60 朋友friend:

string-and-chardoc Feb 092012

p1

A一般變數

類別 變數名稱 宣告句子 輸入句子

1文字 name char name[10]=A123 gets (______)

2字符 grade char grade =A scanf (____ ampgrade)

3整數 mark int mark =100 scanf (____ ampmark)

4小數 average float average scanf (____ ampaverage)

B陣列

類別 變數名稱 宣告陣列 for (i=0ilt100i++)

1文字 name char names[100][10] gets(________)

2字符 grade char grades[100] scanf(c ampgrades__ )

3整數 mark int marks[100] scanf(i ampmarks__ )

4小數 average float avgs[100] scanf(f ampavgs__ )

C文字char name[20] = Chan Tai Man t[20]

1 字長 strlen n = strlen(name)

2 複製 strcpy strcpy (t name)

3 複製 strncpy strncpy (t name+__ 4) an T

4 比較 strcmp n = strcmp (t ____)

5 單字字符 name[0] name[1] name[__]

6 轉大寫 for (i=0 iltstrlen(name) i++)

name = toupper(name )

7 輸出 printf (s name)

for (i=0 iltstrlen(name) i++)

printf (____ name )

D文字陣列char names[100][10] = Chan KKChan YYChan 7

for (i=0 ilt100 i++)

1 字長 strlen n = strlen(names )

2 複製 strcpy strcpy (t names )

3 複製 strncpy strncpy (t names 4)

4 比較 strcmp n = strcmp (t names )

5 單字字符 names [2]=e names [4]=

6 轉大寫

names[0]

for (i=0 iltstrlen(names[0]) i++)

names = toupper(names )

7 輸出文字 printf (____ names[0])

8 輸出 for (i=0 iltstrlen(names[0]) i++)

printf (____ names[0]__)

  • time-table13
  • ascii-art13
  • C-IO13
  • Story
  • printf13
  • oct-hex13
  • agec13
  • xy13
  • printf13
  • bmi13
  • flash-cal13
  • clock13
  • consolec13
  • if13
  • DeMorgan13
  • 保安員
  • 常見錯誤
  • boy-girl
  • age
  • greeting
  • 生肖
  • Quadratic equation
  • 三角形測試
  • 保險
  • while13
  • for13
  • factors13
  • 範圍檢查
  • adm-fee13
  • dow13
  • 黑色星期五
  • calendar13
  • for13
  • taxi-fare13
  • 火柴遊戲
  • loan13
  • Array陣列
  • swap-rev13
  • 質數
  • cards13
  • String
  • strcmp13
  • strcpy
  • file13
  • strncpy13
  • 文字陣列13
  • idno13
  • string-char13