2013-11-26
展开全部
加法运算a+b=c算法:先确定a和b中的最大位数k,然后依照由低至高位的顺序进行加法运算。注意进位,若高位有进位,则c的长度为k+1。源程序如下:#include <iostream.h>
#include "string"
main()
{
int a[240] = {0}, b[240] = {0}, c[241] = {0};
int i, ka, kb, k;
char a1[240], b1[240];
gets(a1); ka = strlen(a1);
gets(b1); kb = strlen(b1);
if(ka >= kb) k = ka;
else k = kb;
for(i = 0; i < ka; i++) a[i] = a1[ka-i-1] - '0';
for(i = 0; i < kb; i++) b[i] = b1[kb-i-1] - '0';
for(i = 0; i < k; i++)
{
c[i] = a[i] + b[i] + c[i];
c[i+1] = c[i+1] + c[i]/10;
c[i] = c[i]%10;
}
if(c[k]) k++;
for(i = k-1; i >= 0; i--) cout<<c[i];
}
#include "string"
main()
{
int a[240] = {0}, b[240] = {0}, c[241] = {0};
int i, ka, kb, k;
char a1[240], b1[240];
gets(a1); ka = strlen(a1);
gets(b1); kb = strlen(b1);
if(ka >= kb) k = ka;
else k = kb;
for(i = 0; i < ka; i++) a[i] = a1[ka-i-1] - '0';
for(i = 0; i < kb; i++) b[i] = b1[kb-i-1] - '0';
for(i = 0; i < k; i++)
{
c[i] = a[i] + b[i] + c[i];
c[i+1] = c[i+1] + c[i]/10;
c[i] = c[i]%10;
}
if(c[k]) k++;
for(i = k-1; i >= 0; i--) cout<<c[i];
}
推荐于2017-05-21
展开全部
#include<iostream>
using namespace std;
int main()
{ string s1,s2;
int a[120],b[120],he;
int i;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>s1>>s2;
a[0]=s1.length();
for(i=1;i<=a[0];i++)
{a[i]=s1[a[0]-i]-'0'; }
b[0]=s2.length();
for(i=1;i<=b[0];i++)
{b[i]=s2[b[0]-i]-'0'; }
he=(a[0]>b[0]?a[0]:b[0]);
for(i=1;i<=he;i++)
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
he++;
while((a[he]==0)&&(he>1)) he--;
for(i=he;i>=1;i--)
cout<<a[i];
}
using namespace std;
int main()
{ string s1,s2;
int a[120],b[120],he;
int i;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>s1>>s2;
a[0]=s1.length();
for(i=1;i<=a[0];i++)
{a[i]=s1[a[0]-i]-'0'; }
b[0]=s2.length();
for(i=1;i<=b[0];i++)
{b[i]=s2[b[0]-i]-'0'; }
he=(a[0]>b[0]?a[0]:b[0]);
for(i=1;i<=he;i++)
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
he++;
while((a[he]==0)&&(he>1)) he--;
for(i=he;i>=1;i--)
cout<<a[i];
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-26
展开全部
哥们,说详细点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char a1[100],b1[100];
int a[100],b[100],c[100],lena,lenb,lenc,i,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(a1);
gets(b1);
lena=strlen(a1);
lenb=strlen(b1);
for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
lenc=1;
x=0;
while (lenc<=lena||lenc<=lenb)
{
c[lenc]=a[lenc]+b[lenc]+x;
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if (c[lenc]==0)
lenc--;
for (i=lenc;i>=1;i--)
cout<<c[i];
cout<<endl;
return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char a1[100],b1[100];
int a[100],b[100],c[100],lena,lenb,lenc,i,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(a1);
gets(b1);
lena=strlen(a1);
lenb=strlen(b1);
for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
lenc=1;
x=0;
while (lenc<=lena||lenc<=lenb)
{
c[lenc]=a[lenc]+b[lenc]+x;
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if (c[lenc]==0)
lenc--;
for (i=lenc;i>=1;i--)
cout<<c[i];
cout<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询