1. Adding large numbers
stringSumstringS1,stringS2) { if(S1.length () <s2.length ()) { stringtemp=S1; S1=S2; S2=temp; } inti,j; for(I=s1.length ()-1, J=s2.length ()-1; i>=0; I--, j--) {S1[i]=Char(S1[i]+ (j>=0? s2[j]-'0':0));//attention to detail if(s1[i]-'0'>=Ten) {S1[i]=Char((s1[i]-'0')%Ten+'0'); if(i) s1[i-1]++; Elses1='1'+S1; } } returnS1; }
2. Multiply the number of integers by number of
stringMultiply (stringSintX//large number multiplied by number of shaping{reverse (S.begin (), S.end ()); intcmp=0; for(intI=0; I<s.size (); i++) {CMP= (s[i]-'0') *x+CMP; S[i]= (cmp%Ten+'0'); CMP/=Ten; } while(CMP) {s+ = (cmp%Ten+'0'); CMP/=Ten; } reverse (S.begin (), S.end ()); returns; }
stringExcept (stringSintX//large number divided by number of shaping{ intcmp=0, ok=0; stringans=""; for(intI=0; I<s.size (); i++) {CMP= (cmp*Ten+s[i]-'0'); if(cmp>=x) {OK=1; Ans+ = (cmp/x+'0'); CMP%=x; } Else{ if(ok==1) ans+='0';//watch this. To find out what was wrong } } returnans; }
C + + large number calculation template