• 沒有找到結果。

課程⼤大綱

N/A
N/A
Protected

Academic year: 2022

Share "課程⼤大綱"

Copied!
40
0
0

加載中.... (立即查看全文)

全文

(1)

講師: 李根逸 (Ken-Yi Lee), E-mail: feis.tw@gmail.com

C/C++ 程式設計基礎課程簡介

【第⼀一講】

!9

(2)

課程⼤大綱

!10

課程簡介

[P.11]

課程使⽤用的軟體: Dev C++ [P.12]

課程網⾴頁: http://g.csie.org/~kez/c期數 [P.13]

課程講義 [P.14]

參考書籍: C 程式設計藝術 [P.15]

成績評量 [P.16]

其他資訊 [P.17]

程式語⾔言簡介

[P.19]

快速上⼿手

[P.24]

啟動 Dev C++ [P.25]

編譯與執⾏行程式 [P.28]

Hello world [P.29]

記憶體的概念 [P.34]

C / C++ 的運算 [P.35]

(3)

課程簡介

本課程教導 C / C++ 程式語⾔言的基礎,適合稍微或 沒有程式設計經驗但有興趣的學員。

但是修習本課程需要有基本的英⽂文打字能⼒力

本課程內容包含 C 與 C++ 語⾔言的基礎,在前半段 介紹 C 語⾔言與 C++ 共通的部份,後半段補充 C++

語⾔言與 C 語⾔言的差異 (主要是介紹物件導向程式設計 的概念 )。

雖然這麼說不盡精確,但可以先把 C++ 算是 C 的⼀一種 擴充加強版

【思考】那為什麼我們需要兩個語⾔言⽽而不是發明 C 語⾔言 2.0 ?

!11

(4)

課程使⽤用的軟體

撰寫程式需要安裝具有編譯 C/C++ 程式能⼒力的整 合性開發環境或編譯軟體 :

編譯軟體 (Compiler):

Microsoft Visual C++ (MSVC++) The GNU Compiler Collection (GCC)

整合開發環境 (IDE: 包含某套編譯軟體):

Microsoft Visual Studio (MSVC++) Dev C++ (GCC)

其他: Code::blocks, Qt Creator, 等等.

教室電腦內裝有 Microsoft Visual Studio 、 Code::Blocks 與 Dev C++

上課⽰示範將以 Dev C++ 為主。

!12

(5)

課程網⾴頁

課程網⾴頁的網址: http://g.csie.org/~kez/c 期數 課程網⾴頁上會放置課程預定進度、每次上課使⽤用的投 影⽚片、範例檔和補充教材供學員下載閱讀與練習。如 果課程臨時有什麼變動,我們會更新網⾴頁通知。

因天氣因素停⽌止上課的標準以⼈人事⾏行政局為準

因為課程進度將隨著上課情形與同學反應⽽而有所變動,

所以我們的課程網⾴頁將會動態更新。

網⾴頁內容如果有任何缺失或資料遺漏還⿇麻煩⼤大家提醒!

Facebook 討論區:http://goo.gl/6GpVf

提供訊息發佈、問題發問或資訊交流的管道

!13

(6)

課程講義

上課⽤用的投影⽚片紙本會作為上課⽤用的講義

講義為講師個⼈人學習、實務與教學經驗的集成,雖然已經不停的更新 補充,但是編寫時錯誤缺漏或不盡如⼈人意的地⽅方難免,有任何問題或 建議請跟講師反應。

講義內容主要包含四個部分:觀念、範例、補充與習 題

範例 :

我們上課會細講某些選過的範例,⽽而未講述的範例請回家⾃自⾏行練習。

範例解答在課程網⾴頁會提供,請盡量做到能夠不參考解答下完全獨⼒力 寫出範例。初學者請每隔⼀一段時間重複練習範例到熟練。

補充:

上課不會細講的內容,通常是已經學過要複習時看。

習題:

我們不提供完整的習題解答也不建議每個⼈人都要寫習題,尤其初學者 請以範例為主。對於習題有任何問題請直接詢問講師,講師會依照學

⽣生的反應給予適當提⽰示或⽰示範。

!14

(7)

參考書籍

本課程主要參考書籍:

C : How to Program 《C 程式設計藝術》

其他課程相關 C 參考書籍有:

The C Programming Language

