C++ 数字

通常,当我们需要用到数字时,我们会使用原始的数据类型,如 int、short、long、float 和 double 等等。这些用于数字的数据类型,其可能的值和数值范围,我们已经在 C++ 数据类型一章中讨论过。

C++ 定义数字

我们已经在之前章节的各种实例中定义过数字。下面是一个 C++ 中定义各种类型数字的综合实例:

实例

#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;
}

当上面的代码被编译和执行时,它会产生下列结果:

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

C++ 数学运算

在 C++ 中,除了可以创建各种函数,还包含了各种有用的函数供您使用。这些函数写在标准 C 和 C++ 库中,叫做内置函数。您可以在程序中引用这些函数。

C++ 内置了丰富的数学函数,可对各种数字进行运算。下表列出了 C++ 中一些有用的内置的数学函数。

为了利用这些函数,您需要引用数学头文件 <cmath>

序号函数 & 描述
1double cos(double);
该函数返回弧度角(double 型)的余弦。
2double sin(double);
该函数返回弧度角(double 型)的正弦。
3double tan(double);
该函数返回弧度角(double 型)的正切。
4double log(double);
该函数返回参数的自然对数。
5double pow(double, double);
假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。
6double hypot(double, double);
该函数返回两个参数的平方总和的平方根,也就是说,参数为一个直角三角形的两个直角边,函数会返回斜边的长度。
7double sqrt(double);
该函数返回参数的平方根。
8int abs(int);
该函数返回整数的绝对值。
9double fabs(double);
该函数返回任意一个浮点数的绝对值。
10double floor(double);
该函数返回一个小于或等于传入参数的最大整数。

下面是一个关于数学运算的简单实例:

实例

#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;
}

当上面的代码被编译和执行时,它会产生下列结果:

sin(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;
 
   // 设置种子
   srand( (unsigned)time( NULL ) );
 
   /* 生成 10 个随机数 */
   for( i = 0; i < 10; i++ )
   {
      // 生成实际的随机数
      j= rand();
      cout <<"随机数: " << j << endl;
   }
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

随机数: 31748
随机数: 12517
随机数: 3799
随机数: 25651
随机数: 10167
随机数: 7889
随机数: 3904
随机数: 11441
随机数: 30803
随机数: 6397

使用随机数来发红包:

实例

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
    int i, number;
    int best;//手气最佳
    float total;

    cout << "请输入红包金额:";
    cin >> total;
    cout << "请输入抢红包人数:";
    cin >> number;
    /* 生成随机数 */
    // 设置种子
    srand((unsigned)time(NULL));
    float a[1024];//保存每个人的随机数。最多支持1024个人抢红包。
    float b[1024];//保存每个人获得的红包金额。
    float suma = 0;//随机数总和。
    float sumb = 0;//红包总和。
    int max = 0;
    for (i = 0; i < number; i++)
    {
        // 生成实际的随机数
        a[i] = rand() % 100;
                
        if (a[i] > max){
            max = a[i];
            best = i;//获取手气最佳
        }
        suma += a[i];
    }

    for (i = 0; i < number - 1; i++)
    {
        b[i] = a[i] / suma * total;//按照随机数计算每个人实际获得的金额
        sumb += round(b[i] * 100) / 100.0;//将红包金额保留两位小数
        //输出信息
        cout << "第" << setiosflags(ios::right)<< setw(3) << i + 1 << 
            "个人的红包是:" << setiosflags(ios::right) << setw(6) << 
            setiosflags(ios::fixed) << setprecision(2) << 
            round(b[i] * 100) / 100.0 ;
        if (best == i){
            cout << "(手气最佳)" << endl;
        }
        else {
            
            cout << endl;
        }
    }
    //最后一人的红包金额等于总金额减去前面的金额。
    cout << "第" << setiosflags(ios::right)<<
        setw(3) << number << "个人的红包是:" <<
        setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) <<
        setprecision(2) << round((total - sumb) * 100) / 100.0;
    if (best == number - 1){
        cout << "(手气最佳)" << endl;
    }
    else {

        cout << endl;
    }
    return 0;
}

结果:

请输入红包金额:200
请输入抢红包人数:10
第  1个人的红包是: 31.64
第  2个人的红包是: 18.51
第  3个人的红包是:  0.00
第  4个人的红包是:  8.36
第  5个人的红包是: 13.73
第  6个人的红包是: 49.55(手气最佳)
第  7个人的红包是:  0.00
第  8个人的红包是:  4.18
第  9个人的红包是: 33.43
第 10个人的红包是: 40.60