大数据的运算加减乘除

BigData.h:

#ifndef __BIG_DATA_H__
#define __BIG_DATA_H__

#include <assert.h>
#include <string>


typedef long long INT64;

//#define MININT64 -9223372036854775808 // 编译器检查有错
//
//#define MAXINT64 +9223372036854775807 

const INT64 MININT64 = +9223372036854775807 + 1;
const INT64 MAXINT64 = +9223372036854775807;

class BigData
{
	friend std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);
public:
	BigData(INT64 value = 0xCCCCCCCCCCCCCCCC);
	BigData(const char* str);
	BigData operator+(const BigData& bigData);
	BigData operator-(const BigData& bigData);
	BigData operator*(const BigData& bigdata);
	BigData operator/(const BigData& bigdata);
protected:
	void _Int64ToStr();
	bool _IsINT64OverFlow()const;
	std::string AddStr(const BigData& bigData)const;
	std::string SubStr(const BigData& bigData)const;
	std::string MulStr(const BigData& bigData)const;
	std::string DivStr(const BigData& bigData)const;
	bool IsLeftStrBig(const char* pLeft, int iLSize, const char* pRight, int iRSize)const;
	char LoopSub(char* pLeft, int iLSize, const char* pRight, int iRSize)const;
private:
	INT64 _value;
	std::string _pData;
}; 

std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);

#endif /*__BIG_DATA_H__*/

BigData.cpp:

#define _CRT_SECURE_NO_WARNINGS 1

#include "BigData.h"

BigData::BigData(INT64 value) :_value(value)
{
	this->_Int64ToStr();
}

BigData::BigData(const char* str) : _value(0)
{
	assert(str);
	char* temp = (char*)str;
	char cSign = '+';
	while (isspace(*temp)) //*temp == ' '不能消除tab
	{
		++temp;
	}
	if ((*temp == '-') || (*temp == '+'))
	{
		cSign = *temp;
		++temp;
	}
	this->_pData.resize(strlen(str) + 1);
	this->_pData[0] = cSign;
	int iCount = 1;
	while (*temp == '0')//处理000123
	{
		++temp;
	}
	while ((*temp >= '0') && (*temp <= '9'))
	{
		this->_value = this->_value * 10 + (*temp - '0');
		this->_pData[iCount++] = *temp++;
	}
	this->_pData.resize(iCount);
	if (cSign == '-')
	{
		this->_value = 0 - this->_value;
	}
	if (!this->_IsINT64OverFlow())//溢出
	{
		this->_value = 0xCCCCCCCCCCCCCCCC;
	}
}/*BigData(const char* str)*/

BigData BigData::operator+(const BigData& bigData)
{
	if (this->_IsINT64OverFlow() && bigData._IsINT64OverFlow()) //两个均没有溢出
	{
		if (this->_pData[0] != bigData._pData[0])//异号,相加肯定没有溢
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值