2 c++ 程式概論 2.1c++ 程式結構 2-2 2.1c++ 程式結構 2-2 2.1.1 程式註解 //2-3 2.1.1...

43
2 C++ 2 C++ 程程程程 程程程程 2.1 2.1 C++ C++ 程程程程 程程程程 2-2 2-2 2.1.1 2.1.1 程程程程 程程程程 // // 2-3 2-3 2.1.2 2.1.2 程程程程程 程程程程程 #includ #includ e e 2-3 2-3 2.1.3 main() 2.1.3 main() 程程 程程 2-7 2-7 2.1.4 2.1.4 程程程程 程程程程 cout 2-8 cout 2-8 2.2 2.2 程程程程程 程程程程程 2-12 2-12 2.2.1 2.2.1 程程程程 程程程程 2-12 2-12 2.2.2 2.2.2 程程程程 程程程程 = = 2-13 2-13 2.2.3 2.2.3 程程程程 程程程程 const 2- const 2- 15 15 2.2.4 2.2.4 程程程程 程程程程 #define #define 2-17 2-17 2.2.5 C++ 2.2.5 C++ 程程程 程程程 2-18 2-18 2.3 2.3 C++ C++ 程程程程 程程程程 2-19 2-19 2.3.1 2.3.1 程程程程 程程程程 int int 2-19 2-19 2.3.2 2.3.2 程程程程 程程程程 char 2-2 char 2-2 1 1 2.3.3 2.3.3 程程程程 程程程程 float, do float, do uble uble 2-23 2-23 2.3.4 2.3.4 程程程程 程程程程 bool bool 2-25 2-25 2.3.5 2.3.5 程程程程程程 程程程程程程 sizeof sizeof 2-26 2-26

Post on 21-Dec-2015

230 views

Category:

Documents


4 download

TRANSCRIPT

2 C++ 2 C++ 程式概論程式概論 2.12.1 C++ C++ 程式結構 程式結構 2-22-2 2.1.1 2.1.1 程式註解 程式註解 //// 2-32-3 2.1.2 2.1.2 插入標題檔 插入標題檔 #incl#incl

udeude 2-32-3 2.1.3 main() 2.1.3 main() 函數函數 2-72-7 2.1.4 2.1.4 輸出函數輸出函數 cout 2-8cout 2-8 2.22.2 常數與變數常數與變數 2-122-12 2.2.1 2.2.1 宣告變數宣告變數 2-122-12 2.2.2 2.2.2 指定資料 指定資料 == 2-132-13 2.2.3 2.2.3 宣告常數宣告常數 const 2-const 2-

1515

2.2.4 2.2.4 宣告符號 宣告符號 #define#define2-172-17

2.2.5 C++ 2.2.5 C++ 保留字保留字 2-182-18 2.32.3 C++ C++ 資料型態 資料型態 2-192-19 2.3.1 2.3.1 整數資料整數資料 intint 2-192-19 2.3.2 2.3.2 字元資料字元資料 char 2-21char 2-21 2.3.3 2.3.3 浮點資料浮點資料 float, doufloat, dou

bleble 2-232-23 2.3.4 2.3.4 邏輯資料邏輯資料 boolbool

2-25 2-25 2.3.5 2.3.5 取得型態大小取得型態大小 sizeosizeo

ff 2-262-26

2.1 C++ 2.1 C++ 程式結構程式結構

程式註解區 // 儲存檔名:d:\C++02\C0209.cpp

#include <iostream.h>

const int num1 = 12345;

void main(void){

unsigned short num2 = 65432long num3 = 1234567890

cout << "有號整數:" << num1 << endl<< "無號短整數:" << num2 << endl<< "長整數:" << num3 << endl<< endl;

}

前置處理區

常數宣告區

變數宣告區

程式敘述區

宣告 main 函數

2.1.1 2.1.1 程式註解 程式註解 //// // // 註解註解 /* /* 註解 註解 */*/

範例範例//// 儲存檔名:儲存檔名: d:\C++02\C0201.cppd:\C++02\C0201.cpp/* /* 宣告整數變數練習 宣告整數變數練習 */*/