C Programming: A Modern Approach Headfirst C

其他課程相關 C++ 參考書籍有:

C++ How to Program

The C++ Programming Language 其他中⽂文參考書籍:

C 語⾔言教學⼿手冊 〈旗標〉

C/C++ 無痛學習教本〈旗標〉

建議初學程式語⾔言者還是需要⼀一本⼊入⾨門參考書籍

!15

(8)

成績評量

出席率與隨堂練習 (40%) 期中與期末測驗 (各 30%)

19:00

隨堂練習

19:20

批改與講解

19:30

休息與 Q&A

19:40

上課

20:40

休息

20:40

上課

21:30

下課

第 2-4, 6-8, 10-12 堂課

19:00

上課

20:00

休息

20:10

上課

21:00

隨堂練習休息 Q&A

21:30

下課

第 1 堂課

19:00

測驗開始

20:50

測驗結束

21:00

檢討與Q & A

21:30

下課

第 5, 9 堂課

!16

70 分及格

(9)

其他資訊

⾨門禁系統:

假⽇日整天或平⽇日的晚上⼤大⾨門會上鎖,我們會發放感應式的⾨門禁 卡讓學員使⽤用 (需100元押⾦金)。於最後⼀一次上課結束時需要繳回 卡並拿回押⾦金。請注意⾨門禁卡只能使⽤用在系館正⾨門。

垃圾處理:

請千萬不要將垃圾丟棄⾄至廁所內垃圾桶!

請拿⾄至⼀一樓中庭 (電梯旁⼩小⾨門外) 垃圾分類區丟棄 在校內停⾞車費⽤用:

可以使⽤用學員證或請講師在停⾞車單據上加蓋本班班章會有等同 校內學⽣生停⾞車的折扣

學員證與收據

學員證約在第⼀一次上課發放 收據約在第⼆二次上課發放

錯過的同學請記得在上課期間跟⽼老師索取

!17

(10)

程式語言簡介

!18

(11)

程式語⾔言

程式語⾔言 (Programming Language) :

!

!

!

!

程式語⾔言 (programming language) 是⽤用來控制 (control) 機器 (machine) ⾏行為 (behavior) 的形式 (formal) 語⾔言

“A programming language is a formal language

designed to communicate instructions to a

machine, particularly a computer.

Programming languages can be used to create programs that control the behavior of a

machine and/or to express algorithms

precisely.” - from Wikipedia

!19

(12)

什麼是機器 /電腦 ?

from Wikipedia

!20

(13)

電腦的組成

抽象來說,電腦內包含了:

輸⼊入單元 (Input): 例如鍵盤和滑⿏鼠 (感官)

輸出單元 (Output): 例如螢幕和印表機 (嘴巴、肢體) 輔助儲存單元 (Storage) : 例如硬碟和光碟

記憶單元 (Memory): 例如記憶體 中央處理單元 (CPU)

程式設計核⼼心 (記憶與計算)

!21

(14)

程式語⾔言的分階

程式語⾔言⼀一般來說可分為三個階層 :

機器語⾔言 :

01001101011111010001010000010110 01010011011110110101110000001011 01000111100010101011101001101011

組合語⾔言 :

LOAD A
 ADD B


STORE SUM

⾼高階語⾔言 :

SUM = A + B

指令集: 硬體⽀支援的指令 (x86, ARM, ...)

!22

常⾒見的⾼高階語⾔言有 C, C++, Java, Pascal, Python, ...

(15)

C / C++ 語⾔言的歷史

C 語⾔言是⼀一種⾼高階語⾔言

⻄西元 1972 年由 Dennis Ritchie 所開發。

⻄西元 1989 年,美國國家標準局通過 ANSI X3.159-1989 (俗稱 C89) [⺫⽬目前常⽤用]

⻄西元 1990 年,國際標準組織 (ISO) 通過 ISO/IEC 9899:1990 (俗稱 C90) [內容與 C89 雷同]

⻄西元 1999 年,國際標準組織 (ISO) 通過 ISO/IEC 9899:1999 (俗稱 C99)

C++ 語⾔言原則上包含了 C 語⾔言

⻄西元 1998 年,國際標準組織 (ISO) 通過 ISO/IEC 14882:1998 (俗稱 C++98) [⺫⽬目前常⽤用]

