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
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值