C++ Primer Plus詳解
1.各種數(shù)據(jù)類型的長度
int main(void) { using namespace std; int n(3); // C++還可以這么賦值 int m{6}; int f = {8}; int n_int = INT_MAX; //聲明變量同時賦值=初始化 short n_short = SHRT_MAX; long n_long = LONG_MAX; long long n_llong = LLONG_MAX; cout << "n = " << n << endl; cout << "m = " << m << endl; cout << "f = " << f << endl; cout << "int is " << sizeof(int) << " bytes." << endl; // sizeof是運算符不是clmits中的函數(shù).INT_MAX是 cout << "short is " << sizeof n_short << " bytes." << endl; //函數(shù)必須帶括號,sizeof(運算符)在這里可以不跟括號,但查看數(shù)據(jù)類型必須加括號 cout << "long is " << sizeof n_long << " bytes." << endl; cout << "long long is " << sizeof n_llong << " bytes." << endl; cout << "Maximum values: " << endl; cout << "int: " << n_int << endl; cout << "short: " << n_short << endl; cout << "long: " << n_long << endl; cout << "long long: " << n_llong << endl; return 0; }
2. 無符號數(shù)據(jù)類型及cout進制顯示
2.1 無符號數(shù)據(jù)類型及溢出
#include <iostream> #include <climits> //可以查看數(shù)據(jù)類型最大值最小值 int main(void) { using namespace std; short sam = SHRT_MAX; //-32768-32767 (16位) longlong > long(至少32位)> int > short(至少16位) unsigned short sue = sam; // 0-65535 cout << "sam has " << sam << " dolloars and sue has " << sue << " dolloars" << endl; cout << "Add 1 dolloars to each account" << endl; sam = sam + 1; sue = sue + 1; cout << "Now sam has " << sam << " dolloars and sue has " << sue << " dolloars" << endl; sam = 0; sue = 0; sam = sam - 1; sue = sue - 1; cout << "Now sam has " << sam << " dolloars and sue has " << sue << " dolloars" << endl; return 0; }
2.2 cout十六進制顯示
#include <iostream> int main(void) { using namespace std; int cheat = 42; //十進制 int waist = 0x42; //十六進制 int inseam = 042; //八進制 cout << hex; //修改cout顯示整數(shù)的方式 cout << "cheat = " << cheat << " 42 (in decimal)" << endl; cout << "waist = " << waist << " 0x42 (in hexadicimal)" << endl; cout << "inseam = " << inseam << " 042 (in octal)" << endl; // cout默認以十進制顯示整數(shù) return 0; }
2.3 cout 八進制十進制十六進制顯示
#include <iostream> int main(void) { using namespace std; int cheat = 42; int waist = 0x42; int inseam = 042; cout << "cheat = " << cheat << " 42 (in decimal)" << endl; // cout默認十進制顯示 cout << hex; //修改cout為十六進制顯示 cout << "waist = " << waist << " 0x42 (in hexadicimal)" << endl; cout << oct; //修改cout為八進制顯示 cout << "inseam = " << inseam << " 042 (in octal)" << endl; return 0; }
3.char、ASCII、\n
3.1 char類型
#include <iostream> int main(void) { using namespace std; char ch; cout << "Enter a character:" << endl; cin >> ch; //cin將鍵盤輸入的M轉(zhuǎn)換為77 cout << "Hello, thanks you for the " << ch << " character." << endl; //cout將77轉(zhuǎn)換為M,cin和cout的行為由變量類型引導 return 0; }
3.2 ASCII與char、cout.put()
#include <iostream> int main() { using namespace std; char ch = 'M'; int i = ch; cout << "The ASCII code for " << ch << " is " << i << endl; cout << "Add one to the character code:" << endl; ch = ch + 1; //顯示下一個字符 i = ch; cout << "The ASCII code for " << ch << " is " << i << endl; cout << "Displaying char ch using cout.put(ch)" << endl; // iostream類(數(shù)據(jù)以及操作數(shù)據(jù)的方法)的對象cin、cout 矩形(對象)平移(方法) cout.put(ch); //用對象訪問類里面的操作方法(函數(shù))、 cout.put('!'); cout.put('A'); cout << "Done" << endl; return 0; }
cin與cout的行為由變量引導
3.3 轉(zhuǎn)義字符換行
#include <iostream> int main(void) { using namespace std; int n = 10; cout << "Hello world!" << endl; cout << "Good morning!\n";//使用轉(zhuǎn)義字符換行,顯示字符串這種方法簡單一點,\n是一個字符哦(換行符) cout << "What's your name?" << '\n'; cout << "What's your name?" << "\n"; cout << "n = " << n << endl; cout << "n = " << n << "\n"; return 0; }
4.const
#include <iostream> int main() { using namespace std; const int toes = 10; //必須在聲明時賦值(之后不準修改)(對const初始化) /*const int toes; toes = 10;這種是錯誤的*/ return 0; }
5.浮點數(shù)(整數(shù)部分+小數(shù)部分)
#include <iostream> int main() { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); float tub = 10.0 / 3.0; const float million = 1.0E6; cout << "tub = " << tub << endl; // cout默認輸出小數(shù)點后六位 cout << "A million tubs = " << million * tub << endl; // float精度達不到 cout << "Ten million tubs = " << 10 * million * tub << endl; // float精度達不到 double mint = 10.0 / 3.0; cout << "A million mint = " << million * mint << endl; cout << "Ten million mint = " << 10 * million * mint << endl; // double精度比float高 return 0; }
6. 比較大的浮點數(shù)
#include <iostream> int main() { using namespace std; float a = 2.34E22; // 2.34e/E+22 2.34*10^22 float b = a + 1.0; cout << "a = " << a << endl; cout << "b = " << b << endl; //a = b cout << "b - a = " << b - a << endl; // float位數(shù)只有前六位或前七位有效 return 0; }
7.float與double的精度
float單精度 至少32位 double雙精度 至少48位
浮點數(shù)在內(nèi)存中如何存儲的? int 類型 5 以0101二進制存儲這個好理解
float 8.25 單精度 內(nèi)存中32位(bit) double 64位 計算機存浮點數(shù)都是以科學計數(shù)法的方式存儲的 IEEE標準
8.25 科學計數(shù)法 8.25 * 10^0 112.5 科學計數(shù)法 1.125 * 10^2 十進制的科學計數(shù)法 但是計算機只認識二進制的科學計數(shù)法
8.25 二進制的科學計數(shù)法(分成整數(shù)部分(倒序)+小數(shù)部分(正序)) 1000.01 轉(zhuǎn)換為科學計數(shù)法 為1.00001*2^3
50.25 整數(shù)部分(32+16+2) 110010.01 任何一個浮點數(shù)二進制科學計數(shù)法為 1.?????*2^x 整數(shù)部分一定為1
符號占1bit 正數(shù)為 0,負數(shù)為 1; 1.00001*2^3 指數(shù)3表示小數(shù)點右移
0 - 255 中間127 0-126(負次冪) 127-255(代表正次冪)
8.25在內(nèi)存中的表示: 符號位0 8位指數(shù)位 為127+3=130(3次冪) 130 -- 10000010 0(符號位) 10000010(指數(shù)位) 00001(小數(shù)位,23位) 000000000000000000(18個0)
整數(shù)部分1位,小數(shù)部分23位,共24位(二進制) 4位對應一個十進制的數(shù) 24/4=6 對應十進制有效位6位 二進制32位 課本56頁
double類型: 十進制13(52/4)位有效位 二進制64位 符號位1 指數(shù)位11 小數(shù)位 52
11.17 二進制 1011.001010111100001........(小數(shù)部分很難取整,無限長 ) 而float最多表示32位小數(shù)部分, 二進制超過32位就不對了,十進制超過6位就不對了
26.0在內(nèi)存中如何存儲的? 0100000110100000............
11010.00000000............
1.10100.......0 * 2 ^ 4
符號位:0 指數(shù)位:127 + 4 = 131 二進制:10000011 小數(shù)位:010000000000...........
0.75 二進制 0.110000.............
1.10*(2^-1)
符號位:0 指數(shù)位:127 + (-1) = 126 二進制:01111110 小數(shù)位:1000000000...........
-2.5 二進制 10.1
-1.01*2^1
符號位:1 指數(shù)位:127 + 1 = 128 二進制:10000000 小數(shù)位:0100000000...........
8.float的誤差
/* * @Description: * @Date: 2022-02-14 13:25:40 * @LastEditTime: 2022-02-14 13:56:58 * @FilePath: \C++\第三章\class6\6_1.cpp */ // 26.0在內(nèi)存中如何存儲的? 11010.00000000............ 1.10100.......0 * 2 ^ 4 符號位:0 指數(shù)位:127 + 4 = 131 二進制:10000011 小數(shù)位:010000000000........... #include <iostream> int main(void) { using namespace std; float hats, heads; cout.setf(ios_base::fixed, ios_base::floatfield); //可以強制打印小數(shù)點后六位(沒辦法四舍五入),去掉這一句就可以四舍五入,輸出61.42000000.......0可以不顯示 cout << "Enter a number: "; // 50.25 cin >> hats; cout << "Enter another number: "; // 11.17 cin >> heads; cout << "hats = " << hats << " heads = " << heads << endl; cout << "hats + heads = " << hats + heads << endl; // 61.42 的小數(shù)部分 0.42 二進制01101...........無限,但是float二進制小數(shù)部分只有23位有效,十進制六位61.4199(近似有效) cout << "hats - heads = " << hats - heads << endl; cout << "hats * heads = " << hats * heads << endl; cout << "hats / heads = " << hats / heads << endl; // folat精度不夠,二進制只能顯示小數(shù)點后23位,所以不準,去掉setf()可四舍五入 return 0; }
注視掉 cout.setf()
解決辦法:用double 可顯示二進制小數(shù)點后53位,十進制13位,我們用setf()顯示6位顯然也是準確的
9.乘除法
#include <iostream> int main(void) { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); //顯示小數(shù)點后六位 cout << "Integer division : 9/5 = " << 9 / 5 << endl; //整數(shù)相除結(jié)果取出整數(shù)部分 cout << "Float division : 9.0/5.0 = " << 9.0 / 5.0 << endl; cout << "Mixed division : 9.0/5 = " << 9.0 / 5 << endl; //混合類型 cout << "Mixed division : 9/5.0 = " << 9 / 5.0 << endl; //混合類型 浮點型精度高于整型,提升精度為浮點型 cout << "Double division : 1e7 / 9.0 = " << 1e7 / 9.0 << endl; //都當成double類型來處理(精度高) 一般定義浮點數(shù)用double,編譯器默認當成double處理,除非你強制float cout << "FLoat constance division : 1e7f / 9.0f = " << 1e7f / 9.0f << endl; //都當成float類型來處理 return 0; }
10.求模運算符
#include <iostream> int main(void) { using namespace std; const int pounds_per_stone = 14; //聲明整型類型常量 14榜(pounds)=1英石(stone) cout << "Enter your weight in pounds: "; int lbs; cin >> lbs; int stone = lbs / pounds_per_stone; int pounds = lbs % pounds_per_stone; cout << lbs << "pounds = " << stone << " stone, " << pounds << " pounds." << endl; return 0; }
11.數(shù)值轉(zhuǎn)換
#include <iostream> int main(void) { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); float tree = 3; int guess(3.9832); // C++特有賦值方式,這里取出小數(shù)的整數(shù)部分 int debt = 6.2E22; //超過了整型的取值范圍,結(jié)果不確定,對于int(32位 -2^31---2^31-1)太大了 cout << "tree = " << tree << endl; cout << "guess = " << guess << endl; cout << "debt = " << debt << endl; return 0; }
12.強制類型轉(zhuǎn)換
#include <iostream> int main(void) { using namespace std; int auks, bats, coots; auks = 19.99 + 11.99; //計算機用double類型相加計算再轉(zhuǎn)換為整型,結(jié)果取整=31 bats = (int)19.99 + (int)11.99; // C語言格式 coots = int(19.99) + int(11.99); // C++語言格式 cout << "auks = " << auks << endl; cout << "bats = " << auks << endl; cout << "coots = " << auks << endl; char ch = 'Z'; cout << "The code for " << ch << " is " << int(ch) << endl; cout << static_cast<int>(ch) << endl; //強制類型轉(zhuǎn)換 return 0; }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
C++ Template 基礎篇(一):函數(shù)模板詳解
這篇文章主要介紹了C++ Template函數(shù)模板,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細介紹
本篇文章是對wprintf 中使用%I64d格式化輸出LONGLONG進行了詳細的分析介紹,需要的朋友參考下2013-05-05C語言素數(shù)(質(zhì)數(shù))判斷的3種方法舉例
這篇文章主要給大家介紹了關于C語言素數(shù)(質(zhì)數(shù))判斷的3種方法,質(zhì)數(shù)是只能被1或者自身整除的自然數(shù)(不包括1),稱為質(zhì)數(shù),文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11C++賦值函數(shù)+移動賦值函數(shù)+移動構(gòu)造函數(shù)詳解
這篇文章主要介紹了C++賦值函數(shù)+移動賦值函數(shù)+移動構(gòu)造函數(shù)詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08C++實現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼
這篇文章主要為大家詳細介紹了C++中實現(xiàn)字符格式相互轉(zhuǎn)換的方法,主要有UTF8與string互轉(zhuǎn)、wstring與string互轉(zhuǎn),感興趣的小伙伴可以了解一下2022-11-11