类和运算符重载的完美结合:高精度四则运算

//HugeInt.h
#ifndef HUGEINT_H
#define HUGEINT_H

#include<iostream>
using std::ostream;

class HugeInt
{
	friend ostream &operator<<( ostream &output, const HugeInt &A) {
		for (int i=A.getLength();i>=0;i--) output<<A.integer[i];
		return output;
	}
public:
    HugeInt(int =0); // conversion/default constructor
    HugeInt(const char *); // conversion constructor
    // addition operator; HugeInt + HugeInt
    HugeInt operator+(const HugeInt &) const;
    // addition operator; HugeInt + int
    HugeInt operator+(int) const; 
    // addition operator; 
    // HugeInt + string that represents large integer value
    HugeInt operator+(const char *) const;
    bool operator==(const HugeInt &) const; // equality operator
    bool operator!=(const HugeInt &) const; // inequality operator
    bool operator<(const HugeInt &) const; // less than operator
    // less than or equal to operator
    bool operator<=(const HugeInt &) const; 
    bool operator>(const HugeInt &) const; // greater than operator
    // greater than or equal to operator
    bool operator>=(const HugeInt &) const;
    HugeInt operator-(const HugeInt &) const; // subtraction operator
    HugeInt operator*(const HugeInt &) const; // multiply two HugeInts
    HugeInt operator/(const HugeInt &) const; // divide two HugeInts
    int getLength() const;
private:
    int integer[30];
}; // end class HugeInt

#endif
//HugeInt.cpp
#include<iostream>
#include"HugeInt.h"
using namespace std;

HugeInt::HugeInt(int number) {
	int cnt=0;
	while (number!=0) {
		integer[cnt++]=number%10;
		number/=10;
	}
	while (cnt<30) integer[cnt++]=0;
}

HugeInt::HugeInt(const char *s) {
	int len=strlen(s);
	int cnt=0;
	while (cnt<len) integer[cnt++]=s[len-cnt-1]-'0';
	while (cnt<30) integer[cnt++]=0;
}
 
HugeInt HugeInt::operator+(const HugeInt &A) const {
	int len=max(this->getLength(),A.getLength());
	HugeInt ans;
	for (int i=0;i<=len;i++) {
		ans.integer[i]+=integer[i]+A.integer[i];
		ans.integer[i+1]+=ans.integer[i]/10;
		ans.integer[i]%=10;
	}
	return ans;
}

HugeInt HugeInt::operator+(int number) const {
	return (*this)+HugeInt(number);
}

HugeInt HugeInt::operator+(const char *s) const {
	return (*this)+HugeInt(s);
}
    
bool HugeInt::operator==(const HugeInt &A) const {
	for (int i=0;i<30;i++) 
		if (integer[i]!=A.integer[i]) return 0;
	return 1;
}

bool HugeInt::operator!=(const HugeInt &A) const {
	return !((*this)==A);
}
    
bool HugeInt::operator<(const HugeInt &A) const {
	for (int i=30;i>=0;i--) 
		if (integer[i]<A.integer[i]) return 1;             ///
	    else if (integer[i]>A.integer[i]) return 0;
	return 0;
}
    
bool HugeInt::operator<=(const HugeInt &A) const {
	return ((*this)<A)||((*this)==A);
}
    
bool HugeInt::operator>(const HugeInt &A) const {
	for (int i=30;i>=0;i--) 
		if (integer[i]>A.integer[i]) return 1;               ///
		else if (integer[i]<A.integer[i]) return 0;
	return 0;
}

bool HugeInt::operator>=(const HugeInt &A) const {
	return ((*this)>A)||((*this)==A);
}
    
HugeInt HugeInt::operator-(const HugeInt &A) const {
	HugeInt ans;
	HugeInt tmp=(*this);
	for (int i=0;i<30;i++) {
		while (tmp.integer[i]<A.integer[i]) {tmp.integer[i+1]--;tmp.integer[i]+=10;}
		ans.integer[i]=tmp.integer[i]-A.integer[i];
	}
	return ans;
}

HugeInt HugeInt::operator*(const HugeInt &A) const {
	if ((*this)==HugeInt(0)||A==HugeInt(0)) return HugeInt(0); 
	HugeInt ans;
	int len=A.getLength();
	for (int i=len;i>=0;i--) {
		for (int j=ans.getLength();j>=0;j--) ans.integer[j+1]=ans.integer[j]; ans.integer[0]=0;
		HugeInt tmp=(*this);
		int t=A.integer[i],o=0;
		for (int j=0;j<=getLength();j++) {
			tmp.integer[j]*=t;
			tmp.integer[j]+=o;
			o=tmp.integer[j]/10;
			tmp.integer[j]%=10;
		}
		t=tmp.getLength();
		while (tmp.integer[t]>=10) {
			tmp.integer[t+1]=tmp.integer[t]/10;
			tmp.integer[t]%=10;
			t++;
		}
		ans=ans+tmp;
	}
	return ans;
}
    
HugeInt HugeInt::operator/(const HugeInt &A) const {
	if ((*this)<A) return HugeInt(0);
	HugeInt ans;
	HugeInt tmp;
	for (int i=getLength();i>=0;i--) {
		for (int j=tmp.getLength();j>=0;j--) tmp.integer[j+1]=tmp.integer[j]; tmp.integer[0]=0;
		for (int j=ans.getLength();j>=0;j--) ans.integer[j+1]=ans.integer[j]; ans.integer[0]=0;
		tmp=tmp+integer[i];
		while (tmp>=A) {
			tmp=tmp-A;
			ans=ans+1;
		}
	}
	return ans;
}
    
int HugeInt::getLength() const {
	int cnt=29;
	while (!integer[cnt]&&cnt>0) cnt--;
	return cnt;
}
//HugeIntTest.cpp
#include<iostream>
#include"HugeInt.h"

using namespace std;

int main()
{
	HugeInt n1(7654321),n2(7891234);
	HugeInt n3("9999999999999999999999999");
	HugeInt n4(1),n5(12341234),n6(7888);
	cout<<"n1 is "<<n1<<endl;
	cout<<"n2 is "<<n2<<endl;
	cout<<"n3 is "<<n3<<endl;
	cout<<"n4 is "<<n4<<endl;
	cout<<"n5 is "<<n5<<endl;
	cout<<"n6 is "<<n6<<endl;

	cout<<endl;

	cout<<"n1 is"<<(n1==n2 ? " ":" not ")<<"equal to n2\n";
	cout<<"n1 is"<<(n1<n2 ? " ":" not ")<<"less than n2\n";
	cout<<"n1 is"<<(n1<=n2 ? " ":" not ")<<"less than or equal to n2\n\n";
	cout<<n1<<" + "<<n1<<" = "<<n1+n2<<endl;

	cout<<endl;

	cout<<n3<<" + "<<n4<<" = "<<n3+n4<<endl;

	cout<<endl;

	cout<<n1<<" + 9 = "<<n1+9<<endl;
	cout<<n2<<" + 10000 = "<<n2+10000<<endl;
	cout<<n5<<" * "<<n6<<" = "<<n5*n6<<endl;
	cout<<n5<<" - "<<n6<<" = "<<n5-n6<<endl;
	cout<<n5<<" / "<<n6<<" = "<<n5/n6<<endl;

	system("pause");
	return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值