1 / 38

程式設計

程式設計. 謝建興 元智大學機械系. E-Mail: jsshieh@saturn.yzu.edu.tw Web: www.mech.yzu.edu.tw/users/intctrl/index.htm TEL: (03) 463-8800 ext.470 FAX: (03) 455-8013. 第一章 程式語言簡介. 1-0 Brief History of Computing ( See Textbook Ch. 1 ) 登月:飛控電腦、最佳飛行路徑分析 … 衛星:通訊、氣象、導航、間諜 … 等等

Télécharger la présentation

程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 程式設計 • 謝建興 • 元智大學機械系 E-Mail: jsshieh@saturn.yzu.edu.tw Web: www.mech.yzu.edu.tw/users/intctrl/index.htm TEL: (03) 463-8800 ext.470 FAX: (03) 455-8013

  2. 第一章 程式語言簡介 • 1-0 Brief History of Computing (See Textbook Ch. 1) • 登月:飛控電腦、最佳飛行路徑分析… • 衛星:通訊、氣象、導航、間諜…等等 • CAD/CAM(電腦輔助設計與製造, Modelling, Simulation:靜動態分析、模擬 等等 許多工程與科學問題,唯有靠電腦模擬才能解決! • 做實驗時用電腦蒐集並記錄數據,並且控制機電儀器設備 • 遠距教學, 遠距醫療

  3. 1-1 Computer Organization • 電腦的內部組成元件 • CPU (Central Processing Unit), 中央處理器: 算術與邏輯處理單元 ,General-Purpose Registers,和Internal Memory內部記憶體 • Input Devices (Interface): 鍵盤、滑鼠、磁碟、網路… 等等 • Output Devices (Interface): 螢幕、印表機、磁碟、網路…等等 • External Memory: RAM、ROM …等等(每個記憶體都有位址) • Data, Address and Control Bus: 資料、位址與控制用匯流線 • 硬體 (Hardware 實體元件 )與軟體(Software 電腦程式)

  4. 1-2 電腦語言 Computer Languages • 電腦如何表示資料1 + 1 ? 十進位:0, 1, 2 … 9 十種符號 二進位:0, 1兩種符號,代表記憶體基本單 (Bit 、Byte)的兩種狀態 使用記憶體時,以 8 個位元為基本單位,稱 為 Byte (位元組) 十六進位:0, 1, 2 … 9, A, B … F 例: 一個位元組的資料的不同表示法 (00111101)2 = (61)10 = (3D)16

  5. 編碼(Coding):同一資料(符號)可以有不同意義 ! 例:(01001111)2可以代表 十進位的數值 79或 清除內部記憶體中A暫存器 的電腦指令 (6811CPU)或 電腦資訊交換碼文數字符號表 中的 “O”(ASCII code) 註:純文字文件檔即為用ASCII code 存儲的 (BIG-5 碼用兩個Byte表示中文資訊) 這些檔案可直接用文書處理軟體閱讀,或 用DOS 指令 type 顯示在螢幕上

  6. 電腦語言的分類:高階(high-level)與低階(low-level)語言電腦語言的分類:高階(high-level)與低階(low-level)語言 自然語言:人說的話,中文英文等等,往往一字(詞)多義 低階語言:電腦看得懂的,用0 和1 寫的程式(二進位語 言),或稱機器語言 高階語言:人用來告訴電腦如何工作的語言,文法及辭彙的 定義十分嚴謹 , 但是…電腦看不懂,所以,得先 用編譯器(Compiler)翻譯為機器語言 組合語言:用些好記的符號來表示機器指令的語言,再用 組譯器(assembler)譯 為機器語言 • 用高階語言寫的程式接近常用語言,易寫易懂; 寫低階語言則需要瞭解CPU內部動作原理與功能, 難寫難懂,可說是高級語言。

  7. 常用的高階語言: FORTRAN : FORmula TRANslation, 最早期的工程計算用語言, 1977修訂後改其結構性功能與可讀性。 PASCAL : 早期具結構性與可讀性的語言,功能較 FORTRAN 強大。 Modula-2: 由 PASCAL 發展出來,結構性與可讀性較 PASCAL 強。 C : 結構性、可讀性與移植性,兼具低階語言功能的高階語言, 對資訊工程特別重要。 COBOL: 長於商業應用與大量檔案資料的處理。 BASIC: 古時候(1960)開發給初學者用的高階語言(Beginner's All-Purpose Symbolic Instruction Code),至今功能 日益增強,易學易懂,但不適於大型軟體開發。 ADA: 多工即時控制用語言,美國國防部開發。 MATLAB: 最流行的科學及工程計算繪圖用套裝軟體,語法接近 BASIC。

  8. 系統軟體與應用軟體: 系統軟體:控制電腦運作的基本作業系統程式,電 腦開機後即自動執行,然後等待使用者 輸入後續指令,如 DOS, Windows 等。 應用軟體:用程式語言編寫,可執行特定功能的 程式,如電動遊戲、文書編輯等。 機械系的你,在大學所必需的應用軟體有 (1)文書編 輯 MS-WORD, WordPerfect …(2) 工商試算表 MS-EXCEL, Lotus-123 (3) 機械繪圖 AutoCAD (4) 數值分析Matlab ?

  9. 程式設計與程式語言: 程式設計課程中: 談的是撰寫開發(大型)程式的方法,技巧與好習慣 程式語言課程中: 談的是語言、文法、 辭彙 上完本課程後,你應該可以在有需要時,自修或旁聽學會各種高階語言的用法,就像是去買本字典或翻譯機一樣。

  10. 第二章 程式設計的解題方法 • 2-1 從編寫程式到執行程式的過程 Running A Compute Program 程式開發流程 (以在 IBM-PC 用Visual C++ 開發C程式為例) 1.撰寫程式(Edit):用編輯軟體撰寫 hello.cpp 2.編譯並除錯 (Compile/Debug):用編譯器編譯 hello.cpp 產生目標 檔hello.obj, 編譯器會指出程式中的語法錯誤(syntax error) 3.連結程式庫 (Link):由連結器(linker)將目標檔及所需程式庫連 結成為可執行檔 hello.exe (機器語言檔) 4.執行並除錯 (Execute/Debug):在 DOS 或 Win-95 下執行,若還有 任何錯誤,就再回去除錯吧 例如 logic and math error, run-time error, execution error: 3 / 0 = ? 5.測試 (Test):程式在各種狀況下是否正常工作? 可執行並不保證結 果正確! Garbage in, garbage out ! Rubbish in, rubbish output !

  11. 編譯器(Compiler)與解譯器(Interpreter,直譯、口譯):編譯器(Compiler)與解譯器(Interpreter,直譯、口譯): • 將高階語言程式翻譯成機器語言(指令)讓電腦去執行的兩種方式 • 編譯:將整個程式翻譯成可執行檔後再執行,如 FORTRAN, C, PASCAL, Quick-Basic • 解譯:翻譯一行程式,就執行一行,然後再翻譯下一行。不會產生可執行檔。如Matlab。執行速度比編譯型慢 10 ~ 30 倍 ! 但開發程式時無須花時間編譯。

  12. 批次(Batch)與分時多工(Time-Sharing Multi-Task)作業: • 人腦可以同時想(做)許多事情,一般的電腦只會一步一步按既定順序(人寫的程式指令)動作,但因其速度很快,因此可以用分時的方式,輪流執行數個程式(在A程式結束前讓他暫停一下,執行B程式0.01 秒,再執行C程式0.01 秒,再回頭執行A程式0.01 秒),讓使用者看起來是同時發生的。古時候的大型電腦、工作站以及現代的個人電腦只要有適當的系統軟體,都可以進行多工作業,或讓許多人同時使用。

  13. 2-2 由上而下(Top-Down)的程式設計技巧 六大步驟 1. 把問題與需求徹底搞清楚(要能清晰的陳述!) 2. 搞清楚輸入與輸出(Input / Output, I/O)的資料內容與格式 3. 把大問題分解成幾個小問題,並用流程圖或虛擬碼來說明整體解答的大綱,直到 不需再分解為止;分裂敵人各個擊破 (divide and conquer) 4. 發展解決每個小問題的演算法,用流程圖或虛擬碼來敘述解答的細節(用手或計算機試算一次) Algorithm, Flowchart, Pseudo-Code 5. 把虛擬碼翻譯成所需的電腦語言程式 (FORTRAN, C, MATLAB …Whatever) 6. 徹底的測試,狀況演練

  14. 如何寫個程式計算一組實驗數據的平均值??? • STEP 1. 釐清需求: 計算一組實驗數據的平均值 • STEP 2. 釐清輸出入格式:由鍵盤輸入數據,一次輸入一個數據 ,用 ”0 ”代表結束 (不是用磁碟檔,也不可以一次鍵入兩筆資料,還有…) 計算出的平均值顯示在螢幕上(不是用磁碟檔或印表機) • STEP 3. 大體分解:方法(1): 讀取資料 -> 計算平均值 -> 顯示結果方法(2): 讀取資料 並計算總和及筆數 -> 計算平均值 -> 顯示結果 • STEP 4. 開發演算法:(以方法二的第一小問題為例) 讀取資料 並計算總和及筆數的演算法的虛擬碼

  15. 2.1.1 Initialize variable, let sum = count = 0 2.1.2 Read a data from keyboard if data = 0, then goto 2.1.4, otherwise continue 2.1.3 2.1.3 Add data to sum, increase count by 1, goto 2.1.2 2.1.4 The End 這演算法有可能出錯嗎? • STEP 5. 撰寫電腦程式: • STEP 6. 測試: • 2-3 流程圖 (Flow Chart) See Web Note • 2-4 演算法 (Algorithm) See Textbook

  16. 2-5 電腦程式的七大組成要素 (分解電腦程式 !?) 1. 算術與邏輯的運算 2. 輸入與輸出的處理 3. 程式流程控制 4. 函數與副程式 5. 資料型態 6. 特殊宣告與定義 7. 記憶體之分配與操作 • 用BASIC 或 MATLAB 語言 通常只會用到前四項 • 用FORTRAN 語言 通常會用到 前五項 • 用C或PASCAL 語言 通常會用到 前六項 • 寫系統軟體或組合語言程式就要全部用到啦

  17. Chapter 3 Getting Started • Program Development Cycle 1. Develop Pseudocode 2. Edit Program 3. Compile Program 4. Performance Analysis 5. Debug and Test Program (a) Syntax Errors (b) Execution Errors (c) Logic Errors 6. Document Code

  18. 3.4.2 Comment Statements • /* */ (a) C compile ignores all text following (b) Avoid nested comment statements (c) Enhance the readability of code

  19. 3.4.3 Layout of the Program Source Code • (a) C us a free-form language (b) Several statements on a single line (c) Spread a single statement over several lines (d) Space between characters as white space that will be disregarded by compilers

  20. 3.4.4 The main() Function • Int main (void) { <= header and opening brace for main() function . • } <= closing brace for body of function main() . • returnType functolonName ( list of arguments) • Int main (void) <= header for function main() . • { <= opening brace for body of function main () . • } <= closing brace for body of function main() . • { • C_statement_1; • C_statement_2; • C_statement_m; • } • printf(“ Approximate value of PI = %f \n“,PI); return (0);

  21. 3.4.5 Header Files and the C Preprocessor # include <stdio.h> /* include declarations for I/O functions */ Stdio.h HEADER FILES (INTERFACE) STANDARD LIBRARY INTERFACE Main() USER-DEFINED FUNCTIONS STANDARD LIBRARY OBJECT CODE Printf() LIBRARY FUNCTIONS (IMPLEMENTATION) STANDARD LIBRARY Figure 3.2 Architecture of Program 3.1

  22. 3.4.6 Symbolic Constants • #define PI 3.1415926 • #define PI 3.1415926535897932846 3.4.7 Standard Output with printf() • Printf(“Approximate value of PI = %f \n“,PI); calls printf() with two arguments, a string constant “Approximate value of PI = %f \n“ Output Modified printf() statement Printf(“PI= %5.3f 2*PI= %5.3f\n,PI,2PI“); PI =3.141 2*PI=6.283

  23. Alternative Conversion Specifications Modified printf() statement Output Printf(“ PI = %f \n“,PI); PI = 3.141593 Printf(“ PI = %14.7f \n“,PI); PI = 3.1415926 Printf(“ PI = %14.7e\n“,PI); PI = 3.1415926e+00 Printf(“ PI = %14.7E \n“,PI); PI = 3.1415926E+00 Printf(“ PI = %14.7g \n“,PI); PI = 3.141593 Printf(“ PI = %14.7G \n“,PI); PI = 3.141593 Printf(“ PI = %-14.7f \n“,PI); PI = 3.1415926 Printf(“ PI = %-14.7e\n“,PI); PI = 3.1415926e+00

  24. 3.5 Program 3.2:Compute and Print Area of Circle 3.5.1 Program Statement and Source Code • Prompt and wait for user to supply radius of circle read the circle radius supplied as keyboard input check that radius is greater than zero. if radius less than or equal to zero. print error message and terminate program execution. use math library functions to compute an approximate value of pi. compute and print area of circle.

  25. Program 3.2 : Compute and Print Area of Circle /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * Compute Area of a Circle : Coefficients are defined in main program . * : Radius and area of circle are printed to screen . * Written by : Mark Austin January , 1994 * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/ #include <stdio.h> /*Standard Input/Output function declarations*/ #include <math.h> /*Math functions, such as sqrt(x), and constant M_PI*/ Int main(void){ Float fRadius; /* Radius of Circle*/ Float fArea; /* Area of Circle*/ Float fPi; /* Variable for “pi“ */ /* [a] : Prompt User for “ radius of circle” */ printf(“= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \n”); printf(“Please input the circle radius (Radius >0):” ); scanf(“%f”,&fRadius);

  26. /* [b] : Check that the radius is greater than zero */ If ( fRadius <=0 ) { printf(“ERROR>>Circle radius must be greater than zero\n”); exit(1); } /* [c] : Compute Area of Circle */ fPi = 4.0*atan(1.0); fArea = fPi*fRadius *fRadius; /* [d] = Print Radius and Area */ printf(“Radius of Circle = %8.3f \n”,fRadius); printf(“Area of Circle = %8.3f \n”,fArea); return(0); }

  27. STANDARD LIBRARY INTERFACE HEADER FILES Stdio.h Math.h (INTERFACE) USER-DEFINED FUNCTIONS Main() Exit() Printf() Scanf() Atan() LIBRARY FUNCTIONS (IMPLEMENTATION) MATH LIBRARY OBJECT CODE STANDARD LIBRARY OBJECT CODE Figure 3.3 Architecture if Program 3.2

  28. Table3.1 Some Important Functions in the Math Library Function Purpose Argument(s) Result Abs(x) Return absolute value of integer argument integer double Acos(x) Return arc cosine of double argument x double double Asin(x) Return arc sine of double argument x double double atan(x) Return arc tangent of double argument x double double cos(x) Return cosine of double argument x double double Exp(x) Return exponent of argument x double double Fabs(x) Return absolute value of argument x double double Log(x) Return natural logarithm of argument x double double Log10(x) Return basc 10 logarithm of argument x double double Pow(x,y) Return x^y with function call pow(x) double double Sin(x) Return sin of angle x (radius) double double Sqrc(x) Return square root of positive argument x double double Tan(x) Return tangent of angle x (radius) double double

  29. 3.5.3 Keywords in C The C language has 32 keywords (or reserved words) . Ex : float if int return void 3.5.4 Basic Data Types TABLE 3.2 Range and Precision of Basic Data Types Date Type No. Of Bytes Range and Precision Char 1 0-255 Int 4 Maximum = 2,147,483,647 Minimum = -2,147,483,648 Float 4 Maximum value = 3.402 * 10^38 Minimum value = 3.402 * 10^-38 Floating point numbers are represented to approximately 6-7 decimal places of accuracy Double 8 Maximum value = 1.797 * 10^308 Minimum value = 1.797 * 10^-308 Double precision numbers are represented to approximately 15-16 decimal places of accuracy

  30. 3.5.5 Declaring Variables • Int main (void) { float fRadius; /* Radius of circle */ float fArea; /* Area of circle */ float fPi; /* Variablre for “pi “ */ …………..details omitted ………….. } 3.5.6 Standard Input with scanf() Float fRadius; /* Radius of circle */ Printf(“Please input the circle radius (Radius > 0):“); Scanf(“ %f “,&Radius) ;

  31. 3.5.7 Arithmetic Expressions Table 3.3 Precedence and Association of Basic Arithmetic Operators Operator Purpose Precedence Association - Unary minus 2 Right to left + Unary plus 2 Right to left * Multiplication 3 Left to Right / Division 3 Left to Right % Modulus 3 Left to Right + Addition 4 Left to Right - Subtraction 4 Left to Right - Assignment 14 Right to left

  32. Table 5.1 Hierachy of Operator Evalution

  33. 3.5.8 Assignment Statements • Identifier = expression; fRadius = 2.0; fPi =4.0*atan(1.0); fArea =fPi*fRadius*fRadius; float fArea1; float fArea2; fArea2 = fArea1 = fPi*fRadius *fRadius;

  34. fRadius fRadius ???????? 2.0000000 fArea fArea ???????? 12.5663706 fPi fPi ???????? 3.1415926 a. before b. after Figure 3.4 Value of variables in memory (a) before and (b) after execution

  35. 3.5.9 Program Flow and Control • If (fRadius >= 0) { …. Statements of C code deleted } printf(“ERROR >> Circle radius muse greater than zero\n“); exit(1); ERROR>>Circle radius must be greater than zero

  36. 3.8 Program Exercise • Problem 3.2 & Problem 3.3 • Homework : Problem 3.5

More Related