用两个整数来模拟浮点数运算


一个做底数,一个做指数,实现加减乘除运算。

注意精度,已经数为0的情况。


#include <stdio.h>
#include <malloc.h>
#include <iostream>
using namespace std;

/*
	int 型是4个字节
	取值访问为 -2147483648 ~ 2147483647
	对于10进制数来说,有9位的精度
*/
double pow10(int exp)            //10的指数
{
	return pow(double(10), double(exp));
}

class  newDouble
{
public:
	newDouble(void){bot=0; exp=0;};
	newDouble( double d )
	{
		bot =0;
		exp =0;
		int flag = d>0?1:-1;  //取符号
		d = abs(d);           //取绝对值

		while( d< pow10(8) && d!=0 )  //取8位精度
		{
			d *= 10;
			exp--;
		}
		bot = flag*(int)(d);
	}


	newDouble operator+( newDouble b )
	{
		(*this).SpecifyPrecision(7); //都指定7位的精度,相加不会越界
		b.SpecifyPrecision(7);         
		int dis = exp - b.exp;
		newDouble c;
		if( dis >0 )
		{
			c.bot = (int)(bot*pow((double)10,(double)dis)) + b.bot;
			c.exp = b.exp;
		}
		else
		{
			c.bot = bot + (int)(b.bot*pow((double)10,(double)dis));
			c.exp = exp;
		}
		return c;
	}

	newDouble operator-( newDouble b )
	{
		(*this).SpecifyPrecision(7); //都指定7位的精度,相减不会越界
		b.SpecifyPrecision(7);     
		int dis = exp - b.exp;
		newDouble c;
		if( dis >0 )
		{
			c.bot = (int)(bot*pow((double)10,(double)dis)) - b.bot;
			c.exp = b.exp;
		}
		else
		{
			c.bot = bot - (int)(b.bot*pow((double)10,(double)dis));
			c.exp = exp;
		}
		return c;
	}

	newDouble operator*( newDouble b )
	{
		(*this).SpecifyPrecision(4); //都指定4位的精度,相乘不会越界
		b.SpecifyPrecision(3);     
		newDouble c;
		c.bot = bot * b.bot;
		c.exp = exp + b.exp;
		
		return c;
	}

	newDouble operator/( newDouble b )
	{
		if( b.bot ==0 )
		{
			cout << "the divider can not be zero"<<endl;
			return newDouble();
		}
		(*this).SpecifyPrecision(7); //被除数指定8位的精度
		b.SpecifyPrecision(4);       //除数指定4位精度    
		newDouble c;
		c.bot = bot / b.bot;
		c.exp = exp - b.exp;
		
		return c;
	}

	newDouble operator=( double d )
	{
		bot =0;
		exp =0;
		int flag = d>0?1:-1;  //取符号
		d = abs(d);           //取绝对值

		while( d< pow10(8)&&d!=0 )  //取8位精度
		{
			d *= 10;
			exp--;
		}
		bot = flag*(int)(d);
		
		return *this;              //可以使用连等
	}

	double ToDouble( newDouble b )
	{
		return (double)(b.bot)*pow((double)10, (double)(b.exp));
	}

	//指定精度,即底数的位数
	void SpecifyPrecision( int num )
	{
		if( bot ==0 ) return;
		int flag = bot>0?1:-1;
		bot = abs(bot);
		while( bot < pow10(num) )
		{
			bot *= 10;
			exp--;
		}
		while( bot > pow10(num+1) )
		{
			bot /=10;
			exp++;
		}
		bot = flag*bot;
	}
private:
	int bot;
	int exp;

};

ostream & operator<<(ostream &out , newDouble d )
	{
		out << d.ToDouble(d);
		return out;
	}


int main(void)
{
	newDouble a(50);
	newDouble b(8);
	cout << "a=" << a<<endl;
	cout << "b=" << b<<endl;
	cout <<"a+b=" << a+b <<endl;
	cout <<"a-b=" << a-b <<endl;
	cout <<"a*b=" << a*b <<endl;
	cout <<"a/b=" << a/b <<endl<<endl;

	a = -0.5;
	b = -0.03;

	cout << "a=" << a<<endl;
	cout << "b=" << b<<endl;
	cout <<"a+b=" << a+b <<endl;
	cout <<"a-b=" << a-b <<endl;
	cout <<"a*b=" << a*b <<endl;
	cout <<"a/b=" << a/b <<endl<<endl;

	a = 2.5;
	b = 4;
	cout << "a=" << a<<endl;
	cout << "b=" << b<<endl;
	cout << "(a*b)+(b-a)+(b/a)=" <<(a*b)+(b-a)+(b/a)<<endl<<endl;

	while(1)
	{
		double t=0;
		cout << "Please input a( double ): "<<endl;
		cin >> t;
		a =t;
		cout << "Please input b( double ): "<<endl;
		cin >> t;
		b =t;
		cout << "a=" << a<<endl;
		cout << "b=" << b<<endl;
		cout <<"a+b=" << a+b <<endl;
		cout <<"a-b=" << a-b <<endl;
		cout <<"a*b=" << a*b <<endl;
		cout <<"a/b=" << a/b <<endl<<endl;
	}

	return 0;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值