home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / ms_dos / rword / rword.pas < prev    next >
Pascal/Delphi Source File  |  1990-06-14  |  6KB  |  216 lines

  1. (************************************************************************)
  2. (*  RWord  1.4    Random Word Generator  でたらめ言葉発生器             *)
  3. (*  Copyright  1989,90  Y.Fujisawa    藤沢 泰全                         *)
  4. (*  Machine:    J-3100B,SS  MS-DOS 2.11, 3.10                           *)
  5. (*  Compiler:   TURBO-PASCAL 5.5                                        *)
  6. (*  History:                                                            *)
  7. (*      89-03-03    1.0     新規作成                                    *)
  8. (*      89-03-05    1.1     procedure ReadFile を変更した              *)
  9. (*      89-03-08    1.2     単語を 1..9 で指定するようにした            *)
  10. (*      89-03-23    1.3     Heap 領域を使う。-n オプションを追加        *)
  11. (*      90-05-13    1.4     Turbo-Pascal 5.5 を使用。                   *)
  12. (*                          改行、スペースで繰り返し、その他で終了      *)
  13. (************************************************************************)
  14.  
  15. program  RWord(input,output);
  16. uses  Dos;
  17.  
  18. const
  19.     WordLength = 60;        (* 単語の長さ(最大) *)
  20.     DelimiterChar = '/';    (* 単語の最後の区切り *)
  21.     CrChar = ^M;            (* 改行文字 *)
  22. type
  23.     WordAttrType = 1..9;
  24.     WordPtr = ^WordRec;
  25.     WordRec = record
  26.                 St: string[WordLength];     (* 単語 *)
  27.                 Attr: WordAttrType;         (* 属性 *)
  28.                 Next: WordPtr;              (* 次の単語へのポインター *)
  29.             end;
  30.     VocabRec = record
  31.                 Num: integer;       (* 単語数 *)
  32.                 Ptr: WordPtr;       (* 単語へのポインター *)
  33.                end;
  34.  
  35. var
  36.     Vocabulary: array[WordAttrType] of VocabRec;    (* 属性ごとの単語 *)
  37.  
  38.  
  39. (* エコーなしで、キー入力 *)
  40. function  ReadKey: char;
  41. var
  42.     Regs: Registers;
  43. begin
  44.     Regs.AH := $08;
  45.     MsDos(Regs);
  46.     ReadKey := Chr(Regs.AL);
  47. end;  { ReadKey }
  48.  
  49.  
  50. (* メッセージを表示する *)
  51. procedure  WriteMessage;
  52. begin
  53.     Writeln('RWord Version 1.4  Date:1990-5-14  Copyright 1989,90 Y.Fujisawa');
  54.     Writeln('Usage:  RWORD [-n] <FileName>       ( n: Repeat times )');
  55. end;  { WriteMessage }
  56.  
  57.  
  58. {$f+}
  59. function  HeapErrorFunc(Size: word): integer;
  60. {$f-}
  61. begin
  62.     Writeln('メモリーが足りませんよ (^_^;)');
  63.     HeapErrorFunc := 0;
  64. end;  { HeapErrorFunc }
  65.  
  66.  
  67. (* 単語ファイルを読み込む *)
  68. procedure  ReadFile(FileName: string);
  69. var
  70.     TextFile: text;
  71.     LineCount: integer;
  72.     Line: string;
  73.  
  74.     procedure  OpenFile;
  75.     begin
  76.         if Pos('.',FileName) = 0 then  FileName := FileName+'.RWD';
  77.         Assign(TextFile,FileName);
  78.         {$i-}  Reset(TextFile);  {$i+}
  79.         if IOResult <> 0 then  begin
  80.             Writeln('ファイルが見つかりませんよ (^_^;)');
  81.             Halt;
  82.         end;  { if }
  83.     end;  { OpenFile }
  84.  
  85.     procedure  ReadLine;
  86.  
  87.         procedure  ErrorExit;
  88.         begin
  89.             Writeln(LineCount:3,' 行目がおかしいですよ (^_^;)');
  90.             Writeln('-->',Line);
  91.             Close(TextFile);
  92.             Halt;
  93.         end;   { ErrorExit }
  94.  
  95.         function  GetWordAttr(Ch: char): WordAttrType;
  96.         begin
  97.             case UpCase(Ch) of
  98.                 '1'..'9':   GetWordAttr := Ord(Ch)-Ord('0');
  99.                 'A' :       GetWordAttr := 1;
  100.                 'N' :       GetWordAttr := 2;
  101.                 else    ErrorExit;
  102.             end;  { case }
  103.         end;  { GetWordType }
  104.  
  105.     var
  106.         p: byte;
  107.         at: WordAttrType;
  108.         NewWord: WordPtr;
  109.     begin
  110.         Readln(TextFile, Line);
  111.         Inc(LineCount);
  112.         p := Pos(DelimiterChar,Line);
  113.         if (p > 0) and ( p < Length(Line) ) then  begin
  114.             New(NewWord);
  115.             at := GetWordAttr(Line[p+1]);
  116.             with NewWord^ do  begin
  117.                 St := Copy(Line,1,p-1);
  118.                 Attr := at;
  119.                 with Vocabulary[at] do  begin
  120.                     Next := Ptr;
  121.                     Ptr := NewWord;
  122.                     Inc(Num);
  123.                 end;  { with }
  124.             end;  { with }
  125.         end  { if }
  126.         else  ErrorExit;
  127.     end;  { ReadLine }
  128.  
  129. var
  130.     i: integer;
  131. begin
  132.     LineCount := 0;
  133.     for i := 1 to 9 do  begin
  134.         with Vocabulary[i] do  begin
  135.             Num := 0;
  136.             Ptr := nil;
  137.         end;  { with }
  138.     end;  { for }
  139.     OpenFile;
  140.     while not Eof(TextFile) do ReadLine;
  141.     Close(TextFile);
  142. end;  { ReadFile }
  143.  
  144.  
  145. (* 乱数で単語を組み合わせる *)
  146. function  RandomWord: string;
  147.  
  148.     function Scan(Ptr: WordPtr; n: integer): string;
  149.     var
  150.         i: integer;
  151.     begin
  152.         for i := 1 to n-1 do  Ptr := Ptr^.Next;
  153.         Scan := Ptr^.St;
  154.     end;  { Scan }
  155.  
  156. var
  157.     s: string;
  158.     at: WordAttrType;
  159. begin
  160.     s := '';
  161.     for at := 1 to 9 do  begin
  162.         with Vocabulary[at] do  begin
  163.             if Num > 0 then  s := s+Scan(Ptr,Random(Num)+1);
  164.         end;  { with }
  165.     end;  { for }
  166.     RandomWord := s;
  167. end;  { RandomWord }
  168.  
  169.  
  170. (* 繰り返しの回数を得る *)
  171. function  GetRepeat(St: string): integer;
  172.  
  173.     procedure  ErrorExit;
  174.     begin
  175.         Writeln('繰り返しの指定がおかしいですよ (^_^;)');
  176.         Halt;
  177.     end;  { ErrorExit }
  178.  
  179. var
  180.     Num,Result: integer;
  181. begin
  182.     if St[1] = '-' then  begin
  183.         Delete(St,1,1);
  184.         Val(St,Num,Result);
  185.         if Result = 0 then
  186.             GetRepeat := Num
  187.         else
  188.             ErrorExit;
  189.     end  { if }
  190.     else  ErrorExit;
  191. end;  { GetRepeat }
  192.  
  193.  
  194. var
  195.     RepeatTimes,i: integer;
  196. begin
  197.     HeapError := @HeapErrorFunc;
  198.     Randomize;
  199.     case ParamCount of
  200.         1:  begin
  201.                 ReadFile(ParamStr(1));
  202.                 repeat
  203.                     WriteLn;
  204.                     Write(RandomWord);
  205.                 until  not(ReadKey in [CrChar,' ']);
  206.             end;
  207.         2:  begin
  208.                 RepeatTimes := GetRepeat(ParamStr(1));
  209.                 ReadFile(ParamStr(2));
  210.                 for i := 1 to RepeatTimes do  Writeln(RandomWord);
  211.             end;
  212.         else    WriteMessage;
  213.     end;  { case }
  214. end.
  215.  
  216.