欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++實現(xiàn)大整數(shù)乘法(字符串乘法)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

本文實例為大家分享了C++實現(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 : 兩個任意長度的長整數(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) 
{
 
  /* 在這里實現(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;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:opengl繪制五星紅旗

欄    目:C語言

下一篇:利用mmap實現(xiàn)文件拷貝功能

本文標題:C++實現(xiàn)大整數(shù)乘法(字符串乘法)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/181.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有