Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用类编的,有重载运算符,但太长了

Posted by Pope at 2007-02-09 11:33:56 on Problem 1001
# include <iostream.h>
# include "integer.h"
# define N 6
void print(Integer &p, int weight, int exp, bool fraction)
{
	if (fraction)								// it's a decimal fraction.
	{
		int n = weight * exp - p.digit;
		cout << '.';
		for (int i=0; i<n; ++i)
		{
			cout << '0';
		}
		p.print();
	}
	else
	{
		
		int max = (N * exp - p.digit) > p.digit ? (N * exp - p.digit) : p.digit;
		char *output = new char[max + 1];
		int n = max - weight * exp, j = p.digit - 1;
		for (int i=0; i<max+1; ++i)
		{
			if ((i != n) && (j >= 0))
			{
				//cout << p.array[j];
				output[i] = (char)(p.array[j] + '0');
				--j;
			}
			else if (i == n)
			{
				//cout << '.';
				output[i] = '.';
			}
			else
			{
				//cout << '0';
				output[i] = '0';
			}
		}
		int d = max;
		cout << "d = " << d << endl;
		while ((output[d] == '0') || (output[d] == '.'))							// 紧缩
		{
			if (output[d] == '.')
			{
				--d;
				break;
			}
			--d;
		}
		cout << "d = " << d << endl;
		
		for (i=0; i<=d; ++i)
		{
			cout << output[i];
		}
	}
	cout << endl;
}
int main()
{
	char s[N];
	int n;
	while (cin >> s >> n)
	{
		int j=0, digits[N], weight = 0;			// 权值(非正数)
		bool fraction = true;
		if (s[0] != '0')
		{
			fraction = false;
		}
		for (int i=0; i<N; ++i)
		{
			
			if (s[i] != '.')
			{
				digits[j] = (int)s[i] - '0';
				++j;
			}
			else weight = N - i - 1;
		}
		/*
		for (i=0; i<j; ++i)
		{
			cout << digits[i];
		}
		*/
		//cout << "weight = -" << weight << endl;
		Integer p(digits, j);
		p ^ n;
		if (weight > 0)
		{
			print(p, weight, n, fraction);
		}
		else
		{
			p.print();
		}
	}
	return 0;
}
//: Integer.h
class Integer
{
public:
	int digit;
	int *array;
	bool negative;
	Integer(int n);
	Integer(int *p, int n);
	~Integer();
	Integer & square();
	Integer & operator = (const Integer &B);
	Integer & operator += (const Integer &B);
	Integer & operator -= (const Integer &B);
	Integer & operator *= (const Integer &B);
	Integer & operator ^ (int exp);
	void print();
};
///~

