HugeInteger 加减乘法实现

举例:

HugeInt.h:

 

#ifndef HUGEINT_H
#define HUGEINT_H

#include <iostream>

using std::ostream;

class HugeInt {
	friend ostream &operator << ( ostream &, const HugeInt & );

public:
	HugeInt( long = 0 );
	HugeInt( constchar * );

	HugeInt operator + ( const HugeInt & );
	HugeInt operator + ( int );
	HugeInt operator + ( constchar * );

	HugeInt operator * ( int );
	HugeInt operator * ( const HugeInt & );

private:
	short integer[ 30 ];

};

#endif

 

HugeInt.cpp:

 

#include <cctype>
#include <cstring>

#include "hugeint.h"

HugeInt::HugeInt( longvalue )
{
	for ( int i = 0; i <= 29; i++ )
		integer[ i ] = 0;

	for ( int j = 29; value != 0 && j >= 0; j-- ){
		integer[ j ] = value % 10;
		value /= 10;

	}

}

HugeInt::HugeInt( constchar *string )
{
	for ( int i = 0; i <= 29; i++ )
		integer[ i ] = 0;

	int length = strlen( string );

	for ( int j = 30 - length, k = 0; j <= 29; j++, k++ )

		if ( isdigit( string[ k ] ) )
			integer[ j ] = string[ k ] - '0';

}

HugeInt HugeInt::operator + ( const HugeInt &op2 )
{
	HugeInt temp;
	int carry = 0;

	for ( int i = 29; i >= 0; i-- ){
		temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry;

		if ( temp.integer[ i ] > 9 ) {
			temp.integer[ i ] %= 10;
			carry = 1;

		}

		else
			carry = 0;

	}

	return temp;

}

HugeInt HugeInt::operator + ( int op2 )
{
	return *this + HugeInt( op2 );

}

HugeInt HugeInt::operator + ( constchar *op2 )
{
	return *this + HugeInt( op2 );

}

HugeInt HugeInt::operator * ( const HugeInt &mul )
{
	HugeInt temp;
	HugeInt tempT;
	HugeInt tempN;
	int carry = 0;
	int n = 0;

	for ( int i = 29; i >= 0; i-- ){
		
		for ( int t = 0; t <= 29; t++ ){
			tempT.integer[ t ] = 0;
			tempN.integer[ t ] = 0;

		}

		for ( int j = 29; j >= 0; j-- ){
			tempT.integer[ j ] = integer[ j ] * mul.integer[ i ] + carry;

			if ( tempT.integer[ j ] > 9 ){
				carry = tempT.integer[ j ] / 10;
				tempT.integer[ j ] %= 10;				

			}

 			else
				carry = 0;

		}
		
		for ( int y = 29; y >= 0; y-- )
			tempN.integer[ y - n ] = tempT.integer[ y ];

		++n;

		temp = temp + tempN;

	}

	return temp;

}

HugeInt HugeInt::operator * ( int mul )
{
	return *this * HugeInt( mul );

}

ostream &operator << ( ostream &output, const HugeInt &num )
{
	int i;

	for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ )
		;

	if ( i == 30 )
		output << 0;
	elsefor ( ; i <= 29; i++ )
			output << num.integer[ i ];

	return output;

}

 

main.cpp:

 

#include <iostream>

using std::cout;
using std::endl;

#include "hugeint1.h"

int main()
{
	HugeInt n1( 7654321 );
	HugeInt n2( 7891234 );
	HugeInt n3( "9999999999999999999999999" );
	HugeInt n4( "1" );
	HugeInt n5;
	HugeInt n6( 1234 );
	HugeInt n7( 3478 );

	cout << "n1 is " << n1 << "\nn2 is " << n2
		 << "\nn3 is " << n3 << "\nn4 is " << n4
		 << "\nn5 is " << n5 << "\nn6 is " << n6
		 << "\nn7 is " << n7 << "\n\n";

	n5 = n1 + n2;
	cout << n1 << " + " << n2 << " = " << n5 << "\n\n";

	cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 )
		 << "\n\n";

	n5 = n1 + 9;
	cout << n1 << " + " << 9 << " = " << n5 << "\n\n";

	n5 = n2 + "10000";
	cout << n2 << " + " << "10000" << " = " << n5 << endl;

	n5 = n7 * n6;
	cout << n7 << " * " << n6 << " = " << n5 << endl;

	return 0;

}

 

输出结果为:
{1586B489-88E0-4FE4-A107-B2A2CB761E60}0.jpg

posted on 2006-12-20 15:03 bullGao 阅读(3345) 评论(0)  编辑 收藏 引用 所属分类: C++ programming


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

留言簿(1)

随笔分类

随笔档案

收藏夹

搜索

最新评论