Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機概論實習課- 程式設計使用C++ 教 授:童曉儒 教授 助 教:吳政鴻.

Similar presentations


Presentation on theme: "計算機概論實習課- 程式設計使用C++ 教 授:童曉儒 教授 助 教:吳政鴻."— Presentation transcript:

1 計算機概論實習課- 程式設計使用C++ 教 授:童曉儒 教授 助 教:吳政鴻

2 課程大綱 基礎程式設計 結構化程式設計 物件導向程式設計 資料結構程式設計

3 基礎程式設計 環境設定 C++程式概論 數學運算 條件選擇 重複迴圈

4 第一單元:環境設定 安裝Visual Studio完成 啟動Visual C++ 建立專案
開始/程式集/Microsoft Visual Studio 6.0/Microsoft Visual C++ 6.0 建立專案 File/New

5 選擇Win32 Console Application
Project name隨便輸入

6 選擇An empty project

7 顯示Project屬性

8 出現自己的 Project Class

9 建立編輯資源檔案 File/New 選擇C++ Source File File 隨便輸入

10

11 環境設定完成 程式編輯區

12 第二單元:C++程式概論 C++程式結構 常數與變數 C++ 資料型態

13 C++程式結構 範例程式 載入標頭檔 //前置處理區 #include <iostream.h> //常數宣告區
const int num1 =12345; //宣告main函數 int main() { //變數宣告區 unsigned short num2 =54321; //程式敘述區 cout << "有號整數:" << "\n" << num1 << "\n"; cout << "無號短整數:" << "\n" << num2 << "\n"; return 0; } Cons t與 #define 主程式撰寫區 程式起始點

14 常數與變數 增加 math.h 這個標頭檔 由於宣告為float,所以變數值後面要加一個f,否則C++編譯器會把變數當成double存
//儲存檔名:d:\C++02\C0208.cpp #include <iostream.h> #include <math.h> const float PI = f; //宣告浮點常數符號PI void main(void) { float area, circu; //宣告浮點數area,circu float radius = 5; //宣告浮點數radius=5 area = PI * float(pow(radius, 2)); //area=圓面積1 circu = 2 * PI * radius; //circu=圓周長1 cout << "\n圓面積1 = " << area << "\t圓周長1 = " << circu; radius = 10; //改變radius=10 area = PI * float(pow(radius, 2)); //area=圓面積2 circu = 2 * PI * radius; //circu=圓周長2 cout << "\n圓面積2 = " << area << "\t圓周長2 = " << circu << "\n"; } 由於宣告為float,所以變數值後面要加一個f,否則C++編譯器會把變數當成double存 變數宣告並同時給予初始值 回傳值為void,所以不需要return

15 使用 sizeof 這個函數,回傳資料型態所佔的記憶體空間
C++ 資料型態 //儲存檔名:d:\C++02\C0214.cpp #include <iostream.h> void main(void) { double dType; cout << "int 型態的位元組數 = " << sizeof(int) << "bytes\n"; //取得int型態大小 cout << "short 型態的位元組數 = " << sizeof(short) << "bytes\n"; //取得short型態大小 cout << "bool 型態的位元組數 = " << sizeof(bool) << "bytes\n"; //取得bool型態大小 cout << "變數dType的位元組數 = " << sizeof(dType) << "bytes"; //取得變數dType的大小 } 使用 sizeof 這個函數,回傳資料型態所佔的記憶體空間

16 小作業(1) 寫一個程式顯示庫存清單如下

17 小作業(2) 寫一個程式查出C++的所有資料型態所使用的儲存空間大小,利用sizeof()函數

18 第三單元:數學運算 鍵盤輸入 算術運算 數值函數

