C++项目之大数四则运算

本文详细探讨了如何使用C++编程语言实现大数的加减乘除四则运算。通过介绍大数存储方式、算法设计及具体代码实现,深入理解大数运算的原理和技巧。适合C++初学者和对大数运算感兴趣的开发者阅读。
摘要由CSDN通过智能技术生成
项目描述 : 实现大数的读入,输出,实现大数的加,减,乘,除,取模和比较运算。
开发技术: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
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值