C++實(shí)現(xiàn)大整數(shù)乘法(字符串乘法)
更新時(shí)間:2019年09月18日 09:37:56 作者:qiuchenl
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)大整數(shù)乘法、字符串乘法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++實(shí)現(xiàn)大整數(shù)乘法的具體代碼,供大家參考,具體內(nèi)容如下
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string add(string a,string b)
{
if(a.length()==0)
return b;
if(b.length()==0)
return a;
a.length()<b.length()?a.swap(b):a.length();
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int flag=0;
for(int i=0;i<a.length();i++)
{
int aInt=a[i]-'0';
int bInt=0;
if(i<b.length())
bInt=b[i]-'0';
int result=aInt+bInt+flag;
a[i]=result%10+'0';
flag=result/10;
}
if(flag!=0)
{
a=a+(char)(flag+'0');
}
reverse(a.begin(),a.end());
return a;
}
string multiply(std::string strMultiplierA,char x)
{
int b=x-'0';
int flag=0;
for(int i=strMultiplierA.length()-1;i>=0;i--)
{
int a=strMultiplierA[i]-'0';
int result=a*b+flag;
strMultiplierA[i]=result%10+'0';
flag=result/10;
}
if(flag!=0)
strMultiplierA=(char)(flag+'0')+strMultiplierA;
while(strMultiplierA.length()>1&&strMultiplierA[0]=='0')
strMultiplierA=strMultiplierA.substr(1,strMultiplierA.length());
return strMultiplierA;
}
/*****************************************************************************
Prototype : multiply
Description : 兩個(gè)任意長(zhǎng)度的長(zhǎng)整數(shù)相乘, 輸出結(jié)果
Input Param :
const std::string strMultiplierA 乘數(shù)A
const std::string strMultiplierB 乘數(shù)B
Output :
std::string strRst 乘法結(jié)果
Return Value :
int 0 正確
-1 異常
*****************************************************************************/
int multiply (const std::string strMultiplierA,const std::string strMultiplierB, std::string &strRst)
{
/* 在這里實(shí)現(xiàn)功能 */
if(strMultiplierA.length()<=0||strMultiplierB.length()<=0)
return -1;
bool flag=false;//false"+" true"-"
string strA=strMultiplierA,strB=strMultiplierB;
if(strMultiplierA[0]=='-')
{
flag=~flag;
strA=strMultiplierA.substr(1,strMultiplierA.length());
}
if(strMultiplierB[0]=='-')
{
flag==true?flag=false:flag=true;
strB=strMultiplierB.substr(1,strMultiplierB.length());
}
for(int i=strB.length()-1;i>=0;i--)
{
string result=multiply(strA,strB[i]);
int j=i;
while(++j<strB.length())
result=result+"0";
// while(result.length()>1&&result[0]=='0')
// result=result.substr(1,result.length());
strRst=add(strRst,result);
}
while(strRst.length()>1&&strRst[0]=='0')
strRst=strRst.substr(1,strRst.length());
if(flag==true&&strRst!="0")
strRst='-'+strRst;
return 0;
}
int main()
{
std::string strResult = "";
multiply("-5489324", "0", strResult);
cout<<strResult<<endl;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
QT使用SQLite數(shù)據(jù)庫(kù)超詳細(xì)教程(增刪改查、對(duì)大量數(shù)據(jù)快速存儲(chǔ)和更新)
這篇文章主要給大家介紹了關(guān)于QT使用SQLite數(shù)據(jù)庫(kù)的相關(guān)資料,其中包括增刪改查以及對(duì)大量數(shù)據(jù)快速存儲(chǔ)和更新,SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它是一個(gè)軟件庫(kù),提供了一個(gè)自包含、無服務(wù)器、零配置的、事務(wù)性的SQL數(shù)據(jù)庫(kù)引擎,需要的朋友可以參考下2024-01-01
c語言實(shí)現(xiàn)兩個(gè)單鏈表的交叉合并方式
今天小編就為大家分享一篇c語言實(shí)現(xiàn)兩個(gè)單鏈表的交叉合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
C語言實(shí)現(xiàn)經(jīng)典小游戲井字棋的示例代碼
這個(gè)三子棋游戲是在學(xué)習(xí)C語言的過程中自己編寫的一個(gè)小游戲,現(xiàn)在將自己的思路(主要以流程圖形式和代碼中的注釋表達(dá))和具體代碼以及運(yùn)行結(jié)果分享出來以供大家學(xué)習(xí)參考,希望對(duì)大家有所幫助2022-11-11
C++使用棧實(shí)現(xiàn)括號(hào)匹配的代碼詳解
在編程中,括號(hào)匹配是一個(gè)常見問題,尤其是在處理數(shù)學(xué)表達(dá)式、編譯器解析等任務(wù)時(shí),棧是一種非常適合處理此類問題的數(shù)據(jù)結(jié)構(gòu),能夠精確地管理括號(hào)的匹配問題,本文將通過 C++ 代碼,詳細(xì)講解如何使用棧來實(shí)現(xiàn)括號(hào)匹配,需要的朋友可以參考下2025-02-02
函數(shù)外初始化與函數(shù)內(nèi)初始化詳細(xì)解析
函數(shù)內(nèi)初始化:bool FillStr(char *&szDst, int nSize);第一個(gè)參數(shù)中的&一定不能少,這是因?yàn)樵诤瘮?shù)外部我們只聲明了這個(gè)指針,具體這個(gè)指針指向內(nèi)存中的哪個(gè)地址我們并不知道,所以&是為了說明傳遞的是這個(gè)指針的引用,那么在函數(shù)內(nèi)初始化后這個(gè)指針的地址也就是外面指針的地址了2013-09-09

