欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ Primer Plus詳解

 更新時(shí)間:2022年02月22日 10:21:59   作者:燦爛千陽 群山回唱  
這篇文章主要為大家詳細(xì)介紹了C++ Primer Plus,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.各種數(shù)據(jù)類型的長度

int main(void)
{
	using namespace std;
	int n(3); // C++還可以這么賦值
	int m{6};
	int f = {8};
	int n_int = INT_MAX; //聲明變量同時(shí)賦值=初始化
	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是運(yùn)算符不是clmits中的函數(shù).INT_MAX是
	cout << "short is " << sizeof n_short << " bytes." << endl; //函數(shù)必須帶括號(hào),sizeof(運(yùn)算符)在這里可以不跟括號(hào),但查看數(shù)據(jù)類型必須加括號(hào)
	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. 無符號(hào)數(shù)據(jù)類型及cout進(jìn)制顯示

2.1 無符號(hào)數(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十六進(jìn)制顯示

#include <iostream>
int main(void)
{
	using namespace std;
	int cheat = 42;	  //十進(jìn)制
	int waist = 0x42; //十六進(jìn)制
	int inseam = 042; //八進(jìn)制
	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默認(rèn)以十進(jìn)制顯示整數(shù)
	return 0;
}

2.3 cout 八進(jìn)制十進(jìn)制十六進(jìn)制顯示 

#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默認(rèn)十進(jìn)制顯示
	cout << hex; //修改cout為十六進(jìn)制顯示
	cout << "waist = " << waist << " 0x42 (in hexadicimal)" << endl;
	cout << oct; //修改cout為八進(jìn)制顯示
	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的行為由變量類型引導(dǎo)
	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; //顯示下一個(gè)字符
	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ù)的方法)的對(duì)象cin、cout    矩形(對(duì)象)平移(方法)
	cout.put(ch); //用對(duì)象訪問類里面的操作方法(函數(shù))、
	cout.put('!');
	cout.put('A');
	cout << "Done" << endl;
	return 0;
}

cin與cout的行為由變量引導(dǎo) 

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)義字符換行,顯示字符串這種方法簡(jiǎn)單一點(diǎn),\n是一個(gè)字符哦(換行符)
	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; //必須在聲明時(shí)賦值(之后不準(zhǔn)修改)(對(duì)const初始化)
    /*const int toes;
    toes = 10;這種是錯(cuò)誤的*/
    return 0;
}

5.浮點(diǎn)數(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默認(rèn)輸出小數(shù)點(diǎn)后六位
    cout << "A million tubs = " << million * tub << endl;        // float精度達(dá)不到
    cout << "Ten million tubs = " << 10 * million * tub << endl; // float精度達(dá)不到
    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. 比較大的浮點(diǎn)數(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位

浮點(diǎn)數(shù)在內(nèi)存中如何存儲(chǔ)的?         int 類型 5 以0101二進(jìn)制存儲(chǔ)這個(gè)好理解

float 8.25 單精度 內(nèi)存中32位(bit)       double 64位    計(jì)算機(jī)存浮點(diǎn)數(shù)都是以科學(xué)計(jì)數(shù)法的方式存儲(chǔ)的   IEEE標(biāo)準(zhǔn)

8.25 科學(xué)計(jì)數(shù)法 8.25 * 10^0          112.5   科學(xué)計(jì)數(shù)法   1.125 * 10^2    十進(jìn)制的科學(xué)計(jì)數(shù)法  但是計(jì)算機(jī)只認(rèn)識(shí)二進(jìn)制的科學(xué)計(jì)數(shù)法

8.25 二進(jìn)制的科學(xué)計(jì)數(shù)法(分成整數(shù)部分(倒序)+小數(shù)部分(正序))  1000.01    轉(zhuǎn)換為科學(xué)計(jì)數(shù)法 為1.00001*2^3

50.25 整數(shù)部分(32+16+2)   110010.01         任何一個(gè)浮點(diǎn)數(shù)二進(jìn)制科學(xué)計(jì)數(shù)法為     1.?????*2^x    整數(shù)部分一定為1

符號(hào)占1bit  正數(shù)為 0,負(fù)數(shù)為 1;                    1.00001*2^3     指數(shù)3表示小數(shù)點(diǎn)右移      

0 - 255   中間127  0-126(負(fù)次冪) 127-255(代表正次冪)

8.25在內(nèi)存中的表示: 符號(hào)位0           8位指數(shù)位 為127+3=130(3次冪)    130 -- 10000010       0(符號(hào)位)    10000010(指數(shù)位)  00001(小數(shù)位,23位) 000000000000000000(18個(gè)0)

整數(shù)部分1位,小數(shù)部分23位,共24位(二進(jìn)制)     4位對(duì)應(yīng)一個(gè)十進(jìn)制的數(shù)    24/4=6   對(duì)應(yīng)十進(jìn)制有效位6位   二進(jìn)制32位   課本56頁

double類型: 十進(jìn)制13(52/4)位有效位   二進(jìn)制64位   符號(hào)位1      指數(shù)位11   小數(shù)位 52

11.17    二進(jìn)制       1011.001010111100001........(小數(shù)部分很難取整,無限長 )   而float最多表示32位小數(shù)部分, 二進(jìn)制超過32位就不對(duì)了,十進(jìn)制超過6位就不對(duì)了

26.0在內(nèi)存中如何存儲(chǔ)的? 0100000110100000............

11010.00000000............

1.10100.......0 * 2 ^ 4

符號(hào)位:0    指數(shù)位:127 + 4 = 131 二進(jìn)制:10000011  小數(shù)位:010000000000...........

0.75 二進(jìn)制 0.110000.............

1.10*(2^-1)

符號(hào)位:0    指數(shù)位:127 + (-1) = 126 二進(jìn)制:01111110  小數(shù)位:1000000000...........

-2.5 二進(jìn)制 10.1

-1.01*2^1

符號(hào)位:1    指數(shù)位:127 + 1 = 128 二進(jìn)制: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)存中如何存儲(chǔ)的? 11010.00000000............ 1.10100.......0 * 2 ^ 4 符號(hào)位:0 指數(shù)位:127 + 4 = 131 二進(jìn)制:10000011 小數(shù)位:010000000000...........
#include <iostream>
int main(void)
{
    using namespace std;
    float hats, heads;
    cout.setf(ios_base::fixed, ios_base::floatfield); //可以強(qiáng)制打印小數(shù)點(diǎn)后六位(沒辦法四舍五入),去掉這一句就可以四舍五入,輸出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 二進(jìn)制01101...........無限,但是float二進(jìn)制小數(shù)部分只有23位有效,十進(jìn)制六位61.4199(近似有效)
    cout << "hats - heads = " << hats - heads << endl;
    cout << "hats * heads = " << hats * heads << endl;
    cout << "hats / heads = " << hats / heads << endl; // folat精度不夠,二進(jìn)制只能顯示小數(shù)點(diǎn)后23位,所以不準(zhǔn),去掉setf()可四舍五入
    return 0;
}

注視掉 cout.setf()

解決辦法:用double   可顯示二進(jìn)制小數(shù)點(diǎn)后53位,十進(jìn)制13位,我們用setf()顯示6位顯然也是準(zhǔn)確的

9.乘除法

#include <iostream>
int main(void)
{
    using namespace std;
    cout.setf(ios_base::fixed, ios_base::floatfield); //顯示小數(shù)點(diǎn)后六位
    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; //混合類型  浮點(diǎn)型精度高于整型,提升精度為浮點(diǎn)型
    cout << "Double division : 1e7 / 9.0 = " << 1e7 / 9.0 << endl;              //都當(dāng)成double類型來處理(精度高) 一般定義浮點(diǎn)數(shù)用double,編譯器默認(rèn)當(dāng)成double處理,除非你強(qiáng)制float
    cout << "FLoat constance division : 1e7f / 9.0f = " << 1e7f / 9.0f << endl; //都當(dāng)成float類型來處理
    return 0;
}

10.求模運(yùn)算符

#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é)果不確定,對(duì)于int(32位 -2^31---2^31-1)太大了
    cout << "tree = " << tree << endl;
    cout << "guess = " << guess << endl;
    cout << "debt = " << debt << endl;
    return 0;
}

