php高精度相加,c++,_高精度加法的一个问题,c++ - phpStudy

高精度加法的一个问题

/*ReadMe

*only plus & multiply return superInt.

*The others print out.

*the function is_bigger return true whie a>=b,

*function name:

*superInt superPlus(superInt a,superInt b)

*superInt superMultiply(superInt a,superInt b)

*void superMinus(superInt a,superInt b)

*void print(superInt a)

*void print(superInt a)

*bool is_bigger(superInt a,superInt b)

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define MAXN (100000+10)

using namespace std;

struct superInt{

int bit;

int ia[MAXN];

};

superInt a,b,add;

void clear(superInt &a)

{

a.bit = 0;

memset(a.ia, 0, sizeof(a.ia));

}

void read(superInt &a)

{

char temp[MAXN];

cin >> temp;

int size = strlen(temp);

a.bit = size;

int tempNum = 0;

for (int i = size; i >= 1; i--)

{

a.ia[i] = temp[tempNum] - 48;

tempNum++;

}

}

void print(superInt a)

{

for (int i = a.bit; i >= 1; i--)

cout << a.ia[i];

cout << endl;

}

bool is_bigger(superInt a, superInt b)

{

if (a.bit>b.bit)

return true;

else if (a.bit == b.bit)

{

if (a.ia[a.bit] >= b.ia[b.bit])

return true;

}

else

return false;

}

superInt superPlus(superInt a,superInt b)

{

int size = max(a.bit,b.bit);

superInt final;

for(int i=1;i<=size;i++)

{

int temp=a.ia[i]+b.ia[i]+final.ia[i];

final.ia[i]=temp%10;

final.ia[i+1]+=temp/10;

}

if(final.ia[size+1]!=0)

final.bit=size+1;

else

final.bit=size;

return final;

}

void superMinus(superInt a, superInt b)

{

if (is_bigger(a, b) == false)//a is smaller

{

cout << "-";

superInt temp = b;

for (int i = 1; i <= temp.bit; i++)

{

if (temp.ia[i]

{

temp.ia[i + 1]--;

temp.ia[i] += 10;

}

temp.ia[i] -= a.ia[i];

}

while(temp.ia[temp.bit]==0&&temp.bit>1)

{

temp.bit-=1;

}

print(temp);

}

else

{

superInt temp = a;

for (int i = 1; i <= temp.bit; i++)

{

if (temp.ia[i]

{

temp.ia[i + 1]--;

temp.ia[i] += 10;

}

temp.ia[i] -= b.ia[i];

}

while(temp.ia[temp.bit]==0&&temp.bit>1)

{

temp.bit-=1;

}

print(temp);

}

return;

}

superInt superMultiply(superInt a, superInt b)//1*1

{

superInt final;

clear(final);

for (int i = 1; i <= b.bit; i++)//

{

int bit=b.ia[i];//bit=1

superInt temp;

clear(temp);

for(int j=1;j<=a.bit;j++)

{

int sd;//sd==number of significant digit

temp.ia[j]+=bit*a.ia[j];

temp.ia[j+1]+=temp.ia[j]/10;

temp.ia[j]%=10;

if(temp.ia[a.bit+1]!=0)

temp.bit=a.bit+1;

sd=temp.bit;

temp.bit+=i;

temp.bit--;

for(int k=0;k

{

temp.ia[temp.bit-k]=temp.ia[sd-k];

}

}

final=superPlus(final,temp);//ToDo

}

return final;

}

int main()

{

clear(a);

clear(b);

read(a);

read(b);

superInt c;

c=superMultiply(a,b);

print(c);

return 0;

}

乘法有问题……调试抛出sigsegv;

问题应该是final=superPlus(final,temp);//ToDo这一行,但是菜鸟实在不懂为什么

相关阅读:

ubuntu里的hostname

如何重构现有代码

为什么大家都不推荐使用MySQL触发器而用存储过程?

swiper tab 左右移动栏目的时候怎么禁止a的触发

angularjs指令中的template没效果

JavaScript 的html标签 怎么用?

java关于webservice的代码求助

如何生成描边的字体并保存成图片,用canvas或php解决?

swiper和seajs结合使用,谁用过吗?

python如何获取当前工程根目录

android style文件 parent @android与android,Theme有什么不同 如图

一个关于JS的正则匹配问题

设计HTML5插件有什么设计原则或设计经验,这些原则或经验的产生原因又是什么?

IOS 外国的经纬度 反地理编码 报错

java main方法中通过ApplicationContext去拿bean(用注解的bean)getbean拿不到?

Jfinal Resty-router框架中,如何获取POST请求中的参数.

d3js画动态tree图,点击后层级混乱,求助

springMVC启动报错

下载了别人的源码,出现ld:library not found for -lXXX 的问题

mysql 怎么从多种维度删选商品

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值