1.簡單變量
- 信息將存儲在哪裏。
- 要存儲什麼值。
- 存儲何種類型的值。
- int a=5;//程序找到一塊能夠存儲整數的內存,將該內存單元標記爲a,將5複製到該內存單元上。
(1)變量名命名規則:a.在名稱中只能使用字母字符、數字和下劃線;b.名稱的第一個字符不能是數字;c.區別大小寫;d.不能將關鍵字用作名稱;e.對名稱長度沒有限制;f.以兩個下劃線或下劃線和大寫字母打頭的名稱被保留給實現使用。
(2)幾個基本概念
- 寬度用於描述存儲整數時使用的內存量,使用內存越多則越寬。
- 計算機內存的基本單位是位(bit),字節通常指的是8位的內存單元。
- 自然長度:計算機處理起來效率最高的長度。
- 初始化,在對變量進行初始化之前,該變量的值是不確定的,是它在被創建之前該內存單元保存的值。
- 整型常量:顯式書寫的常量如217和5等。基數通常爲10、8、16.不管是什麼形式,都被存儲爲二進制數(以2爲基數).
- 默認情況下c++將整型常量存儲爲int類型,把浮點數常量存儲爲double類型。
- bool類型 預定義false和true爲0和1。任何數字值或指針值都可以被隱式轉換爲bool值,任何非零值都被轉換爲true。
- const限定符,應該在聲明中對const進行初始化。
- 有效位:數字中有意義的位。12345有效位有5位,而14000有效位爲2位,其他三位只不過是佔位符而已。
- cout<<hex;cout<<oct;
1 //cout<<hex;和cout<<oct;控制符實際上是一條消息,告訴cout採取何種行爲 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 using namespace std; 7 int chest =42; 8 int waist =42; 9 int inseam=42; 10 11 cout<<"Monsieur cuts a striking figure!"<<endl; 12 cout<<"chest= "<<chest<<" (decimal)"<<endl; 13 cout<<hex;//不會再屏幕上顯示任何內容,而只是顯示cout顯示整數的方式 14 cout<<"waist= "<<waist<<" hexadecimal"<<endl; 15 cout<<oct; 16 cout<<"inseram= "<<inseam<<" (octal)"<<endl; 17 return 0; 18 }
(3)int、long、short(有符號和無符號的區別,表示範圍) 對類型名如(int)使用操作符sizeof時,應該將名稱放在括號中,對變量名使用,則括號名是可選的。
1 /short int long
2 #include<iostream>
3 #include<limits.h>
4 int main()
5 {
6 using namespace std;
7 int n_int=INT_MAX;
8 short n_short=SHRT_MAX;
9 long n_long=LONG_MAX;
10
11 cout<<"int is "<<sizeof(int)<<" bytes."<<endl;
12 cout<<"short is "<<sizeof n_short<<" bytes."<<endl;
13 cout<<"long is "<< sizeof n_long<<" bytes."<<endl;
14
15 cout<<"Maximum values:"<<endl;
16 cout<<"int: "<<n_int<<endl;
17 cout<<"short: "<<n_short<<endl;
18 cout<<"long: "<<n_long<<endl;
19
20 cout<<"Minimum int value= "<<INT_MIN<<endl;
21 cout<<"Bits per byte= "<<CHAR_BIT<<endl;
22 return 0;
23 }
(4)無符號數 使用unsigned標記
1 /如何使用無符號類型,程序試圖超越整型的限制時將產生的結果
2 //這些整型變量的行爲就像里程錶,一旦超出了限制,其值將爲範圍另一端的取值
3 #include<iostream>
4 #define ZERO 0
5 #include<climits>
6 int main()
7 {
8 using namespace std;
9 short sam=SHRT_MAX;
10 unsigned short sue=sam;
11
12 cout<<"Sam has "<<sam<<" dollars and sue has "<<sue<<" dollars deposited."<<endl;
13 cout<<"Add $1 to each account."<<endl<<"Now ";
14 sam=sam+1;
15 sue=sue+1;
16 cout<<"Sam has "<<sam<<" dollars and sue has "<<sue<<" dollars deposited."<<endl;
17 sam=ZERO;
18 sue=ZERO;
19 cout<<"Sam has "<<sam<<" dollars and sue has "<<sue<<" dollars deposited."<<endl;
20 sam=sam-1;
21 sue=sue-1;
22 cout<<"Sam has "<<sam<<" dollars and sue has "<<sue<<" dollars deposited."<<endl;
23 cout<<"Lucky sue!"<<endl;
24 return 0;
25 }
(5)char類型:字符和小整數
1 //值的類型引導cout選擇如何顯示值
2 //ch實際上是一個整數,因此可以對它使用整數操作
3 #include<iostream>
4 int main()
5 {
6 using namespace std;
7 char ch='M';
8 int i=ch;
9 cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
10
11 cout<<"Add one to the character code:"<<endl;
12 ch=ch+1;//對ch使用整數操作
13 i=ch;
14 cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
15 cout<<"Displaying char ch using cout.put(ch):";
16 cout.put(ch);//成員函數調用 通過類對象cout來使用函數put()
17 cout.put('!');
18 cout<<endl<<"Done!"<<endl;
19 return 0;
20 }
21
22 //cout<<'$';//打印$的ASCII值
23 //cout.put('$');//打印字符$
- char字符實際上是一個整數,可以使用整數操作。
- 關於成員函數cout.put():通過類對象cout來使用函數put()
- 轉義序列字符如'\n'、'\a'、'\b'等(換行、蜂鳴、退格).
2.浮點數
- 計算機將浮點數的值分爲兩部分存儲,一部分表示值,另一部分對值進行放大或縮小。它的存儲是基於二進制的,因此縮放因子是2.
- 7.2 e6是非法的,數字和e之間不能有空格。
- cout.setf(ios_base::fixed,ios_base::floatfield);//這種調用迫使輸出使用定點表示法。
1 //float和double類型及它們表示數字時在精度方面的差異
2 //float確保至少有6位有效位,double確保至少有15位有效位
3 //浮點常量默認爲double類型
4 #include<iostream>
5 int main()
6 {
7 using namespace std;
8 cout.setf(ios_base::fixed,ios_base::floatfield);
9 float tub=10.0/3.0;
10 double mint=10.0/3.0;
11 const float million=1.0e6;
12
13 cout<<"tub= "<<tub;
14 cout<<",a milloin tubs= "<<million*tub;
15 cout<<",\n and ten million tubs = "<<10*million*tub<<endl;
16
17 cout<<"mint= "<<mint<<" and a million mints = "<<million*mint<<endl;
18 return 0;
19 }
- 優點"可以表示整數之間的值,表示範圍更大"缺點"運算速度比整數慢,精度降低"
1 //浮點數的優缺點
2 //1.可以表示整數之間的值。2.有縮放因子所以表示值的範圍更大
3 //a、運算速度比整數慢。b、精度降低
4 #include<iostream>
5 int main()
6 {
7 using namespace std;
8 float a=2.34e+22f;
9 float b=a+1.0f;
10
11 cout<<"a= "<<a<<endl;
12 cout<<"b-a= "<<b-a<<endl;
13 return 0;
14 }
15
16 //程序運行結果b-a的值等於0. 問題在於a的值是一個小數點左邊有23位的數字。加上1就是在第23位加1
17 //但float類型只能表示數字中的前6位或前7位,因此修改第23位對這個數的值不會有影響。
3.C++算術操作符
(1)基本操作符+、-、*、/、%(要求兩個操作數都是整型)
(2)僅當兩個操作符被用於同一個操作數時,優先級和結合性規則纔有效。同一優先級如2*3+3*4沒有指出應該先算哪個乘法。
(3)除法分支:當有操作數爲浮點數時,結果也爲浮點數。對不同類型進行運算時,C++把它們轉換成同一類型。
1 //c++中的/運算
2 //在對不同類型進行運算時,把它們轉換爲同一類型
3 #include<iostream>
4 int main()
5 {
6 using namespace std;
7 cout.setf(ios_base::fixed,ios_base::floatfield);
8 cout<<"Integer division:9/5= "<<9/5<<endl;
9 cout<<"Floating-point division:9.0/5.0= "<<9.0/5.0<<endl;
10 cout<<"Mixed division: 9.0/5= "<<9.0/5<<endl;
11 cout<<"double constants:1E7/9.0= "<<1e7/9.0<<endl;
12 cout<<"float constants:1e7f/9.0f= "<<1e7f/9.0f<<endl;
13 return 0;
14 }
15
16
(4)類型轉換
- C++中有11種整型和3種浮點類型。
- C++允許將一種類型的值賦給另一種類型的變量,值將被轉變成接受變量的類型。
- 整型提升和強制類型轉換。(typename) value或者typename (value)
1 //強制類型轉換
2 #include<iostream>
3 int main()
4 {
5 using namespace std;
6 int duks,bats,coots;
7
8 duks=19.99+11.99; //31.98賦給變量duks時,被截短爲31
9 bats=(int)19.99+(int)11.99; //在進行加法運算之前先被截斷爲19和11,所以變量的值爲30
10 coots=int(19.99)+int(11.99);
11
12 cout<<"duks= "<<duks<<",bats= "<<bats<<",coots= "<<coots<<endl;
13 char ch='Z';
14 cout<<"The code for "<<ch<<" is "<<int(ch)<<endl;//利用強制轉換打印字符對應的整數。
15 return 0;
16 }