Download - C++ 程式設計入門
![Page 1: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/1.jpg)
變數與運算子作者:黃建庭
![Page 2: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/2.jpg)
變數◦ 變數是程式放資料的空間,每個變數佔有特定的記憶體空
間,由編譯器分配。◦ 程式將資料進行運算,就是對變數與記憶體進行運算。◦ 範例: y=x+12
![Page 3: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/3.jpg)
1. 開頭不能為數字。 2. 只能使用大小寫英文字母、數字與底線 (_) 所組
成。 3. 不能使用保留字 ( 如 if) 。 4. 變數大小寫有差別。 5. 使用有意義的單字命名,如 score 命名成績變數。
![Page 4: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/4.jpg)
(a) 宣告變數◦ 資料型別 變數 ; ◦ 例如: int a;
(b) 宣告變數並初始化◦ 資料型別 變數 = 初始值 ;◦ 例如: int a=10;
(c) 宣告多個變數◦ 以逗點隔開◦ 例如: int a,b,c;
![Page 5: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/5.jpg)
C 語言的資料型別分成整數、浮點數與字元等。◦ 整數與浮點數(小數點)◦ 資料型別有空間限制,超過空間範圍後,數值就無法正確
表示◦ 字元是任何英文字母、數字與半形標點符號。
![Page 6: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/6.jpg)
整數分為 short 、 int 、 long 與 long long 。資料型別 所佔記憶
體空間(位元組 )
數值範圍 格式化輸出與輸入代碼
short 2 -32768 ~ 32767 %hi
int 2 或4
32768 ~ 32767 或-2147483648 ~ 2147483647
%d
long int 4 -2147483648 ~ 2147483647 %ld
long long int 8 -263-1~263-1 %lld
![Page 7: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/7.jpg)
浮點數分為單精度與倍精度浮點數資料型別 所佔
記憶體空間 (位元 )
數值範圍 格式化輸出與輸入代碼
float 4 3.4x10-38 到 3.4x10+38
-3.4x10+38 到 -3.4x10-38
註:準確到小數點以下第 7 位
%f
double 8 1.7x10-308 到 1.7x10+308
-1.7x10+308 到 -1.7x10-308
註:準確到小數點以下第 15 位
%lf
![Page 8: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/8.jpg)
大小寫英文字母、數字、英文標點符號。
字串 格式代碼 %s
資料型別 所佔記憶體空間 (位元組 )
數值範圍 格式化輸出與輸入代碼
char 1 -128 ~ 127 %c
![Page 9: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/9.jpg)
#include <stdio.h> 輸出 printf 。函式與參數 說明 範例
printf( 控制字串 ,
參數 )
輸出字串到螢幕
printf(“C++”);輸出結果: C++
printf(“%sC++”,” 我也愛” );輸出結果:我也愛 C++雙引號表示字串。
printf(“%c”,’C’);輸出結果: C單引號表示字元。
printf(“%d+%d=%d”,1,1,2);輸出結果: 1+1=2
![Page 10: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/10.jpg)
函式與參數 說明 範例scanf( 控制字串 , 參數 ) 由鍵盤輸入到變數 scanf(“%d” , &score);
結果:輸入數值會存入score 中。註: & 表示取址符號。
![Page 11: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/11.jpg)
\n 換行字元 \0 表示字串的結束 \t 相當於按「 tab 」鍵 \a 發出嗶一聲
![Page 12: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/12.jpg)
#include <iostream> using namespace std;
cout的使用
分類 格式 說明 範例
輸出字串 cout << “字串” ; 「 <<」表示
輸出,字串部
分前後要加上
雙引號。
cout << “C++”;輸出結果: C++
輸出變數 cout << 變數 ; 「 <<」表示
輸出,變數部
分直接使用不
用加雙引號。
int x=12;cout << x;輸出結果: 12
輸出多個 cout << 變數 << “字串” << endl; endl表示換行
![Page 13: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/13.jpg)
cin的使用
分類 格式 說明 範例
單一輸入 cin >> 變數 ; 輸入到變數 int a;
cin >> a;
多個輸入 cin >> 變數 1 >>
變數 2 >> 變數 3;
輸入多個變數 int a,b,c;
cin >> a >> b >> c;
![Page 14: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/14.jpg)
C 語言中要讓特殊字元失去原本的意義要在字元前面新增「 \ 」,注意斜線方向。◦ 例如要輸出「”」,要 cout << “ \” ”;◦ 例如要輸出「 \ 」,要 cout << “ \\ ”;
![Page 15: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/15.jpg)
指定運算子◦ 將等號右邊計算結果儲存到等號左邊。
程式 說明score=90; 將 90 儲存入變數 score 。
x=y 將 y 儲存入 x 。
![Page 16: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/16.jpg)
負責數值的運算,如加、減、乘、除與求餘數運算子 說明 範例+ 相加 A=2+1;
結果A=3
- 相減 A=2-1;結果A=1
* 相乘 A=2*3;結果A=6
/ 相除 A=6/2;結果A=3
% 求餘數 A=4%3;結果A=1
![Page 17: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/17.jpg)
#include <iostream>using namespace std;int main() { double F,C; cin >> F; C=(F-32)*5/9; cout << " 攝氏為 " << C << endl; system("pause");}
![Page 18: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/18.jpg)
運算子 說明 範例 (註: true 為 1 , false 為 0 。 )
< 判斷是否小於,若是回傳 true ,否則回傳 false
4<1 ,計算結果為 false ,回傳 0 。
> 判斷是否大於,若是回傳 true ,否則回傳 false
4>1 ,計算結果為 true ,回傳 1 。
<= 判斷是否小於等於,若是回傳 true ,否則回傳 false
4<=1 ,計算結果為 false ,回傳 0 。
>= 判斷是否大於等於,若是回傳 true ,否則回傳 false
4>=1 ,計算結果為 true ,回傳 1 。
== 判斷是否等於,若是回傳 true ,否則回傳 false
4==1 ,計算結果為 false ,回傳 0 。
!= 判斷是否不等於,若是回傳 true ,否則回傳 false
4!=1 ,計算結果為 true ,回傳 1 。
![Page 19: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/19.jpg)
邏輯運算子有三個且 (&&) 、或 (||) 、非 (!)
X && Y Y=True Y=False
X=True True False
X=False False False
X || Y Y=True Y=False
X=True True True
X=False True False
! X
X=True False
X=False True
![Page 20: C++ 程式設計入門](https://reader033.vdocuments.pub/reader033/viewer/2022061518/56813587550346895d9ce631/html5/thumbnails/20.jpg)
#include <iostream>using namespace std;int main() { int score; cin >> score; cout << “ 成績 >=60 且 <80 結果為 " << ((score >=60)&& (score<80)) << endl; system("pause");}