數(shù)字

2018-08-12 22:03 更新

數(shù)字

當(dāng)我們使用數(shù)字時,通常我們使用原始數(shù)據(jù)類型,例如 int,short,long,float 和 double 等。數(shù)字?jǐn)?shù)據(jù)類型,它們的可能值和取值范圍在討論 C++ 數(shù)據(jù)類型時已經(jīng)解釋了。

在 C++ 程序中定義數(shù)字

在之前的章節(jié)中,我們定義了多種數(shù)字類型。下面的例子定義了多種數(shù)字類型:

    #include <iostream>
    using namespace std;

    int main ()
    {
       // number definition:
       short  s;
       inti;
       long   l;
       float  f;
       double d;

       // number assignments;
       s = 10;  
       i = 1000;
       l = 1000000; 
       f = 230.47;  
       d = 30949.374;

       // number printing;
       cout << "short  s :" << s << endl;
       cout << "inti :" << i << endl;
       cout << "long   l :" << l << endl;
       cout << "float  f :" << f << endl;
       cout << "double d :" << d << endl;

       return 0;
    }

當(dāng)上述代碼被編譯執(zhí)行時,它將產(chǎn)生下面的結(jié)果:

    short  s :10
    inti :1000
    long   l :1000000
    float  f :230.47
    double d :30949.4

C++ 中的數(shù)學(xué)運(yùn)算

您除了可以創(chuàng)建各種各樣的函數(shù)外,C++ 中還包括一些你可以使用的已經(jīng)存在的函數(shù)。這些函數(shù)在標(biāo)準(zhǔn)的 C 和 C++ 庫中都可以使用,被成為內(nèi)置函數(shù)。這些函數(shù)都可以包含在您的程序中,然后使用。

C++ 具有一套豐富的數(shù)學(xué)運(yùn)算,可以運(yùn)用在各種各樣的數(shù)字上。下表中列出了一些在 C++ 中可以使用的內(nèi)置數(shù)學(xué)函數(shù)。

要利用這些函數(shù),你需要包括<cmath>這個數(shù)學(xué)頭文件。

序號 函數(shù)和功能
1 double cos(double); 這個函數(shù)輸入一個角度(作為一個雙精度)并返回余弦值。
2 double sin(double); 這個函數(shù)輸入一個角度(作為一個雙精度)并返回正弦值。
3 double tan(double); 這個函數(shù)輸入一個角度(作為一個雙精度)并返回正切值。
4 double log(double); 這個函數(shù)輸入一個數(shù)字并返回該數(shù)字的自然對數(shù)。
5 double pow(double,double); 第一個參數(shù)是你想要增長的數(shù)字,第二個參數(shù)是你想要將它增長的倍數(shù)。
6 double hypot(double,double); 如果你向該函數(shù)傳遞一個直角三角形的兩個邊的長度,它將返回你的直角三角形的斜邊長度。
7 double sprt(double); 您向該函數(shù)傳遞一個參數(shù),它將給你這個數(shù)字的平方根。
8 int abs(int); 這個函數(shù)返回傳遞給該函數(shù)的整數(shù)的絕對值。
9 double fabs(double); 這個函數(shù)返回傳遞給該函數(shù)的任何十進(jìn)制數(shù)的絕對值。
10 double floor(double); 查找小于或者等于輸入?yún)?shù)的整數(shù)。

以下是一個用來說明少數(shù)數(shù)學(xué)運(yùn)算的簡單的示例:

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main ()
    {
       // number definition:
       short  s = 10;
       inti = -1000;
       long   l = 100000;
       float  f = 230.47;
       double d = 200.374;

       // mathematical operations;
       cout << "sin(d) :" << sin(d) << endl;
       cout << "abs(i)  :" << abs(i) << endl;
       cout << "floor(d) :" << floor(d) << endl;
       cout << "sqrt(f) :" << sqrt(f) << endl;
       cout << "pow( d, 2) :" << pow(d, 2) << endl;

       return 0;
    }

當(dāng)上述代碼被編譯執(zhí)行時,它將產(chǎn)生如下結(jié)果:

    sign(d) :-0.634939
    abs(i)  :1000
    floor(d) :200
    sqrt(f) :15.1812
    pow( d, 2 ) :40149.7

C++ 中的隨機(jī)數(shù)字

在很多情況下,您會希望生成一個隨機(jī)數(shù)字。這里有兩個你需要知道的隨機(jī)數(shù)字的生成函數(shù)。第一個是 rand(),這個函數(shù)只返回一個偽隨機(jī)數(shù)。要解決這個問題的方式是,首先調(diào)用 srand() 函數(shù)。

以下是一個用來產(chǎn)生幾個隨機(jī)數(shù)字的簡單例子。這個示例使用 time() 函數(shù)來獲取在您的系統(tǒng)時間中的秒數(shù)來隨機(jī)的生成 rand() 函數(shù):

    #include <iostream>
    #include <ctime>
    #include <cstdlib>

    using namespace std;

    int main ()
    {
       int i,j;

       // set the seed
       srand( (unsigned)time( NULL ) );

       /* generate 10  random numbers. */
       for( i = 0; i < 10; i++ )
       {
      // generate actual random number
      j= rand();
      cout <<" Random Number : " << j << endl;
       }

       return 0;
    }

當(dāng)上述代碼被編譯執(zhí)行時,它將產(chǎn)生下面的結(jié)果:

     Random Number : 1748144778
     Random Number : 630873888
     Random Number : 2134540646
     Random Number : 219404170
     Random Number : 902129458
     Random Number : 920445370
     Random Number : 1319072661
     Random Number : 257938873
     Random Number : 1256201101
     Random Number : 580322989
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號