//任意输入两个10以内的大数据,然后进行相加计算
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
main(){
int a[SIZE]={0};
int b[SIZE]={0};
int c[SIZE+2]={0};
int i;
int n=SIZE;
srand(time(NULL));
for(i=0;i<n;i++){
a[i]=rand()%10;
b[i]=rand()%10;
}
for(i=n-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n+\n");
for(i=n-1;i>=0;i--){
printf("%d",b[i]);
}
printf("\n=\n");
/*下面进行大数相加算法*/
for(i=0;i<SIZE;i++){
c[i]=0;
}
for(i=0;i<SIZE;i++){
if(a[i]+b[i]+c[i]>=10){
c[i+1]=c[i+1]+1;
c[i]=(a[i]+b[i]+c[i])%10;
}else{
c[i]=a[i]+b[i]+c[i];
}
}
for(i=SIZE+1;i>=0;i--){
printf("%d",c[i]);
}
getchar();
getchar();
}
继续完善大数运算,相减运算,不过暂时只能实现a较b大时的运算。
</pre><pre code_snippet_id="381515" snippet_file_name="blog_20140607_2_9363456" name="code" class="html">
//任意输入两个10以内的大数据,然后进行相减计算
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
main(){
int a[SIZE]={0};
int b[SIZE]={0};
int c[SIZE+2]={0};
int i;
int n=SIZE;
srand(time(NULL));
for(i=0;i<n;i++){
a[i]=rand()%10;
b[i]=rand()%10;
}
for(i=n-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n-\n");
for(i=n-1;i>=0;i--){
printf("%d",b[i]);
}
printf("\n=\n");
/*下面进行大数相减算法*/
/*仅讨论a较大时成立。*/
for(i=0;i<SIZE;i++){
if(a[i]<b[i]){
c[i]=a[i]+10-b[i];
a[i+1]=a[i+1]-1;
}else{
c[i]=a[i]-b[i];
}
}
for(i=SIZE+1;i>=0;i--){
printf("%d",c[i]);
}
getchar();
}
</pre><p></p><pre code_snippet_id="381515" snippet_file_name="blog_20140607_2_9363456" name="code" class="html">实现大数之间的比较大小:
</pre><pre code_snippet_id="381515" snippet_file_name="blog_20140607_2_9363456" name="code" class="html">/*位数相同的数字之间比较大小*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
main(){
int a[SIZE];
int b[SIZE];
int i;
int n=SIZE;
srand(time(NULL));
for(i=0;i<n;i++){
a[i]=rand()%10;
b[i]=rand()%10;
}
printf("a=");
for(i=n-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n");
printf("b=");
for(i=n-1;i>=0;i--){
printf("%d",b[i]);
}
for(i=n-1;i>=0;i--){
if(a[i]>b[i]){
printf("\na大");
break;
}
else if(b[i]>a[i]){
printf("\nb大");
break;
}else{
continue;
}
}
getchar();
}
</pre><pre code_snippet_id="381515" snippet_file_name="blog_20140607_2_9363456" name="code" class="html">结合了前面的大数比较和大数减法,下面将它综合写出算法。
</pre><pre code_snippet_id="381515" snippet_file_name="blog_20140607_2_9363456" name="code" class="html">
<pre name="code" class="cpp">//任意输入两个10以内的大数据,然后进行相减计算
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
main(){
int a[SIZE]={0};
int b[SIZE]={0};
int c[SIZE]={0};
int i;
int n=SIZE;
int compare=0;
srand(time(NULL));
for(i=0;i<n;i++){
a[i]=rand()%10;
b[i]=rand()%10;
}
for(i=n-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n-\n");
for(i=n-1;i>=0;i--){
printf("%d",b[i]);
}
printf("\n=\n");
/*下面进行大数相减算法*/
compare=Compare(a,b,n);
if(compare==1){
for(i=0;i<SIZE;i++){
if(a[i]<b[i]){
c[i]=a[i]+10-b[i];
a[i+1]=a[i+1]-1;
}else{
c[i]=a[i]-b[i];
}
}
}else{
for(i=0;i<SIZE;i++){
if(b[i]<a[i]){
c[i]=b[i]+10-a[i];
b[i+1]=b[i+1]-1;
}else{
c[i]=b[i]-a[i];
}
}
}
if(compare==0){
printf("-");
}
for(i=SIZE-1;i>=0;i--){
printf("%d",c[i]);
}
getchar();
}
/*若输入前面数较大,则函数返回1.若后面的数较大,则返回0*/
int Compare(int a[],int b[],int n){
int i;
for(i=n-1;i>=0;i--){
if(a[i]>b[i]){
return 1;
break;
}
else if(b[i]>a[i]){
return 0;
break;
}else{
continue;
}
}
}