Advertisement
mixster

mixster

Jan 8th, 2009
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.38 KB | None | 0 0
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, SysUtils, CustApp
  10.   { you can add units after this };
  11.  
  12. type
  13.  
  14.   { problem1 }
  15.  
  16.   problem1 = class(TCustomApplication)
  17.   protected
  18.     procedure DoRun; override;
  19.   public
  20.     constructor Create(TheOwner: TComponent); override;
  21.     destructor Destroy; override;
  22.     procedure WriteHelp; virtual;
  23.   end;
  24. { problem1 }
  25.  
  26. procedure AddString(var s: string; a: string);
  27. var
  28.   i, t, o: Integer;
  29.   n: Boolean;
  30.   m: string;
  31. begin
  32.   if Length(s) < Length(a) then
  33.     exit;
  34.  
  35.   n := false;
  36.   o := Length(s) - Length(a);
  37.   for i := Length(a) downto 1 do
  38.   begin
  39.     t := StrToIntDef(Copy(s, i + o, 1), 0);
  40.     t := t + StrToIntDef(Copy(a, i, 1), 0);
  41.     if n then
  42.     begin
  43.       t := t + 1;
  44.       n := false;
  45.     end;
  46.     if t >= 10 then
  47.     begin
  48.       n := true;
  49.       t := t mod 10;
  50.     end;
  51.     m := IntToStr(t);
  52.     s[i + o] := m[1];
  53.   end;
  54.  
  55.   if n and (o > 0) then
  56.   begin
  57.     t := StrToIntDef(Copy(s, 1, o), 0);
  58.     t := t + 1;
  59.     m := IntToStr(t);
  60.     if Length(m) > o then
  61.     begin
  62.       s := '0' + s;
  63.       o := o + 1;
  64.     end;
  65.     for i := 1 to o do
  66.       s[i] := m[i];
  67.   end
  68.   else if n then
  69.     s := '1' + s;
  70. end;
  71.  
  72. function ConvertNum(num: Integer): string;
  73. begin
  74.   case num of
  75.     0: Result := 'zero';
  76.     1: Result := 'one';
  77.     2: Result := 'two';
  78.     3: Result := 'three';
  79.     4: Result := 'four';
  80.     5: Result := 'five';
  81.     6: Result := 'six';
  82.     7: Result := 'seven';
  83.     8: Result := 'eight';
  84.     9: Result := 'nine';
  85.   end;
  86. end;
  87.  
  88. function ConvertTeen(num: Integer): string;
  89. begin
  90.   case num mod 10 of
  91.     0: Result := 'ten';
  92.     1: Result := 'eleven';
  93.     2: Result := 'twelve';
  94.     3: Result := 'thirteen';
  95.     4: Result := 'fourteen';
  96.     5: Result := 'fifteen';
  97.     6: Result := 'sixteen';
  98.     7: Result := 'seventeen';
  99.     8: Result := 'eighteen';
  100.     9: Result := 'nineteen';
  101.   end;
  102. end;
  103.  
  104. function NumToTen(num: Integer): string;
  105. begin
  106.    case num div 10 of
  107.     1: Result := ConvertTeen(num);
  108.     2: Result := 'twenty';
  109.     3: Result := 'thirty';
  110.     4: Result := 'fourty';
  111.     5: Result := 'fifty ';
  112.     6: Result := 'sixty';
  113.     7: Result := 'seventy';
  114.     8: Result := 'eighty';
  115.     9: Result := 'ninety';
  116.   end;
  117.  
  118.   if num mod 10 <> 0 then
  119.     if num >= 20 then
  120.       Result := Result + ' ' + ConvertNum(num mod 10);
  121. end;
  122.  
  123. function NumToHundred(num: Integer): string;
  124. begin
  125.   Result := ConvertNum(num div 100) + ' hundred';
  126.   if (num div 10) mod 10 <> 0 then
  127.     Result := Result + ' and ' + NumToTen(num mod 100)
  128.   else if (num mod 10 <> 0) then
  129.     Result := Result + ' and ' + ConvertNum(num mod 10);
  130. end;
  131.  
  132. function NumToThousand(num: Integer): string;
  133. begin
  134.   Result := ConvertNum(num div 1000) + ' thousand';
  135.   if (num div 100) mod 10 <> 0 then
  136.     Result := Result + ' ' + NumToHundred(num mod 1000)
  137.   else if (num div 10 mod 10 <> 0) then
  138.     Result := Result + ' and ' + NumToTen(num mod 100)
  139.   else if (num div 100 mod 10 <> 0) then
  140.     Result := Result + ' and ' + ConvertNum(num mod 10);
  141. end;
  142.  
  143. function NumToStr(num: Integer): string;
  144. begin
  145.   case Length(IntToStr(num)) of
  146.     1: Result := ConvertNum(num);
  147.     2: Result := NumToTen(num);
  148.     3: Result := NumToHundred(num);
  149.     4: Result := NumToThousand(num);
  150.   end;
  151. end;
  152.  
  153. procedure problem1.DoRun;
  154. var
  155.   ErrorMsg, s: string;
  156.   i, c: Integer;
  157. begin
  158.   // quick check parameters
  159.   ErrorMsg:=CheckOptions('h','help');
  160.   if ErrorMsg<>'' then begin
  161.     ShowException(Exception.Create(ErrorMsg));
  162.     Halt;
  163.   end;
  164.  
  165.   // parse parameters
  166.   if HasOption('h','help') then begin
  167.     WriteHelp;
  168.     Halt;
  169.   end;
  170.  
  171.   c := 0;
  172.   for i := 1 to 1000 do
  173.   begin
  174.     s := StringReplace(NumToStr(i), ' ', '', [rfReplaceAll]);
  175.     c := c + Length(s);
  176.     writeln(s, ' :: ', c);
  177.   end;
  178.  
  179.   writeln(c);
  180.   //ReadLn;
  181.  
  182.   // stop program loop
  183.   Terminate;
  184. end;
  185.  
  186. constructor problem1.Create(TheOwner: TComponent);
  187. begin
  188.   inherited Create(TheOwner);
  189.   StopOnException:=True;
  190. end;
  191.  
  192. destructor problem1.Destroy;
  193. begin
  194.   inherited Destroy;
  195. end;
  196.  
  197. procedure problem1.WriteHelp;
  198. begin
  199.   { add your help code here }
  200.   writeln('Usage: ',ExeName,' -h');
  201. end;
  202.  
  203. var
  204.   Application: problem1;
  205. begin
  206.   Application:=problem1.Create(nil);
  207.   Application.Title:='problem1';
  208.   Application.Run;
  209.   Application.Free;
  210. end.
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement