Presentation is loading. Please wait.

Presentation is loading. Please wait.

變數與資料型態 2013.03 綠園.

Similar presentations


Presentation on theme: "變數與資料型態 2013.03 綠園."— Presentation transcript:

1 變數與資料型態  綠園

2 前言… 蓋房子的時候,你需要兩件東西:磚頭和建造的藍圖。 在程式設計的領域裡,你也只需要兩件東西:資料(變數)和指令(程式碼)。
在房子動工之前,得先訂購所需要的建材:磚頭、小磚塊、石板…。 C++必須先宣告變數之後,才能使用變數。你必須對每個「磚頭」(變數)命名,並且告訴C++要用何種類型的「磚頭」。

3 前言… 建築最基本的架構是一個房間,將每個房間組合在一起,就構成一棟建築物。
在C++中,基本的結構是「函式」,結合數個函式就組成了一個程式。 一個建築新手無法立即蓋出摩天大樓,他只會從只有一個房間的屋子開始著手。這節課我們將開始建立一個簡單的程式。

4 Program II : I am …… years old.
#include <iostream> #include <cstdlib> using namespace std; int main() { int myage; myage = 16; cout << "Hello, I am Jenny." << endl; cout << "I am " << myage << " years old. "<< endl; cout << " After 10 years, I will be " << myage+10 << " years old. " << endl; system("pause"); return 0; } myage 16

5 Program III : 5 + 7 = ? #include <iostream>
#include <cstdlib> using namespace std; int main() { int a, b, c; a = 5; b = 7; c = a + b; cout << a << "+" << b << "=" << c << endl; system("pause"); return 0; } a b c 5 7 12 5+7

6 變數的宣告 變數的宣告有三種作用 定義變數名稱 定義變數的型態(整數、實數、字元等) 讓程式員了解變數的功能
int answer; // 運算式的結果

7 Program IV : a + b = ? #include <iostream>
#include <cstdlib> using namespace std; int main() { int a, b, c; cout << "Enter the first value:" << endl; cin >> a; cout << "Enter the second value:" << endl; cin >> b; c = a + b; cout << a << "+" << b << "=" << c << endl; system("PAUSE"); return 0; }

8 Program V : What’s your name?
#include <iostream> #include <cstdlib> #include <string> using namespace std; int main() { string first_name; cout << "Please enter your name "; cout << "(followed by 'enter'):\n"; cin >> first_name; cout << "Hello, " << first_name << "! \n"; system("pause"); return 0; }

9 變數名稱的限制 變數名稱的限制 1、字元可以是英文字母、數字或底線。 2、名稱中不能有空白字元。 3、不能使用底線以外的其它符號。
4、第一個字元不能是數字。 5、有大小寫之分。 6、不能使用到C++的關鍵字。 (7、最好能以有意義的名稱來命名。)

10 識別字 何謂識別字 變數、函數或類別的名稱為識別字 識別字的習慣命名原則
常數:全部字元皆由英文大寫字母及底線組成。 ex:PI, MIN_NUM 變數:英文小寫字母開始,若由數個英文單字組成,則 後面的英文字由大寫起頭,其餘小寫。   ex:radius、myAddressBook 函數:英文小寫字母開始,若由數個英文單字組成,則 後面的英文字由大寫起頭,其餘小寫。 ex:show、addNum 類別:英文大寫字母開始,若由數個英文單字組成,則 後面的英文字由大寫起頭,其餘小寫。 ex:CWin、MaxSize

11 資料型態 整數:int (integer values) int num;
浮點數:float (floating point values) float num; 單一字元:char (single character values) char ch; 字串:character strings (arrays of characters, discussed later) char name[20]; 布林型態:bool (true:1、false:0) bool status=false; bool status=1;

12 C++的基本資料型態 (character, 字元)char (integer, 整數)int
(long integer, 長整數)long int (short integer, 短整數)short int (浮點數)float (倍精度浮點數)double (布林)bool 佔1 byte 佔4 bytes 佔2 bytes 佔8 bytes

13 How "cout" work? cout << “Hello!”;
cout << “Hello!” << endl; cout << “Hello!\n”; cout << b; cout << a << "+" << b << "=" << a+b << endl;

14 How "cin" work? int a; cin >> a; char ch; cin.get(ch);
string str; //需加 #include <string>  cin >> str; //str不能有空白  getline(cin, str); //str可以有空白

15 字元型態的列印 #include <iostream> #include <cstdlib>
using namespace std; int main(void) { char ch = 'h'; int i = ch; cout << "ch=" << ch << endl; cout << "The ASCII code is " << i <<endl; system("pause"); return 0; }

16 計算BMI 輸入:身高(height), 體重(weight) 運算式: 輸出:BMI的值 BMI = weight / (height)2
BMI = weight / (height * height) 輸出:BMI的值


Download ppt "變數與資料型態 2013.03 綠園."

Similar presentations


Ads by Google