Positive integer summation (eighty-four thousand Dharma)

The clearance task: write to achieve a positive integer of 100 or less and two small programs.
Programming requirements of programming a positive integer and less than two 100. For example, input two positive integers "9876543210123456789012345" and "3264823749328439473242109", and the program output between the two "13141366959451896262254454."
Requirements: Design of at least 4 prepared by a program implemented functions and gives annotations for each function in the program, detailed description of the performance function.
The program does not need to consider the case of integer digits exceed 100. You can not have other extra cin and cout statements when submitting code.
Results are as follows:
Input: 9876543210123456789012345 3264823749328439473242109
Output: 13141366959451896262254454

#include<iostream> 
#include<cstring> 
using namespace std; 
int main() 
{ 
string str1,str2; 
int a[250],b[250],len; 
int i; 
memset(a,0,sizeof(a)); 
memset(b,0,sizeof(b)); 
cin>>str1>>str2; 
a[0]=str1.length(); 
for(i=1;i<=a[0];i++) 
{a[i]=str1[a[0]-i]-'0';} 
b[0]=str2.length(); 
for(i=1;i<=b[0];i++) 
{b[i]=str2[b[0]-i]-'0'; } 
len=(a[0]>b[0]?a[0]:b[0]); 
for(i=1;i<=len;i++) 
{ 
a[i]+=b[i]; 
a[i+1]+=a[i]/10; 
a[i]%=10; 
} 
len++; 
while((a[len]==0)&&(len>1)) len--; 
for(i=len;i>=1;i--) 
cout<<a[i]; 
cout<<endl; 
system("pause"); 
return 0; 
} 

or

#include<iostream>
#define n 101
using namespace std;
void fun1(char a1[],char a2[]);
void fun2(char b1 [],char b2[]);
void jinweiqiuhe(char a2[],char b2[],char c[]);
void fuzhi(char c[],char d[]);
int main()
{
    char a1[n]={0},a2[n]={0},b1[n]={0},b2[n]={0},c[n]={0},d[n]={0};
  cin>>a1;
  cin>>b1;
  fun1( a1, a2);
  fun2( b1, b2);
  jinweiqiuhe( a2, b2, c);
  fuzhi(c,d);;
  cout<<d;
    return 0;
}
void fun1(char a1[],char a2[])
{
    int i,j;
    for(i=0;a1[i]!=0;i++);  
    {
     for(j=0;j<i;j++)
     {
         a2[n-i+j]=a1[j]-'0';
        }
    }
}
void fun2(char b1 [],char b2[])    
{
    int i,j;
    for(i=0;b1[i]!=0;i++);
    {
     for(j=0;j<i;j++)
     {
         b2[n-i+j]=b1[j]-'0';
        }
 }
}
void jinweiqiuhe(char a2[],char b2[],char c[])
{
    int i,j;
        for(i=n-1,j=0;i>-1;i--)
    {
        c[i]=(a2[i]+b2[i]+j)%10+'0';
        j=(a2[i]+b2[i]+j)/10;
    }
}
void fuzhi(char c[],char d[])
{
    int i,j;
    for(i=0;c[i]=='0';i++);
    for(j=0;j<n-i;j++)
    {
        d[j]=c[i+j];
    }
}
Published 102 original articles · won praise 93 · views 4974

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104639159