#include #include #include #include #include #include #include #include using namespace std;
// 實現兩個數的加法
// 判斷乙個字串是否為有效的數
bool isvalidnumber( const string &s )
if( 1 == len && ( '+' == s[0] || '-' == s[0] ) )
for( int i = 0; i < len; i++ )
else
} }
return ret;
}// 實現兩個正數的加法
string twopositiveplus( const string &lhs, const string &rhs )
int len1 = lhs.length();
int len2 = rhs.length();
int len = len1 > len2 ? len1 + 2 : len2 + 2;
result.resize(len);
string lhs1 = lhs;
string rhs1 = rhs;
if( '+' == lhs[0] )
if( '+' == rhs[0] )
int len11 = lhs1.length();
int len22 = rhs1.length();
int i = 0;
int j = 0;
int up = 0;
int idx = 0;
for( i = len11 - 1, j = len22 - 1; i >= 0 && j >= 0; i--, j-- )
else
} while( i >= 0 )
else
i--;
} while( j >= 0 )
else
j--;
} if( 1 == up )
result[idx] = '\0';
result.resize( idx + 1 );
for( i = 0, j = idx - 1; i < j; i++, j-- )
return result; }
// 正數加上負數
string posiveaddnegative( const string &lhs, const string &rhs )
int len1 = lhs.length();
int len2 = rhs.length();
int len = len1 > len2 ? len1 + 2 : len2 + 2;
result.resize(len);
string lhs1 = lhs;
string rhs1 = rhs;
if( '+' == lhs[0] )
if( '-' == rhs[0] )
int flag = lhs1.compare( rhs1 );
if( 0 == flag )
int isminus = 0;
int len11 = lhs1.length();
int len22 = rhs1.length();
if( ( len11 == len22 && flag < 0 ) || len11 < len22 )
int up = 0;
int idx = 0;
int i = 0;
int j = 0;
len11 = lhs1.length();
len22 = rhs1.length();
//cout << lhs1 << ' ' << rhs1 << endl;
for( i = len11 - 1, j = len22 - 1; i >= 0 && j >= 0; i--, j-- )
else
}//cout << i << ' ' << j << ' ' << result[0] << endl;
while( i >= 0 )
else
i--;
} while( j >= 0 )
else
j--;
} result[idx] = '\0';
idx + 1 );
//cout << "result=" << result << endl;
for( i = 0, j = idx - 1; i < j; i++, j-- ) //逆置一下
//cout << idx << ' ' << "result=" << result << endl; // 0001
int k = 0;
int cur = 0;
while( '0' == result[k] )
while( k <= idx )
result[cur] = '\0';
result.resize( cur + 1 ); //預留乙個字元給符號位
//cout << result << " sf" << ' ' << cur << endl;
idx = cur;
if( 1 == isminus ) //結果為負數
result[0] = '-';
result[ idx + 1 ] = '\0';
result.resize( idx + 2 );
} //cout << result << endl;
return result;
}int judgeaddtype( const string &s1, const string &s2 )
if( s1[0] != '-' && s2[0] != '-' )
else if( s1[0] != '-' && '-' == s2[0] )
else if( '-' == s1[0] && s2[0] != '-' )
else if( '-' == s1[0] && '-' == s2[0] )
return type;
}// 大數乘法
/*思想:
0 1 2 3 4
0 1 2
-----------------------
20 21 22 23 24
10 11 12 13 14
00 01 02 03 04
------------------------------------
*/string multipy( const string &s1, const string &s2 )
// 任意元素為0,乘積為0
if( ( 1 == s1.length() && '0' == s1[0] ) || ( 1 == s2.length() && '0' == s2[0] ) )
bool ispositive = true;
if( ( '-' == s1[0] && s2[0] != '-' ) || ( s1[0] != '-' && '-' == s2[0] ) )
string ss1 = s1;
string ss2 = s2;
if( !isdigit(s1[0]) )
if( !isdigit(s2[0]) )
int len1 = ss1.length();
int len2 = ss2.length();
int *presult = new int[ len1 + len2 ];
if( null == presult )
for( int i = 0; i < len1 + len2; i++ )
for( int i = len1 - 1; i >= 0; i-- ) }
for( int i = len1 + len2 - 1; i >= 1; i-- ) }
char *pch = new char[ len1 + len2 + 1 ];
if( null == pch )
int k = 0;
while( 0 == presult[k] ) // 跳過頭部0元素
int idx = 0;
if( !ispositive )
for( ; k < len1 + len2; k++ )
pch[idx] = '\0';
if( !ispositive )
string result(pch);
delete pch;
pch = null;
delete presult;
presult = null;
return result;
}int main()
else if( 0 == type ) // 正+正
else if( 1 == type ) // 正+負
else if( 2 == type ) // 負+正
else if( 3 == type ) // 負+負
}return ret;
}
大數加法,大數乘法
include include include typedef struct node pnode 函式宣告 int multiply char strmultipliera,char strmultiplierb,char strrst pnode initlist char strmultipl...
大數加法和大數乘法的實現
問乙個facebook大數相乘的題 兩個大數相乘 char multiply char char 比如 char str1 23456789009877666555544444 char str2 346587436598437594375943875943875 最後求出他們的乘積 就是實現乘法的...
大數乘法與大數加法 java實現
最近碰到大數乘法與大數加法的需求,如果直接用int型別會溢位。於是自己寫了個大數乘法與大數加法的 就當自己練練手了。大數加法的邏輯相對乘法稍微簡單一些。先將輸入的兩個字串逆序生成字元陣列,然後相同位上的字元相加儲存在乙個結果陣列中。最後遍歷結果陣列,如果當前位的結果大於或等於10,則先將當前位的數值...