C++ 加⼊入了⼀一些傳統 C 語⾔言沒有的『語⾔言特性』,最為⼈人 所知的就是『物件導向程式設計 (OOP)』的相關語法

!23

(16)

快速上手

!24

(17)

開始 > 所有程式 > Bloodshed Dev C++ > Dev C++

啟動 Dev C++

!25

執⾏行檔 (*.exe) 程式碼編輯器 (*.cpp) 編譯器

整合開發環境 (IDE)

(18)

Dev C++ 的縮排設定

⼀一般像是 Dev C++ 和 Microsoft Visual Studio 這 類的整合開發環境 (IDE),可以⾃自動判斷程式內容幫助 你縮排。因此在程式碼的撰寫過程中,軟體會⾃自動加⼊入 Tab 或空⽩白。以 Dev C++ 為例,可以到這裡修改縮排 設定 :

「Tools (⼯工具) > Editor Options (編輯器選項) > General (⼀一般)」

此外,Dev C++ 預設會⾃自動在輸⼊入左括號後加上右括 號,不喜歡的話可以到下⾯面的設定修改 :

Tools (⼯工具) > Editor Options (編輯器選項) > Code Insertion (插⼊入程式碼) > Symbol Completion (符號完

成)」把 「Enable symbol completion (啟⽤用符號完成)」關

(19)

基本操作

開新檔案 :

File (檔案) > New (開新檔案) > Source file (原始碼)」

儲存檔案 :

「File (檔案) > Save [As] (儲存)」

檔名請勿含有空⽩白並避免含有特殊符號 (例如中⽂文) 使⽤用 cpp 作為副檔名

開啟⼀一個已經存在的檔案 :

File (檔案) > Open (開啟)」

!27

(20)

編譯與執⾏行程式

程式檔 (.c, .cpp) 需要經由編譯 (Compile) 的動作 才能成為執⾏行檔 (.exe) 讓電腦執⾏行 (Run)

「Execute (執⾏行) > Compile (編譯)」《F9》

編譯成功後會產⽣生執⾏行檔

!

!

!

「Execute (執⾏行) > Run (執⾏行)」《F10》

要先編譯成功才會有執⾏行檔可以執⾏行

常⽤用的指令:編譯並執⾏行《F11》

「Execute (執⾏行) > Compile&Run(編譯並執⾏行)《F11》

Done 』表⽰示編譯成功 可以⽤用《F11》進⾏行編譯後直接執⾏行

!28

(21)

【範例】 Hello world [1]

請開啟⼀一個新檔案並輸⼊入下列程式碼後編譯並執⾏行

F11》:

/* My first program */


#include <stdio.h>

int main() {


printf(“Hello world !\n”);


return 0;


}

printf(“字串”) 是⼀一個可以⽤用來印出字串在螢幕上的函式

!29

【範例】 helloworld_1.cpp 試著在電腦裡找到你的程式碼檔案跟執⾏行檔後點擊執⾏行看看

(22)

【範例】 Hello world [2]

請寫⼀一程式,輸⼊入下列程式碼後編譯並執⾏行《 F11》

/* My first program */


#include <stdio.h>


#include <stdlib.h>

int main() {


printf(“Hello world !\n”);


system(“pause”);


return 0;


}

!30

system(“指令”) 是⼀一個在作業系統上執⾏行指令的函式 pause 是 MS-DOS 作業系統中⽤用來暫停並等待輸⼊入的指令

【範例】 helloworld_2.cpp

(23)

【範例】 Hello world [3]

請寫⼀一程式,輸⼊入下列程式碼後編譯並執⾏行《 F11》

/* My first program */


#include <stdio.h>


#include <stdlib.h>

int main() {


printf(“Hello “);


printf(“world !\n”);


system(“pause”);;


return 0;


}

!31

【範例】 helloworld_3.cpp

(24)

【範例】顯⽰示⽂文字

請寫⼀一個程式顯⽰示下⾯面的⽂文字圖案給使⽤用者看 



 


!

!

!

!

【思考】我們需要使⽤用幾次 printf(...)?

H H A
 H H A A
 HHHHHH A A 
 H H AAAAAAA
 H H A A

!32

【範例】 print.cpp

(25)

printf 的格式字串中,我們需要使⽤用跳脫串列 來讓電腦顯⽰示⼀一些無法或不易直接輸⼊入的符號或⽂文字

