• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            通關(guān)旅

            softgamer的痕跡
            posts - 16, comments - 13, trackbacks - 0, articles - 0

            C++體會(huì) -- 類(lèi)型

            Posted on 2007-02-05 21:00 softgamer 閱讀(218) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 學(xué)習(xí)日志
            我在使用C++時(shí)有一些體會(huì),相信大家都會(huì)的。我寫(xiě)出來(lái)也是和大家分享一下
            1。在C++中數(shù)據(jù)類(lèi)型float和double.
            ???? 同類(lèi)型為float的變量相比,類(lèi)型為double的變量可保存的值較大且較為準(zhǔn)確,因此我比較喜歡使用double而不是float.這里有一個(gè)重要的點(diǎn)要說(shuō)一下:比如我們定義兩個(gè)變量
            ???? int sum = 1230;
            ???? int score = 230;
            ???? double avrg? = 0.0f;
            ???? 如果我們用:
            ???? avrg = sum/score;
            ???? 其中(sum/score)這個(gè)計(jì)算結(jié)果是一個(gè)整型, 因?yàn)閟um和score都是整型。在這樣的計(jì)算中。小數(shù)部分會(huì)
            ??? 丟失,因此C++提供了一元強(qiáng)制類(lèi)型轉(zhuǎn)換
            ???? avrg = static_cast< double > ( sum ) / score;

            ??? static_cast < double > (), 這位sum創(chuàng)建了一個(gè)臨時(shí)性的浮點(diǎn)數(shù)副本,這就是我們說(shuō)的顯式轉(zhuǎn)換。對(duì)比顯式轉(zhuǎn)換,當(dāng)然就是隱式轉(zhuǎn)換,例如當(dāng)score 被提升為double后,開(kāi)始執(zhí)行浮點(diǎn)除法運(yùn)算。
            ??? 然后我們輸出
            ??? cout << "aver is " << setprecision(2)
            ??????????? <<setiosflags( ios::fixedm ios::showpoint )
            ??????????? << avrg <<endl;
            ?? setprecision(2) 是被稱(chēng)作參數(shù)化操作元的東西,要加一條預(yù)編譯指令
            ?? #include <iomanip>
            ?? endl是一個(gè)非參數(shù)化操縱元,它不需要<iomanip>頭文件,如果未指定精度,浮點(diǎn)值會(huì)采用6個(gè)精度(默認(rèn))輸出。
            ?? 注意不要愚蠢的想比較兩個(gè)浮點(diǎn)數(shù)是否相等,應(yīng)該測(cè)試兩個(gè)浮點(diǎn)數(shù)的差值的絕對(duì)值是否小于一個(gè)指定的小值。這一點(diǎn)在游戲的坐標(biāo)跟蹤中常被用到。

            # include <iostream>

            using std::cout;
            using std::cin;
            using std::endl;
            using std::ios;

            # include <iomanip>

            using std::setprecision;
            using std::setiosflags;

            int main()
            {
            ??? int score = 0;
            ??? int sum = 0;
            ??? int count = 0;

            ??? double avrg;

            ??? cout<<"enter score - to end: ";
            ??? cin>>score;
            ??? while ( score != -1 )
            ??? {
            ??????? sum = sum + score;
            ??????? count = count + 1;
            ??????? cout<< "enter score , -1 to end : ";
            ??????? cin>>score;
            ??? }

            ??? if( count != 0 )
            ??? {
            ??????? avrg = static_cast< double > (sum ) / count;
            ??????? cout << "avrg is " << setprecision(2)
            ??????????? << setiosflags(ios::fixed | ios::showpoint )
            ??????????? << avrg << endl;
            ??? }
            ??? else
            ??? {??? cout << " NO!!!!!";

            ??? }
            ??? return 0;
            }

            enter score - to end: 75
            enter score , -1 to end : 93
            enter score , -1 to end : 23
            enter score , -1 to end : 98
            enter score , -1 to end : 43
            enter score , -1 to end : 54
            enter score , -1 to end : 56
            enter score , -1 to end : 2334
            enter score , -1 to end : 45
            enter score , -1 to end : 43
            enter score , -1 to end : 454
            enter score , -1 to end : 232
            enter score , -1 to end : -1
            avrg is 295.83
            亚洲国产精品成人久久蜜臀| 一本伊大人香蕉久久网手机| 久久久久久精品久久久久| 久久笫一福利免费导航| 久久香综合精品久久伊人| 久久久久久夜精品精品免费啦| 99精品久久精品| 久久影视综合亚洲| 少妇久久久久久被弄高潮| 一本伊大人香蕉久久网手机| 中文字幕无码久久人妻| 精品国产乱码久久久久久1区2区| 很黄很污的网站久久mimi色| 久久精品国产AV一区二区三区| 久久精品9988| 久久久婷婷五月亚洲97号色| 久久精品免费大片国产大片| 久久精品国产亚洲AV麻豆网站| 久久一区二区三区99| 九九热久久免费视频| 国产精品久久久久久福利漫画| 一级a性色生活片久久无少妇一级婬片免费放| 中文字幕无码精品亚洲资源网久久| 韩国三级大全久久网站| 久久综合噜噜激激的五月天| 色偷偷91久久综合噜噜噜噜| 婷婷综合久久狠狠色99h| 久久精品中文騷妇女内射| 久久婷婷午色综合夜啪| 久久久精品国产Sm最大网站| 18岁日韩内射颜射午夜久久成人| 久久精品中文騷妇女内射| 五月丁香综合激情六月久久| yy6080久久| 久久99久国产麻精品66| 久久精品人妻中文系列| 久久久久精品国产亚洲AV无码| 亚洲一区精品伊人久久伊人| 欧美亚洲日本久久精品| 久久精品无码一区二区app| 久久成人永久免费播放|