⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fnumber.h

📁 计算机组成原理的一个浮点数运算器。 C++编写
💻 H
字号:

#include <string>
#include <iostream>
#include<String.h>
#include<cstdlib>
using namespace std ;
class fnumber
{
public:
  int       Sbit;
  int       Mbit;
  int       stepcodesign;
  int       mantissasign;
  //int       stepcodesign_1;
  //int       mantissasign_2;
  string    stepcode;   
  string    mantissa;  
  
public:
 fnumber(fnumber& f);
 fnumber(int jiemabit,int weishubit,string jvalue);
 fnumber(int jiemabit,int weishubit,int jiemasign,string jvalue,int weishusign,string wvalue);
 
 int rectify_stepcode(fnumber &f);  
                        
                       
 bool iszero();              
 bool isoverflow();           
 bool stepcodeoverflow();     
 void stepcodeadd();          
 void stepcodeminus();        
 void mantissarmove();         

 fnumber mantissaaddition(fnumber &f);
 int  standardize();           // return value :0 right  1 is stepcode 上溢 -1  is  阶码下溢
                 
            
};
 


fnumber::fnumber(int jiemabit,int weishubit,int jiemasign,string jvalue,int weishusign,string wvalue)
{
	Sbit=jiemabit;
	Mbit=weishubit;
	stepcodesign=jiemasign;
	mantissasign=weishusign;
	// bit stander
	int chazhi;
	chazhi=Sbit-jvalue.length();

	if(chazhi>0)
		for(int i=0;i<chazhi;i++)
		{
			jvalue.insert(0,"0");
		}
	else if(chazhi<0)
		jvalue.substr(0,Sbit);

	// mantissa bit stander
	chazhi=Mbit-wvalue.length();

	if(chazhi>0)
		for(int i=0;i<chazhi;i++)
		{
	      wvalue.insert(wvalue.length(),"0");
		}
   else if(chazhi<0)
	   wvalue.substr(0,Mbit);
    //补码 表示 
	if(stepcodesign==0)
	{
       stepcode=stepcode.insert(0,"00");
        stepcode=stepcode.insert(2,jvalue);
	}
	   
	else
	{
      stepcode=stepcode.insert(0,"00");
	  stepcode=stepcode.insert(2,jvalue);
	  
	  for(int i=0;i<stepcode.length();i++)//reverse
	  {
		  if(stepcode.substr(i,1)=="0")
             stepcode.replace(i,1,"1");
		  else
			  stepcode.replace(i,1,"0");
	  }
	  i=stepcode.length()-1;
	  while(i>=0)           // add 1
	  {
		  if(stepcode.substr(i,1)=="0")
		  {
			  stepcode.replace(i,1,"1");
			  break;
		  }
		  else
			  stepcode.replace(i,1,"0");
		  i--;
	  }
	}
	
  // deal mantissa 	
	mantissa=mantissa.insert(0,"00");
	if(mantissasign==0)
       mantissa=mantissa.insert(2,wvalue);
	
	else
	{
		mantissa=mantissa.insert(2,wvalue);
        for(int i=0;i<mantissa.length();i++)//reverse
	  {
		  if(mantissa.substr(i,1)=="0")
             mantissa.replace(i,1,"1");
		  else
			  mantissa.replace(i,1,"0");
	  }
	  i=mantissa.length()-1;
	  while(i>=0)           // add 1
	  {
		  if(mantissa.substr(i,1)=="0")
		  {
			  mantissa.replace(i,1,"1");
			  break;
		  }
		  else
			  mantissa.replace(i,1,"0");
		  i--;
	  }

	}
		
	

};

 fnumber::fnumber(int jiemabit,int weishubit,string jvalue)
 {
	Sbit=jiemabit;
    Mbit=weishubit;
	stepcode=jvalue;
 };

 fnumber::fnumber(fnumber& f)//copy
 {
	 Sbit=f.Sbit;
	 Mbit=f.Mbit;
	 stepcode=f.stepcode;
	 mantissa=f.mantissa;

 };

bool fnumber::iszero()
{
	if(mantissa.substr(0,1)=="0")
	{
	   if(mantissa.substr(0,Mbit+2).find("1",0)==string::npos)
		  return true; 
    }
  else
  {
	  if(mantissa.substr(0,Mbit+2).find("0",0)==string::npos)
		return true;
  }
	return false;
};

bool fnumber::stepcodeoverflow()
{
	if(stepcode.substr(0,1)==stepcode.substr(1,1))
		return false;
	return true;
};