//: Integer.cpp
Integer::Integer(int n)
{
	if (n < 0)
	{
		negative = true;
	}
	else negative = false;
	int counter = 0;
	int tmp = n;
	while (tmp >= 1)
	{
		tmp /= 10;
		++counter;
	}
	digit = counter;
	array = new int[digit];
	for (int i=0; i<digit; i++)
	{
		array[i] = n % 10;
		n /= 10;
	}
}
Integer::Integer(int *p, int n)
{
	negative = false;
	int d = 0;
	while (p[d] == 0)							// 紧缩
	{
		++d;
	}
	digit = n - d;
	array = new int[digit];
	for (int i=0; i<digit; ++i)
	{
		array[digit-i-1] = p[d];
		++d;
	}
}
Integer::~Integer()
{
	delete [] array;
}
Integer &Integer::operator = (const Integer &B)
{
	negative = false;
	if (&B == this)									// 检查是否自我赋值
	{
		cout << "error:自我赋值" << endl;
		return *this;
	}
	digit = B.digit;
	array = new int[digit];
	for (int i=0; i<digit; ++i)
	{
		array[i] = B.array[i];
	}
	return *this;
}
Integer &Integer::operator += (const Integer &B)
{
	negative = false;
	Integer &plurality = digit > B.digit ? *this : B;
	int max = plurality.digit;						// 较大数
	int min = digit < B.digit ? digit : B.digit;
	int *tmp = new int[max+1];						// 有可能进位
	for (int i=0; i<max+1; i++)						// 初始化
	{
		tmp[i] = 0;
	}
	int sum = 0;
	int carry = 0;									// 进位
	for (i=0; i<min; i++)							// 处理需要两操作数的部分。
	{
		sum = array[i] + B.array[i] + carry;
		tmp[i] += (sum % 10);	
		carry = sum / 10;
	}
	for (i=min; i<max; i++)							// 有可能有进位。
	{
		sum = plurality.array[i] + carry;
		tmp[i] += (sum % 10);
		carry = sum / 10;
	}
	tmp[max] = carry;
	while (tmp[max] == 0)							// 紧缩
	{
		--max;
	}
	delete [] array;
	array = new int[max+1];
	for (i=0; i<max+1; i++)							//初始化
	{
		array[i] = 0;
	}
	digit = max + 1;
	for (i=0; i<digit; i++)
	{
		array[i] = tmp[i];
	}
	delete [] tmp;
	return *this;
}
Integer &Integer::operator -= (const Integer &B)
{
	negative = false;
	Integer &plurality = digit > B.digit ? *this : B;
	int max = plurality.digit;						// 较大数
	int min = digit < B.digit ? digit : B.digit;
	int *tmp = new int[max+1];						// 有可能进位
	for (int i=0; i<max+1; i++)						// 初始化
	{
		tmp[i] = 0;
	}
	int sum = 0;
	int carry = 0;									// 进位
	for (i=0; i<min; i++)							// 处理需要两操作数的部分。
	{
		sum = array[i] + B.array[i] + carry;
		tmp[i] += (sum % 10);	
		carry = sum / 10;
	}
	for (i=min; i<max; i++)							// 有可能有进位。
	{
		sum = plurality.array[i] + carry;
		tmp[i] += (sum % 10);
		carry = sum / 10;
	}
	tmp[max] = carry;
	while (tmp[max] == 0)							// 紧缩
	{
		--max;
	}
	delete [] array;
	array = new int[max+1];
	for (i=0; i<max+1; i++)							//初始化
	{
		array[i] = 0;
	}
	digit = max + 1;
	for (i=0; i<digit; i++)
	{
		array[i] = tmp[i];
	}
	delete [] tmp;
	return *this;
}
Integer &Integer::operator *= (const Integer &B)
{
	negative = false;
	int n = digit + B.digit;
	int *result = new int[n];
	for (int i=0; i<n; i++)							//初始化
	{
		result[i] = 0;
	}
	int product;
	for (i=0; i<B.digit; ++i)
	{
		for (int j=0; j<digit; j++)
		{
			product = B.array[i]*array[j] + result[i+j];
			result[i+j] = product % 10;
			result[i+j+1] += product / 10;
		}
	}
	while (result[n-1] == 0)
	{
		--n;
	}
	delete [] array;
	array = new int[n];
	for (i=0; i<n; ++i)
	{
		array[i] = result[i];
	}
	delete [] result;
	digit = n;
	return *this;
}
Integer & Integer::square()
{
	
	Integer tmp1 = *this;
	tmp1 *= *this;
	*this = tmp1;
	return *this;
}
Integer & Integer::operator ^ (int exp)
{
	if (exp == 0)
	{
		Integer I(1);
		*this = I;
		return *this;
	}
	else
	{
		Integer tmp(1);
		tmp = *this;
		tmp.square();
		Integer sq(1);
		sq = tmp;
		if (exp%2 == 0)		//m is even
		{
			sq.operator ^(exp/2);
			*this = sq;
		}
		else					//m is odd
		{
			sq.operator ^(exp/2);
			*this *= sq;	
		}		
		return *this;
	}		
}
void Integer::print()
{
	if (negative)
	{
		cout << '-';
	}
	bool flag = true;
	for (int i=digit-1; i>=0; i--)
	{
		if (array[i] == 0 && flag)
		{
			continue;
		}
		flag = false;
		cout << array[i];	
	}
}
///~

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator