The best C ++ digital Tutorial In 2024, In this tutorial you can learn C ++ custom number,C ++ math,C ++ Random Number,
Usually, when we need to use numbers, we use the original data types, such as int, short, long, float, and double, and so on. These data types for numbers, its possible values and range of values, we have already discussed in C ++ data type chapter.
We have chapters in various instances before the defined number. Here is a C ++ define the various types of digital integrated example:
#include <iostream> using namespace std; int main () { // 数字定义 short s; int i; long l; float f; double d; // 数字赋值 s = 10; i = 1000; l = 1000000; f = 230.47; d = 30949.374; // 数字输出 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; }
When the above code is compiled and executed, it produces the following results:
short s :10 int i :1000 long l :1000000 float f :230.47 double d :30949.4
In C ++, you can create a variety of functions in addition, also contains a variety of useful functions for you to use. These functions are written in standard C and C ++ libraries, calledbuilt-in functions.You can refer to these functions in the program.
C ++ built a wealth of mathematical functions can be performed on a variety of digital computing. The following table lists the C ++ some useful built-in mathematical functions.
To use these functions, you need to refer to mathematic header<cmath>.
序号 | 函数 & 描述 |
---|---|
1 | double cos(double); 该函数返回弧度角(double 型)的余弦。 |
2 | double sin(double); 该函数返回弧度角(double 型)的正弦。 |
3 | double tan(double); 该函数返回弧度角(double 型)的正切。 |
4 | double log(double); 该函数返回参数的自然对数。 |
5 | double pow(double, double); 假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。 |
6 | double hypot(double, double); 该函数返回两个参数的平方总和的平方根,也就是说,参数为一个直角三角形的两个直角边,函数会返回斜边的长度。 |
7 | double sqrt(double); 该函数返回参数的平方根。 |
8 | int abs(int); 该函数返回整数的绝对值。 |
9 | double fabs(double); 该函数返回任意一个十进制数的绝对值。 |
10 | double floor(double); 该函数返回一个小于或等于传入参数的最大整数。 |
Here is a simple example of mathematical operations:
#include <iostream> #include <cmath> using namespace std; int main () { // 数字定义 short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // 数学运算 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; }
When the above code is compiled and executed, it produces the following results:
sign(d) :-0.634939 abs(i) :1000 floor(d) :200 sqrt(f) :15.1812 pow( d, 2 ) :40149.7
In many cases, we need to generate a random number. Random number generator, there are two related functions. One is therand (), the function only returns a pseudo-random number.You must callsrand () function before generating a random number.
Here is a simple example of generating random numbers. Examples of the use oftime () function to obtain the number of seconds the system time, function to generate a random number by calling the rand ():
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int i,j; // 设置种子 srand( (unsigned)time( NULL ) ); /* 生成 10 个随机数 */ for( i = 0; i < 10; i++ ) { // 生成实际的随机数 j= rand(); cout <<"随机数: " << j << endl; } return 0; }
When the above code is compiled and executed, it produces the following results:
随机数: 1748144778 随机数: 630873888 随机数: 2134540646 随机数: 219404170 随机数: 902129458 随机数: 920445370 随机数: 1319072661 随机数: 257938873 随机数: 1256201101 随机数: 580322989