int  fnumber::rectify_stepcode(fnumber &f)
{
	if(stepcode.substr(0,1)==f.stepcode.substr(0,1))
	{	
		if(stepcode<f.stepcode)
			while(stepcode<f.stepcode)
			{
				stepcodeadd();
				mantissarmove();
				if(iszero())
				{
					return 2;
				}
			}  
			else if(stepcode>f.stepcode)
				while(stepcode>f.stepcode)
				{
					f.stepcodeadd();
					f.mantissarmove();
					if(f.iszero())
					{
						return 1;
					}
				}  
	}
	else  // differ  sign
	{
		if(stepcode.substr(0,1)=="1")
			while(stepcode.substr(0,1)!=f.stepcode.substr(0,1))
			{
				stepcodeadd();
				mantissarmove();
				if(iszero())
					return 2;
			}
			else
			{
				while(stepcode.substr(0,1)!=f.stepcode.substr(0,1))
				{
					f.stepcodeadd();
					f.mantissarmove();
					if(f.iszero())
					{
						return 1;
					}
				}  
			}
	}
	return 0;
};


 void fnumber::stepcodeadd()
 {
	int i=Sbit+2;
	while(i>0)
	{
		if(stepcode.substr(i-1,1)=="0")
		{
			stepcode.replace(i-1,1,"1");
			break;
		}
		else
			stepcode.replace(i-1,1,"0");
		  i--;

	} 	 
 };	 
	 
void fnumber::stepcodeminus()
{
  int i=Sbit+2;
	while(i>0)
	{
		if(stepcode.substr(i-1,1)=="1")
		{
			stepcode.replace(i-1,1,"0");
			break;
		}
		else
			stepcode.replace(i-1,1,"1");
		  i--;

	} 	 
};	 



 void fnumber::mantissarmove()
 { 
	 if(mantissa.substr(0,1)=="0")
     	 mantissa.insert(0,"0");
	 else
		 mantissa.insert(0,"1");
 };

 fnumber fnumber::mantissaaddition(fnumber &f)
 {
	 int temp=0;
     string s1; 
	 string s2;
	 string s3;
	 
	 
	 s1=mantissa;
	 s2=f.mantissa;
	 
	 //
	 int i=Mbit+1; 	   // Mbit-1
	 while(i>=0)
	 {
		 if(s1.substr(i,1)=="0" && s2.substr(i,1)=="0")
		 {
			 if(temp==0)
				 s3.insert(0,"0");
             else
			 {
				 s3.insert(0,"1");
				 temp=0;
			 }
		 }
		 else if(s1.substr(i,1)=="0" && s2.substr(i,1)=="1"||s1.substr(i,1)=="1" && s2.substr(i,1)=="0")
		 {
			 if(temp==0)
				 s3.insert(0,"1");
             else
			 {
				 s3.insert(0,"0");
				 temp=1;
			 }
			 
		 }
		 else
		 {
			 if(temp==0)
			 {
				 s3.insert(0,"0");
				 temp=1;
			 } 
             else
			 {
				 s3.insert(0,"1");
				 temp=1;
			 }
		 }
		 i--;
	 }
     if(s1.length()>s2.length())
		 s3.insert(s3.length(),s1.substr(Mbit,1));
	 else if(s1.length()<s2.length())
		 s3.insert(s3.length(),s2.substr(Mbit,1));
	 fnumber result(Sbit,Mbit,stepcode);
	 result.mantissa=s3;
	 return result;
	 
 };

 int  fnumber::standardize()
 {
	 if(mantissa.substr(0,1)!=mantissa.substr(1,1))
	 {
		 mantissa.insert(0,mantissa.substr(0,1));
		 stepcodeadd();
		 if(stepcodeoverflow())
			 return 1;
		 if(mantissa.substr(Mbit-1,1)=="0")
			 mantissa.replace(Mbit-1,1,"1");
	 }
	 else
	 {
		 while(mantissa.substr(1,1)==mantissa.substr(2,1))
		 {
			 mantissa.erase(0,1);
			 if(mantissa.length()==Mbit+1)   //shorttage a bit
			 {
				 if(mantissa.substr(0,1)=="1")
					 mantissa.insert(Mbit+1,"0");
				 else
					 mantissa.insert(Mbit+1,"1");
			 }
			 
			 
			 stepcodeminus();
			 if(stepcodeoverflow())
				 return -1;
		 }
		 if(mantissa.substr(Mbit-1,1)=="1")
		 {
			 int i=Mbit+2;
			 while(i>0)
			 {
				 if(mantissa.substr(i-1,1)=="0")
				 {
					 mantissa.replace(i-1,1,"1");
					 break;
				 }
				 mantissa.replace(i-1,1,"0");
				 i--;
			 }
		 }
		 return 0;
	 }
	 
 };



 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -