Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- {$mode objfpc}{$H+}
- uses
- {$IFDEF UNIX}{$IFDEF UseCThreads}
- cthreads,
- {$ENDIF}{$ENDIF}
- Classes, SysUtils, CustApp
- { you can add units after this };
- type
- { problem1 }
- problem1 = class(TCustomApplication)
- protected
- procedure DoRun; override;
- public
- constructor Create(TheOwner: TComponent); override;
- destructor Destroy; override;
- procedure WriteHelp; virtual;
- end;
- { problem1 }
- procedure AddString(var s: string; a: string);
- var
- i, t, o: Integer;
- n: Boolean;
- m: string;
- begin
- if Length(s) < Length(a) then
- exit;
- n := false;
- o := Length(s) - Length(a);
- for i := Length(a) downto 1 do
- begin
- t := StrToIntDef(Copy(s, i + o, 1), 0);
- t := t + StrToIntDef(Copy(a, i, 1), 0);
- if n then
- begin
- t := t + 1;
- n := false;
- end;
- if t >= 10 then
- begin
- n := true;
- t := t mod 10;
- end;
- m := IntToStr(t);
- s[i + o] := m[1];
- end;
- if n and (o > 0) then
- begin
- t := StrToIntDef(Copy(s, 1, o), 0);
- t := t + 1;
- m := IntToStr(t);
- if Length(m) > o then
- begin
- s := '0' + s;
- o := o + 1;
- end;
- for i := 1 to o do
- s[i] := m[i];
- end
- else if n then
- s := '1' + s;
- end;
- function ConvertNum(num: Integer): string;
- begin
- case num of
- 0: Result := 'zero';
- 1: Result := 'one';
- 2: Result := 'two';
- 3: Result := 'three';
- 4: Result := 'four';
- 5: Result := 'five';
- 6: Result := 'six';
- 7: Result := 'seven';
- 8: Result := 'eight';
- 9: Result := 'nine';
- end;
- end;
- function ConvertTeen(num: Integer): string;
- begin
- case num mod 10 of
- 0: Result := 'ten';
- 1: Result := 'eleven';
- 2: Result := 'twelve';
- 3: Result := 'thirteen';
- 4: Result := 'fourteen';
- 5: Result := 'fifteen';
- 6: Result := 'sixteen';
- 7: Result := 'seventeen';
- 8: Result := 'eighteen';
- 9: Result := 'nineteen';
- end;
- end;
- function NumToTen(num: Integer): string;
- begin
- case num div 10 of
- 1: Result := ConvertTeen(num);
- 2: Result := 'twenty';
- 3: Result := 'thirty';
- 4: Result := 'fourty';
- 5: Result := 'fifty ';
- 6: Result := 'sixty';
- 7: Result := 'seventy';
- 8: Result := 'eighty';
- 9: Result := 'ninety';
- end;
- if num mod 10 <> 0 then
- if num >= 20 then
- Result := Result + ' ' + ConvertNum(num mod 10);
- end;
- function NumToHundred(num: Integer): string;
- begin
- Result := ConvertNum(num div 100) + ' hundred';
- if (num div 10) mod 10 <> 0 then
- Result := Result + ' and ' + NumToTen(num mod 100)
- else if (num mod 10 <> 0) then
- Result := Result + ' and ' + ConvertNum(num mod 10);
- end;
- function NumToThousand(num: Integer): string;
- begin
- Result := ConvertNum(num div 1000) + ' thousand';
- if (num div 100) mod 10 <> 0 then
- Result := Result + ' ' + NumToHundred(num mod 1000)
- else if (num div 10 mod 10 <> 0) then
- Result := Result + ' and ' + NumToTen(num mod 100)
- else if (num div 100 mod 10 <> 0) then
- Result := Result + ' and ' + ConvertNum(num mod 10);
- end;
- function NumToStr(num: Integer): string;
- begin
- case Length(IntToStr(num)) of
- 1: Result := ConvertNum(num);
- 2: Result := NumToTen(num);
- 3: Result := NumToHundred(num);
- 4: Result := NumToThousand(num);
- end;
- end;
- procedure problem1.DoRun;
- var
- ErrorMsg, s: string;
- i, c: Integer;
- begin
- // quick check parameters
- ErrorMsg:=CheckOptions('h','help');
- if ErrorMsg<>'' then begin
- ShowException(Exception.Create(ErrorMsg));
- Halt;
- end;
- // parse parameters
- if HasOption('h','help') then begin
- WriteHelp;
- Halt;
- end;
- c := 0;
- for i := 1 to 1000 do
- begin
- s := StringReplace(NumToStr(i), ' ', '', [rfReplaceAll]);
- c := c + Length(s);
- writeln(s, ' :: ', c);
- end;
- writeln(c);
- //ReadLn;
- // stop program loop
- Terminate;
- end;
- constructor problem1.Create(TheOwner: TComponent);
- begin
- inherited Create(TheOwner);
- StopOnException:=True;
- end;
- destructor problem1.Destroy;
- begin
- inherited Destroy;
- end;
- procedure problem1.WriteHelp;
- begin
- { add your help code here }
- writeln('Usage: ',ExeName,' -h');
- end;
- var
- Application: problem1;
- begin
- Application:=problem1.Create(nil);
- Application.Title:='problem1';
- Application.Run;
- Application.Free;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement