Io 根据用户输入读取并检查txt文件中的简单数据库

Io 根据用户输入读取并检查txt文件中的简单数据库,io,prolog,user-input,Io,Prolog,User Input,我想创建一个代码,prolog要求用户输入大小和计算,以获得下一个输入,例如Matric ID,并将检查简单txt文件中的输入是否包含学生详细信息。因此,基本上它将读取文件,如果id在txt文件中,那么如果不是false,则为true。代码如下: main :- write('How many people? '), read(N), integer(N), N > 0, N < 5, length(L, N), maplist(read_n, L), write_list(L),

我想创建一个代码,prolog要求用户输入大小和计算,以获得下一个输入,例如Matric ID,并将检查简单txt文件中的输入是否包含学生详细信息。因此,基本上它将读取文件,如果id在txt文件中,那么如果不是false,则为true。代码如下:

main :-
write('How many people? '),
read(N),
integer(N),
N > 0,
N < 5,
length(L, N),
maplist(read_n, L),
write_list(L),
nl,
open('test.txt',read),
check(N).

read_n(N) :-
  write('Enter id: '),
  read(N),
  integer(N), N >= 10**4, N < 10**5. %the id must be size of 5 and integer
  %if not error message

write_list(L) :-
  write_list(L, 1).

write_list([H|T], N) :-
  nl,
  format('You have entered id ~w: ~w', [N, H]),
  N1 is N + 1,
  write_list(T, N1).

check(N):-
   %to read the file and check if the ID is in the file or not.
   open('test.txt',read,Int),
   read_file(Int,N),
    close(Int),
   write('your ID exist', N), nl.
read_file(Stream,[]):-
     at_end_of_stream(Stream).

read_file(Stream,[X|L]):-
     \+  at_end_of_stream(Stream),
     read(Stream,X),
     read_houses(Stream,L).
文件中的数据是学生的信息。因此,prolog将根据ID进行检查,最后如果ID存在,它将给出ID详细信息的消息。例如:

| ?- main.
Please enter an integer value: 2.
Enter id: 16288.

You have entered id 1: 16288
在那之后,会有这样的信息: 欢迎卢克。您的房间是V5G,您是ICT学生

差不多吧。那么,如何使用Prolog实现这个函数呢? 另外,检查文件的输出为false。我尝试过很多读取、查看、打开文件的方法,但都是徒劳的。T_ut

提前感谢主界面中的

,您的checkN调用已将N实例化为整数:

integer(N),
...
open('test.txt', open),          % <--- this looks like an error
check(N).
根据您的数据,您应该在文本文件中使用Prolog术语来构造它。请注意字符串周围以大写字母开头的单引号使其成为原子。否则,Prolog会将它们视为变量

student(16288, 'Luke', 'V5G', 'ICT').
student(16277, 'Kristin', 'V2D', 'EE').
student(16177, 'Catrine', 'V4E', 'CV').
student(16256, 'Mambo', 'V1A', 'MECHE').
student(15914, 'Armenio', 'V5H', 'PE').
student(12345, 'Lampe', V3C, PG).
在程序开始时,请将文件作为事实进行查阅。调用文件students.pl:

然后你有一个学生事实数据库。然后,主节点变为:

main :-
    write('How many people? '),
    read(N),
    integer(N),
    N > 0,
    N < 5,
    length(Ids, N),
    maplist(read_n, Ids),
    write_list(Ids),
    check_ids(Ids).

check_ids([Id|Ids]) :-
    (   student(Id, Name, Room, Type)
    ->  format('Welcome ~w. Your room is ~w and you are a ~w student.~n', [Name, Room, Type])
    ;   format('Student id ~w was not found.~n', [Id])
    ),
    check_ids(Ids).
check_ids([]).

我收到一个错误:apply:maplist\u2:Undefined procedure:read\u n/1异常:9 read\u n\u G2442?@umika1150请查看我的答案,因为自从你上次抓到它后,我可能已经修复了一个拼写错误。对我来说没问题。我应该把“:-包括“学生”放在“主要:-”之前的顶部,对吗?“students.pl”是一个外部文件,我需要保存为prolog而不是txt文件,对吗?我已经这么做了,但我仍然犯了同样的错误,甚至更多error@umika1150include是一个Prolog指令,实际上可以出现在文件中的任何位置,但通常在开始时就包含了它们。将Prolog事实数据库放在Prolog源文件中也是典型的做法。include指令假定一个Prolog源文件扩展名,例如.pl,当您不提供它时::-include'students'将找到students.pl。您可以使用:-include'students.txt',但事实实际上是Prolog,因此.pl扩展是合适的。现在它工作得很好。非常感谢你的帮助。Y
student(16288, 'Luke', 'V5G', 'ICT').
student(16277, 'Kristin', 'V2D', 'EE').
student(16177, 'Catrine', 'V4E', 'CV').
student(16256, 'Mambo', 'V1A', 'MECHE').
student(15914, 'Armenio', 'V5H', 'PE').
student(12345, 'Lampe', V3C, PG).
:- include('students').
main :-
    write('How many people? '),
    read(N),
    integer(N),
    N > 0,
    N < 5,
    length(Ids, N),
    maplist(read_n, Ids),
    write_list(Ids),
    check_ids(Ids).

check_ids([Id|Ids]) :-
    (   student(Id, Name, Room, Type)
    ->  format('Welcome ~w. Your room is ~w and you are a ~w student.~n', [Name, Room, Type])
    ;   format('Student id ~w was not found.~n', [Id])
    ),
    check_ids(Ids).
check_ids([]).
write_list(Ids) :-
    write_list(Ids, 1).

write_list([Id|Ids], N) :-
    format('~nYou have entered id ~w: ~w', [N, Id]),
    N1 is N + 1,
    write_list(Ids, N1).
write_list([], _) :- nl.