【補充】跳脫串列

跳脫串列 說明

\n Newline (換⾏行)

\t Tab (跳位)

\\ 反斜線

\” 雙引號

\b 發出逼逼聲

!33

(26)

【範例】計算兩數相加

試寫⼀一程式如下:

/* Addition program */


#include <stdio.h>


#include <stdlib.h>


int main(){


int integer1;


int integer2;


int sum;


printf("Please enter the first integer: ");


scanf("%d", &integer1); 


printf("Please enter the second integer: ");


scanf("%d", &integer2);


sum = integer1 + integer2;


printf("Sum is %d\n", sum);


system("pause");


return 0;


}

!34

【範例】 add.cpp

(27)

/* Addition program */


#include <stdio.h>


#include <stdlib.h>


int main() {


int integer1;


int integer2;


int sum;


printf("Please enter the first integer: ");


scanf("%d", &integer1); 


printf("Please enter the second integer: ");


scanf("%d", &integer2);


sum = integer1 + integer2;


printf("Sum is %d\n", sum);


system("pause");


return 0;


}

記憶體的概念

integer1

integer2

sum

(int)


?

2293616

(int)


?

2293620

(int)


?

2293612

!35

int 表⽰示整數型別

printf 内 %d 表⽰示印出⼀一個整數

【思考】 printf(“Sum is sum\n”); 不⾏行嗎?

scanf 函式會從鍵盤讀⼊入資料後存放在 指定的變數 (%d 表⽰示讀⼊入⼀一個整數 )

&integer1

&integer2

&sum

(28)

C / C++ 的運算

運算 運算⼦子

加法 + 減法 - 乘法 * 除法 / 餘數 % 賦值 = 3 + 4 * 5 是多少 ?

運算時請注意運算⼦子優先順 序 !

先乘除後加減

可以適當加上括號 ( ) 來調 整優先順序!

賦值運算⼦子 (=)

『賦值』是指將等號左邊的 變數賦予等號右邊的值

⼀一般情況下只有賦值運算⼦子可以 改變變數的值

!36

(29)

【範例】計算乘積

請寫⼀一個程式讓使⽤用者依序輸⼊入兩個整數後算出兩個 整數的乘積顯⽰示給使⽤用者看

提⽰示 : 請由 add.cpp 開始改起 







請寫⼀一個程式讓使⽤用者依序輸⼊入三個整數後算出三個 整數的乘積顯⽰示給使⽤用者看

提⽰示 : 請由 multi2.cpp 開始改起

Please enter the first integer: 3
 Please enter the second integer: 4
 Please enter the third integer: 5
 Product is 60


Please enter the first integer: 3
 Please enter the second integer: 4
 Product is 12


!37

【範例】 multi3.cpp

【範例】 multi2.cpp

(30)

【範例】更多計算

請寫⼀一個程式讓使⽤用者分別輸⼊入三個整數後,算出三 個整數的和、平均值、乘積並顯⽰示給使⽤用者看 


!

!

!

請寫⼀一個程式讓使⽤用者輸⼊入⼀一個整數後顯⽰示該數的平

⽅方數值

Please enter the first integer: 3
 Please enter the second integer: 4
 Please enter the third integer: 5
 Sum is 12


Average is 4
 Product is 60


Please enter the integer: 3
 9

!38

【範例】 squared.cpp

【範例】 calc.cpp

(31)

printf 與 scanf 函式

printf 可以連續輸出多個變數 :


可連在⼀一起寫成 :

!

scanf 可以連續讀取多個變數 :


printf 可以在 % 後加上最少顯⽰示位數:

printf(“Sum is %d\n”, sum);


printf(“Average is %d\n”, average);


printf(“Product is %d\n”, product);

printf(“Sum is %d\nAverage is %d\nProduct is %d\n”, sum, average, product);

scanf(“%d%d%d”, &integer1, &integer2, &integer3);

printf(“Sum is %5d\n, sum);

printf(“Sum is %05d\n, sum);

留五格

留五格,不⾜足補零

!39

(32)

【範例】位數解析

試寫⼀一程式讓使⽤用者輸⼊入⼀一整數後,顯⽰示其個位數字

提⽰示:% 運算⼦子可以⽤用來求餘數






