高精度减法
来源:互联网 发布:linux系统管理员职业 编辑:程序博客网 时间:2024/04/29 15:15
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
void subtract(char *a,char *b)
{
int i,j,top=-1,k;
if(strlen(a)>=strlen(b))
{
int *a1=(int *)malloc(sizeof(int)*strlen(a));
for(i=strlen(a)-1,j=strlen(b)-1;i>=0||j>=0;i--,j--)
{
if(j>=0)
{
int temp=a[i]-b[j];
if(temp>=0)
{
top++;
a1[top]=temp;
}
else
{
top++;
temp=a[i]-b[j]+10;
a[i-1]--;
a1[top]=temp;
}
}
else
{
top++;
a1[top]=a[i]-'0';//一定要减去'0'
}
}
for(i=top;i>-1&&a1[i]==0;i--);
for(k=i;k>-1;k--)
cout<<a1[k];
free(a1);
}
}
int main()
{
char a[1000],b[1000];
cin>>a;
cin>>b;
if(strlen(a)>strlen(b))
{
subtract(a,b);
}
else
{
cout<<"-";
subtract(b,a);
}
system("pause");
return 0;
}
#include<string>
#include<malloc.h>
using namespace std;
void subtract(char *a,char *b)
{
int i,j,top=-1,k;
if(strlen(a)>=strlen(b))
{
int *a1=(int *)malloc(sizeof(int)*strlen(a));
for(i=strlen(a)-1,j=strlen(b)-1;i>=0||j>=0;i--,j--)
{
if(j>=0)
{
int temp=a[i]-b[j];
if(temp>=0)
{
top++;
a1[top]=temp;
}
else
{
top++;
temp=a[i]-b[j]+10;
a[i-1]--;
a1[top]=temp;
}
}
else
{
top++;
a1[top]=a[i]-'0';//一定要减去'0'
}
}
for(i=top;i>-1&&a1[i]==0;i--);
for(k=i;k>-1;k--)
cout<<a1[k];
free(a1);
}
}
int main()
{
char a[1000],b[1000];
cin>>a;
cin>>b;
if(strlen(a)>strlen(b))
{
subtract(a,b);
}
else
{
cout<<"-";
subtract(b,a);
}
system("pause");
return 0;
}
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 高精度减法
- 可以自定义导出数据库存储过程(含加密过程)的一个存储过程
- complie the open source flex sdk is so easy
- 高精度加法
- 【人工智能】算法--Precision/Recall和ROC曲线原理以及Matlab源码
- “设计模式”学习之九:策略、模板方法与访问者(行为型)
- 高精度减法
- Gauss-Jordan elimination
- Android腾讯微博客户端开发二:相关工具篇
- 关于javascript判断不同浏览器的类型和版本号
- 安装Realtek HD Audio Driver 失败
- Android腾讯微博客户端开发三:多账号管理的实现
- 多元函数微分学
- mysql的主从构架,复制,半同步,SSL加密复制的实现
- 面试题总结 - C 语言 - 位操作(bit operation)