通常,當(dāng)我們需要用到數(shù)字時(shí),我們會(huì)使用原始的數(shù)據(jù)類型,如 int、short、long、float 和 double 等等。這些用于數(shù)字的數(shù)據(jù)類型,其可能的值和數(shù)值范圍,我們已經(jīng)在 C++ 數(shù)據(jù)類型一章中討論過(guò)。
我們已經(jīng)在之前章節(jié)的各種實(shí)例中定義過(guò)數(shù)字。下面是一個(gè) C++ 中定義各種類型數(shù)字的綜合實(shí)例:
#include <iostream>
using namespace std;
int main ()
{
// 數(shù)字定義
short s;
int i;
long l;
float f;
double d;
// 數(shù)字賦值
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
// 數(shù)字輸出
cout << "short s :" << s << endl;
cout << "int i :" << i << endl;
cout << "long l :" << l << endl;
cout << "float f :" << f << endl;
cout << "double d :" << d << endl;
return 0;
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
short s :10 int i :1000 long l :1000000 float f :230.47 double d :30949.4
在 C++ 中,除了可以創(chuàng)建各種函數(shù),還包含了各種有用的函數(shù)供您使用。這些函數(shù)寫在標(biāo)準(zhǔn) C 和 C++ 庫(kù)中,叫做內(nèi)置函數(shù)。您可以在程序中引用這些函數(shù)。
C++ 內(nèi)置了豐富的數(shù)學(xué)函數(shù),可對(duì)各種數(shù)字進(jìn)行運(yùn)算。下表列出了 C++ 中一些有用的內(nèi)置的數(shù)學(xué)函數(shù)。
為了利用這些函數(shù),您需要引用數(shù)學(xué)頭文件 <cmath>。
序號(hào) | 函數(shù) & 描述 |
---|---|
1 | double cos(double);
該函數(shù)返回弧度角(double 型)的余弦。 |
2 | double sin(double);
該函數(shù)返回弧度角(double 型)的正弦。 |
3 | double tan(double);
該函數(shù)返回弧度角(double 型)的正切。 |
4 | double log(double);
該函數(shù)返回參數(shù)的自然對(duì)數(shù)。 |
5 | double pow(double, double);
假設(shè)第一個(gè)參數(shù)為 x,第二個(gè)參數(shù)為 y,則該函數(shù)返回 x 的 y 次方。 |
6 | double hypot(double, double);
該函數(shù)返回兩個(gè)參數(shù)的平方總和的平方根,也就是說(shuō),參數(shù)為一個(gè)直角三角形的兩個(gè)直角邊,函數(shù)會(huì)返回斜邊的長(zhǎng)度。 |
7 | double sqrt(double);
該函數(shù)返回參數(shù)的平方根。 |
8 | int abs(int);
該函數(shù)返回整數(shù)的絕對(duì)值。 |
9 | double fabs(double);
該函數(shù)返回任意一個(gè)十進(jìn)制數(shù)的絕對(duì)值。 |
10 | double floor(double);
該函數(shù)返回一個(gè)小于或等于傳入?yún)?shù)的最大整數(shù)。 |
下面是一個(gè)關(guān)于數(shù)學(xué)運(yùn)算的簡(jiǎn)單實(shí)例:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// 數(shù)字定義
short s = 10;
int i = -1000;
long l = 100000;
float f = 230.47;
double d = 200.374;
// 數(shù)學(xué)運(yùn)算
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í)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
sign(d) :-0.634939 abs(i) :1000 floor(d) :200 sqrt(f) :15.1812 pow( d, 2 ) :40149.7
在許多情況下,需要生成隨機(jī)數(shù)。關(guān)于隨機(jī)數(shù)生成器,有兩個(gè)相關(guān)的函數(shù)。一個(gè)是 rand(),該函數(shù)只返回一個(gè)偽隨機(jī)數(shù)。生成隨機(jī)數(shù)之前必須先調(diào)用 srand() 函數(shù)。
下面是一個(gè)關(guān)于生成隨機(jī)數(shù)的簡(jiǎn)單實(shí)例。實(shí)例中使用了 time() 函數(shù)來(lái)獲取系統(tǒng)時(shí)間的秒數(shù),通過(guò)調(diào)用 rand() 函數(shù)來(lái)生成隨機(jī)數(shù):
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int i,j;
// 設(shè)置種子
srand( (unsigned)time( NULL ) );
/* 生成 10 個(gè)隨機(jī)數(shù) */
for( i = 0; i < 10; i++ ){
// 生成實(shí)際的隨機(jī)數(shù)
j= rand();
cout <<"隨機(jī)數(shù): " << j << endl;
}
return 0;
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
隨機(jī)數(shù): 1748144778 隨機(jī)數(shù): 630873888 隨機(jī)數(shù): 2134540646 隨機(jī)數(shù): 219404170 隨機(jī)數(shù): 902129458 隨機(jī)數(shù): 920445370 隨機(jī)數(shù): 1319072661 隨機(jī)數(shù): 257938873 隨機(jī)數(shù): 1256201101 隨機(jī)數(shù): 580322989
更多建議: