类的应用实例:高精度加减

高精度加减并不难,但是套到类上就level up了
很久没有贴完整代码了,然而感觉这道题可以充分体现:

  • 构造函数的重载
  • 构造函数的巧妙应用
  • 将对象作为返回类型的函数
  • 将对象作为传入参数的函数
  • 理解:类内部可以自由调用私有数据成员(无论是当前对象的私有数据成员,还是成员函数内定义的新对象的私有数据成员)
  • 对拷贝构造函数的初步理解
  • 对耐心与毅力的严峻考验
  • 程序姬的基本职业素养
  • 。。。

总的来说这是一道很腻害很全面的题,就放在这里让大家一起挑错吧~

//HugeInteger.h
#ifndef HUGEINTEGER_H 
#define HUGEINTEGER_H
class HugeInteger { 
public: 
    HugeInteger(int =0); 
    HugeInteger(const char*); 
   
    HugeInteger add(const HugeInteger &); 
    HugeInteger add(int); 
    HugeInteger add(const char*); 
    
    HugeInteger subtract(const HugeInteger &); 
    HugeInteger subtract(int); 
    HugeInteger subtract(const char *); 

    bool isEqualTo(const HugeInteger &); 
    bool isNotEqualTo(const HugeInteger &); 
    bool isGreaterThan(const HugeInteger &);  
    bool isLessThan(const HugeInteger &); 
    bool isGreaterThanOrEqualTo(const HugeInteger &); 
    bool isLessThanOrEqualTo(const HugeInteger &); 
    bool isZero();

    void input(const char *);
    void output();
private: 
    int integer[40]; 
}; 
#endif
//HugeInteger.cpp
#include<iostream>
#include"HugeInteger.h"

using namespace std;

HugeInteger::HugeInteger(int value) {
	int cnt=0;
	while (value) {
		cnt++;
		integer[cnt]=value%10;
		value/=10;
	}
	integer[0]=cnt;             //存储位数
	while (cnt<39) {            //初始化
		cnt++;
		integer[cnt]=0;
	}
}
   
HugeInteger::HugeInteger(const char* s) {     //逆序存储
	int l=strlen(s),cnt=0;
	integer[0]=l;
	while (cnt<strlen(s)) {
		cnt++;
		integer[l-cnt+1]=s[cnt-1]-'0';
	}
	while (cnt<39) {
		cnt++;
		integer[cnt]=0;
	}
}
   
HugeInteger HugeInteger::add(const HugeInteger &addValue) {     
	int l=max(integer[0],addValue.integer[0]);
	HugeInteger ans(0);
	for (int i=1;i<=l;i++) {
		ans.integer[i]+=integer[i]+addValue.integer[i];   //累加
		ans.integer[i+1]+=ans.integer[i]/10;
		ans.integer[i]%=10;
	}
	ans.integer[0]=l;
	if (ans.integer[l+1]!=0) ans.integer[0]++;
	return ans;
} 
    
HugeInteger HugeInteger::add(int addValue) {
	return add(HugeInteger(addValue));
}

HugeInteger HugeInteger::add(const char* addValue) {
	return add(HugeInteger(addValue));
}
    
HugeInteger HugeInteger::subtract(const HugeInteger &subValue) {
	int l=max(integer[0],subValue.integer[0]);
	int tmp[40];
	for (int i=0;i<40;i++) tmp[i]=integer[i];
	HugeInteger ans(0);
	for (int i=1;i<=l;i++) {
		while (tmp[i]<subValue.integer[i]) {
			tmp[i+1]--;
			tmp[i]+=10;
		}
		ans.integer[i]=tmp[i]-subValue.integer[i];
	}
	ans.integer[0]=l;
	while (!ans.integer[l]) l--;
	return ans;
}
    
HugeInteger HugeInteger::subtract(int minusValue) {
	return subtract(HugeInteger(minusValue));
} 

HugeInteger HugeInteger::subtract(const char* minusValue) {
	return subtract(HugeInteger(minusValue));
}

bool HugeInteger::isEqualTo(const HugeInteger &value) {
	if (integer[0]!=value.integer[0]) return 0;
	for (int i=1;i<=integer[0];i++) 
		if (integer[i]!=value.integer[i]) return 0;
	return 1;
}
    
bool HugeInteger::isNotEqualTo(const HugeInteger &value) {
	return !isEqualTo(value);
} 

bool HugeInteger::isGreaterThan(const HugeInteger &value) {
	if (integer[0]<value.integer[0]) return 0;
	if (integer[0]>value.integer[0]) return 1;
	for (int i=integer[0];i>=1;i--)
		if (integer[i]>value.integer[i]) return 1;
	return 0;
}  

bool HugeInteger::isLessThan(const HugeInteger &value) {
	if (integer[0]<value.integer[0]) return 1;
	if (integer[0]>value.integer[0]) return 0;
	for (int i=integer[0];i>=1;i--)
		if (integer[i]<value.integer[i]) return 1;
	return 0;
}

bool HugeInteger::isGreaterThanOrEqualTo(const HugeInteger &value) {
	return ((isGreaterThan(value))||(isEqualTo(value)));
} 
    
bool HugeInteger::isLessThanOrEqualTo(const HugeInteger &value) {
	return ((isLessThan(value))||(isEqualTo(value)));
} 

bool HugeInteger::isZero() {
	return (integer[0]==0)? 1:0;
}

void HugeInteger::input(const char* s) {
	int l=strlen(s),cnt=0;
	integer[0]=l;
	while (cnt<strlen(s)) {
		cnt++;
		integer[l-cnt+1]=s[cnt-1]-'0';
	}
	while (cnt<39) {
		cnt++;
		integer[cnt]=0;
	}
}

void HugeInteger::output() {
	if (integer[0]==0) cout<<0;
	else {for (int i=integer[0];i>=1;i--) cout<<integer[i];}
}
//HugeIntegerTest.cpp
#include<iostream>
#include"HugeInteger.h"

using namespace std;

int main()
{
	int a,b;
	char c[41];

	cout<<"Please input the first integer: "; cin>>a;
	cout<<"Please input the second integer: "; cin>>b;
	cout<<"Please input the string: "; cin>>c;

    HugeInteger A(a), B(b), C(c), D(0);

	A.output(); cout<<" + "; C.output(); cout<<" = "; A.add(C).output(); cout<<endl;
    A.output(); cout<<" - "; C.output(); cout<<" = "; A.subtract(C).output(); cout<<endl;

    A.output(); cout<<" is "<<(A.isEqualTo(HugeInteger(a)) ? "" : "not ")<<"equal to "<<a<<".\n";
	A.output(); cout<<" is "<<(A.isEqualTo(B) ? "" : "not ")<<"equal to "; B.output(); cout<<".\n";
    B.output(); cout<<" is "<<(B.isGreaterThan(A) ? "" : "not ")<<"greater than "; A.output(); cout<<".\n";
    C.output(); cout<<" is "<<(C.isLessThan(B) ? "" : "not ")<<"less than "; B.output(); cout<<".\n";
    C.output(); cout<<" is "<<(C.isLessThanOrEqualTo(HugeInteger(c)) ? "" : "not ")<<"less than or euqal to "<<c<<".\n";
    D.output(); cout<<" is "<<(D.isGreaterThanOrEqualTo(HugeInteger(0)) ? "" : "not ")<<"greater than or equal to 0.\n";
    cout<< "D is " <<(D.isZero() ? "" : "not ")<<"zero.\n";
	
	system("pause");
    return 0;
}
发布了964 篇原创文章 · 获赞 235 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/wu_tongtong/article/details/104660712