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 00448312 at 2005-04-01 11:45:05 on Problem 1001
因为最近有一道相似的题目是用运算符重载来做的
所以将原来处理整数的函数加了一点修改用在了这道题上面

//*********************************** 1001 cpp **********************************
 
# include < string.h>
# include < iostream.h>
# include < math.h>
int pp;//   全局变量用来传递输出的数据应该有几位小数 
const int con= 125;
class HugeInt
{
	friend ostream &operator<<( ostream &, HugeInt & );
public :
	HugeInt( long = 0 );
	HugeInt( const char * );
	HugeInt operator+( HugeInt & );
	HugeInt operator+( int );
	HugeInt operator+( const char* );
    HugeInt operator*( int );
    HugeInt operator*( const char *op2 );
	HugeInt operator*( const HugeInt &  op2 );
	void getOne( char[] );// 从外界取得要处理的数据
 
    
private:
	int  integer[con];
	
    // it means the position of the pointer;
};
HugeInt::HugeInt( long val  )
{
	int i;
	for ( i=0; i<=con-1; i++ )
		integer[i] =0;
	for ( i=con-1; val!=0 && i>=0; i-- )
	{
		integer[i]= val % 10;
		val =val/ 10;
	}
	 
	 
}//

 

HugeInt::HugeInt ( const char *string  )
{
	int i, j;
	for ( i=0; i<=con-1; i++ )
		integer[i]=0;

	for ( i= con- strlen( string ), j=0; i<=con-1; i++, j++ )
	{
		integer[i] = string[j] -'0';
	}
     
}// 将数据储存到数组integer[]中去,转化为
//  重载运算符*可计算的类型

 


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

	for ( int i=con-1; i>=0; i-- )
	{
		temp.integer[i] = integer[i] + op2.integer[i] + carry;
		if ( temp.integer[i] > 9 )
		{
			temp.integer[i]  = 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 );
}

ostream& operator<< ( ostream &output , HugeInt &num )//  此函数还需要进一步修改,如处理末尾的0的问题
{
	int i;
	for ( i=0; ( num.integer[i] == 0 ) && ( i<=con-1 ); i++ )
	{
		if( i== con-pp )
			break;
	}
	 
	if( i == con  )
		output << 0;
	else
		for ( ; i<=con-1; i++ )
		{
		
			int NumOfZero=0;
			for ( int j=i; j<=con-1; j++ )//处理数据末尾的0
			{
				if( num.integer[j]== 0 )
					NumOfZero++;
			}
			if( NumOfZero== con-i )//如果后面全是0,就跳出循环
				break;
	        if( i== con-pp )//在适当的位置添加小数点
				output<<".";
			output << num.integer[i];
		}

		return output;
}


HugeInt HugeInt::operator*( const HugeInt &  op2 )// 先从最简单的情况开始考虑
{
	
	HugeInt op4;//  储存最终的计算结果
	int c=0;//  退位置的计数 
	for ( int i = con-1; i>=0 ; i-- )
	{
		int t=0;//  save the higer_position number
		HugeInt op3;// 储存中间的计算结果
		for ( int j=con-1; j>=0; j-- )
		{
			int m= integer[j]* op2.integer[i]+t ;
			op3.integer[j-c]= m % 10;
			t= m/10;
		}
		c++;
		op4= op4 + op3;
	}
    return
		op4;
}

HugeInt HugeInt::operator*( const char *op2 )//  no declaration in the class
{
	return (*this) * HugeInt ( op2 );
}

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

void HugeInt::getOne( char ch2[] )
{
	for ( int j=4,i=con-1; j>=0 ; j--, i-- )
	{
		this->integer[i]=ch2[j]-'0';
	}
}

void main()
{
	HugeInt a1("0144")
		,a2("0"),a3("1234567891"),a4("15"),
		a5,a6 ;
	char ch1[7], ch2[7];
	int n,m;// 其中n纪录循环的次数
	cin>>ch1>>n;
	int i;
	for(  i=0; i<7; i++ )
	{
		if( ch1[i]=='.' )
			break;
	}
	m=6-i-1;// m纪录小数点后面有几位数字
     

	int cach=0;
	for ( int j=0; j<7; j++ )
	{
		if( ch1[j]!='.' )
			ch2[ cach++ ]= ch1[j];
	}
	 
	a5.getOne( ch2 );
	pp= m*n;
         a6=a5;
	for ( i=1; i<=n-1; i++ )
	{
		a6=a6*a5;
	}
	cout<<a6;
}



	 
  
     
	  
	  
	 
	 
 
 


	      

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