2.1.2 2.1.2 插入標題檔 插入標題檔 #include#include #include <#include < 標題檔名標題檔名 >> // // 第一式第一式 #include “#include “ 標題檔名”標題檔名” // // 第二式第二式

範例範例 #include <iostream.h>#include <iostream.h> // // 插入插入 ioio

stream.hstream.h #include "user.h"#include "user.h" // // 插入使用者標題插入使用者標題

檔檔

2.1.2 2.1.2 插入標題檔 插入標題檔 #include #include (( 續續 ))

插入舊型標題檔插入舊型標題檔#include <iostream.h>#include <iostream.h> //// 插入插入 iostream.hiostream.h標題檔標題檔#include <string.h>#include <string.h> //// 插入插入 string.hstring.h 標題檔標題檔

插入新型標題檔插入新型標題檔#include <iostream>#include <iostream> //// 插入插入 iostreamiostream 標題檔標題檔using namespace stdusing namespace std //// 宣告程式使用新型標題宣告程式使用新型標題檔檔

2.1.3 main() 2.1.3 main() 函數函數 傳回型態 傳回型態 main(main( 參參

數數 )){{..

return return 傳回值傳回值 ;;

}}

不傳回任何值給系統不傳回任何值給系統void main(void) void main(void) void main() void main()

傳回整數值給系統傳回整數值給系統int main(void) int main(void) int main() int main()

2.1.3 main() 2.1.3 main() 函數 函數 (( 續續 )) 範例二範例二

void main(void)void main(void){{ //main//main 函數起始點函數起始點

//// 敘述區敘述區//// 不須要不須要 returnreturn 敘述敘述

}} //main//main 函數結束點函數結束點 範例二範例二

int main()int main(){{ //main//main 函數起始點函數起始點

//// 敘述區敘述區return 0;return 0; //// 傳回整數傳回整數 00 給作業系給作業系

統統}} //main//main 函數結束點函數結束點

2.1.4 2.1.4 輸出函數輸出函數 coutcout coutcout    << << 變數或字串變數或字串 1 << 1 << 變數或字串變數或字串 2 << . . . << 2 << . . . << 變變

數或字串數或字串 n;n; endlendl( end of line) ( end of line) 為結束輸出行,下一次輸出將跳至下為結束輸出行,下一次輸出將跳至下

一行起頭一行起頭 (( 俗稱跳行俗稱跳行 ))

範例範例cout << num1;cout << num1; //// 顯示變數顯示變數 num1num1 的值的值cout << "ANSI/ISO C++";cout << "ANSI/ISO C++";

//// 顯示字串顯示字串 ANSI/ISO C++ANSI/ISO C++cout << "cout << " 有號整數:有號整數: " << num1 << endl;" << num1 << endl;

//// 顯示字串、數值、跳行顯示字串、數值、跳行

cout cout 程式範例程式範例

// 檔案名稱: C0201.cpp

#include <iostream.h>

void main(){

cout << " 紅葉晚蕭蕭,長亭酒一瓢。 ";}

2.1.4 2.1.4 輸出函數輸出函數 cout (cout ( 續續 ))字元值字元值 字元格式字元格式 字元功能字元功能

0 0 \0 \0 空格(空格( null spacenull space ) ) 77 \a\a 響鈴(響鈴( bell ringbell ring ) ) 88 \b\b 倒退(倒退( backspacebackspace ) ) 99 \t\t 移到下一定位點(移到下一定位點( tabtab ) )

1010 \n\n 插入新行(插入新行( newlinenewline ) ) 1212 \f\f 跳至下一頁起點(跳至下一頁起點( form feedform feed ) ) 1313 \r\r 跳至同一行起點(跳至同一行起點( carriage returcarriage retur

nn ) ) 3434 \”\” 插入雙引號(插入雙引號( double quotedouble quote ) ) 3939 \’\’ 插入單引號(插入單引號( single quotesingle quote ) ) 9292 \\\\ 插入反斜線(插入反斜線( back slashback slash ) )

Exercise 01Exercise 01

Write a program to print the following:Write a program to print the following:************************************************************************************

程式檔案名稱請使用「學號程式檔案名稱請使用「學號 _01_01 」,檔案上載至」,檔案上載至ftp://192.192.155.217ftp://192.192.155.217上的上的Ex01Ex01目錄目錄

