C++ Primer Plus詳解
1.各種數據類型的長度
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中的函數.INT_MAX是
cout << "short is " << sizeof n_short << " bytes." << endl; //函數必須帶括號,sizeof(運算符)在這里可以不跟括號,但查看數據類型必須加括號
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. 無符號數據類型及cout進制顯示
2.1 無符號數據類型及溢出
#include <iostream>
#include <climits> //可以查看數據類型最大值最小值
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顯示整數的方式
cout << "cheat = " << cheat << " 42 (in decimal)" << endl;
cout << "waist = " << waist << " 0x42 (in hexadicimal)" << endl;
cout << "inseam = " << inseam << " 042 (in octal)" << endl; // cout默認以十進制顯示整數
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轉換為77
cout << "Hello, thanks you for the " << ch << " character." << endl; //cout將77轉換為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類(數據以及操作數據的方法)的對象cin、cout 矩形(對象)平移(方法)
cout.put(ch); //用對象訪問類里面的操作方法(函數)、
cout.put('!');
cout.put('A');
cout << "Done" << endl;
return 0;
}cin與cout的行為由變量引導


3.3 轉義字符換行
#include <iostream>
int main(void)
{
using namespace std;
int n = 10;
cout << "Hello world!" << endl;
cout << "Good morning!\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.浮點數(整數部分+小數部分)
#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默認輸出小數點后六位
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. 比較大的浮點數
#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位數只有前六位或前七位有效
return 0;
}
7.float與double的精度
float單精度 至少32位 double雙精度 至少48位
浮點數在內存中如何存儲的? int 類型 5 以0101二進制存儲這個好理解
float 8.25 單精度 內存中32位(bit) double 64位 計算機存浮點數都是以科學計數法的方式存儲的 IEEE標準
8.25 科學計數法 8.25 * 10^0 112.5 科學計數法 1.125 * 10^2 十進制的科學計數法 但是計算機只認識二進制的科學計數法
8.25 二進制的科學計數法(分成整數部分(倒序)+小數部分(正序)) 1000.01 轉換為科學計數法 為1.00001*2^3
50.25 整數部分(32+16+2) 110010.01 任何一個浮點數二進制科學計數法為 1.?????*2^x 整數部分一定為1
符號占1bit 正數為 0,負數為 1; 1.00001*2^3 指數3表示小數點右移
0 - 255 中間127 0-126(負次冪) 127-255(代表正次冪)
8.25在內存中的表示: 符號位0 8位指數位 為127+3=130(3次冪) 130 -- 10000010 0(符號位) 10000010(指數位) 00001(小數位,23位) 000000000000000000(18個0)
整數部分1位,小數部分23位,共24位(二進制) 4位對應一個十進制的數 24/4=6 對應十進制有效位6位 二進制32位 課本56頁
double類型: 十進制13(52/4)位有效位 二進制64位 符號位1 指數位11 小數位 52
11.17 二進制 1011.001010111100001........(小數部分很難取整,無限長 ) 而float最多表示32位小數部分, 二進制超過32位就不對了,十進制超過6位就不對了
26.0在內存中如何存儲的? 0100000110100000............
11010.00000000............
1.10100.......0 * 2 ^ 4
符號位:0 指數位:127 + 4 = 131 二進制:10000011 小數位:010000000000...........
0.75 二進制 0.110000.............
1.10*(2^-1)
符號位:0 指數位:127 + (-1) = 126 二進制:01111110 小數位:1000000000...........
-2.5 二進制 10.1
-1.01*2^1
符號位:1 指數位:127 + 1 = 128 二進制:10000000 小數位: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在內存中如何存儲的? 11010.00000000............ 1.10100.......0 * 2 ^ 4 符號位:0 指數位:127 + 4 = 131 二進制:10000011 小數位:010000000000...........
#include <iostream>
int main(void)
{
using namespace std;
float hats, heads;
cout.setf(ios_base::fixed, ios_base::floatfield); //可以強制打印小數點后六位(沒辦法四舍五入),去掉這一句就可以四舍五入,輸出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 的小數部分 0.42 二進制01101...........無限,但是float二進制小數部分只有23位有效,十進制六位61.4199(近似有效)
cout << "hats - heads = " << hats - heads << endl;
cout << "hats * heads = " << hats * heads << endl;
cout << "hats / heads = " << hats / heads << endl; // folat精度不夠,二進制只能顯示小數點后23位,所以不準,去掉setf()可四舍五入
return 0;
}
注視掉 cout.setf()

解決辦法:用double 可顯示二進制小數點后53位,十進制13位,我們用setf()顯示6位顯然也是準確的

9.乘除法
#include <iostream>
int main(void)
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield); //顯示小數點后六位
cout << "Integer division : 9/5 = " << 9 / 5 << endl; //整數相除結果取出整數部分
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類型來處理(精度高) 一般定義浮點數用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.數值轉換
#include <iostream>
int main(void)
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float tree = 3;
int guess(3.9832); // C++特有賦值方式,這里取出小數的整數部分
int debt = 6.2E22; //超過了整型的取值范圍,結果不確定,對于int(32位 -2^31---2^31-1)太大了
cout << "tree = " << tree << endl;
cout << "guess = " << guess << endl;
cout << "debt = " << debt << endl;
return 0;
}
12.強制類型轉換
#include <iostream>
int main(void)
{
using namespace std;
int auks, bats, coots;
auks = 19.99 + 11.99; //計算機用double類型相加計算再轉換為整型,結果取整=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; //強制類型轉換
return 0;
}
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
C/C++中關于std::string的compare陷阱示例詳解
這篇文章主要給大家介紹了關于C/C++中關于std::string的compare陷阱的相關資料,文中先對C/C++中的std::string進行了簡單的介紹,通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2017-11-11
如何使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境
這篇文章主要介紹了使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單)
這篇文章主要介紹了OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