19 鍵盤輸入 //儲存檔名:d:\C++03\C0301.cpp #include <iostream.h>
void main(void) { char key; //宣告字元變數key cout << "請按任意鍵:"; //輸出訊息字串 cin >> key; //取得鍵盤輸入 cout << “輸入按鍵是:” << key << “\n”; //顯示訊息輸入字元 } 將輸入字元傳給變數

20 算術運算 //儲存檔名:d:\C++03\C0317.cpp #include <iostream.h>
void main(void) { float a, b, c, d, x = 6; // 宣告變數a,b,c,d,x int f = 20; // 宣告變數 f = 20 a = b = c = d = float(f); // 令 a=b=c=d= 20 cout << "a = b = c = d = f = 20, x = 6" // 輸出字串 << "\n a += x => a = " << (a += x) // 輸出跳行、字串、a 值 << "\n b -= x => b = " << (b -= x) // 輸出跳行、字串、b 值 << "\n c *= x => c = " << (c *= x) // 輸出跳行、字串、c 值 << "\n d /= x => d = " << (d /= x) // 輸出跳行、字串、d 值 << "\n f %= x => f = " << (f %= 6) // 輸出跳行、字串、f 值 << endl; // 輸出跳行 } 將變數 f 轉換資料格式為float 做C++的算術運算

21 數值函數 //儲存檔名:d:\C++03\C0319.cpp #include <iostream.h> //插入字串標題檔
#include <stdlib.h> //插入亂數函數標題檔 void main(void) { unsigned seed; //unsigned int seed; cout << "請輸入種子數:"; cin >> seed; //輸入種子數 srand(seed); //設定亂數種子數 cout << rand() << endl; //輸出亂數 }

22 隨著時間的變化,亂數函數產生的數值也會都不一樣
//儲存檔名:d:\C++03\C0320.cpp #include <iostream.h> //插入字串標題檔 #include <stdlib.h> //插入亂數函數標題檔 #include <time.h> //插入時間函數標題檔 void main(void) { srand(time(NULL)); //以時間函數為種子數 cout << rand() << endl; //輸出亂數 } 增加 time.h 這個標頭檔 隨著時間的變化,亂數函數產生的數值也會都不一樣

23 //儲存檔名:d:\C++03\C0322.cpp #include <iostream.h> #include <iomanip.h> #include <math.h> //數值函數標題檔 void main(void) { float degree = ( f) / 180; //degree=徑/度 int d; //宣告整數變數 cout << "請輸入整數:"; cin >> d; //取得鍵盤輸入 cout << setprecision(3) << setiosflags(ios::fixed); //設定有效位數 cout << "d = " << d << endl; //輸出角度 cout << "sin("<< d << ") = " << sin(degree*d) << endl; //輸出正弦函數值 cout << "cos("<< d << ") = " << cos(degree*d) << endl; //輸出餘弦函數值 cout << "tan("<< d << ") = " << tan(degree*d) << endl; //輸出正切函數值 }

24 //儲存檔名:d:\C++03\C0323.cpp #include <iostream.h> #include <iomanip.h> #include <math.h> //數值函數標題檔 void main(void) { int x; //宣告整數變數 cout << "請輸入整數:"; cin >> x; //取得鍵盤輸入 cout << setprecision(3) << setiosflags(ios::fixed); //設定有效位數 cout << "x = " << x; //輸出數值 cout << "\nlog("<< x << ") = " << log(x); //輸出自然對數值 cout << "\nlog10("<< x << ") = " << log10(x); //輸出基底10對數值 cout << "\nexp("<< x << ") = " << exp(x); //輸出 e 的x次方值 cout << endl; }

25 小作業(1) 寫一長度換算C++程式,輸入英吋(in)值並轉換成公分(cm)輸出
(1 cm = in 或 1 in = 2.54 cm)

26 小作業(2) 寫一C++程式,計算一個人身體攝取指數(Body Mess Index ; BMI),由鍵盤輸入一個人的體重(磅)與身高(英尺),計算並輸出這個人的BMI值 (BMI=體重/(身高)^2)

27 第四單元:條件選擇 程式基本結構 條件選擇

28 程式基本結構 循序式結構 條件式結構 重覆式結構 Goto結構

29 循序式結構 //循序式結構 #include <iostream.h> void main(void) {
int var = 5; cout << "var初始值=" << var << "\n"; }

30 條件式結構 //條件式結構 #include <iostream.h> void main(void) {
char inkey; cout << "輸入 Y 鍵,再按 Enter!" << "\n"; cin >> inkey; if(inkey=='Y‘ || inkey=='y') cout << "輸入正確!!" << "\n"; else cout << "你耍我!!" << "\n"; }

31 重覆式結構 //重複式結構 #include <iostream.h> //插入字串標題檔 void main(void) {
int count = 1, sum = 0; do{ sum += count; count++; }while(count<=10); cout << "sum=" << sum << "\n"; }

32 Goto結構 禁止使用><所以不教

33 條件選擇 關係運算符號 邏輯運算符號 If-else if-else敘述 巢狀if敘述 Switch-case-default敘述

34 關係運算符號 //儲存檔名:d:\C++04\C0401.cpp #include <iostream.h>
void main(void) { int a = 1, b = 4, c = 4; //宣告並啟始 a, b, c 值 cout << "a=" << a << endl; //輸出字串、a 值、跳行 cout << "b=" << b << endl; //輸出字串、b 值、跳行 cout << "c=" << c << endl; //輸出字串、c 值、跳行 bool x = a<b, y = c>=a; cout << "a<b 為 " << x << endl; //輸出字串、a<b 值 cout << "c>=a 為 " << y << endl; //輸出字串、c>=a 值 cout << "a!=b 為 " << (a!=b) << endl; //輸出字串、a!=b 值 cout << "a==c 為 " << (a==c) << endl; //輸出字串、a==c 值 }

35 邏輯運算符號 //儲存檔名:d:\C++04\C0402.cpp #include <iostream.h>
void main(void) { int a = 1, b = 4, c = 4; //宣告並啟始 a, b, c 值 bool x = !(a < b && b == c || c <=a); //宣告並指定 x 值 cout << "a=" << a << endl; //輸出字串、a 值、跳行 cout << "b=" << b << endl; //輸出字串、b 值、跳行 cout << "c=" << c << endl; //輸出字串、c 值、跳行 cout << "!(a<b && b==c || c<=a) = " << x; //輸出字串、x 值 }

36 If-else if-else敘述 //儲存檔名:d:\C++04\C0408.cpp
#include <iostream.h> void main(void) { char letter; int cin_value; cout << “請鍵入Ascii code數字,再按 Enter:"; cin >> cin_value; //輸入Ascii code letter=cin_value; //Ascii 轉換成 char if (letter >= 'A' && letter <= 'Z') //若'A'<=letter<='Y'則 cout << letter << " 為大寫鍵\n"; // 輸出字串並結束 if else if (letter >= 'a' && letter <= 'z') //若'a'<=letter<='y'則 cout << letter << " 為小寫鍵\n"; // 輸出字串並結束 if else if (letter >= '0' && letter <= '9') //若'0'<=letter<='9'則 cout << letter << " 為數字鍵\n"; // 輸出字串並結束 if else //以上皆非則 cout << letter << " 為符號鍵\n"; // 輸出字串並結束 if } 利用Ascii code轉換成char 利用Ascii code作判斷

37 巢狀if敘述 //儲存檔名:d:\C++04\C0411.cpp #include <iostream.h>
void main(void) { int year; cout << "請輸入西元年份:"; cin >> year; if (year % 4 != 0) //若year不是4的倍數 cout << year << "不是閏年\n"; //則顯示year不是閏年 else if (year % 100 == 0) //若year是100的倍數 if (year % 400 == 0) //且year不是400的倍數 cout << year << "是閏年\n"; //則顯示year是閏年 else //否 cout << year << "不是閏年\n"; //則顯示year不是閏年 } else //否 cout << year << "是閏年\n"; /則顯示year是閏年

38 Switch-case-default敘述
//儲存檔名:d:\C++04\C0413.cpp #include <iostream.h> void main(void) { char inChar; cout << "a. 新增資料\tb. 插入資料\tc. 刪除資料\t其他. 結束程式:"; cin >> inChar; //inChar=輸入字元 switch (inChar) case 'A': //若inChar為A字元 case 'a': //或inChar為a字元 cout << "新增資料\n"; break; case 'B': //若inChar為B字元 case 'b': //或inChar為b字元 cout << "插入資料\n"; case 'C': //若inChar為C字元 case 'c': //或inChar為c字元 cout << "刪除資料\n"; default: //inChar為其他字元 cout << "結束程式\n"; }

39 小作業(1) 寫一C++程式,由鍵盤輸入一整數,然後判斷並顯示該整數是否為5的倍數

40 小作業(2) 寫一C++程式,由鍵盤輸入每月用水量,然後計算並顯示水費,其中每月基本費為17元,而用水量與水費對應如下表 實用度數
1-10度 11-30度 31-50度 50度以上 每度單價 7.00元 9.00元 11.00元 11.50元

41 第五單元:重複迴圈 增減運算符號 迴圈敘述 巢狀迴圈 無窮迴圈 中斷與連續

42 增減運算符號 增量運算符號++ 減量運算符號—

43 增量運算符號++ //儲存檔名:d:\C++05\C0501.cpp #include <iostream.h>
void main(void) { it c = 0; //宣告變數 cout << ++c << endl; //c=0+1=1後輸出c值 cout << ++c << endl; //c=1+1=2後輸出c值 cout << ++c << endl; //c=2+1=3後輸出c值 cout << "=====================\n"; c = 0; cout << c++ << endl; //輸出c=0後c=0+1=1 cout << c++ << endl; //輸出c=1後c=0+1=2 cout << c++ << endl; //輸出c=2後c=0+1=3 }

44 減量運算符號— //儲存檔名:d:\C++05\C0503.cpp #include <iostream.h>
void main(void) { int c = 3; //宣告變數 cout << --c << endl; //c=3-1=2後輸出c值 cout << --c << endl; //c=2-1=1後輸出c值 cout << --c << endl; //c=1-1=0後輸出c值 cout << "====================\n"; c = 3; cout << c-- << endl; //輸出c=3後c=3-1=2 cout << c-- << endl; //輸出c=2後c=2-1=1 cout << c-- << endl; //輸出c=1後c=1-1=0 }

45 迴圈敘述 前測迴圈 while 後測迴圈 do-while For迴圈

46 前測迴圈 while //儲存檔名:d:\C++05\C0509.cpp #include <iostream.h>
#include <iomanip.h> #include <math.h> void main(void) { int count = 0; //while迴圈初值 double power; //宣告power變數 cout << "計數\t" << "2的n次方\n"; //輸出字串 while (count++ < 10) //判斷count<10後再加1 power = pow(2, count); //計算2的n次方 cout << setw(3) << count << '\t'; //輸出計數值 cout << setw(6) << power << endl; //輸出2的n次方值 }

47 後測迴圈 do-while //儲存檔名:d:\C++05\C0514.cpp #include <iostream.h>
#include <iomanip.h> void main(void) { int count = 1, factor = 1; //while 迴圈初值 cout << "計數\t" << setw(8) << "階乘\n"; //輸出字串 do //do 迴圈 factor *= count; //計算階乘 cout << setw(3) << count << '\t'; //輸出計數值 cout << setw(7) << factor << endl; //輸出階乘 } while (++count <= 10); //count<=10迴圈繼續 }

48 For迴圈 //儲存檔名:d:\C++05\C0515.cpp #include <iostream.h>
void main(void) { for(int i=1; i<=9; i++) //設定for初值,條件,更新 cout << i << ' '; //輸出控制變數i值 } cout << "\n";

49 巢狀迴圈 //儲存檔名:d:\C++05\C0520.cpp #include <iostream.h>
#include <iomanip.h> void main(void) { int multiplier, faciend; for (faciend=1; faciend<=9; faciend++) //定義被乘數迴圈由1到9 for (multiplier=2; multiplier<=9; multiplier++) //乘數由2數到9 cout << multiplier << '*'; //輸出multiplier * cout << faciend << '='; //輸出faciend = cout << setw(2); //設定輸出二位數 cout << multiplier*faciend << '\t'; //輸出運算值後跳下一定位 } cout << endl; //輸出跳行

50 無窮迴圈 //儲存檔名:d:\C++05\C0521.cpp #include <iostream.h>
#include <iomanip.h> void main(void) { cout.setf(ios::fixed | ios::right); //設定輸出向右對齊 cout.fill('0'); //若左邊空白則補0 while (true) //無窮迴圈 for (int hrs=0; hrs<24; hrs++) //時: 從 0 數到 23 for (int min=0; min<60; min++) //分: 從 0 數到 59 for (int sec=0; sec<60; sec++) //秒: 從 0 數到 59 cout << setw(2) << hrs << ':'; //輸出時數 cout << setw(2) << min << ':'; //輸出分鐘數 cout << setw(2) << sec << '\r'; //輸出秒數 for(int count=0;count<100000;count++) {} }

51 中斷與連續 中斷 break 連續 continue

52 中斷 break //儲存檔名:d:\C++05\C0523.cpp #include <iostream.h>
void main(void) { cout << "1 至 30 間的質數有: "; for(int i=2; i<=30; i++) //搜尋 2 至 30 for(int j=2; j<i; j++) //除數從 2 至 i if(i%j==0) //若整除則非質數 break; //中斷內迴圈 else if(j==i-1) //確定為質數 cout << i << " "; }

53 連續 continue //儲存檔名:d:\C++05\C0526.cpp #include <iostream.h>
void main(void) { int count; cout << "1 至 30 間 3 的倍數為:"; for (count = 1; count <= 30; count++) //定義迴圈 if (count % 3 != 0) //若不是3的倍數 continue; //返回迴圈起點 cout << count << '\0'; //顯示3的倍數並空格 } cout << endl; //輸出跳行

54 小作業(1) 寫一C++程式,計算1+2+3…+100的總和

55 小作業(2) 寫一C++程式,以亂數丟10000次銅板然後計算並列出正面與反面的次數

56 自由練習時間


Download ppt "計算機概論實習課- 程式設計使用C++ 教 授:童曉儒 教授 助 教:吳政鴻."

Similar presentations


Ads by Google