12.強(qiáng)制類型轉(zhuǎn)換 

 
#include <iostream>
int main(void)
{
    using namespace std;
    int auks, bats, coots;
    auks = 19.99 + 11.99;            //計(jì)算機(jī)用double類型相加計(jì)算再轉(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; //強(qiáng)制類型轉(zhuǎn)換
    return 0;
}

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!   

相關(guān)文章

  • C++ Template 基礎(chǔ)篇(一):函數(shù)模板詳解

    C++ Template 基礎(chǔ)篇(一):函數(shù)模板詳解

    這篇文章主要介紹了C++ Template函數(shù)模板,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 簡(jiǎn)單談?wù)凜語言中的= 和==、!=

    簡(jiǎn)單談?wù)凜語言中的= 和==、!=

    這篇文章主要給大家介紹了關(guān)于C語言中= 和==、!=的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細(xì)介紹

    解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細(xì)介紹

    本篇文章是對(duì)wprintf 中使用%I64d格式化輸出LONGLONG進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C/C++中extern

    C/C++中extern "C" 的作用分析

    這篇文章主要介紹了C/C++中extern "C" 的作用,是在進(jìn)行C/C++程序設(shè)計(jì)中非常常見的用法,需要的朋友可以參考下
    2014-09-09
  • QT應(yīng)用程序cout輸出中文亂碼解決方法

    QT應(yīng)用程序cout輸出中文亂碼解決方法

    本文主要介紹了QT應(yīng)用程序cout輸出中文亂碼解決方法,文中通過圖文的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • C語言素?cái)?shù)(質(zhì)數(shù))判斷的3種方法舉例

    C語言素?cái)?shù)(質(zhì)數(shù))判斷的3種方法舉例

    這篇文章主要給大家介紹了關(guān)于C語言素?cái)?shù)(質(zhì)數(shù))判斷的3種方法,質(zhì)數(shù)是只能被1或者自身整除的自然數(shù)(不包括1),稱為質(zhì)數(shù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解

    C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解

    這篇文章主要介紹了C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • C++實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼

    C++實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼

    這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的方法,主要有UTF8與string互轉(zhuǎn)、wstring與string互轉(zhuǎn),感興趣的小伙伴可以了解一下
    2022-11-11
  • 淺談C++左值引用和右值引用

    淺談C++左值引用和右值引用

    下面小編就為大家?guī)硪黄獪\談C++左值引用和右值引用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • C語言實(shí)現(xiàn)哈希搜索算法及原理詳解

    C語言實(shí)現(xiàn)哈希搜索算法及原理詳解

    本文主要介紹了C語言實(shí)現(xiàn)哈希搜索算法及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評(píng)論