A~A 断面) - ceri.go.jpMd Ss 幕別ダム 堤体標準断面図及び縦断図 図-2 堤体標準断面図(堤体標準断面図 A~A断面) 幕別ダム A A 本 堤 ブランケット工
電腦程式語言 ANSI C - CCT · 2 螢幕上輸入與輸出(In- and Output on Monitor)...
Transcript of 電腦程式語言 ANSI C - CCT · 2 螢幕上輸入與輸出(In- and Output on Monitor)...
-
電腦程式語言ANSI C
Chen-Ching Ting(丁振卿)
Mechanical Engineering, National Taipei University of Technology(國立台北科技大學機械系)
Homepage: http://cct.me.ntut.edu.tw/E-mail: [email protected]
CCT Group
April 13, 2010
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 1 / 69
-
課程大綱 I
1 基本架構(Basic Structure)
2 螢幕上輸入與輸出(In- and Output on Monitor)結果回報當面報告-螢幕上輸出
命令下達口頭下達-螢幕上輸入
3 檔案讀進與寫出(In- and Output in File)結果回報文書回報-檔案寫出
命令下達文書下達-檔案讀進
4 算數(Mathematics)
5 邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 2 / 69
-
參考文獻
施威銘研究室, 最新C 程式語言, 旗標出版股份有限公司,ISBN=957-717-964-9, 2005.
M. Zandstra, Teach Yourself PHP4 in 24 Hours, SAMS, 2000.
W.J. Gilmore, A Programmer’s Introduction to PHP 4.0, Apress,ISBN(pbk):1-893115-85-2, 2001.
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 3 / 69
-
基本架構(Basic Structure)
基基基本本本架架架構構構
(Basic Structure)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 4 / 69
-
基本架構(Basic Structure)
基本架構
1 註解
2 語法庫(Library)
3 定義特殊變數及代表意義
4 副程式
5 主程式
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 5 / 69
-
基本架構(Basic Structure)
基本架構
任何一種程式語言的基本結構不外乎以下幾個部份﹕
1 註解,作為解釋或提示用,在編譯過程中被忽略。
2 引進所需的語法庫(Library),其實就是一種具集(macro),以簡化編輯工作。
3 定義特殊變數及代表意義,以方便程式編輯,為C 檔案內程式本文之前直接製作的具集(macro)。
4 引入副程式文,也是一種具集(macro),定義副程式函數並在主程式內加以呼叫,簡化主程式及重複性的指令輸入並方便程式編寫。
5 引入主程式文。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 6 / 69
-
基本架構(Basic Structure)
註註註解解解
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 7 / 69
-
基本架構(Basic Structure)
註解
所謂註解,顧名思義乃加註作解釋的意思,在程式語言特別指的是對所編寫的程式之原始檔案所下的命令作說明之意,由於電腦程式語言並非如一般人類口語般容易了解,自己所寫的內容往往隨著時間或內容的不斷增大而無法完全了解,註解乃在編寫程式時對可能會忘記的部份以口語的方式加註解釋,以方便日後查看,因此,註解內容將會被編譯器所忽略。
C與PHP語言﹕單行註解﹕// 註解內容多行註解﹕/* 註解內容 */
HTML語言﹕多行註解﹕
LaTeX語言﹕單行註解﹕% 註解內容
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 8 / 69
-
基本架構(Basic Structure)
語語語法法法庫庫庫(Library)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 9 / 69
-
基本架構(Basic Structure)
ANSI C 語法庫(Library)
ANSI C 語法庫為∗.h 的檔案,不同的檔案內儲存不同的程式語法資訊,程式編寫時所需用到語法庫,當程式編寫如果需要用到各種語法時,必須在程式編寫的開端預先引入(寫上) 適當的語法庫,才能使用該語法。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 10 / 69
-
基本架構(Basic Structure)
一些ANSI C所預備的圖書館及其用途
圖書館 用途 範例 範例說明(Library)
stdio.h 基本輸出與輸入 scanf(′′%s′′,&in); 請求在Monitor上輸入in所代表的字元
scanf(′′%s′′,in); 請求在Monitor上輸入in所代表的字串
printf(′′%s′′,in); 請求在Monitor上輸出in所代表的字串
string.h 引入字串 strcat(dir, file); 將字串file連接在字串dir之後成dirfile
math.h 引入數學函數 sqrt(x) x 的平方根
exp(x) 自然指數(ex )
pow(x,y) x 的y 次方(xy )
fabs(x) x 的絕對值(|x|)
sin(x) x 的正弦值
cos(x) x 的餘弦值
tan(x) x 的正切值
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 11 / 69
-
基本架構(Basic Structure)
定定定義義義特特特殊殊殊變變變數數數
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 12 / 69
-
基本架構(Basic Structure)
定義特殊變數
1 資料形態?
2 何謂變數?
3 何謂常數?
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 13 / 69
-
基本架構(Basic Structure)
資料形態
所謂資料形態乃指變數或常數為整數,小數,字元或字串等等形態,在以C語言編寫程式時,對於所用到的任何一個變數都必須預先進行所謂的資料形態宣告,PHP語言則在文字前加上$符號代表為變數,程式便以$符號為辨識變數而直接使用變數不必宣告。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 14 / 69
-
基本架構(Basic Structure)
ANSI C 的變數宣告屬性
宣告 用途 格式符號 範例 範例說明
int 整數 %d %3.2d 3為預留空格,2為位數,即5在此寫成05
char 字元 %c
字串 %s
float 浮點 %f %3.2f 3為預留整數空格,2為小數位數
科學符號表示 %e
double 雙倍浮點 %f
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 15 / 69
-
基本架構(Basic Structure)
變數
所謂變數乃指某個代號,此代號將被指定成任意的東西,此東西可以是數字或文字等,甚至也可以是另一個變數。
ANSI C﹕char var1[4]="test";float var2=3.14159;
PHP﹕$var1="test";$var2=3.14159;
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 16 / 69
-
基本架構(Basic Structure)
常數
所謂常數乃指固定不變的東西,此東西可以是數字或文字等。
ANSI C﹕#define 常數名稱 "所指文字"範例﹕#define user "I am CCT"#define 常數名稱 "所指數字"範例﹕#define pi 3.14159
PHP﹕define("常數名稱","所指文字");範例﹕define("user","I am CCT");define("常數名稱",所指數字);範例﹕define("pi",3.14159);
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 17 / 69
-
基本架構(Basic Structure)
ANSI C 定義特殊變數
簡化程式的方法之一,將程式中經常重覆出現且繁瑣的語句,於程式基本架構前以define的語法進行簡化定義,再於程式中以所定義的符號取代經常重覆出現且繁瑣的語句。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 18 / 69
-
基本架構(Basic Structure)
ANSI C一些常用有關define的用法範例
語句 用途
#define π 3.14159 定義π 的值為3.14159
#define like ′′I like ANSI C.′′ 定義like 代表雙引號內的句子I like ANSI C.
#define a {\ 定義a 代表之後的數學運算式,cm=mm/10.;\ 其中mm、cm 及m 均為變數(Variable)
m=mm/1000.;\}
#define a {\ 定義a 代表之後的螢幕寫出意義printf(′′%d′′\n,i);\printf(′′%f′′\n,j);\
}
#define clear printf(′′\x1b[2J′′) 定義clear 指令以清除畫面
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 19 / 69
-
基本架構(Basic Structure)
副副副程程程式式式
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 20 / 69
-
基本架構(Basic Structure)
ANSI C副程式
簡化程式的另一方法,將程式中經常重覆出現且繁瑣的語句,於程式基本架構前以function的語法進行簡化定義,再於程式中以所定義的function取代經常重覆出現且繁瑣的語句。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 21 / 69
-
基本架構(Basic Structure)
ANSI C副程式
#includevoid subfun(){
printf("計算機概論");}int main(){
內容subfun();
}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 22 / 69
-
基本架構(Basic Structure)
主主主程程程式式式
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 23 / 69
-
基本架構(Basic Structure)
主程式
#include // 引入語法庫(Library)int main(){
內容return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 24 / 69
-
基本架構(Basic Structure)
範例
#include#define x "我愛計算機程式語言"void area(){
printf("%s",x);}int main(){
area();return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 25 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
螢螢螢幕幕幕上上上輸輸輸入入入與與與輸輸輸出出出
(In- and Output on Monitor)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 26 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
螢幕上輸入與輸出(In- and Output on Monitor)
1 結果回報1 當面報告-螢幕上輸出2 文書回報-檔案寫出
2 命令下達1 口頭下達-螢幕上輸入2 文書下達-檔案讀進
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 27 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
1 當面報告-螢幕上輸出
2 文書回報-檔案輸出
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 28 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
當當當面面面報報報告告告-螢螢螢幕幕幕上上上輸輸輸出出出
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 29 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
ANSI C 螢幕顯示
printf(“內容”);
在主程式大括號內下printf()命令,如上所示,電腦螢幕上將會列印出雙引號內的內容,另外,C與PHP語言都是使用分號結束命令。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 30 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
課堂練習
請以ANSI C編寫一程式,程式經Compiler編譯後,會在螢幕上顯示出一列”我愛計算機程式語言”文字。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 31 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
解答範例
#includeint main(){
printf("我愛計算機程式語言");return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 32 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
ANSI C 列印排列方式
printf(“我愛計算機程式語言\n”);
語句 用途
scanf( ′′%s′′ , file ); 電腦於螢幕上提發問並等待使用者寫入”字串”
字串輸入並在遇到空白(\0)鍵與enter (\r)鍵時結束
scanf( ′′%c ′′ , &file ); 電腦於螢幕上提發問並等待使用者寫入”字元”
fscanf(infile, ′′%s′′ , file ); 電腦於螢幕上提發問並等待使用者寫入”字串”於檔案infile 內
fscanf(infile, ′′%s′′ , &file ); 電腦於螢幕上提發問並等待使用者寫入”字元”於檔案infile 內
getchar(); 字元輸入
putchar(); 輸出由getchar(); 所輸入的字元
gets(); 字串輸入並在遇到enter (\r)鍵時結束
puts(); 輸出由gets(); 所輸入的字串
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 33 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
課堂練習
請以ANSI C語言編寫一程式,並應用定義特殊變數的技巧定義z 等同我愛計算機程式語言,此程式經Compiler編譯後,會在螢幕上顯示d:\outfile.txt檔案輸出成功,請開啟查看內容,謝謝。並在檔案d:\outfile.txt中列印出十列”我愛計算機程式語言“文字。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 34 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
1 口頭下達-螢幕上輸入
2 文書下達-檔案輸入
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 35 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
口頭下達-螢幕上輸入
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 36 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
問題
請以ANSI C編寫一程式,程式經Compiler編譯後,會在螢幕上同時顯示出一列”台北科技大學”及一列”我愛計算機程式語言”文字並發出”嘟”聲。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 37 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
解答範例
#includeint main(){
printf("台北科技大學\n");printf("我愛計算機程式語言\a");
return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 38 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
文文文書書書回回回報報報-檔檔檔案案案輸輸輸出出出
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 39 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
檔案類別
檔案又分為文字檔與二進位檔兩種。例如利用Windows 的記事本或漢書等所寫的檔案就是文字檔,內容可以是英文字母、數字、中文等等,也可以在『命令提示字元』下,用type 指令正常顯示出檔案的內容。通常文字檔的副檔名會用.txt,但這並非必要。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 40 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
二進位檔(Binary)
另一種是二進位檔,例如圖片檔、音樂檔、Word 檔、資料庫檔、執行檔等等都是。此種檔案的內容包含超出ASCII 字元範圍的內容,所以如果直接type 內容,則會出現許多奇怪的符號,或是聽到電腦的嗶聲等。將程式輸入與輸出的對象,由鍵盤、螢幕等標準輸出入裝置改為檔案。藉由呼叫適當的函式,可將檔案內容讀入程式中處理,也可以將資料寫入檔案中保存。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 41 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
FILE 結構體
用程式來處理檔案時,都必須經過以下3個步驟:
1 開啟檔案
2 讀寫檔案
3 關閉檔案
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 42 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
檔案寫出
char x[40];FILE *outfile;outfile=fopen(’’d:\\in.txt’’,’’w’’);fprintf(outfile,’’%s’’,x);fclose(outfile);
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 43 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
檔案的讀寫權限
命令 用途
outfile=fopen(′′file.txt′′,′′r′′); 開啟唯讀的ASCII 檔file.txt
outfile=fopen(′′file.txt′′,′′w′′); 開啟可讀寫的ASCII 檔file.txt,若檔案已存在,則內容將被替換掉(overwrite)
outfile=fopen(′′file.txt′′,′′a′′); 開啟可讀寫的ASCII 檔file.txt,寫入的內容將連接在原有內容之後
outfile=fopen(′′file.txt′′,′′rb′′); 開啟唯讀的binary 檔file.txt
outfile=fopen(′′file.txt′′,′′wb′′); 開啟可讀寫的binary 檔file.txt,若檔案已存在,則內容將被替換掉(overwrite)
outfile=fopen(′′file.txt′′,′′ab′′); 開啟可讀寫的binary 檔file.txt,寫入的內容將連接在原有內容之後
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 44 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
問題
請以ANSI C編寫一程式,以宣告變數並指定變數為所欲列印文字的方式,程式經Compiler編譯後,會在螢幕上同時顯示出一列”台北科技大學”及一列”我愛計算機程式語言”文字並發出”嘟”聲。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 45 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
解答範例
#includeint main(){char x[20]="台北科技大學";char y[30]="我愛計算機程式語言";
printf("%s\n",x);printf("%s\a",y);
return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 46 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
問題
請以ANSI C語言編寫一程式,並應用定義特殊變數的技巧定義z 等同我愛計算機程式語言,此程式經Compiler編譯後,會在螢幕上顯示出十列”我愛計算機程式語言”文字。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 47 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 結果回報
解答範例
#include#define z "我愛計算機程式語言"int main(){
printf("%s\a",z);return 0;}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 48 / 69
-
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
口口口頭頭頭下下下達達達-螢螢螢幕幕幕上上上輸輸輸入入入
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
螢幕讀進與寫出
整數數字﹕讀進﹕
int x;scanf(“%d”,&x);
寫出﹕
int x;printf(“%d”,x);
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
螢幕讀進與寫出
小數數字﹕讀進﹕
float x;scanf(“%f”,&x);
寫出﹕
float x;printf(“%f”,x);
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
螢幕讀進與寫出
不含空白鍵的字元文字﹕
讀進﹕
char x;scanf(“%c”,&x);
寫出﹕
char x;printf(“%c”,x);
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
螢幕讀進與寫出
不含空白鍵的字串文字﹕
讀進﹕
char x[20];scanf(“%s”,x);
寫出﹕
char x[20];printf(“%s”,x);
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
螢幕讀進與寫出
含空白鍵的字串文字﹕
讀進﹕
char x[20];gets(x);
寫出﹕
char x[20];printf(“%s”,x);
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
課堂練習
請以ANSI C語言編寫一程式﹐當此程式經Compiler編譯後並執行﹐會首先在螢幕上顯示出一行文字如下﹕
請問妳/你喜歡計算機概論嗎﹖
等待回答喜歡或不喜歡後﹐顯示我喜歡或不喜歡計算機概論。
問題﹕
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
課堂練習
請以ANSI C語言編寫一程式﹐當此程式經Compiler編譯後並執行﹐會首先在螢幕上顯示出一行文字如下﹕
Do you like Computer?
等待回答Yes, I like.或No, I do not like.後﹐顯示Clear!! Yes, I like.或Clear!! No, I do not like.
問題﹕
螢幕上輸入與輸出(In- and Output on Monitor) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 49 / 69
-
檔案讀進與寫出(In- and Output in File) 結果回報
檔檔檔案案案讀讀讀進進進與與與寫寫寫出出出
(In- and Output in File)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 50 / 69
-
檔案讀進與寫出(In- and Output in File) 結果回報
檔案讀進與寫出(In- and Output in File)
1 結果回報1 當面報告-螢幕上輸出2 文書回報-檔案寫出
2 命令下達1 口頭下達-螢幕上輸入2 文書下達-檔案讀進
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 51 / 69
-
檔案讀進與寫出(In- and Output in File) 結果回報
文文文書書書回回回報報報-檔檔檔案案案寫寫寫出出出
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
檔案類別
• 檔案又分為文字檔與二進位檔兩種。例如利用Windows 的記事本或漢書等所寫的檔案就是文字檔, 內容可以是英文字母、數字、中文等等, 也可以在『命令提示字元』下, 用 type 指令正常顯示出檔案的內容。通常文字檔的副檔名會用 .txt , 但這並非必要。
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
二進位檔(Binary)• 另一種是二進位檔, 例如圖片檔、音樂檔、Word 檔、資料庫檔、執行檔等等都是。此種檔案的內容包含超出 ASCII 字元範圍的內容, 所以如果直接 type 內容, 則會出現許多奇怪的符號, 或是聽到電腦的嗶聲等。
• 將程式輸入與輸出的對象, 由鍵盤、螢幕等標準輸出入裝置改為檔案。藉由呼叫適當的函式, 可將檔案內容讀入程式中處理, 也可以將資料寫入檔案中保存。
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
檔案讀寫的原理
• C 語言標準函式庫讀寫檔案的原理, 是將整個檔案的內容, 看成是由檔案中的每個位元組依序排列而成的『資料流』。所有關於此資料流的相關資訊都紀錄在一個 "FILE" 結構體變數, 透過這個結構體, 我們就能進行檔案讀寫的動作。
–資料流的觀念–FILE 結構體
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
資料流的觀念
• 檔案是存在磁碟中的電子資料。要存取檔案內容前, 必須先開啟 (open) 檔案。如此一來, 就好像在程式與檔案之間建立了一條通道, 程式可讀取檔案或寫入檔案, 這條連通的管道就是資料流(stream)。我們可以將資料流視為一個連續的字元串流, 這個字元串流就是檔案內容:
…
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
資料流的觀念
• 因此在讀取檔案或寫入檔案 (也稱為檔案 I/O) 時, 其實就是對資料流做讀取或寫入。若沒有開啟資料流, 就不能存取檔案。
• 而當使用完檔案時, 也需要關閉(close) 檔案, 如同是將資料流關閉, 結束這條溝通的管道。
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
FILE 結構體
• 在 stdio.h 裡有一個以 typedef定義的 FILE 結構體型別, 它就是用來建立代表資料流的變數, 讓程式據以存取檔案內容:
…
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
FILE 結構體
• 用程式來處理檔案時, 都需要經過以下 3 個步驟:
1. 開啟檔案:2. 讀寫檔案:3. 關閉檔案:
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
檔案寫出
char x[40];FILE *outfile;outfile=fopen(“d:\\in.txt”,”w”);fprintf(outfile,“%s”,x);fclose(outfile);
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
檔案寫出
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
課堂練習
請以ANSI C語言編寫一程式﹐並應用定義特殊變數的技巧定義 z 等同我愛計算機程式語言﹐此程式經Compiler編譯後﹐會在螢幕上顯示d:\outfile.txt檔案輸出成功﹐請開啟查看內容﹐謝謝。﹐並在檔案d:\outfile.txt中列印出十列”我愛計算機程式語言“文字。
問題﹕
檔案讀進與寫出(In- and Output in File) 結果回報
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 52 / 69
-
檔案讀進與寫出(In- and Output in File) 結果回報
問題
請以ANSI C語言編寫一程式,當此程式經Compiler編譯後並執行,會首先在螢幕上顯示出一行文字如下﹕請問妳/你喜歡計算機概論嗎﹖等待回答喜歡或不喜歡後,顯示我喜歡或不喜歡計算機概論。在檔案out.txt內,並列印檔案out.txt已寫出,請關閉視窗並開啟檔案查看。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 53 / 69
-
檔案讀進與寫出(In- and Output in File) 結果回報
解答範例
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 54 / 69
-
檔案讀進與寫出(In- and Output in File) 命令下達
文文文書書書下下下達達達-檔檔檔案案案讀讀讀進進進
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
檔案讀進
整數數字﹕
int x;FILE *infile;infile=fopen("d:\\temp\\1.txt","r");fscanf(infile,"%d",&x);printf("%d\n",x);fclose(infile);
檔案讀進與寫出(In- and Output in File) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
檔案讀進
小數數字﹕
float x;FILE *infile;infile=fopen("d:\\temp\\1.txt","r");fscanf(infile,"%f",&x);printf("%f\n",x);fclose(infile);
檔案讀進與寫出(In- and Output in File) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
檔案讀進
不含空白鍵的字元文字﹕
char x;FILE *infile;infile=fopen("d:\\temp\\1.txt","r");fscanf(infile,"%c",&x);printf("%c\n",x);fclose(infile);
檔案讀進與寫出(In- and Output in File) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
檔案讀進
不含空白鍵的字串文字﹕
char x[10];FILE *infile;infile=fopen("d:\\temp\\1.txt","r");fscanf(infile,"%s",x);printf("%s\n",x);fclose(infile);
檔案讀進與寫出(In- and Output in File) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
螢幕讀進與寫出
含空白鍵的字串文字﹕
char x[10];FILE *infile;infile=fopen("d:\\temp\\1.txt","r");fgets(x,10,infile);printf("%s\n",x);
檔案讀進與寫出(In- and Output in File) 命令下達
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 55 / 69
-
檔案讀進與寫出(In- and Output in File) 命令下達
問題
請以ANSI C語言編寫一程式,當此程式經Compiler編譯後並執行,程式會讀進檔案out.txt的內容並將所讀入的內容寫入檔案move.txt內並在螢幕上顯示出一行文字檔案out.txt的內容已寫入檔案move.txt中,請關閉視窗並開啟檔案查看。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 56 / 69
-
檔案讀進與寫出(In- and Output in File) 命令下達
解答範例
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 57 / 69
-
算數(Mathematics)
算算算數數數
(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
數學運算符與邏輯符號
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
課程大綱
算數運算符
數學運算函數
數學運算符與邏輯符號
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
算數運算符
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
算數運算符
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
運算符(Operator)
運算符 範例 同義
+= $x+=5$x-=5$x*=5$x/=5
$x%=5$x.=“test”
$x=$x+5-= $x=$x-5*= $x=$x*5/= $x=$x/5
%= $x=$x%5.= $x=$x”test”
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
課堂練習
問題1﹕試用ANSI C 語言編寫程式﹐程式將可進行溫度單位互換﹐如攝氏與華氏及攝氏與絕對溫度單位互換。注意﹕程式經編譯並執行後將列印發問﹕”請輸入一攝氏溫度值以換算成華氏溫度﹐攝氏溫度值= “﹐待輸入一值後﹐程式將列印“攝氏溫度X度C時﹐華氏溫度=X度F”
9325
F C= +提示﹕
273.15K C= +
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
解答範例
#include
int main(){
float x,y;printf("請輸入一攝氏溫度值以換算成華氏溫度﹐攝氏溫度值= ");scanf("%f",&x);y=(9./5.)*x+32;printf("攝氏溫度 %.2f 時﹐華氏溫度 = %.2f\n",x,y);
return 0;}
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
數學運算函數
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
math.h
sin(x) sin x,x以弧度解釋cos(x) cos x,x以弧度解釋tan(x) tan x,x以弧度解釋
asin(x) sin-1 x,定義域是 [-1, 1]、值
域是 [-pi/2, pi/2]
acos(x) cos-1 x,定義域是 [-1, 1]、值
域是 [0, pi] atan(x) tan-1 x,值域是 [-pi/2, pi/2] atan2(y,x) tan-1 y/x,值域是 [-pi, pi]
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
math.hsinh(x) sinh xcosh(x) cosh xtanh(x) tanh xexp(x) ex
log(x) ln x,x>0 log10(x) log10 x,x>0
pow(x,y) xy,若 x=0、 y必須 >0,若 x=0 ceil(x) x的最小整數上界floor(x) x的最大整數下界fabs(x) |x| ldexp(x,n) 2nxfmod(x,y) x/y得整數商的餘數
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
課堂練習
請以ANSI C語言編寫一程式﹐當此程式經Compiler編譯後並執行﹐會首先在螢幕上顯示出一行文字如下﹕請輸入一 x 值以求 x 值的開根號﹐x=等待輸入一x後﹐在螢幕上顯示計算結果x 的開根號 = **。
問題﹕
提示﹕#include開根號sqrt(x) ﹐次方pow(x,1./2.)
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
課堂練習
請以ANSI C語言編寫一程式來解二次方程式 ﹐其中a, b, c為任意給定值。
問題﹕
提示﹕
2 0ax bx c+ + =
首先編寫一行發問a, b, c值﹐待輸入a, b, c值後進行運算並列印
1 2[ ( )][ ( )] 0x x x x+ + =
其中2
14
2b b acx
a− + −
=2
24
2b b acx
a− − −
=
另外﹐嘗試將return 0;改成return main();看看有何不同。
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
解答範例
#include#includeint main(){
float a,b,c,x1,x2;printf(“解二次方程ax^2+bx+c=0”);printf("請輸入 a = ");scanf("%f",&a);printf("請輸入 b = ");scanf("%f",&b);printf("請輸入 c = ");scanf("%f",&c);x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf( "解答﹕\n ");printf( " [x+(%f)][x+(%f)]=0\n",-x1,-x2);
return 0;}
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
課堂練習
請以ANSI C語言編寫一程式來解三角函數sin(x)及cos(x) 值﹐其中x為任意給定整數值﹐單位是degree﹐請將結果在螢幕上顯示﹐另外﹐計算sin(x)+cos(x)值﹐當結果為正時﹐顯示結果並列印正向振動﹐反之﹐當結果為負時﹐顯示結果並列印反向振動。
問題﹕
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
數學運算符與邏輯符號
算數(Mathematics)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 58 / 69
-
數學運算符與邏輯符號比較
Mathematics+ - * / %
= > >= < >= <
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
邏邏邏輯輯輯、、、迴迴迴圈圈圈、、、陣陣陣列列列
(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 59 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
流流流程程程控控控制制制
(Flow Control)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 60 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
流程控制(Flow Control)
1 邏輯(Logic)
2 迴圈(Loop)
3 選項控制(Switch)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 61 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
邏邏邏輯輯輯
(Logic)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 62 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
邏輯(Logic)
()內下條件,{}內下命令。1 假如(怎樣),那麼就(怎樣)。
if(condition){
command}
2 假如(怎樣),那麼就(怎樣),否則就(怎樣)。if(condition){
command}else{
command}
3 假如(怎樣),那麼就(怎樣),否則假如(怎樣),那麼就(怎樣)。if(condition){
command}else if(condition){
command}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}
if…
#includeint main(){
return 0;}
Condition
Command
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}
if…
x== '1'
printf ("The command is performed successfully.\n");
#includeint main(){
return 0;}
char x;scanf("%s",&x);
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}else{
}
if…else…
Condition
Command
Command
#includeint main(){
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}else{
}
if…else…
x=='1'
printf ("The command is performed successfully.\n");
#includeint main(){
return 0;}
printf ("The command can not be performed.\n");
char x;scanf("%s",&x);
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}else if ( ){
}
if…else if…
Condition
Command
Condition
Command
#includeint main(){
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
if ( ){
}else if ( ){
}
if…else if…
x=='1'
printf ("The command is performed successfully.\n");
#includeint main(){
return 0;}
char x;scanf("%s",&x);
x=='2'
printf ("The command is performed successfully.\n");
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
課堂練習
請以 ANSI C 語言編寫一程式來解二次方程式 ﹐其中 a, b, c 為任意給定值﹐注意﹗﹗程式會進行開根號值判斷﹐當根號內為負值時﹐會印出根號內值為負。
問題﹕
提示﹕
2 0ax bx c+ + =
首先編寫一行發問 a, b, c 值﹐待輸入a, b, c 值後進行運算並列印
1 2[ ( )][ ( )] 0x x x x+ + =其中
2
14
2b b acx
a− + −=
2
24
2b b acx
a− − −=
另外﹐嘗試將 return 0; 改成 return main(); 看看有何不同。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
課堂練習
請以 ANSI C 語言編寫一程式來解三角函數 sin(x) 及 cos(x) 值﹐其中 x 為任意給定整數值﹐單位是 degree﹐ 請將結果在螢幕上顯示﹐另外﹐計算 sin(x)+cos(x)值﹐當結果為正時﹐顯示結果並列印正向振動﹐反之﹐當結果為負時﹐顯示結果並列印反向振動。
問題﹕
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 63 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
迴迴迴圈圈圈
(Loop)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 64 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
迴圈(Loop)
所謂迴圈(Loop)包括兩個概念,
1 重複循環執行某件事的概念,在符合條件的情況下,會不斷循環的執行命令,直到不再符合條件時才停止。
2 有限的執行次數的概念,對於第二項概念,編織有限執行次數的方法需要三個條件來完成,即起點、終點、間隔。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 65 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
迴圈(Loop)
()內下條件,{}內下命令。1 當(什麼狀況)時,則做(什麼事)。
while(condition){
command}
2 做(什麼事),當(什麼狀況)時,則做(什麼事)。do{
command}while(condition);
3 在(什麼條件)下,做(什麼事)。for(condition){
command}
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
while ( ){
}
Condition
Command
#includeint main(){
return 0;}
while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
while ( ){
}
x=='1'
printf ("The command is performed successfully.\n");
#includeint main(){
return 0;}
char x;
while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
do{
} while ( );Command Condition
#includeint main(){
return 0;}
do…while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
do{
} while ( );x==‘1’printf ("The command is performed successfully.\n");
#includeint main(){
return 0;}
char x;
do…while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
Exercise for Menu Selection#include
void subfunc1( ) // 副程式文{ printf("This is the first subfunction.\n");}
void subfunc2( ) // 副程式文{ printf("This is the second subfunction.\n");}
int main( ) // 主程式文{ char sel; do { printf ( "\n0) Program end\n" ); printf ( "1) Run subfunc1 ......\n" ); printf ( "2) Run subfunc2 ......\n" ); printf ( "Please input a number: " ); scanf("%s",&sel); if ( sel == '1' ) subfunc1 ( ); else if (sel == '2') subfunc2 ( ); } while ( sel != '0' ); printf( "\nPlease close the window.\n" );return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
課堂練習
請以 ANSI C 語言編寫一具選項功能的程式﹐程式先列印選項等待使用者進行選擇﹐按 1 時﹐程式將解三角函數 sin(x) 值 ;按 2 時﹐程式將解三角函數 cos(x) 值 ; 按 3 時﹐程式將解三角函數 sin(x)+cos(x)值﹐當結果為正時﹐顯示結果並列印正向振動﹐反之﹐當結果為負時﹐顯示結果並列印反向振動。其中 x 為任意給定整數值﹐單位是 degree 。 按 0 時﹐程式將列印請關閉視窗。
問題﹕
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
for ( ){
}
for…
Condition
Command
#includeint main(){
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
for ( ){
}
for…
i=1;i
-
while ( ){
}
Condition
Command
#includeint main(){
return 0;}
while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
while ( ){
}
i
-
do{
} while ( );
Command
Condition
#includeint main(){
return 0;}
do…while…
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
do{
} while ( );i
-
Exercise 9 x 9 Table (for…)
#includeint main( ){ int i,j; for(i=2;i
-
Exercise 9 x 9 Table (while…)#includeint main( ){ int i,j; i=2; while(i
-
Exercise 9 x 9 Table (do…while…)#includeint main( ){ int i,j; i=2; do { j=2; do { printf("%d x %d = %2d\t",i,j++,i*j); }while(j
-
選項控制 (Switch)
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 66 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
選選選項項項控控控制制制
(switch)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 67 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
選項控制(switch)
()內下條件,{}內下命令。1 設置(條件),符合條件1(做什麼事),符合條件2(做什麼事),內定(做什麼事)。switch(condition){
case condition1:commandbreak;
case condition2:commandbreak;
default:command
}2 做(選項表),當(不是這種狀況)時。
do{
command}while (condition);
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 68 / 69
-
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
選項控制(switch)
switch選項控制使用的條件一般設置一變數,而case中的條件則為此一變數的指定值。變數為數字時不加任何引號,變數為字元時加單引號,變數為字串時加雙引號。
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
switch…
switch( ){
}
#includeint main(){
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
switch…
switch(variable){ case var1: command break; case var2: command break; default: command}
#includeint main(){
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
switch…
switch(x){ case 1: printf(“I like ANSI C.”); break; case 2: printf(“I like NTUTME.”); break; default: printf(“You have to type value 1 or 2.”);}
#includeint main(){ int x; scanf(“%d”,&x);
return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
#include
void subfunc1( ) // 副程式文{ printf("This is the first subfunction.\n");}
void subfunc2( ) // 副程式文{ printf("This is the second subfunction.\n");}
int main( ) // 主程式文{ int sel; printf ( "\n0) Program end\n" ); printf ( "1) Run subfunc1 ......\n" ); printf ( "2) Run subfunc2 ......\n" ); printf ( "Please input a number: " ); scanf("%d",&sel); switch(sel) { case 1: subfunc1(); break; case 2: subfunc2(); break; default: printf( "\nPlease close the window.\n" ); break; }return 0;}
switch
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
請以 ANSI C 語言的 switch 語法編寫一具選項功能的程式﹐程式先列印選項等待使用者進行選擇﹐按 1 時﹐程式將解三角函數 sin(x) 值 ;按 2 時﹐程式將解三角函數 cos(x) 值 ; 按 3 時﹐程式將解三角函數 sin(x)+cos(x)值﹐當結果為正時﹐顯示結果並列印正向振動﹐反之﹐當結果為負時﹐顯示結果並列印反向振動。其中 x 為任意給定整數值﹐單位是 degree 。 按 0 時﹐程式將列印請關閉視窗。
問題﹕
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
巨集,函數與陣列(Macro, Function and Array)
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課程大綱
Macro
Function
Array
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Macro
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
基本結構
任何一種程式語言的基本結構不外乎以下幾個部份﹕1.註解,作為解釋或提示用,在編譯過程中被忽略。2.引進所需的語法庫(Library) ,其實就是一種具集(macro),以簡化編輯工作。3.定義特殊變數及代表意義,以方便程式編輯,為 C 檔案內程式本文之前直接製作的具集(macro)。4.引入副程式文,也是一種具集(macro),定義副程式函數並在主程式內加以呼叫,簡化主程式及重複性的指令輸入並方便程式編寫。5.引入主程式文。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Macro
簡化程式的方法﹐將程式中經常重覆出現且繁瑣的語句﹐於程式基本架構前以define的語法進行簡化定義﹐再於程式中以所定義的符號取代經常重覆出現且繁瑣的語句 , 或以自訂函數(function)的方式進行簡化主程式。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
cct.h
#define clrscr() printf("\x1b[2J") #define pi 3.14159
自訂Library來簡化主程式,可以#define的語法事先將所要簡化的內容寫在自訂的Library檔案,再引入使用 .
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Function
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
函數乃副程式﹐意即在主程式內﹐對於重複出現的工作先以函數寫成﹐再嵌入此函數使完成工作﹐函數除了簡化主程式外﹐更可方便對程式檢查與修改。
Function
函數的使用可以寫出非常漂亮的程式結構,使程式 簡單化,容易偵錯。將重複之某些指令撰寫成一個函數,可減少編輯程 式時間,更可使程式精簡,清晰了解。函數的使用其呼叫方法與數學上使用函數完全相同 。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Function
定義一函數代表某些內容以簡化主程式。
#includevoid hello(){ printf(“Hello!!”);}
int main(){ hello();return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Function
定義一函數代表另一新指令以簡化執行工作的主程式語法。
#includevoid printwords(){ printf(“Hello!!”);}
int main(){ printwords();return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Function
定義一函數以進行算數運算並簡化主程式。
#includeint addnum(x,y){ int results; results=x+y; return results;}
int main(){ printf("%d",addnum(5,6));return 0;}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
自訂函數
1.設定函數目的僅在簡化主程式,與主程式間無變數相互傳遞關係 .2. 設定函數目的除了在簡化主程式外,與主程式間還有變數相互傳遞關係 .
自訂函數擺放位置可在主程式之前,也可在主程式之後,但必須先宣告 .
#includevoid hello();int main(){ hello();return 0;}void hello(){ printf(“Hello!!”);}
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
函式的組成
● 傳回值的型別:當函式依函式主體的內容敘述作執行完畢 , 然後把結果傳回給呼叫者 (例如 main ( ) 函式 ), 這個結果我們稱為傳回值 , 其型別稱為傳回值的型別。傳回值型別可為 int、 float、 char、void ... 等 , void 表示無傳回值。
● 函式名稱:每個函式都需要給予一個獨立的名稱 , 以便與其他函式、變數區隔 , 函式的命名規則與變數相同。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
函式的組成
● 引數與引數型別:main ( ) 或其他函式可以傳遞資料 (可為變數、運算式或常數) 到被呼叫函式裡作運算處理 , 這些資料我們稱之為引數 , 其型別稱為引數型別。
● 函式主體:就是函式的內容程式碼 , 函式就如同一般程式一樣 , 可以進行各種運算、輸出入等。語法也與一般程式相同。
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
請以ANSI C語言之副程式的方式編寫單位換算程式
溫度 : Fahrenheit (F) Celsius (C)©Kelvin (K)Rankine (R)
F=3295C
K=273.15C
R=F459.69
長度 : Meter (M) Centimeter (CM)©Millimeter (MM)
M=10xCM
CM=10xMM
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Array
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
One Dimensional Array
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
One Dimensional Array
Number[0] Number[1] Number[2] ......
int Number[10]={1,2,3,4,5,6,7,8,9,10};Number[0]=1Number[1]=2Number[2]=3Number[3]=4Number[4]=5Number[5]=6Number[6]=7Number[7]=8Number[8]=9Number[9]=10
陣列簡單來說乃針對所有資料給予個別特定的儲存空間或位址,通常自零位址[0]開始計算.
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
問題﹕試用ANSI C語言編寫0到 9橫列數字。
提示﹕可用for迴圈。
0 1 2 3 4 5 6 7 8 9
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
#include
int main(i){ for(i=0;i
-
課堂練習
問題﹕試用ANSI C語言以陣列格式編寫0到 9橫列數字。
提示﹕可用for迴圈。
0 1 2 3 4 5 6 7 8 9
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
#includeint main(i){ int x[10]; for(i=0;i
-
Two Dimensional Array
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
::::
…….Number[3][3]Number[3][2]Number[3][1]Number[3][0]
…….Number[2][3]Number[2][2]Number[2][1]Number[2][0]
…….Number[1][3]Number[1][2]Number[1][1]Number[1][0]
…….Number[0][3]Number[0][2]Number[0][1]Number[0][0]
::::
…….(3,0)(3,0)(3,0)(3,0)
…….(2,0)(2,0)(2,0)(2,0)
…….(1,3)(1,2)(1,1)(1,0)
…….(0,3)(0,2)(0,1)(0,0)
Two dimensional Matrix - array
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
Two dimensional Matrix - array
Number[ 列 ][ 行 ]Number[Row][Column]
(1,2)(1,1)(1,0)
(0,2)(0,1)(0,0)列 (Row)
行 (Column)
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
問題﹕試用ANSI C語言以陣列格式編寫0到 9的二維表格。
提示﹕可用for迴圈。
::::
…….(3,0)(3,0)(3,0)(3,0)
…….(2,0)(2,0)(2,0)(2,0)
…….(1,3)(1,2)(1,1)(1,0)
…….(0,3)(0,2)(0,1)(0,0)
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
問題﹕試用ANSI C語言以陣列格式編寫0到 9的二維表格。
提示﹕可用for迴圈。
0 1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9 102 3 4 5 6 7 8 9 10 113 4 5 6 7 8 9 10 11 124 5 6 7 8 9 10 11 12 135 6 7 8 9 10 11 12 13 146 7 8 9 10 11 12 13 14 157 8 9 10 11 12 13 14 15 168 9 10 11 12 13 14 15 16 179 10 11 12 13 14 15 16 17 18
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
課堂練習
問題﹕試用ANSI C語言編寫0到 9的二維表格。
提示﹕可用for迴圈。
::::
…….(3,0)(3,0)(3,0)(3,0)
…….(2,0)(2,0)(2,0)(2,0)
…….(1,3)(1,2)(1,1)(1,0)
…….(0,3)(0,2)(0,1)(0,0)
邏輯、迴圈、陣列(Logic, Loop, and Matrix)
Chen-Ching Ting (丁振卿) Mechanical Engineering, National Taipei University of Technology (國立台北科技大學機械系) Homepage: http://cct.me.ntut.edu.tw/ E-mail: [email protected] CCT Group ()電腦程式語言ANSI C April 13, 2010 69 / 69
-
解答範例
邏輯、迴圈