1、
- string add(string s1,string s2)
- {
- if(s1.length()<s2.length())
- {
- string temp=s1;
- s1=s2;
- s2=temp;
- }
- for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--)
- {
- s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
- if(s1[i]-'0'>=10)
- {
- s1[i]=char((s1[i]-'0')%10+'0');
- if(i)
- s1[i-1]++;
- else
- s1='1'+s1;
- }
- }
- return s1;
- }
2、
void add(char* a,char* b,char* c) { int i,j,k,max,min,n,temp; char *s,*pmax,*pmin; max=strlen(a); min=strlen(b); if (max<min) { temp=max; max=min; min=temp; pmax=b; pmin=a; } else { pmax=a; pmin=b; } s=(char*)malloc(sizeof(char)*(max+1)); s[0]='0'; for (i=min-1,j=max-1,k=max; i>=0; i--,j--,k--) s[k]=pmin[i]-'0'+pmax[j]; for (; j>=0; j--,k--) s[k]=pmax[j]; for (i=max; i>=0; i--) if (s[i]>'9') { s[i]-=10; s[i-1]++; } if (s[0]=='0') { for (i=0; i<=max; i++) c[i-1]=s[i]; c[i-1]=' '; } else { for (i=0; i<=max; i++) c[i]=s[i]; c[i]=' '; } free(s); }