高精度加法
来源:互联网 发布:巫女服 知乎 编辑:程序博客网 时间:2024/05/16 12:56
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str1,str2;
int a[250],b[250],len; //数组的大小决定了计算的高精度最大位数
int i;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cout<<"Please input two addend :"<<endl;
cin>>str1>>str2; //输入两个字符串
a[0]=str1.length(); //取得第一个字符串的长度
for(i=1;i<=a[0];i++) //把第一个字符串转换为整数,存放在数组a中
a[i]=str1[a[0]-i]-'0';
b[0]=str2.length(); //取得第二个字符串长度
for(i=1;i<=b[0];i++) //把第二个字符串中的每一位转换为整数,存放在数组B中
b[i]=str2[b[0]-i]-'0';
len=(a[0]>b[0]?a[0]:b[0]); //取两个字符串最大的长度
for(i=1;i<=len;i++) //做按位加法,同时处理进位
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
len++; //下面是去掉最高位的0,然后输出
while((a[len]==0)&&(len>1))
len--;
for(i=len;i>=1;i--)
cout<<a[i];
return 0;
#include<cstring>
using namespace std;
int main()
{
string str1,str2;
int a[250],b[250],len; //数组的大小决定了计算的高精度最大位数
int i;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cout<<"Please input two addend :"<<endl;
cin>>str1>>str2; //输入两个字符串
a[0]=str1.length(); //取得第一个字符串的长度
for(i=1;i<=a[0];i++) //把第一个字符串转换为整数,存放在数组a中
a[i]=str1[a[0]-i]-'0';
b[0]=str2.length(); //取得第二个字符串长度
for(i=1;i<=b[0];i++) //把第二个字符串中的每一位转换为整数,存放在数组B中
b[i]=str2[b[0]-i]-'0';
len=(a[0]>b[0]?a[0]:b[0]); //取两个字符串最大的长度
for(i=1;i<=len;i++) //做按位加法,同时处理进位
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
len++; //下面是去掉最高位的0,然后输出
while((a[len]==0)&&(len>1))
len--;
for(i=len;i>=1;i--)
cout<<a[i];
return 0;
}
注意:两个数相加,结果的位数,应该比两个数中大的那个数多一位。
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度 加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- 高精度加法
- Androdi Droid Fu(第三方库介绍)
- JNI 常用API
- 数据结构中串部分,对于一些问题的认识
- Common Tasks :Connecting to the Local Host as a Specified User
- 类的实例与record在指针上的本质不同
- 高精度加法
- 技术不如你,但老板就是赏识他,为什么?
- SQL Tips:兼顾检索速度和精确性
- SQL Server与Oracle对比学习:表的管理和组织
- struts2——ServletActionContext
- Common Tasks :Connecting to the Local Host as the Current User
- SQL Tips:大数据量下的数据库日期操作问题
- 为什么大部分创业公司都活不过5年?
- 第八周上机任务1-撞错体验