ACM(高精度算法(+ - *))

acm每周总结
今周还是做题做题做题,另外总结了一点高精度算法。
还是加油!

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
struct Maxnum{
int len;
int num[100001];
};
Maxnum a,b,w;
char c[100001],d[100001];
bool negative;
void pd(){
negative=((strlen(c)<strlen(d))||((strlen(c)==strlen(d)&&(strcmp(c,d)<0))))?true:false;
if(negative==true)
{
swap(c,d);//只有两个字符数组长度一样时才可交换;
}
}//判断正负
void prepare(){
a.len=strlen(c);
b.len=strlen(d);
for(int q=0;q<a.len;q++){a.num[a.len-q]=c[q]-'0';}
for(int q=0;q<b.len;q++){b.num[b.len-q]=d[q]-'0';}
w.len=max(a.len,b.len);
}
void Plus(){
for(int q=1;q<=w.len;q++)
{
w.num[q]+=a.num[q]+b.num[q];
w.num[q+1]+=w.num[q]/10;
w.num[q]%=10;
}
if(w.num[w.len+1]!=0){w.len+=1;}
}
void Minus() {
for(int q=1;q<=w.len;q++)
{
if(a.num[q]<b.num[q])
{
a.num[q+1]--;
a.num[q]+=10;
}
w.num[q]+=a.num[q]-b.num[q];
}
while((w.num[w.len]==0)&&(w.len!=1)){w.len--;}
}
void Multiply(){
      int x;
	for(int q=1;q<=a.len;q++)
	 {x = 0;
		for(int e=1;e<=b.len; e++)
		{
			w.num[q+e-1] += a.num[q] * b.num[e] + x;
			x = w.num[q+e-1] / 10;
			w.num[q+e-1] %= 10;
		}
		w.num[q+b.len] = x;
	}
	w.len = a.len + b.len;
	while((w.num[w.len]==0)&&(w.len!=1)){w.len--;}
}
void print()
{
for(int q=w.len;q>0;q--)
{
cout<<w.num[q];
}
}
int main()
{
    cin>>c;
    char op;
    cin>>op;
    cin>>d;
    switch(op)
   {case '+':
    {prepare();
    Plus();
    break;
    }
    case '-':
    {pd();
    prepare();
    if(negative==true){cout<<"-";}
    Minus();
    break;
    }
    case '*':
    {
    prepare();
    Multiply();
    }
   }
    print();
    return 0;
}

还是加油!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值