大数加减
2015-07-14 17:02
204 查看
代码:
#include<iostream> #include<stdio.h> #include<string.h> #define Len 3000//大数的长度 using namespace std; int Input (char n[])//将大数读入的函数 { char s[Len]; int i,l; for(i=0; i<Len; i++) n[i]=0; if(scanf("%s",s)<1) return -1; l= strlen(s); for(i=0; i<l; i++) //将输入的大数逆置 n[i]=s[l-i-1]-'0'; } int Print (char n[]) { int i; for(i=Len-1; i>0; i--) if(n[i]!=0) break; for(; i>=0; i--) printf("%d",n[i]); printf("\n"); } void Add (char a[],char b[],char c[])//大数加法 { int i=0; for(i=0; i<Len; i++) c[i]=a[i]+b[i]; for(i=0; i<Len; i++) //处理进位 { if(c[i]>=10) { c[i+1]+=c[i]/10; c[i]=c[i]%10; } } } void Mul(char a[],char b[],char c[])//大数乘法 { int i,j; int alen=strlen(a),blen=strlen(b); for(i=0; i<Len; i++) c[i]=0; for(i=0; i<alen; i++) for(j=0; j<blen; j++) //处理进位 { c[i+j]+=a[i]*b[j]; if(c[i+j]>=10) { c[i+j+1]+=c[i+j]/10; c[i+j]%=10; } } } int main() { char a[Len],b[Len],c[Len]; Input(a); Input(b); Add(a,b,c); Print(c); Mul(a,b,c); Print(c); return 0; }
相关文章推荐
- Android开发数据相关
- adb devices后不显示设备号解决办法
- 埃博拉病毒和微生物现代战争
- Ajax方法不执行success回调函数,而执error回调函数
- Leetcode #43 Multiply Strings
- 链表
- 七、高精度
- 解读MT7620A上的DTS文件
- [转载]树、森林和二叉树的转换
- [LeetCode] Excel Sheet Column Number
- What's the difference between all the Selection Segues?
- 求两个有序数组的中位数
- spring Bean范围scope=singleton或prototype
- GRE写作必备句型
- Git Stat(Git 代码提交统计)
- 你会编写高质量的Objective-C代码
- avro 1.7.7中存在致命的bug, 可以使用1.7.4版本
- 并查集
- 课程设计最基本的多文件组织
- 单例