64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
void add(char *large,char *small,const int llenght,const int slenght)
{
char *answer = large;
char *pLarge = &large[llenght];
char *pSmall = &small[slenght];
int count = llenght;
while(pSmall != small)
{
answer[count] = *pLarge + *pSmall;
pLarge--;
pSmall--;
count--;
}
answer[count] =*pLarge + *pSmall;
for(count = llenght;count > 0;count--)
{
if(answer[count] > 9)
{
answer[count] -= 10;
answer[count-1] += 1;
}
}
count = 0;
while(count < llenght)
cout<<(int)answer[count++];
}
int main()
{
int times;
cin>>times;
char numA[1002];
char numB[1002];
for(int i = 1;i <= times;i++)
{
cin>>numA;
cin>>numB;
cout<<"Case "<<i<<':'<<endl;
cout<<numA<<' '<<'+'<<' '<<numB<<' '<<'='<<' ';
int alenght = strlen(numA);
int blenght = strlen(numB);
int count = 0;
for(;count < alenght;count++)
numA[count] -= 48;
count = 0;
for(;count < blenght;count++)
numB[count] -= 48;
if(alenght > blenght)
add(numA,numB,alenght,blenght);
else
add(numB,numA,blenght,alenght);
cout<<endl;
if(i != times)
cout<<endl;
}
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
void Reverse(char array[], int size)
{
int i = 0, j = size-1;
char tmp;
while(i<j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}
void Add(char * a, char * b, char * c)
{
int i = 0;
int tmp, r = 0;
while(a[i]!='\0' && b[i]!='\0')
{
tmp = a[i] - '0' + b[i] - '0' + r;
if(tmp > 9)
{
r=1;
tmp -= 10;
}
else
r=0;
c[i] = tmp + '0';
i++;
}
if(a[i] == '\0' && b[i] == '\0')
{
if(r==1)
c[i] = '1';
}
else if(a[i] == '\0')
{
if(r==0)
{
strcpy(c+i, b+i);
}
else
{
Add("1", b+i, c+i);
}
}
else // b[i] == '\0'
{
if(r==0)
{
strcpy(c+i, a+i);
}
else
{
Add("1", a+i, c+i);
}
}
Reverse(c, strlen(c));
}
int main()
{
char a[] = {'1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9','\0'};
char b[] = {'9','9','8','8','7','7','6','6','5','5','4','4','3','3','2','2','1','1','\0'};
char c[1000];
memset(c, 1000, 0);
Add(a, b, c);
cout<<c<<endl;
cin.get();
}