INPUT AND OUTPUT in Prolog

Problem 1


Define a predicate makelower/0 which reads in a line of characters from the
keyboard and outputs it again as a single line with any upper case letters converted
to lower case. (The ASCII values of the characters a, z, A and Z are 97, 122, 65 and
90, respectively.)

1.Make a notepad/txt file like the picture below, then save it in .pl format


Problem 1

2.Then consult it in prolog, then type "makelower. Input the letter to be lowercased , and the result will be like this.




Problem 2

Define a predicate copyterms which reads all the terms in a text file and
outputs them as terms to another text file one by one on separate lines.
The output file should be in a format suitable for use as the input file in a
subsequent call of copyterms. Thus for example if the input file contained

'first term'. 'second term'.
'third term'.
fourth. 'fifth term'.
sixth.
The output file would contain
'first term'.
'second term'.
'third term'.
fourth.
'fifth term'.
sixth.

1. Make infile (save in .txt) and outfile (save in .pl) like in the picture.








2.Then consult .pl file then type copyterms('Infile.txt','Outfile.txt').




3. The result will be like this




Problem 3

Create a text file testa.txt containing two lines, each of five characters followed
by a new line, e.g.
abcde
fghij
Define a predicate readfile that will read fifteen characters from this file one by
one and output the ASCII value of each character. Use this to establish whether the Input and Output 83 representations of 'end of file' and 'end of record' for your version of Prolog are as suggested in Sections 5.9.1 and 5.9.2, respectively.

1.make a new prolog file (.pl) like in the picture



2.Then make one .txt file that contain character like in the picture



3.Then consult your prolog file then type readfile('testa.txt'). Then the result will be shown like this :




Problem 4

Using a text editor, create two text files in1.txt and in2.txt, each comprising a
number of terms terminated by end.Define and test a predicate combine that takes the names of two input files as its first two arguments and the name of an output file as its third argument. The output file should contain the terms in the first input file followed by the terms in the second, one per line and terminated by end.

1.make a prolog code like in the picture




2.then make 2 .txt that contain something we want to combine





3. Then consult the prolog file then type combine('In1.txt','In2.txt','Hasil.txt').



4.And the result will be made into hasil.txt

Tidak ada komentar:

Posting Komentar