21xrx.com
2024-09-20 00:12:37 Friday
登录
文章检索 我的文章 写文章
C++乘法代码示例
2023-07-02 21:17:09 深夜i     --     --
C++ 乘法 代码示例

C++是一种通用的、面向对象的编程语言,使用广泛。在现代编程中,C++被广泛用于开发操作系统、游戏和其他计算机程序。其中,乘法是C++中最基础的运算之一,也是实现很多算法的核心之一。下面,我们来看一下C++中的乘法代码示例。

在C++中,实现两个数字相乘的方式有很多种。如果两个数字比较小,我们可以使用简单的乘法运算符“*”来实现。例如,如果我们要计算5和10的乘积,可以用以下代码实现:

int a = 5;

int b = 10;

int c = a * b;

cout << c << endl; //输出50

上面的代码中,我们首先定义了两个整型变量a和b,分别赋值为5和10,然后使用“*”运算符将它们相乘,并将结果赋给变量c。最后,我们通过cout语句将c的值输出到控制台。

但是,如果两个数字比较大,使用简单的乘法会导致溢出。在这种情况下,我们可以使用C++提供的高精度计算库来实现乘法。以下是一个使用高精度库的C++代码示例:

#include

#include

#include

using namespace std;

const int MAXN=10010;

struct bign{

  int d[MAXN], len;

  void clean() { while(len > 1 && !d[len-1]) len--; }

  bign()    { memset(d, 0, sizeof(d)); len = 1; }

  bool operator < (const bign& b) const{

    if(len != b.len) return len < b.len;

    for(int i = len-1; i >= 0; i--)

      if(d[i] != b.d[i])

        return d[i] < b.d[i];

    return false;

  }

  bool operator > (const bign& b) const{return b < *this;}

  bool operator <= (const bign& b) const{return !(b < *this);}

  bool operator >= (const bign& b) const{return !(*this < b);}

  bool operator != (const bign& b) const{return b < *this || *this < b;}

  bool operator == (const bign& b) const{return !(b < *this) && !(b > *this);}

  bign operator + (const bign& b) const{

    bign c = b; int i;

    for(i = 0; i < len || i < b.len; i++) {

      c.d[i] += d[i];

      if(c.d[i] > 9) c.d[i+1]++, c.d[i]-=10;

    }

    c.len = i+1; c.clean();

    return c;

  }

  bign operator - (const bign& b) const{

    bign c = *this; int i;

    for(i = 0; i < b.len; i++) {

      c.d[i] -= b.d[i];

      if(c.d[i] < 0) c.d[i+1]--, c.d[i] += 10;

    }

    c.clean();

    return c;

  }

  bign operator * (const bign& b) const{

    int i, j; bign c; c.len = len + b.len;

    for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)

      c.d[i+j] += d[i] * b.d[j];

    for(i = 0; i < c.len-1; i++)

      c.d[i+1] += c.d[i]/10, c.d[i] %= 10;

    c.clean();

    return c;

  }

  bign operator / (const bign& b) const{

    int i, j;

    bign c = *this, a = 0;

    for(i = len - 1; i >= 0; i--)

    {

      a = a*10 + d[i];

      for(j = 0; j < 10; j++) if(a < b*(j+1)) break;

      c.d[i] = j;

      a = a - b*j;

    }

    c.clean();

    return c;

  }

  bign operator % (const bign& b) const{

    int i, j;

    bign a = 0;

    for(i = len - 1; i >= 0; i--)

    {

      a = a*10 + d[i];

      for(j = 0; j < 10; j++) if(a < b*(j+1)) break;

      a = a - b*j;

    }

    return a;

  }

  bign operator += (const bign& b){

    *this = *this + b;

    return *this;

  }

  bign operator -= (const bign& b){

    *this = *this - b;

    return *this;

  }

  bign operator *= (const bign& b){

    *this = *this * b;

    return *this;

  }

  bign operator /= (const bign& b){

    *this = *this / b;

    return *this;

  }

  bign operator %= (const bign& b){

    *this = *this % b;

    return *this;

  }

  bool operator ! () const{

    return len == 1 && !d[0];

  }

};

ostream& operator << (ostream& out, const bign& x){

  out << x.d[x.len-1];

  for(int i = x.len - 2; i >= 0; i--) {

    char buf[10];

    sprintf(buf, "%d", x.d[i]);

    for(int j = 0; j < strlen(buf); j++) out << buf[j];

  }

  return out;

}

istream& operator >> (istream& in, bign& x){

  char buf[MAXN*4], ch = getchar();

  int k = 0;

  while(ch < '0' || ch > '9') ch = getchar();

  while(ch >= '0' && ch <= '9') buf[k++] = ch - '0', ch = getchar();

  for(int i = k-1; i >= 0; i-=4) {

    x = x*10000 + buf[i];

    if(i-1 >= 0) x = x*1000 + buf[i-1];

    if(i-2 >= 0) x = x*100 + buf[i-2];

    if(i-3 >= 0) x = x*10 + buf[i-3];

  }

  return in;

}

bign a, b, c;

int main(){

  cin >> a;

  cin >> b;

  c = a * b;

  cout << c << endl;

  return 0;

}

上面的代码中,我们首先定义了一个结构体bign,用来保存高精度的整数。bign结构体中包含了一些运算符重载,分别用于实现高精度的加、减、乘、除和取模操作。另外,我们还重载了输入输出流运算符,用于输入输出高精度的整数。

在主函数中,我们首先从控制台输入两个整数a和b,然后使用乘法运算符“*”将它们相乘,并将结果赋给变量c。最后,我们通过cout语句将c的值输出到控制台。

总之,C++提供了多种方式来实现乘法运算,开发者可以根据具体需求选择适合自己的实现方式。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复