C++: 高精度加法与高精度减法

C++: 高精度加法与高精度减法

标签:C++ 高精度加法 高精度减法

by 小威威


在学C语言的时候,我有实现过高精度乘法(想了解的戳它)。
其实高精度加法、减法比乘法简单多了,所以可以通过高精度乘法这篇文章来入门高精度的算法。

现在是用C++实现,显然有了string类,一切的操作都变的简单多了。

题目如下:(Author: 欧文杰(TA))
This is an easy big integer. Your task is to finish the class big integer:

Tips:

size_ means the size of the big integer

friend function should have been declared outside;

when you declare the friend function, you should forward declare the class.

when you cannot compile, try to read the informations or google it.

overload “<<” : please output like this format: “123456”, no wrap.

overload “-“: we promise that a >= b when a - b .

其中,main.cppBigInteger.h已给出。

\\ main.cpp
#include "BigInteger.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s1, s2, s3, s4;
  cin >> s1 >> s2;
  cin >> s4;
  BigInteger a(s1);
  BigInteger b(s2);
  BigInteger c(a);
  BigInteger d(s4);
  BigInteger sum(a+b);
  BigInteger diff(c-d);
  cout << sum << endl;
  cout << diff << endl;
  return 0;
}

\\ BigInteger.h
#ifndef _BIG_INTEGER_
#define _BIG_INTEGER_

#include <iostream>
#include <string>
using std::string;
using std::ostream;

// forward declaration
class BigInteger;

// operators overloading
BigInteger operator+(const BigInteger& left, const BigInteger& right);
BigInteger operator-(const
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是同时实现高精度加法和减法的C++代码: ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; // 高精度加法 string add(string num1, string num2) { // 先将两个数的位数补齐 int len1 = num1.length(), len2 = num2.length(); if (len1 < len2) { num1 = string(len2 - len1, '0') + num1; len1 = len2; } else { num2 = string(len1 - len2, '0') + num2; len2 = len1; } // 从低位到高位逐位相加 string result = ""; int carry = 0; for (int i = len1 - 1; i >= 0; i--) { int sum = num1[i] - '0' + num2[i] - '0' + carry; carry = sum / 10; result += to_string(sum % 10); } // 如果还有进位,需要在高位加上进位的1 if (carry == 1) { result += "1"; } // 将结果翻转过来 reverse(result.begin(), result.end()); // 去掉前导0 int pos = 0; while (pos < result.length() && result[pos] == '0') { pos++; } result = result.substr(pos); if (result.empty()) { result = "0"; } return result; } // 高精度减法 string subtract(string num1, string num2) { // 先将两个数的位数补齐 int len1 = num1.length(), len2 = num2.length(); if (len1 < len2) { num1 = string(len2 - len1, '0') + num1; len1 = len2; } else { num2 = string(len1 - len2, '0') + num2; len2 = len1; } // 从低位到高位逐位相减 string result = ""; int borrow = 0; for (int i = len1 - 1; i >= 0; i--) { int diff = num1[i] - num2[i] - borrow; if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } result += to_string(diff); } // 去掉前导0 reverse(result.begin(), result.end()); int pos = 0; while (pos < len1 && result[pos] == '0') { pos++; } result = result.substr(pos); if (result.empty()) { result = "0"; } // 判断结果是否为负数 if (borrow == 1) { result = "-" + result; } return result; } int main() { string num1, num2, op; cin >> num1 >> op >> num2; // 根据运算符进行相应的运算 if (op == "+") { cout << add(num1, num2) << endl; } else if (op == "-") { cout << subtract(num1, num2) << endl; } else { cout << "Invalid operator!" << endl; } return 0; } ``` 这里同时实现了高精度加法函数 `add` 和高精度减法函数 `subtract`。在输入部分和之前一样,使用了 `cin` 来读取输入的两个数字和运算符。然后根据运算符进行相应的运算,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值