Advertisement
KennasSticky

p20_Kenna

Feb 9th, 2022
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.12 KB | None | 0 0
  1. unit main_u;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;
  8.  
  9. type
  10.   TForm2 = class(TForm)
  11.     Label1: TLabel;
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.     function stringMultiply(a, b : string) : string;
  16.     function stringAdd(a, b : string) : string;
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   Form2: TForm2;
  23.  
  24. implementation
  25.  
  26. {$R *.dfm}
  27.  
  28. { TForm2 }
  29.  
  30. procedure TForm2.FormCreate(Sender: TObject);
  31. var
  32.   sprod : string;
  33.   i, isum : integer;
  34.   start, freq, stop: int64;
  35. begin
  36.  
  37.   QueryPerformanceFrequency(freq);
  38.   QueryPerformanceCounter(start);
  39.  
  40.   sprod := '1';
  41.  
  42.   for I := 2 to 100 do
  43.     begin
  44.       sprod := stringMultiply(sprod, inttostr(i));
  45.     end;
  46.  
  47.   isum := 0;
  48.  
  49.   for I := 1 to length(sprod) do
  50.     begin
  51.       isum := isum + strtoint(sprod[i]);
  52.     end;
  53.  
  54.   QueryPerformanceCounter(stop);
  55. //  ShowMessage(FloattostrF((stop - start) / freq, ffNumber, 10, 3)); // Solution time will be shown upon program completion.
  56.  
  57.   ShowMessage(inttostr(isum));
  58.   Label1.Caption := FloattostrF((stop - start) / freq, ffNumber, 10, 3);
  59. end;
  60.  
  61. function TForm2.stringAdd(a, b: string): string;
  62. var
  63.   i, j, alen, blen, carry, itotal : integer;
  64.   stemp : string;
  65. begin
  66.   if (length(a) < length(b)) then
  67.     begin
  68.       stemp := a;
  69.       a := b;
  70.       b := stemp;
  71.     end;
  72.  
  73.   // append the 0's to keep the peace
  74.   alen := Length(a);
  75.   blen := Length(b);
  76.  
  77.   for I := 1 to (alen - blen) do
  78.      begin
  79.       b := '0' + b;
  80.      end;
  81.  
  82.   carry := 0;
  83.   stemp := '';
  84.  
  85.   // perform the addition
  86.   for I := alen downto 1 do
  87.       begin
  88.         itotal := strtoint(a[i]) + strtoint(b[i]);
  89.  
  90.         itotal := itotal + carry;
  91.  
  92.         carry := itotal div 10;
  93.         itotal := itotal mod 10;
  94.  
  95.         stemp := inttostr(itotal) + stemp;
  96.       end;
  97.  
  98.   result := stemp;
  99. end;
  100.  
  101. function TForm2.stringMultiply(a, b: string): string;
  102. var
  103.   alen, blen : integer;
  104.   i, j, carry, curprod : integer;
  105.   stemp, stotal : string;
  106. begin
  107.  
  108.   alen := Length(a);
  109.   blen := length(b);
  110.  
  111.   stotal := '';
  112.  
  113.   for I := blen downto 1 do
  114.       begin
  115.         carry := 0;
  116.         stemp := '';
  117.         for j := alen downto 1 do
  118.           begin
  119.             // find product from current pointers
  120.             curprod := strtoint(a[j]) * strtoint(b[i]);
  121.             curprod := curprod + carry;
  122.  
  123.             // adjust carry
  124.             if (curprod > 9) then
  125.               begin
  126.                 carry := curprod div 10;
  127.                 curprod := curprod mod 10;
  128.               end
  129.             else carry := 0;
  130.  
  131.             // append to the tempString
  132.             stemp := IntToStr(curprod) + stemp;
  133.           end;
  134.  
  135.           // position multiplier
  136.           for j := 1 to blen - i do
  137.               begin
  138.                 stemp := stemp + '0';
  139.               end;
  140.  
  141.           // find the string total
  142.  
  143.           stotal := stringAdd(stotal, stemp);
  144.       end;
  145.  
  146.   stotal := inttostr(carry) + stotal;
  147.   result := stotal;
  148. end;
  149.  
  150. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement