
- C++基礎
- C++ 首頁
- C++ 概述
- C++ 環境搭建
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期與時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
- C++ 檔案處理
- C++ 檔案和流
- C++ 從檔案中讀取
C++中的數字
通常,當我們使用數字時,我們使用諸如 int、short、long、float 和 double 等原始資料型別。在討論 C++ 資料型別時,已經解釋了數字資料型別、它們可能的值和數字範圍。
在 C++ 中定義數字
您已經在前面章節給出的各種示例中定義了數字。這是一個定義 C++ 中各種型別數字的另一個綜合示例:
#include <iostream> using namespace std; int main () { // number definition: short s; int i; 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 << "int i :" << i << endl; cout << "long l :" << l << endl; cout << "float f :" << f << endl; cout << "double d :" << d << endl; return 0; }
編譯並執行上述程式碼時,將產生以下結果:
short s :10 int i :1000 long l :1000000 float f :230.47 double d :30949.4
C++ 中的數學運算
除了您可以建立的各種函式之外,C++ 還包含一些您可以使用的有用函式。這些函式在標準 C 和 C++ 庫中可用,稱為**內建**函式。這些是可以包含在您的程式中然後使用的函式。
C++ 有一套豐富的數學運算,可以對各種數字執行這些運算。下表列出了一些 C++ 中可用的有用的內建數學函式。
要使用這些函式,您需要包含數學標頭檔案**`
序號 | 函式及用途 |
---|---|
1 | double cos(double); 此函式接受一個角度(作為雙精度數)並返回餘弦值。 |
2 | double sin(double); 此函式接受一個角度(作為雙精度數)並返回正弦值。 |
3 | double tan(double); 此函式接受一個角度(作為雙精度數)並返回正切值。 |
4 | double log(double); 此函式接受一個數字並返回該數字的自然對數。 |
5 | double pow(double, double); 第一個是您要提升的數字,第二個是您要提升它的冪 |
6 | double hypot(double, double); 如果您將直角三角形的兩條邊的長度傳遞給此函式,它將返回斜邊的長度。 |
7 | double sqrt(double); 您將一個數字傳遞給此函式,它會為您提供平方根。 |
8 | int abs(int); 此函式返回傳遞給它的整數的絕對值。 |
9 | double fabs(double); 此函式返回傳遞給它的任何十進位制數的絕對值。 |
10 | double floor(double); 查詢小於或等於傳遞給它的引數的整數。 |
以下是一個簡單的示例,用於顯示一些數學運算:
#include <iostream> #include <cmath> using namespace std; int main () { // number definition: short s = 10; int i = -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; }
編譯並執行上述程式碼時,將產生以下結果:
sign(d) :-0.634939 abs(i) :1000 floor(d) :200 sqrt(f) :15.1812 pow( d, 2 ) :40149.7
C++ 中的隨機數
在許多情況下,您需要生成一個隨機數。實際上,您需要了解兩個關於隨機數生成的函式。第一個是`rand()`,此函式只返回一個偽隨機數。解決此問題的方法是首先呼叫`srand()`函式。
以下是一個生成一些隨機數的簡單示例。此示例使用`time()`函式獲取系統時間中的秒數,以隨機播種`rand()`函式:
#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; }
編譯並執行上述程式碼時,將產生以下結果:
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