2

テキストの最大値を認識できるプログラムが必要です。最初にテキストファイルをストリーミングし、

charごとにinfosを取得しますが、正方形かどうかに関係なく計算と結果を取得できません。正方形の場合は、sample.txtの座標に数値を入力します。

    setup:-process('sample.txt').
process(File) :-
        open(File, read, In),
        get_char(In, Char1),
        process_stream(Char1, In),
        close(In).

process_stream(end_of_file, _) :- !.
process_stream(Char, In) :-
        get_char(In, Char2),
        look(Char,Char2), 
        process_stream(Char2, In).
4

1 に答える 1

2

Prologでは、使いやすい入力分析ツールはDCG(Definite Clause Grammar)と呼ばれる拡張機能です。そのシンプルさのおかげで、ほとんどすべてのシステムで利用できます。

その機能を使用して、組み込みのread_line_to_codesとユーティリティ整数//1を記述できます。

:- [library(readutil),
    library(http/dcg_basics)].

setup :- process('sample.txt').

process(File) :-
    open(File, read, Stream),
    repeat,
    read_line_to_codes(Stream, Codes),
    (   Codes == end_of_file
    ->  close(Stream), !
    ;   phrase(check_rectangle, Codes, _), fail
    ).

% just print when |X2-X1|=|Y2-Y1|, fails on each other record
check_rectangle -->
    integer(X1), ",", integer(X2), ",",
    integer(Y1), ",", integer(Y2),
    {   abs(X2-X1) =:= abs(Y2-Y1)
        ->  writeln(found(X1,Y1,X2,Y2))
    }.

出力(テストのために拡大された長方形を追加しました):

?- setup.
found(10,30,20,40)
found(100,300,200,400)
true.
于 2012-03-07T14:22:40.360 に答える