Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3

Similar presentations


Presentation on theme: "2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3"— Presentation transcript:

1 2 C++ 程式概論 2.1 C++ 程式結構 2-2 2.1.1 程式註解 // 2-3 2.1.2 插入標題檔 #include 2-3
程式註解 // 2-3 插入標題檔 #include 2-3 main() 函數 2-7 輸出函數cout 2-8 2.2 常數與變數 2-12 宣告變數 2-12 指定資料 = 2-13 宣告常數const 2-15 宣告符號 #define 2-17 C++ 保留字 2-18 2.3 C++ 資料型態 2-19 整數資料int 2-19 字元資料char 2-21 浮點資料float, double 2-23 2.3.4 邏輯資料bool 取得型態大小sizeof 2-26

2 2.1 C++ 程式結構

3 2.1.1 程式註解 // // 註解 /* 註解 */ 範例 //儲存檔名:d:\C++02\C0201.cpp
程式註解 // // 註解 /* 註解 */ 範例 //儲存檔名:d:\C++02\C0201.cpp /* 宣告整數變數練習 */

4 2.1.2 插入標題檔 #include #include <標題檔名> // 第一式
範例 #include <iostream.h> //插入iostream.h #include "user.h" //插入使用者標題檔

5 2.1.2 插入標題檔 #include (續) 插入舊型標題檔
#include <iostream.h> //插入iostream.h標題檔 #include <string.h> //插入string.h標題檔 插入新型標題檔 #include <iostream> //插入iostream標題檔 using namespace std //宣告程式使用新型標題檔

6 2.1.3 main() 函數 傳回型態 main(參數) { . return 傳回值; } 不傳回任何值給系統
void main(void) void main() 傳回整數值給系統 int main(void) int main()

7 2.1.3 main() 函數 (續) 範例二 void main(void) { //main函數起始點 //敘述區
//不須要return敘述 } //main函數結束點 int main() return 0; //傳回整數0給作業系統

8 輸出函數cout cout << 變數或字串1 << 變數或字串2 << << 變數或字串n; endl( end of line) 為結束輸出行,下一次輸出將跳至下一行起頭(俗稱跳行) 範例 cout << num1; //顯示變數num1的值 cout << "ANSI/ISO C++"; //顯示字串ANSI/ISO C++ cout << "有號整數:" << num1 << endl; //顯示字串、數值、跳行

9 cout 程式範例 //檔案名稱: C0201.cpp #include <iostream.h> void main() {
}

10 2.1.4 輸出函數cout (續) 字元值 字元格式 字元功能 \0 空格(null space) 7 \a 響鈴(bell ring)
\0 空格(null space) 7 \a 響鈴(bell ring) 8 \b 倒退(backspace) 9 \t 移到下一定位點(tab) 10 \n 插入新行(newline) 12 \f 跳至下一頁起點(form feed) 13 \r 跳至同一行起點(carriage return) 34 \” 插入雙引號(double quote) 39 \’ 插入單引號(single quote) 92 \\ 插入反斜線(back slash)

11 Exercise 01 Write a program to print the following: ****** ****** ****** ****** ****** ****** ****** 程式檔案名稱請使用「學號_01」,檔案上載至ftp:// 上的Ex01目錄

12 2.2 常數與變數 變數(variable)代表電腦記憶體中的一個儲存位置。 常數(constant)在程式執行中是不可改變的資料項目。

13 宣告變數 資料型態 變數名稱1, 變數名稱2, …; int intVar; //宣告整數型態的變數intVar

14 2.2.2 指定資料 = 資料型態 變數名稱1, 變數名稱2, …; 變數名稱1 = 初值1; 變數名稱2 = 初值2; …; 範例
指定資料 = 資料型態 變數名稱1, 變數名稱2, …; 變數名稱1 = 初值1; 變數名稱2 = 初值2; …; 範例 short shortVar; //宣告短整數變數shortVar shortVar = 5; //shortVar的初值等於5 . shortVar = 10; //改變shortVar的值為10

15 指定資料 = (續) 資料型態 變數名稱1=初值, 變數名稱2=初值, …; short shortVar = 5;

