您的位置:首页 > 职场人生

面试题之自创 大数相除

2014-01-04 11:28 274 查看

大数相除(整数)


真言


厚积薄发,滴水穿石。


引言


总调试不是最好的,向免调试前进,锻炼思路尽可能完整,少缺陷。


题目


实现大数相除,大数超出了整型的表示范围。


思路


用字符串处理数据,利用字符串的长度的优势。


实验







代码


test.cpp
#include <iostream>
#include <string>
using namespace std;

// extra the class of string
class String:public string
{
public:

// mode the add of int
static string ADD_Int(string a,string b)
{
// exception of input
if( a.empty() )
return b;
else if( b.empty() )
return "0";
if(!check_all_number(a) || !check_all_number(b))
{
return "exception of input ADD_Int";
}
Standardization(a);
Standardization(b);

if(a[0] != '-' && b[0] != '-')
return AddInt(a,b);
else if(a[0] != '-'&& b[0] == '-')
return MinusInt( a,b.substr( 1,b.size() ) );
else if(a[0] == '-'&& b[0] != '-')
return MinusInt(b,a.substr(1,a.size()));
else return '-'+AddInt(a.substr(1,a.size()),b.substr( 1,b.size() ));
}

// make a-b mode int a - b;
static string MINUS_Int(string a,string b)
{
// exception of input
if( a.empty() )
return b;
else if( b.empty() )
return "0";
if(!check_all_number(a) || !check_all_number(b))
{
return "exception of input Multiplies_Int";
}
Standardization(a);
Standardization(b);
if(a[0] != '-' && b[0] != '-')
return MinusInt(a,b);
else if(a[0] != '-' && b[0] == '-')
return AddInt(a,b.substr(1,b.size()));
else if(a[0] == '-' && b[0] != '-')
return "-"+AddInt(a.substr(1,a.size()),b);
else return MinusInt( b.substr(1,b.size()) , a.substr(1,a.size()) );
}

// make a*b mode int a * b;
static string MULT_Int(string a,string b)
{
// exception of input
if( a.empty() )
return b;
else if( b.empty() )
return "0";
if(!check_all_number(a) || !check_all_number(b))
{
return "exception of input Multiplies_Int";
}
Standardization(a);
Standardization(b);
string::size_type i = a.size(),j = b.size();
string c = "0",d = "";
bool fushu = (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');
if(a[0] == '-')
a = a.substr(1,a.size());
if(b[0] == '-')
b = b.substr(1,b.size());

int jinwei = 0;
for( j = b.size()-1 ; j < b.size() ;j--)
{
// each number of b to * a
jinwei = 0;
for( i = a.size()-1 ; i < a.size() ;i-- )
{
d = IntToChar(   ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) % 10 )+ d ;
jinwei = ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) / 10 ;
}
if(jinwei)
d = IntToChar(jinwei) +d;
// add all number result
c = ADD_Int(c,d);
d = "";
unsigned int zero = 0 ;
while( zero < b.size() - j )
{
d = d + '0';
zero ++;
}

}

Standardization(c);
if( fushu )
return '-'+c;
else return c;
}

