C++ 高精度

高精度加法

#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<cstring>
#include<algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
  char a1[100], b1[100];
  int a[100], b[100], c[100];
  int lena1, lenb1;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  gets(a1);
  gets(b1);
  //scanf("%s%s", &a1, &b1);
  lena1 = strlen(a1);
  lenb1 = strlen(b1);
  for(int i = 0; i < lena1; ++i)    //将char转化成int,数组倒置
    a[lena1-i-1] = (int)a1[i] - '0';
  for(int i = 0; i < lenb1; ++i)
    b[lenb1-i-1] = (int)b1[i] - '0';
  int lenc = 0, x = 0;    // x记录进位
  while(lenc <= lena1 || lenc <= lenb1)
  {
    c[lenc] = a[lenc] + b[lenc] + x;
    x = c[lenc]/10;
    c[lenc] %= 10;
    lenc++;
  }
  c[lenc] = x;
  if(c[lenc] == 0)
  {
    lenc--;    //判断前导0
  }
  for(int i = lenc-1; i >= 0; --i)
    printf("%d",c[i]);
  return 0;
}

高精度减法

#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<cstring>
#include<algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
  char a1[100], b1[100], n[100];  //借助交换数组
  int a[100], b[100], c[100];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  gets(a1);    //被减数
  gets(b1);    //减数
  if(strlen(a1) < strlen(b1) || strlen(a1) == strlen(b1) && (strcmp(a1, b1) < 0))
  {
    strcpy(n,a1);
    strcpy(a1,b1);
    strcpy(b1,n);
    printf("-");
  }
  int lena1 = strlen(a1);
  int lenb1 = strlen(b1);
  for(int i = 0; i < lena1; ++i)
    a[lena1-i-1] = (int)a1[i] - '0';
  for(int i = 0; i < lenb1; ++i)
    b[lenb1-i-1] = (int)b1[i] - '0';
  int i = 0;
  while(i < lena1)
  {
    if(a[i] < b[i])
    {
      a[i] += 10;    //不够减,借位
      a[i+1]--;        //上一位减一
    }
    c[i] = a[i] - b[i];    //对应位相减
    i++;
  }
  int lenc = i;
  for(int i = lenc; i >= 0; --i)
  {
    if((c[i] == 0) && (lenc > 0))
      lenc--;
    else
      break;
  }
  for(int i = lenc; i >= 0; --i)
    printf("%d",c[i]);

  return 0;
}

高精度乘法

#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<cstring>
#include<algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
  char a1[100], b1[100];
  int a[100], b[100], c[100];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  gets(a1);
  gets(b1);
  int lena1 = strlen(a1);
  int lenb1 = strlen(b1);
  int len = lena1 + lenb1;
  for(int i = 0; i < lena1; ++i)
    a[lena1-i-1] = (int)a1[i] - '0';
  for(int i = 0; i < lenb1; ++i)
    b[lenb1-i-1] = (int)b1[i] - '0';
  for(int i = 0; i < lena1; ++i)
  {
    for(int j = 0 ; j < lenb1; ++j)
    {
      c[i+j] += a[i] * b[j];
      c[i+j+1] = (c[i+j]/10);    //进位
      c[i+j] %= 10;
    }
  }
  if(c[len-1] == 0)    //第一个数位是否为0
    len--;
  for(int i = len-1; i >= 0; --i)
    printf("%d",c[i]);
  return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

窗外藏深海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值