Chapter 4 functions and program structure Speaker: Lung-Sheng Chien.

Slides:



Advertisements
Similar presentations
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?
Advertisements

FCU, Department of ECE, IC Design Research Lab. TEL: # 4945 Pre-SIm , Post-Sim.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
1 Q10276: Hanoi Tower Troubles Again! 星級 : ★★★ 題組: Online-judge.uva.es PROBLEM SET Volume CII 題號: Q10276: Hanoi Tower Troubles Again! 解題者:薛祖淵 解題日期: 2006.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/3 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH3.5 ~ CH /10/31.
3Com Switch 4500 切VLAN教學.
如何將數字變成可用之資訊 現代化資料處理與應用概念. 如何將數字變成可用之資訊 人最容易接受的訊息是圖像化資訊。 在一堆數字中,要進行比較分析,一般會使用表格形 式計算與分析。 所以一般我們會將數字依關聯性, 轉換成表格計算與分析。 此表格一般稱試算表或稱表格。 再將結果轉換為圖表,進行比較與分析。
五小專案 黃詩晴 章乃云. 目錄 計算機 智慧盤 拼圖 記憶大挑戰 數學題庫 心得 參考文獻.
CNA 2008 Project 1 TA: 徐志偉 Office: EA Grading policy: Total (100%) Report (20%) Socket Programming: (80%) –Basic (60%) –Advanced (20%)
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
JAVA 程式設計與資料結構 第十章 GUI Introdution III. File Chooser  File Chooser 是一個選擇檔案的圖形介面, 無論我們是要存檔還是要開啟檔案,使 用這個物件都會讓我們覺得容易且舒適。
第五章 函數與儲存類別. 函數 函數為完成某一次特定任務或工作的小程式 函數的類型 庫存函數 (library functions) 如: scanf() 、 printf() 函數 … 等 此函數為系統所提供可以直接呼叫使用之 自定函數 (user-define functions) 此函數是使用者自行定義之函數.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
Digital Signal Processing with Examples in M ATLAB ® Chap 1 Introduction Ming-Hong Shih, Aug 25, 2003.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: Count DePrimes ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11408: Count DePrimes 解題者:李育賢 解題日期: 2008 年 9 月 2 日 題意: 題目會給你二個數字 a,b( 2 ≦ a ≦ 5,000,000,a.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
從此處輸入帳號密碼登入到管理頁面. 點選進到檔案管理 點選「上傳檔案」上傳資料 點選瀏覽選擇電腦裡的檔案 可選擇公開或不公開 為平台上的資料夾 此處為檔案分類,可顯示在展示頁面上,若要參加 MY EG 競賽,做品一律上傳到 “ 98 MY EG Contest ” 點選此處確定上傳檔案.
第 13 章 檔案與資料夾處理 13-1 檔案與資料夾操作 13-1 檔案與資料夾操作 13-2 循序檔案的文字檔案讀寫 13-2 循序檔案的文字檔案讀寫 13-3 隨機檔案的處理 13-3 隨機檔案的處理 13-4 二進位檔案的讀寫 13-4 二進位檔案的讀寫 13-5 檔案對話方塊 13-5 檔案對話方塊.
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
Management Abstracts Retrieval System; MARS 檢索操作.
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
資料結構實習-二.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Linguistics phonetic symbols. 先下載 IPA 字型檔案,執行安裝。 由於這個程式的字型目錄設定錯誤, 所以等重新開機時就會發現字型消失。 所以必須根據以下步驟來讓 Windows 加入 IPA 字型。
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
: Expressions ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10157: Expressions 解題者:張庭愿 解題日期: 2009 年 8 月 16 日 題意:所有的括號必須成對,且必須先出現過左 括號後才能出現右括號,如果有.
: Help My Brother ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11033: Help My Brother 解題者: 呂明璁 解題日期: 2007 年 5 月 14 日.
組合語言與系統程式 軟體使用教學. 軟體下載及安裝 下載 到課程網頁下載 tw/~jiun/ 安裝 解壓縮後將 System Software 資料夾放在 D 槽 (C 槽版本放在 C 之下 )
: Sum-up the Primes ★★★★☆ 題組: Problem A 題號: 10419: Sum-up the Primes 解題者:林一帆 解題日期: 2006 年 5 月 15 日 題意: 評斷一個給予的數字是否是由給予個數 的質數所組成的.
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
:Stupid Sequence ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11319: Stupid Sequence 解題者:李育賢 解題日期: 2008 年 11 月 23 日 題意: 一個公式 f(x)=a 0 +a 1 x+a.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
-Artificial Neural Network- Matlab操作介紹 -以類神經網路BPN Model為例
NVivo 7在文件分析應用.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
Microsoft Excel.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Engineering Computing I Chapter 4 Functions and Program Structure.
A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev.
Presentation transcript:

Chapter 4 functions and program structure Speaker: Lung-Sheng Chien

OutLine Example: grep - organization of program structure - compilation process Precedence and Associativity External/Internal objects Scope and Lifetime Preprocessor ( 前處理器 )

Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire! Ah Love! could you and I with Fate conspire Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire! Search for the pattern “ould” grep – print lines matching a pattern

while ( there is another line) if ( the line contains the pattern ) then print it endif endwhile Pseudocode Function getline Function strindex Function printf File getline.cppFile strindex.cpp Structure of the program File main.cpp function getline function strindex function main (driver) call

1. project grep 2. source file getline.cpp grep project – getline function [1] Project  Add to Project  New

1. Source file getline.cpp grep project – getline function [2] Question 1: why we need input parameter lim? Question 2: what is execution order in predicate of while-loop? Q1 Q2 definition of getline

1. project grep 2. source file strindex.cpp grep project – strindex function [1] Project  Add to Project  New

grep project – strindex function [2] source file strindex.cpp Question: what is procedure of function strindex ? definition of strindex

1. project grep 2. source file main.cpp grep project – main function [1] Project  Add to Project  New

source file main.cpp remember to add new line character grep project – main function [2] Question: who does function main return found to ? declaration, not definition

Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire! article.txt 1. compile 3 source files 2. feed article.txt into a.out grep project – in Linux machine Upload project grep by sftp first 3. 上一個指令的傳回值

1. pattern 2. File “grep” is embedded in Linux machine Search manual of grep

return-type function-name ( argument declarations) { declarations and statements } Function definition 白話文 文言文 (grammar, page 234, 235) function-definition: declaraction-specifiers(opt) declarator declaraction-list(opt) compound-statement declaration-specifier: storage-class-specifier declaration-specifier(opt) type-specifier declaration-specifier(opt) type-qualifier declaration-specifier(opt) declarator: pointer(opt) direct-declarator direct-declarator: Identifier direct-declarator( parameter-type-list )

declaraction-specifiers declarator compound-statement type-specifiers definition of main function [1]

compound-statement: { declaration-list statement-list } declarator: Pointer(opt) direct-declarator direct-declarator: direct-declarator ( parameter-type-list ) Identifier definition of main function [2]

Why we need declaration of getline and strindex in main.cpp Compiler 是逐個檔案作編譯, 當他讀 main.cpp 且看見 getline(line, MAXLINE) 時需作 type checking, 可是 getline 的 defintion 是在 getline.cpp 內, 並不在 main.cpp, 所以我們需在 call getline 之前宣告 getline 的原型, 注意 : compiler 只讀檔一次, 所以必 需在 compiler 作 type checking 之前 就告訴他函數的原型

秀出編譯過程 先編譯 main.cpp c++ 編譯器 編譯為組合語言 GNU assembler 翻譯組語成 object code, ccEg6xsf.o 檔 編譯順序 -- [1] main.cpp

再編譯 getline.cpp 編譯為組合語言 c++ 編譯器 翻譯組語成 object code, ccnNjsxn.o 檔 編譯順序 -- [2] getline.cpp

c++ 編譯器 最後編譯 strindex.cpp 編譯為組合語言 翻譯組語成 object code, ccQJUtuu.o 檔 編譯順序 -- [3] strindex.cpp

main.cppgetline.cpp strindex.cpp Collect2 被作為 ld (GNU linker 連接器 ) 連接程序 – linking process Compiler g++ / icpc Assembler as Linker collect2 / ld Source code Assembly code.s file object code.o file Executable file a.out

OutLine Example: grep Precedence and Associativity External/Internal objects Scope and Lifetime Preprocessor ( 前處理器 )

Question 2: what is execution order in predicate of while-loop? Q2 Execution order Precedence and Associativity of operators [1]

Precedence and Associativity of operators, page 53 [2] arithmetic, 先乘除後加減 relational logical Precedence : () > arithmetic > relational > logical > assignment assignment

-- is higher than > () is higher than != > is higher than && != is higher than && associativity of && is left to right Parsing tree of Execution order 1.bottom-up 2.Left subtree  right subtree

用括號表示優先次序 Three equivalent coding style c is well-defined

Question: what’s procedure of function strindex ?

OutLine Example: grep Precedence and Associativity External/Internal objects Scope and Lifetime Preprocessor ( 前處理器 )

External/internal objects [1] External objects - external variable (global variable): defined outside any function - functions Internal objects - variables inside function without declaring static - parameters of a function translation-unit: external-declaration translation-unit external-declaration external-declaration: function-definition declaration declaration: declaration-sepcifiers init-declarator-list(opt) Grammar in page 234

External variable’s definition External object: function main Local variables file main.cpp External/internal objects [2]

Local variables file getline.cpp file strindex.cpp External object: function getline External object: function strindex

OutLine Example: grep Precedence and Associativity External/Internal objects Scope and Lifetime Preprocessor ( 前處理器 )

Scope and visibility The scope of a name is the part of the program within which the name can be used. For automatic variable (automatic storage) declared at the beginning of a function, the scope is the function in which the name is declared. Local variables of the same name in different functions are unrelated. The same is true of the parameters of the function, which are local variables also. The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled. External variable in file1 can be accessed by functions in file2 if it is declared by keyword extern in file2. static external variable is encapsulated inside a file1, no other functions of file2 can access it.

Scope graph block scope Level 0 Level 1 Level 2

Lifetime ( 生命周期 ) [1] “Lifetime” is the period during execution of a program in which a variable or function exists. The storage duration of the identifier determines its lifetime. Identifiers with static storage duration (global lifetime) are alive during (main) program execution and die when program terminates. Example: global variable, static variable Identifiers, which are defined inside a function, with automatic storage duration (local lifetime) is allocated new storage each time the function is called and die when function returns. Example: local variable, function parameter Although an identifier with a global lifetime exists throughout the execution of the program, it may not be visible in all parts of the program.

Lifetime ( 生命周期 ) [2]

extern and static [1] A variable declared with the extern storage-class specifier is a reference to a variable with the same name defined at the external level in any of the source files of the program. The internal extern declaration is used to make the external-level variable definition visible within the block. Unless otherwise declared at the external level, a variable declared with the extern keyword is visible only in the block in which it is declared. MSDN Library: search “extern” global variable

file main.cpp file strindex.cpp declaration definition extern and static [2]

OutLine Example: grep Precedence and Associativity External/Internal objects Scope and Lifetime Preprocessor ( 前處理器 )

C ProProcessor (cpp) [1] File inclusion #include “filename” #include Macro ( 巨集 ) substitution #define forever for ( ; ; ) // infinite loop #define square(x) (x)*(x) Conditional inclusion #ifndef _INC_STDIO #define _INC_STDIO /* content of stdio.h */ #endif /* _INC_STDIO */

Compiler g++ / icpc Assembler as Linker collect2 / ld Source code Assembly code.s file object code.o file Executable file a.out C preprocessor cpp C PreProcessor (cpp) [2]

CPP – macro substitution Question: Why no max operation in math.h cpp handles this but the compiler don’t see this statement

輸出至檔案 main_cpp.txt 讀檔案 main_cpp.txt 一次一頁 cpp 將替換 HELLO_STRING cpp 會複製 stdio.h 到此檔案以便 compiler 作 type checking CPP - #include “filename” [1]

line 28, include feature.h CPP - #include “filename” [2] Recursively file inclusion, 需滿足因果律 按空白鍵接下一頁

CPP - #include “filename” [3] standard input standard output

Result in Visual Studio Result in Linux 如果巨集 _WIN32 或巨集 __WIN32__ 有定義, 則執行程式碼 printf("This is Win32 Application\n"); 否則執行程式碼 printf("This is Linux Application\n"); CPP – conditional inclusion [1]

CPP – conditional inclusion [2] Macro _WIN32 is always defined in Visual Studio

add header file getline.h content of getline.h CPP – causality ( 因果律 ) [1]

content of strindex.h add header file strindex.h CPP – causality ( 因果律 ) [2]

Question 1: why we have infinite recursion? Question 2: How to solve infinite recursion? CPP – causality ( 因果律 ) [3]

strindex.hgetline.h getline.cppstrindex.cpp CPP – causality ( 因果律 ) [4]

1. no macro STRINDEX_H is defined 2. define STRINDEX_H 3. include header file getline.h no macro GETLINE_H is defined 5 5. define GETLINE_H STRINDEX_H is defined, ignore these codes 8 CPP – causality ( 因果律 ) [5] preprocess source strindex.cpp