Guest User

Adunare

a guest
Mar 27th, 2011
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.84 KB | None | 0 0
  1. //Autor:Paul4games
  2. program Project1;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7.   SysUtils;
  8.  
  9. function Adunare(nr,nr1: integer): string; stdcall;
  10. var
  11.   Str,Str1: string;
  12.   j,i,k: integer;
  13.   a,b: array of char;
  14. begin
  15. Str := IntToStr(nr);
  16. Str1 := IntToStr(nr1);
  17. if Length(Str)>Length(Str1) then
  18.   repeat
  19.     Insert('0', Str1, 1);
  20.   until Length(Str1)=Length(Str)
  21. else if Length(Str1)>Length(Str) then
  22.   repeat
  23.     Insert('0', Str1, 1);
  24.   until Length(Str)=Length(Str1);
  25. SetLength(a, Length(Str)+1);
  26. SetLength(b, Length(Str1)+1);
  27. for j := Length(Str) downto 1 do
  28.   begin
  29.   a[j] := Str[j];
  30.   b[j] := Str1[j];
  31.   end;
  32. a[0] := '0';
  33. b[0] := '0';  
  34. k := 0;
  35. for i := Length(Str) downto 0 do
  36.   begin
  37.   if StrToInt(a[i])+StrToInt(b[i])<>0 then
  38.     begin
  39.     if k>0 then
  40.       begin
  41.       if StrToInt(a[i]) + StrToInt(b[i]) + k>9 then
  42.         begin
  43.         k := StrToInt(a[i]) + StrToInt(b[i]) + k;
  44.         Result := IntToStr(k mod 10) + Result;
  45.         k := k div 10;
  46.         end
  47.       else
  48.         begin
  49.         if (StrToInt(a[i+1]) + StrToInt(b[i+1])) or (StrToInt(a[i+1]) + StrToInt(b[i+1]) + k)>9 then
  50.           begin
  51.           Result :=IntToStr(StrToInt(a[i]) + StrToInt(b[i])+ k) + Result;
  52.           k := k div 10;
  53.           end
  54.         else
  55.           begin
  56.           Result := IntToStr(StrToInt(a[i]) + StrToInt(b[i]));
  57.           end;
  58.         end;
  59.       end
  60.     else
  61.       begin
  62.       if StrToInt(a[i]) + StrToInt(b[i])>9 then
  63.         begin
  64.         k := StrToInt(a[i]) + StrToInt(b[i]);
  65.         Result := IntToStr(k mod 10) + Result;
  66.         k := k div 10;
  67.         end
  68.       else
  69.         Result := IntToStr(StrToInt(a[i])+StrToInt(b[i])) + Result;
  70.       end;
  71.     end
  72.   else
  73.     if (StrToInt(a[i+1]) + StrToInt(b[i+1])) or (StrToInt(a[i+1]) + StrToInt(b[i+1]) + k)>9 then
  74.     Result := IntToStr(k) + Result;
  75.   end;
  76. end;
  77.  
  78. var
  79.   t: string;
  80. begin
  81. t := Adunare(3888, 333);
  82. WriteLn(t);
  83. ReadLn;
  84. end.
Advertisement
Add Comment
Please, Sign In to add comment