16 程式2-07:宣告變數、起始與更改 //檔名:d:\C++02\C0207.cpp #include <iostream.h>
void main() { int Var = 5; //宣告Var = 5 (起始值) cout << "Var起始值 = " << Var; //顯示訊息字串與Var值 Var = 10; //改變Var = 10 (變更值) cout << "\nVar變更值 = " << Var; //顯示訊息字串與Var值 }

17 執行結果 Var起始值 = 5 Var變更值 = 10

18 2.2.3 宣告常數const const 資料型態 常數符號1=數值1, 常數符號2=數值2, …; 範例
const float fPI = f; //宣告浮點常數符號fPI const double dPI = ; //宣告倍精常數符號dPI

19 程式2-08:宣告常數練習 //檔名:d:\C++02\C0208.cpp #include <iostream.h>
void main() { const float PI = f; float radius, area, circu; radius = 5; area = PI * radius * radius; circu = 2 * PI * radius; cout << "圓面積1 = " << area << "\t圓周長1 = " << circu; radius = 10; cout << "\n圓面積2 = " << area << "\t圓周長2 = " << circu << endl; }

20 執行結果

21 宣告符號 #define #define 對等符號 對等資料

22 範例 #define PI void main(void) { float circumference, radius = 10; circumference = 2 * PI * radius; }

23 程式2-09:宣告常數練習 //檔名:d:\C++02\C0209.cpp #include <iostream.h>
#define PI f void main() { float radius, area, circu; radius = 5; area = PI * radius * radius; circu = 2 * PI * radius; cout << "圓面積1 = " << area << "\t圓周長1 = " << circu; radius = 10; cout << "\n圓面積2 = " << area << "\t圓周長2 = " << circu << endl; }

24 執行結果

25 2.2.5 C++ 保留字 asm do inline short typeid auto double int signed
typename break else long sizeof union bool enum mutable static unsigned case explicit namespace struct using catch extern new switch virtual char false operator template void class float private this volatile const for proteted throw while continue friend public true default goto register try delete if return typedef

26 Ex 02 寫一個程式計算小孩子可以平均分配到的水果數量
定義一個常數child為5 定義變數apple 計算並顯示出當蘋果數量為10或25時,小孩可以分配到的蘋果數量 程式檔案名稱請使用「學號_02」,檔案上載至ftp:// 上的Ex02目錄

27 2.3 C++ 資料型態 C++ 的內建資料型態(build-in data type)包括整數型態、字元型態、浮點數型態、與邏輯型態等。
整數又分為短整數、整數、與長整數等型態。 浮點數又分為單精度、倍精度、與長倍精度等型態。

28 2.3.1 整數資料int 宣告型態 宣告功能 數值範圍 short 短整數 -32,768至+32,767 unsigned short
無號短整數 0至65,535 signed short 有號短整數 int 整數 -2,147,483,648至 unsigned int 無號整數 0至4,294,967,295 signed int 有號整數 long 長整數 unsigned long 無號長整數 signed long 有號長整數

29 程式2-10:宣告整數變數練習 //檔名:d:\C++02\C0210.cpp #include <iostream.h>
void main() { signed int num1 = ; unsigned short num2 = 65432; long num3 = ; cout << "有號整數:" << num1 << endl; cout << "無號短整數:" << num2 << endl; cout << "長整數:" << num3 << endl; }

30 執行結果

31 2.3.2 字元資料char 宣告型態 宣告功能 範例 char 宣告字元 char letter = ‘C’; char[n] 宣告字串
char str1[3] = {‘C’, ‘+’, ‘+’}; char str2[4] = “C++”; char str3[] = “C++ 學習講堂”;

32 字元與字串的記憶體空間 char str1[1] = ‘C’ char str2[2] = “C” C C \0

33 字元與字串的記憶體實際儲存資料 char str1[1] = ‘C’ char str2[2] = “C” C 67 C \0 67

34 宣告字元變數說明 char letter0; char letter1 = ‘C’; char letter2 = 67; char letter3 = 0x43; char tab = ‘\t’; char string[] = “ANSI C++”;

35 2.3.3 浮點資料float, double 宣告型態 宣告功能 範例 float 單精度浮點數
±3.4*10-38至±3.4*10+38 double 倍精度浮點數 ±1.7*10-308至±1.7*10+308 long double 長倍精度浮點數 ±1.7* 至±1.7*

36 宣告浮點變數說明 float num0; float pi1 = 3.14159f; float value2 = 4.5e+16f;
double pi3 = ; double value4 = 4.5e+101;

37 2.3.4 邏輯資料bool bool fontBold; bool fontItalic = true; 宣告型態 宣告功能 數值範圍
邏輯變數 true(1)或false(0) bool fontBold; bool fontItalic = true;

38 程式2-13:宣告邏輯變數練習 //檔名:d:\C++02\C0213.cpp #include <iostream.h>
void main() { bool bValue = true; cout << "邏輯預設值:" << bValue << endl; bValue = false; cout << "邏輯更改值:" << bValue << endl; }

39 執行結果

40 2.3.5 取得型態大小sizeof sizeof(資料型態|變數名稱) 範例 double dType;
cout << sizeof(int); //取得int型態大小 cout << sizeof(short); //取得short型態大小 cout << sizeof(bool); //取得bool型態大小 cout << sizeof(dType); //取得dType變數大小

41 Ex03 寫一程式取得下列變數的大小,並顯示於螢幕。 char bookName[] = "C++學習講堂";
程式檔案名稱請使用「學號_03」,檔案上載至ftp:// 上的Ex03目錄

42 Homework 01 寫一個程式計算英吋與公分轉換 下次上課時繳交紙本,應包含 定義一個常數可表示『英吋與公分比值』
定義變數表示英吋及公分的值 計算並顯示當10、58.3公分時 等於多少英吋 下次上課時繳交紙本,應包含 個人資料 題目 程式 執行結果 心得

43 C++ 保留字 asm do inline short typeid auto double int signed typename
break else long sizeof union bool enum mutable static unsigned case explicit namespace struct using catch extern new switch virtual char false operator template void class float private this volatile const for proteted throw while continue friend public true default goto register try delete if return typedef


Download ppt "2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3"

Similar presentations


Ads by Google