BIGNUM

Const fi = 'bignum.inp'; fo = 'bignum.out'; Var f, g: TEXT; a, b, sign: ANSISTRING; kq: array['0'..'9'] Of ANSISTRING; cc: CHAR; Procedure Swap(Var x, y: ANSISTRING); Var temp: ANSISTRING; Begin temp:=x; x:=y; y:=temp; End; Procedure trim(Var x: ANSISTRING); Begin While (Length(x)>1) And (x[1]='0') Do Delete(x, 1, 1); End; Function getMinus(x, y: ANSISTRING):ANSISTRING; Var i, c, mem: LONGINT; temp: ANSISTRING; Begin temp:=''; If x=y Then Exit('0'); mem:=0; For i:=Length(x) DownTo 1 Do Begin c:=(Ord(x[i])-48)-(Ord(y[i])-48+mem); mem:=0; If c<0 Then Begin Inc(c, 10); mem:=1; End; temp:=Chr(c+48)+temp; End; trim(temp); Exit(temp); End; Function getSum(x, y: ANSISTRING):ANSISTRING; Var mem, c, i: LONGINT; temp: ANSISTRING; Begin While Length(x)<Length(y) Do x:='0'+x; While Length(y)<Length(x) Do y:='0'+y; If x<y Then Begin Swap(x, y); End; mem:=0; temp:=''; For i:=Length(x) DownTo 1 Do Begin c:=Ord(x[i])+Ord(y[i])-2*48+mem; mem:=0; If c>9 Then Begin Dec(c, 10); Inc(mem); End; temp:=Chr(c+48)+temp; End; If mem<>0 Then temp:='1'+temp; trim(temp); Exit(temp); End; Function smallMul(x: ANSISTRING; y: CHAR):ANSISTRING; Var res: ANSISTRING; mem, c, i: LONGINT; Begin res:=''; mem:=0; For i:=Length(x) DownTo 1 Do Begin c:=(Ord(x[i])-48)*(Ord(y)-48)+mem; mem:=0; If c>9 Then Begin mem:=c Div 10; c:=c Mod 10; End; res:=Chr(c+48)+res; End; If mem<>0 Then res:=Chr(mem+48)+res; Exit(res); End; Function getMul(x, y: ANSISTRING):ANSISTRING; Var res, temp, sfix, sm: ANSISTRING; i: LONGINT; Begin res:=''; temp:=''; sfix:=''; For i:=Length(y) DownTo 1 Do Begin If kq[y[i]]<>'' Then temp:=kq[y[i]]+sfix Else Begin sm:=smallMul(x, y[i]); kq[y[i]]:=sm; temp:=kq[y[i]]+sfix; End; sfix:=sfix+'0'; sm:=getSum(res, temp); res:=sm; End; trim(res); Exit(res); End; BEGIN Assign(f, fi); Reset(f); Assign(g, fo); Rewrite(g); Readln(f, a); Readln(f, b); For cc:='0' To '9' Do kq[cc]:=''; sign:=''; While Length(a)<Length(b) Do a:='0'+a; While Length(b)<Length(a) Do b:='0'+b; If a<b Then Begin sign:='-'; Swap(a, b); End; Writeln(g, getSum(a, b)); Writeln(g, sign, getMinus(a, b)); Writeln(g, getMul(a, b)); Close(g); END.

1 Response

Nice

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.