!

!

試寫⼀一程式讓使⽤用者輸⼊入⼀一整數後,顯⽰示其百位數字

!40

範例輸⼊入: 12345
 範例輸出: 5

範例輸⼊入: 12345
 範例輸出: 3

範例輸⼊入: 54321
 範例輸出: 1

範例輸⼊入: 54321
 範例輸出: 3

【範例】 digit3.cpp

【範例】 digit1.cpp

(33)

初學 C / C++ 程式設計流程

步驟⼀一 [空殼]:

寫出 main

步驟⼆二 [流程]:

依照執⾏行順序寫出輸⼊入與輸出的程式碼,對於未知或未 定的數值給予⼀一個變數名稱。

步驟三 [資料]:

檢查所有的變數在程式碼中是否在正確的時候給予正確 的值

!41

(34)

初學寫程式碼時在乎的是?

正確性

程式碼是否正確的產⽣生想要的結果

可讀性

程式碼是否容易閱讀與理解

效率

CPU 使⽤用效率 (時間效率)

是否使⽤用最少的指令

記憶體使⽤用效率 (空間效率)

是否配置了最少的記憶體空間

彈性

是否容易依照需求的變更改變程式碼

!42

(35)

習題 [1]

[E0101] 試寫⼀一程式印出下列⽂文字:

!

[E0102] 試寫⼀一程式輸出下列⽂文字

!

提⽰示 : 利⽤用輸⼊入 \” 來顯⽰示雙引號

[E0103] 試寫⼀一程式,輸⼊入⼀一個整數後,印出該整 數加 7 後的值

[E0104] 試寫⼀一程式印出 129263 + 54628 的結

Welcome to “C” world !


Hope you have a nice day !
 Alice: How are you ?


Bob: Fine. Thank you.

!43

(36)

習題 [2]

[E0105] 試寫⼀一程式讓使⽤用者輸⼊入⼀一個⻑⾧長⽅方形的⻑⾧長 跟寬後,顯⽰示該⻑⾧長⽅方形的周⻑⾧長與⾯面積

[E0106] 試寫⼀一程式,讓使⽤用者輸⼊入⼀一個整數 x 後,計算出 3x

2

+4x-1 的值

[E0107] 試將 helloworld_2.cpp 改寫成程式碼 內容只有三⾏行但執⾏行結果也⼀一樣的程式碼

[E0108] 試寫⼀一程式,輸⼊入⻄西元年份後,印出⺠民國 年份

⺠民國年份 = ⻄西元年份 - 1911

!44

(37)

習題 [3]

[E0109] 試寫⼀一程式,輸⼊入華⽒氏溫度後,印出攝⽒氏 溫度的值

攝⽒氏溫度 = 5×(華⽒氏溫度-32)/9

[E0110] 試寫⼀一程式,讓使⽤用者輸⼊入⾃自⼰己的⾝身⾼高與 體重後,顯⽰示使⽤用者的⾝身體質量指數 (BMI) 無條件 捨去⾄至⼩小數點後第⼆二位:

BMI = 體重 (kg) / ⾝身⾼高平⽅方 (m2)

[E0111]* 試寫⼀一程式印出 129263 * 54628 的 結果

[E0112] 試寫⼀一個程式,讓使⽤用者輸⼊入兩個整數 後,計算出兩個整數的平均值⾄至⼩小數點後⼆二位

!45

(38)

!46

(39)

!47

(40)

!48

參考文獻

相關文件

which can be used (i) to test specific assumptions about the distribution of speed and accuracy in a population of test takers and (ii) to iteratively build a structural

We can therefore hope that the exact solution of a lower-dimensional string will provide ideas which could be used to make an exact definition of critical string theory and give

 The class of languages decided by polynomi al-time algorithms 是 the class of languages accepted by polynomial-time algorithms 的 su bset.. G=(V,E) is a simple cycle that contains

Structured programming 14 , if used properly, results in programs that are easy to write, understand, modify, and debug.... Steps of Developing A

To explore different e-learning resources and strategies that can be used to successfully develop the language skills of students with special educational needs in the

After the desired content has been identified, the control point needs to determine which transfer protocol and data format should be used to transfer the content from the

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type.. If there isn’t such a

Central Questions of Computer Science (continued). • How can algorithms be used to manipulate