2.2 2.2 常數與變數常數與變數 變數(變數( variablevariable ))代表電腦記憶體中的一代表電腦記憶體中的一

個儲存位置。個儲存位置。 常數(常數( constantconstant ))在程式執行中是不可改在程式執行中是不可改

變的資料項目。 變的資料項目。

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

0000

0001

0002

0003

.

.

.

FFFF

intVar

2.2.2 2.2.2 指定資料 指定資料 == 資料型態資料型態 變數名稱變數名稱 1, 1, 變數名稱變數名稱 2, …;2, …;

變數名稱變數名稱 1 = 1 = 初值初值 1;1;變數名稱變數名稱 2 = 2 = 初值初值 2;2;…;…;

範例範例short shortVar;short shortVar; //// 宣告短整數變數宣告短整數變數 shortVashortVarrshortVar = 5;shortVar = 5; //shortVar//shortVar 的初值等於的初值等於 55..shortVar = 10;shortVar = 10; //// 改變改變 shortVarshortVar 的值為的值為 1010

2.2.2 2.2.2 指定資料 指定資料 = (= ( 續續 )) 資料型態資料型態 變數名稱變數名稱 1=1= 初值初值 , , 變數名稱變數名稱 2=2= 初值初值 ,,

…; …;short shortVar = 5;short shortVar = 5;

0000

0001

0002

0003

0004

.

.

FFFF

intVar

55 shortVar

程式程式 2-072-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 值

}

執行結果執行結果

Var 起始值 = 5Var 變更值 = 10

2.2.3 2.2.3 宣告常數宣告常數 constconst const const 資料型態資料型態 常數符號常數符號 1=1= 數值數值 1, 1, 常數符號常數符號 2=2= 數數

值值 2, …;2, …;

範例範例const float fPI = 3.14159f;const float fPI = 3.14159f; //// 宣告浮點常數宣告浮點常數符號符號 fPIfPIconst double dPI = 3.141592653;const double dPI = 3.141592653; //// 宣告倍精常數宣告倍精常數符號符號 dPIdPI

程式程式 2-082-08 :宣告常數練習:宣告常數練習// 檔名: d:\C++02\C0208.cpp

#include <iostream.h>

void main(){

const float PI = 3.14159f;float radius, area, circu;

radius = 5;area = PI * radius * radius;circu = 2 * PI * radius;cout << " 圓面積 1 = " << area << "\t 圓周長 1 = " << circu;

radius = 10;area = PI * radius * radius;circu = 2 * PI * radius;cout << "\n 圓面積 2 = " << area << "\t 圓周長 2 = " << circu << endl;

}

執行結果執行結果

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

範例範例#define PI 3.14159#define PI 3.14159void main(void)void main(void){{

float circumference, radius = 10;float circumference, radius = 10;circumference = 2 * PI * radius; circumference = 2 * PI * radius;

}}

程式程式 2-092-09 :宣告常數練習:宣告常數練習// 檔名: d:\C++02\C0209.cpp

#include <iostream.h>#define PI 3.14159f

void main(){

float radius, area, circu;

radius = 5;area = PI * radius * radius;circu = 2 * PI * radius;cout << " 圓面積 1 = " << area << "\t 圓周長 1 = " << circu;

radius = 10;area = PI * radius * radius;circu = 2 * PI * radius;cout << "\n 圓面積 2 = " << area << "\t 圓周長 2 = " << circu << endl;

}

執行結果執行結果

2.2.5 C++ 2.2.5 C++ 保留字保留字asmasm dodo inlineinline shortshort typeidtypeidautoauto doubledouble intint signedsigned typenamtypenam

eebreakbreak elseelse longlong sizeofsizeof unionunionboolbool enumenum mutablemutable staticstatic unsignedunsignedcasecase explicitexplicit namespanamespa

cecestructstruct usingusing

catchcatch externextern newnew switchswitch virtualvirtualcharchar falsefalse operatoroperator templatetemplate voidvoidclassclass floatfloat privateprivate thisthis volatilevolatileconstconst forfor protetedproteted throwthrow whilewhilecontinuecontinue friendfriend publicpublic truetrue

