C++实现大整数乘法(字符串乘法)
作者:qiuchenl 时间:2021-10-23 16:37:22
本文实例为大家分享了C++实现大整数乘法的具体代码,供大家参考,具体内容如下
#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 : 两个任意长度的长整数相乘, 输出结果
Input Param :
const std::string strMultiplierA 乘数A
const std::string strMultiplierB 乘数B
Output :
std::string strRst 乘法结果
Return Value :
int 0 正确
-1 异常
*****************************************************************************/
int multiply (const std::string strMultiplierA,const std::string strMultiplierB, std::string &strRst)
{
/* 在这里实现功能 */
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;
}
来源:https://blog.csdn.net/qiuchenl/article/details/17752073
标签:C++,大整数乘法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring请求参数校验功能实例演示
2023-04-26 02:05:53
Java SSM框架如何配置静态资源加载
2023-03-22 17:40:01
Java与C++实现相同的MD5加密算法简单实例
2023-08-31 09:43:02
![](https://img.aspxhome.com/file/2023/2/60892_0s.jpg)
SpringBoot和Vue.js实现的前后端分离的用户权限管理系统
2023-09-03 14:37:33
Java中Maven项目导出jar包配置的示例代码
2023-01-26 20:09:32
SpringBoot过滤器与拦截 器深入分析实现方法
2023-11-28 23:04:15
spring boot结合Redis实现工具类的方法示例
2023-05-12 09:47:46
spring-cloud-gateway动态路由的实现方法
2021-07-25 15:24:37
JAVA十大排序算法之桶排序详解
2022-11-08 01:07:47
![](https://img.aspxhome.com/file/2023/8/66678_0s.png)
详解Maven profile配置管理及激活profile的几种方式
2022-07-01 08:54:46
android studio2.3如何编译动态库的过程详解
2023-07-11 03:47:48
maven打包如何指定jdk的版本
2022-12-21 20:59:20
![](https://img.aspxhome.com/file/2023/3/67683_0s.png)
C#监控文件夹变化的方法
2023-03-26 09:39:38
![](https://img.aspxhome.com/file/2023/7/70417_0s.png)
图文详解OkHttp的超时时间
2022-05-14 13:50:23
![](https://img.aspxhome.com/file/2023/6/71636_0s.png)
详解Java的MyBatis框架与Spring框架整合中的映射器注入
2021-06-02 00:32:31
浅析Java SPI 与 dubbo SPI
2021-10-23 16:54:12
![](https://img.aspxhome.com/file/2023/3/67503_0s.png)
Java 日志打印的15个好建议
2021-09-08 17:57:42
![](https://img.aspxhome.com/file/2023/3/91473_0s.jpg)
Android实现显示和隐藏密码功能的示例代码
2022-12-14 23:40:09
![](https://img.aspxhome.com/file/2023/0/101870_0s.png)
Java数据结构之链表相关知识总结
2023-11-02 00:29:28
![](https://img.aspxhome.com/file/2023/4/58724_0s.png)
java实现简单的图书管理系统
2022-11-25 03:30:11
![](https://img.aspxhome.com/file/2023/8/61778_0s.png)