• <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>

            通關旅

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

            C++體會 -- 類型

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

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

            # 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
            少妇人妻88久久中文字幕| 亚洲AV无码一区东京热久久| 狠狠久久亚洲欧美专区| 久久香蕉国产线看观看乱码| 国产∨亚洲V天堂无码久久久| 精品久久一区二区| 亚洲嫩草影院久久精品| 久久久久久国产a免费观看不卡| 色偷偷88欧美精品久久久| 亚洲精品无码久久久久久| 久久九九有精品国产23百花影院| 国产精品免费久久| 日韩精品久久无码中文字幕| 88久久精品无码一区二区毛片 | 久久青青草原精品影院| 欧美日韩精品久久久久| 韩国无遮挡三级久久| 久久只这里是精品66| 欧美一区二区精品久久| 久久国产免费直播| 国产精自产拍久久久久久蜜| 久久香蕉超碰97国产精品| 日日噜噜夜夜狠狠久久丁香五月| 色成年激情久久综合| 久久无码AV中文出轨人妻| 亚洲国产精品无码久久久不卡| 精品久久久久久久| 国产午夜精品久久久久九九电影| 久久久久国产精品嫩草影院| 精品国产乱码久久久久久郑州公司 | 日韩人妻无码精品久久免费一| 99久久精品免费看国产一区二区三区 | 国产69精品久久久久9999| 亚洲欧美一区二区三区久久| 狠狠色综合久久久久尤物| 久久99精品综合国产首页| 久久久久久亚洲Av无码精品专口| 久久91这里精品国产2020| 国内精品久久久久影院免费| 精品国产乱码久久久久久郑州公司 | 亚洲AV无码久久|