defaultdefault gotogoto registerregister trytry

deletedelete ifif returnreturn typedeftypedef

Ex 02Ex 02

寫一個程式計算小孩子可以平均分配到的寫一個程式計算小孩子可以平均分配到的水果數量水果數量 定義一個常數定義一個常數 childchild 為為 55 定義變數定義變數 appleapple 計算並顯示出當蘋果數量為計算並顯示出當蘋果數量為 1010 或或 2525 時,小孩時,小孩

可以分配到的蘋果數量可以分配到的蘋果數量 程式檔案名稱請使用「學號程式檔案名稱請使用「學號 _02_02 」,檔案」,檔案

上載至上載至 ftp://192.192.155.217ftp://192.192.155.217上的上的Ex02Ex02目目錄錄

2.3 C++ 2.3 C++ 資料型態資料型態 C++ C++ 的的內建資料型態(內建資料型態( build-in data tybuild-in data ty

pepe ))包括整數型態、字元型態、浮點數型包括整數型態、字元型態、浮點數型態、與邏輯型態等。態、與邏輯型態等。

整數又分為短整數、整數、與長整數等型整數又分為短整數、整數、與長整數等型態。態。

浮點數又分為單精度、倍精度、與長倍精浮點數又分為單精度、倍精度、與長倍精度等型態。 度等型態。

2.3.1 2.3.1 整數資料整數資料 intint宣告型態宣告型態 宣告功能宣告功能 數值範圍數值範圍shortshort 短整數短整數 -32,768-32,768 至至 +32,767+32,767unsigned unsigned shortshort

無號短整數無號短整數 00 至至 65,53565,535

signed signed shortshort

有號短整數有號短整數 -32,768-32,768 至至 +32,767+32,767

intint 整數整數 -2,147,483,648-2,147,483,648 至至 +21474836+214748364747

unsigned intunsigned int 無號整數無號整數 00 至至 4,294,967,2954,294,967,295signed intsigned int 有號整數有號整數 -2,147,483,648-2,147,483,648 至至 +21474836+21474836

4747longlong 長整數長整數 -2,147,483,648-2,147,483,648 至至 +21474836+21474836

4747unsigned unsigned longlong

無號長整數無號長整數 00 至至 4,294,967,2954,294,967,295

signed longsigned long 有號長整數有號長整數 -2,147,483,648-2,147,483,648 至至 +21474836+214748364747

程式程式 2-102-10 :宣告整數變數練習:宣告整數變數練習// 檔名: d:\C++02\C0210.cpp

#include <iostream.h>

void main(){

signed int num1 = -12345;unsigned short num2 = 65432;long num3 = 1234567890;

cout << " 有號整數: " << num1 << endl;cout << " 無號短整數: " << num2 << endl;cout << " 長整數: " << num3 << endl;

}

執行結果執行結果

2.3.2 2.3.2 字元資料字元資料 charchar宣告型態宣告型態 宣告功能宣告功能 範例範例charchar 宣告字元宣告字元 char letter = ‘C’;char letter = ‘C’;

char[n]char[n] 宣告字串宣告字串 char str1[3] = {‘C’, ‘+char str1[3] = {‘C’, ‘+’, ‘+’};’, ‘+’};char str2[4] = “C++”;char str2[4] = “C++”;char str3[] = “C++ char str3[] = “C++ 學習講學習講堂”堂” ;;

字元與字串的記憶體空間字元與字串的記憶體空間 char str1[1] = ‘C’char str1[1] = ‘C’

char str2[2] = “C”char str2[2] = “C”

C

C \0

字元與字串的記憶體實際儲存資料字元與字串的記憶體實際儲存資料 char str1[1] = ‘C’char str1[1] = ‘C’

char str2[2] = “C”char str2[2] = “C”

C

C \0

67

67 0

宣告字元變數說明宣告字元變數說明 char letter0;char letter0;

char letter1 = ‘C’;char letter1 = ‘C’;char letter2 = 67;char letter2 = 67;char letter3 = 0x43;char letter3 = 0x43;char tab = ‘\t’;char tab = ‘\t’;char string[] = “ANSI C++”;char string[] = “ANSI C++”;

