So ive been trying to figure this out on my own and searching the books and the net and still no success. Can anyone give me some input with this thing.

#pragma once
#include<iostream>
#include<cstring>
using namespace std;

class hugeint{

	friend ostream &operator<<(ostream &, const hugeint &);

public:

	hugeint(long = 0);
	hugeint(const char *);

	hugeint operator+(const hugeint &);
	hugeint operator+(int);
	hugeint operator+(const char *);

	hugeint operator*(const hugeint &);
	hugeint operator*(const char *);
	hugeint operator*(int);

	hugeint operator/(const hugeint &);
	hugeint operator/(const char *);
	hugeint operator/(int);

	//hugeint operator==(const hugeint &);
	//hugeint operator==(const char *);
	//hugeint operator==(int);
private:

	short integer[30];

};


hugeint::hugeint(long value){

	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(const char *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 +(const char *op2){

	return *this + hugeint(op2);

}

//Multi
hugeint hugeint::operator*(const hugeint &op2){

	hugeint temp;
	int carry = 0;
	int counter = 0;
	hugeint reset;

	int position = 29;
	
	

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

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

	return temp;
}

hugeint hugeint::operator *(int op2){

	return *this * hugeint(op2);

}

hugeint hugeint::operator *(const char *op2){

	return *this * hugeint(op2);

}




ostream& operator<<(ostream &output, const hugeint &num){

	int i;

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

	if(i==30)

		output<<0;
	else

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

	return output;

}

Recommended Answers

All 4 Replies

part of the problem you are having with your multiplication is you are setting carry to 1 if the result is greater than 9 but that isn't right. 7 * 7 is 49 so your carry should be 4 in this case and not 1 as you have it.

ok thanks gonna try to impliment that if i cant ill be back

Ive tried a couple of things but nothing has worked so im gonna call it a night any help or tips would be greatly appreciated. Thanks.

yes, the algorihtm in * is wrong, we need more loops. if we compute the a*b, and we need to compute a*every digit of b, and then add them together.
i srongly suggest you to read the book of "object-oriented programming in c++) of Robert Lafore. in the chapter 13, there is a class verylong, it uses the char to implemente the hugeint.
Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.