// mode the division a/b
static string DIV_Int(string a,string b)
{
// exception of input
if( a.empty() )
return "0";
else if( b.empty() )
return "e";
if(!check_all_number(a) || !check_all_number(b))
{
return "exception of input DIV_Int";
}
Standardization(a);
Standardization(b);
if(b == "0")
return "e";
bool fushu =  (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');
if( a[0] == '-' )
a = a.substr(1,a.size());
if( b[0] == '-' )
b = b.substr(1,b.size());
if( Compare(a,b) == '<' )
return "0";

string yushu = "";

string beichushu = a.substr(0,b.size());
string shang = Division( beichushu , b);
yushu =  MinusInt( beichushu ,MULT_Int( shang, b) );
string c = shang;

for(string::size_type i = b.size(); i<a.size(); i++)
{
// beichushu = (yushu * 10 + a.substr(i,b.size()))
// shang = beichushu/ b;
// c = c + shang;
// yushu = beichushu  - b* shang;
beichushu =   yushu+ a[i]     ;
shang = Division( beichushu , b);
c = c + shang;
yushu =  MinusInt( beichushu ,MULT_Int( shang, b) );
}
Standardization(c);
return fushu?('-'+c):c;
}

private:
// static char division a/b
static string Division(string a,string b)
{
// exception of input
if( a.empty() )
return "0";
else if( b.empty() )
return "e";
if(!check_all_number(a) || !check_all_number(b))
{
cout<<"exception of input Division"<<endl;
return "e";
}
Standardization(a);
Standardization(b);
int i = 0;
while( i<=9 )
{
// if a - b*i < b
if(  Compare(   MINUS_Int(   a  ,   MULT_Int(IntToChar(i),b)    ) , b ) == '<'    )
break;
i++;
}
if( i>9 )
return "e";
return ""+IntToChar(i);
}

// make a-b mode int a - b;
static string MinusInt(string a,string b)
{
// exception of input
if(!check_all_number(a) || !check_all_number(b))
return "exception of input MinusInt";
Standardization(a);
Standardization(b);
// particular string of input
if(a.empty())
{
if(b.empty())
return "0";
else
return "-"+b;
}
else if(b.empty())
{
return a;
}

// normal number a < b
string c = "";
bool check = true ;
if(Compare(a,b) == '=')
return "0";
else if(Compare(a,b) == '<')
{
c = a ;
a = b ;
b = c ;
c = "";
check = false ;
}
// normal number a >= b
string::size_type i = a.size()-1, j = b.size()-1;
int jiewei = 0,now;

while(i < a.size() && j < b.size())
{
now = CharToNumber(a[i]) - CharToNumber(b[j]) - jiewei ;

if( now < 0 )
{
jiewei = 1;
now = 10 + now ;
}
else jiewei = 0;
c = IntToChar(now)  + c ;
i--;j--;
}
while(i < a.size())
{
now = CharToNumber(a[i]) - jiewei ;
if( now < 0 )
{
jiewei = 1;
now = 10 + now ;
}
else jiewei = 0;
c = IntToChar( now )  + c ;
i--;
}
Standardization(c);
if(!check)
c = '-' + c;
return c;
}

// mode the add of int
static string AddInt(string a,string b)
{
// exception of input
if( a.empty() )
return b;
else if( b.empty() )
return "0";
if(!check_all_number(a) || !check_all_number(b))
{
return "exception of input AddInt";
}
Standardization(a);
Standardization(b);
string::size_type i = a.size()-1 ,j = b.size()-1 , k = 0 ;
string c = "";
int jinwei = 0;
while( i < a.size() && j < b.size() )
{
c = IntToChar( ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) % 10 ) + c;
jinwei = ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) / 10;
j--;i--;
}
while( j < b.size()  )
{
c =  IntToChar( ( CharToNumber(b[j]) + jinwei ) % 10 ) + c;
jinwei = ( jinwei + CharToNumber(b[j]) ) / 10;
j--;
}
while( i < a.size() )
{
c =  IntToChar( ( CharToNumber(a[i]) + jinwei ) % 10 ) + c;
jinwei = ( jinwei + CharToNumber(a[i]) ) / 10;
i--;
}
if( jinwei )
c = IntToChar(  jinwei  ) + c;
Standardization(c);
return c;
}

// make char to the int number
static int CharToNumber(char c)
{
if( c >= '0' && c <= '9' )
return int(c - '0');
else
{
cout<<c<<" exception of input CharToNumber "<<endl;
system("pause");
return 0;
}
}

// make int to the model char
static string IntToChar(int i)
{
if( i >= 0 && i <= 9 )
{
string c = "";
return c+char(i+48);
}
else
{
cout<<i<<" exception of input IntToChar"<<endl;
system("pause");
return "e";
}
}

// check whether the string is legal
static bool check_all_number(string a)
{
if(a.empty())
return true ;
string::size_type L = a.size(),i = 0;
if(a[0] == '-')
i++;
while( i < L )
{
if( a[i] < '0' || a[i] > '9')
return false;
i++;
}
return true ;
}

// compare string a and b
static char Compare(string a,string b)
{
if(a.empty() || b.empty())
{
cout<<"error of input compare";
return 'e';
}
else
{

if(!check_all_number(a) || !check_all_number(b))
{
return 'e';
}
Standardization(a);
Standardization(b);
if(a[0] == '-' && b[0] != '-')
return '<';
else if( a[0] != '-' && b[0] == '-')
return '>';
bool fushu = (a[0] == '-');

if(a.size() > b.size() )
return fushu?'<':'>';
else if(a.size() == b.size())
{
for(string::size_type i = 0;i < a.size(); i++)
{
if(a[i] > b[i])
return fushu?'<':'>';
if(a[i] < b[i])
return fushu?'>':'<';
}
return '=';
}
return fushu?'>':'<';
}
}

// make string into standard number
static void Standardization(string &a)
{
if(!check_all_number(a))
{
cout<<a<<" exception of input Standardization"<<endl;
}
string::size_type i = 0;
bool fushu = false ;
if( a[0] == '-' )
{
fushu = true;
i = 1;
}
while(i < a.size())
{
if(a[i] != '0')
break;
i++;
}
if(i == a.size())
i--;
a = a.substr(i,a.size());
if( fushu && a != "0")
a = '-' + a;
}
};

// main function
int main()
{
string a ;
string b ;
cin>>a>>b;
cout<<"a="<<a<<",b="<<b<<endl;

cout<<"DIV = "<<String::DIV_Int(a,b)<<endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息