2.3.3 2.3.3 浮點資料浮點資料 float, doublefloat, double宣告型態宣告型態 宣告功能宣告功能 範例範例floatfloat 單精度浮點數單精度浮點數 ±3.4*10±3.4*10-38-38 至至 ±3.±3.

4*104*10+38+38

doubledouble 倍精度浮點數倍精度浮點數 ±1.7*10±1.7*10-308-308 至至 ±±1.7*101.7*10+308+308

long long doubledouble

長倍精度浮點數長倍精度浮點數 ±1.7*10±1.7*10-4932-4932 至至 ±±1.7*101.7*10+4832+4832

宣告浮點變數說明宣告浮點變數說明 float num0;float num0;

float pi1 = 3.14159f;float pi1 = 3.14159f;float value2 = 4.5e+16f;float value2 = 4.5e+16f;

double pi3 = 3.141592653;double pi3 = 3.141592653;double value4 = 4.5e+101;double value4 = 4.5e+101;

2.3.4 2.3.4 邏輯資料邏輯資料 boolbool宣告型態宣告型態 宣告功能宣告功能 數值範圍數值範圍boolbool 邏輯變數邏輯變數 truetrue (( 11 )或)或 falsefalse

(( 00 ))

bool fontBold;bool fontItalic = true;

程式程式 2-132-13 :宣告邏輯變數練習:宣告邏輯變數練習// 檔名: d:\C++02\C0213.cpp

#include <iostream.h>

void main(){

bool bValue = true;

cout << " 邏輯預設值: " << bValue << endl;bValue = false;cout << " 邏輯更改值: " << bValue << endl;

}

執行結果執行結果

2.3.5 2.3.5 取得型態大小取得型態大小 sizeofsizeof sizeof(sizeof( 資料型態資料型態 || 變數名稱變數名稱 ))

範例範例double dType; double dType; cout << sizeof(int); cout << sizeof(int); //// 取得取得 intint 型態大小 型態大小 cout << sizeof(short); cout << sizeof(short); //// 取得取得 shortshort 型態大小 型態大小 cout << sizeof(bool); cout << sizeof(bool); //// 取得取得 boolbool 型態大小 型態大小 cout << sizeof(dType); cout << sizeof(dType); //// 取得取得 dTypedType 變數大小 變數大小

Ex03Ex03

寫一程式取得下列變數的大小,並顯示於寫一程式取得下列變數的大小,並顯示於螢幕。螢幕。char bookName[] = "C++char bookName[] = "C++ 學習講堂學習講堂 ";";

程式檔案名稱請使用「學號程式檔案名稱請使用「學號 _03_03 」,檔案」,檔案上載至上載至 ftp://192.192.155.217ftp://192.192.155.217上的上的Ex03Ex03目目錄錄

Homework 01Homework 01 寫一個程式計算英吋與公分轉換寫一個程式計算英吋與公分轉換

定義一個常數可表示『英吋與公分比值』定義一個常數可表示『英吋與公分比值』 定義變數表示英吋及公分的值定義變數表示英吋及公分的值 計算並顯示當計算並顯示當 1010 、、 58.358.3 公分時 等於多少英吋公分時 等於多少英吋

下次上課時繳交紙本,應包含下次上課時繳交紙本,應包含 個人資料個人資料 題目題目 程式程式 執行結果執行結果 心得心得

C++ C++ 保留字保留字asmasm dodo inlineinline shortshort typeidtypeidautoauto doubledouble intint signedsigned typenamtypenam

eebreakbreak elseelse longlong sizeofsizeof unionunionboolbool enumenum mutablemutable staticstatic unsignedunsignedcasecase explicitexplicit namespanamespa

cecestructstruct usingusing

catchcatch externextern newnew switchswitch virtualvirtualcharchar falsefalse operatoroperator templatetemplate voidvoidclassclass floatfloat privateprivate thisthis volatilevolatileconstconst forfor protetedproteted throwthrow whilewhilecontinuecontinue friendfriend publicpublic truetrue

defaultdefault gotogoto registerregister trytry

deletedelete ifif returnreturn typedeftypedef