项目描述 : 实现大数的读入,输出,实现大数的加,减,乘,除,取模和比较运算。
开发技术:string,C++类
项目特点: 1. 使用C++类将超过long long int所能存储的范围的数转换成字符串进行存储。
2. 将存储大数的字符串拆开进行分析,逐位进行运算储存。
3. 实现大数的读入,输出以及大数的加,减,乘,除,取模和比较运算。
4. 将大数问题分解成单个字符的相互运算。
开发技术:string,C++类
项目特点: 1. 使用C++类将超过long long int所能存储的范围的数转换成字符串进行存储。
2. 将存储大数的字符串拆开进行分析,逐位进行运算储存。
3. 实现大数的读入,输出以及大数的加,减,乘,除,取模和比较运算。
4. 将大数问题分解成单个字符的相互运算。
1.头文件:
#ifndef BIG_DATA_H
#define BIG_DATA_H
#include <string>
#include <iostream>
using namespace std;
#define UN_INIT 0xcccccccccccccccc
#define MAX_INT64 0x7fffffffffffffff
#define MIN_INT64 0x8000000000000000
typedef long long INT64;
class BigData
{
public:
BigData(INT64 data = UN_INIT);
BigData(const char *pData);
BigData operator+(BigData& bigData);
BigData operator-(const BigData& bigData);
BigData operator*(const BigData& bigData);
BigData operator/(const BigData& bigData);
BigData operator%(const BigData& bigData);
//=======================================
bool operator<(const BigData& bigData);
bool operator>(const BigData& bigData);
bool operator==(const BigData& bigData);
friend std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);
friend std::istream& operator>>(std::istream& _cin, BigData bigData);
bool IsINT64Overflow()const;
private:
std::string Add(std::string left, std::string right);
std::string Sub(std::string left, std::string right);
std::string Mul(std::string left, std::string right);
std::string Div(std::string left, std::string right);
void INT64ToString();
bool IsLeftStrBig(char *pLeft, size_t LSize, char *pRight, size_t RSize);
char SubLoop(char *&pLeft, int& LSize, char *pRight, size_t RSize);
private:
INT64 m_llValue;
std::string m_strData;
};
#endif
2.算法的实现
#include "BigData.h"
#include <cassert>
BigData::BigData(INT64 data)
: m_llValue(data)
, m_strData("")
{
INT64ToString();
}
int idx = 0;
BigData::BigData(const char *_pdata)
{
assert(_pdata);
char csymbol = _pdata[0];
char* pdata = (char*)_pdata;
if (csymbol == '+' || csymbol == '-')
{
pdata++;
}
else if (*pdata >= '0'&a