大整数加减乘除及取模运算 C++实现

最近事情实在是太多了,一直没有时间写博客,昨天刚刚忙完硕士开题,虽然还是有很多的事情要做,

但实在是不想弄了。想起一个月之前就已经写完的一个大整数加减乘除法的程序,今天把它贴出来和大家分享,

目前我只是做了简单的测试,可能程序中还存在不好的bug,也希望大家能一块帮着指出其中的错误!!

希望和大家一块学习共勉 .....

工程下载地址(也可以留下邮箱,我会将工程发过去):http://download.csdn.net/detail/doufei_ccst/6693721

首先是实现大整数的头文件:BitInt.h

#ifndef		DOUFEI_BIGINT_HEADER
#define	DOUFEI_BIGINT_HEADER
#include <string>
#include <iostream>

class CBigInt
{
public:
	explicit CBigInt() ; 
	CBigInt(const int i) ; 
	CBigInt(const std :: string& strValues) ;
	CBigInt(const CBigInt& bigInt) ; //复制构造函数
	~CBigInt() ;

public:
	bool inline isPositive() {return flag ; };
	int compareBitInt(const CBigInt& rhs)const ; //比较两个数的大小
	CBigInt& operator = (const CBigInt& rhs) ; //赋值操作符重载
	friend std :: ostream& operator << (std :: ostream& ou, const CBigInt& bigInt) ; //重载输出操作符
	friend std :: istream& operator >> (std :: istream& in, CBigInt& bigInt) ; //输入操作符的重载
	friend const CBigInt operator + (const CBigInt& lhs, const CBigInt& rhs ) ; //加法操作重载
	friend const CBigInt operator - (const CBigInt& lhs, const CBigInt& rhs) ; //减法操作符重载
	friend const CBigInt operator * (const CBigInt& lhs, const CBigInt& rhs) ; //乘法操作符重载
	friend const CBigInt operator / (const CBigInt& lhs, const CBigInt& rhs) ; //除法操作重载
	friend const CBigInt operator % (const CBigInt& lhs, const CBigInt& rhs) ; //取模运算操作符重载


	void setValue(const std :: string& strValues) ; //根据字符串设置数值
	const CBigInt absolute()const ; //绝对值


private:
	std :: string values ; //保存所有位上的数字
	bool flag ; //true表示正数,false表示负数,0默认为正数
};

std :: ostream& operator << (std :: ostream& ou, const CBigInt& bigInt) ;
std :: istream& operator >> (std :: istream& in, CBigInt& bigInt) ;
const CBigInt operator + (const CBigInt& lhs, const CBigInt& rhs ) ; //加法操作符重载
const CBigInt operator - (const CBigInt& lhs, const CBigInt& rhs) ; //剑法操作符重载
const CBigInt operator * (const CBigInt& lhs, const CBigInt& rhs) ; //乘法操作符重载
const CBigInt operator / (const CBigInt& lhs, const CBigInt& rhs) ; //除法操作重载
const CBigInt operator % (const CBigInt& lhs, const CBigInt& rhs) ; //取模运算操作符重载

#endif


BitInt.cpp

#include "BigInt.h"
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <time.h>
#include <casse
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值