Input and Output - PowerPoint PPT Presentation

About This Presentation
Title:

Input and Output

Description:

Chapter 3 Input and Output – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 23
Provided by: cs195
Category:
Tags: input | output

less

Transcript and Presenter's Notes

Title: Input and Output


1
Chapter 3
  • Input and Output

2
Turbo Prolog Editor
  • Ctrl K B Start block
  • Ctrl K K End block
  • Ctrl K C Copy block
  • Ctrl K V Move block
  • Ctrl K Y Delete block
  • Ctrl Y Delete line

3
AND, OR, NOT
  • and use ,
  • or use
  • not

HELP Command file ?PROLOG open with notepad
4
EX04EX05 is letter
  • predicates
  • isletter(char)
  • clauses
  • / When applied to characters, 'lt' means
    "alphabetically precedes or is the same as" /
  • isletter(Ch) - 'a' lt Ch, Ch lt 'z'.
  • isletter(Ch) - 'A' lt Ch, Ch lt 'Z'.

goal isletter(K) yes
  • arithematics
  • lt gt lt gt
  • - / not
  • char
  • put .... when asking

5
Setup makewindow
  • makewindow(1, 120, 35, "Left operand", 2, 5, 5,
    25)
  • Location
  • 1 window number
  • 2 background color and text color in the window
  • 3 frame color
  • 4 text center of the border
  • 5 row
  • 6 column
  • 7 Height
  • 8 Width
  • cursor(row,column)
  • cursor(25,80)

6
Setup color
  • HOW TO CALCULATE SCREEN ATTRIBUTES.
  • Choose ONE foreground color and ONE background
    color.
  • Add the corresponding integer values below.
  • 3. Add 128 if you want whatever is displayed
    with that attribute to blink.
  • BACKGROUND-COLORS (Screen)
    FOREGROUND-COLORS (Characters)
  • ------------------------------------------------
    ------------
  • Black 0 Black 0
  • Blue 16 Blue 1
  • Green 32 Green 2
  • Cyan 48 Cyan 3
  • Red 64 Red 4
  • Magenta 80 Magenta 5
  • Brown 96 Brown 6
  • White 112 White 7
  • Grey 8
  • Light Blue 9
  • Light Green 10
  • Light Cyan 11
  • 23 16 7 Light Red 12

7
EX14EX01.PRO makewindow
  • predicates
  • run
  • clauses
  • run -
  • makewindow(1, 20, 7, "A blue window", 2, 5,
    10, 50),
  • write("The characters are red"), nl,
  • makewindow(2, 176, 7, "A light cyan
    window", 14, 25, 10, 40),
  • write("This window is light cyan and the
    "),
  • write("letters are black and blink."), nl,
  • write("Please type an integer to exit."),
    nl,
  • readint(_),
  • removewindow,
  • write("Please type an integer to exit."),
    nl,
  • readint(_),
  • removewindow.

8
Window1.PRO EX04EX03.PRO
predicates run(char) goal run(X). / This
is a internal goal. / clauses run(X) -
makewindow(1, 7, 7, " Hello World Program
",0,0,25,80), write("Hello World
(first)"), readchar(X),
removewindow. run(X) - write("hello
World (second)"), readchar(X).

9
Window2.PRO
predicates run(char) goal run(X).
/ This is a internal goal. /
clauses run(X) -
makewindow(1,7,7,"hello",0,0,25,80),
write("hello World (second)"), readchar(X).
run(X) - makewindow(1, 7, 7," Hello World
Program ", 0, 0, 25, 80), write("Hello World
(first)"), readchar(X), removewindow.

10
Window3.PRO show 2 windows
predicates run1(char) run2(char)
goal run1(X), run2(Y). / This is a
internal goal. / clauses run2(X) -
makewindow(1,7,7,"hello",0,0,25,80),
write("hello World (second)"),
readchar(X). run1(X) - makewindow(1,
7, 7," Hello World Program ", 0, 0, 25, 80),
write("Hello World (first)"),
readchar(X), removewindow.

11
EX14EX02.pro SUM 2 NUMBERS
  • predicates
  • start
  • run(integer)
  • do_sums
  • set_up_windows
  • clear_windows
  • clauses
  • start - set_up_windows, do_sums.
  • set_up_windows -
  • makewindow(1, 7, 7, "", 0, 0, 25, 80),
  • makewindow(1, 7, 7, "Left operand", 2, 5,
    5, 25),
  • makewindow(2, 7, 7, "", 2, 35, 5, 10),
  • nl, write(" PLUS"),
  • makewindow(2, 7, 7, "Right operand", 2, 50,
    5, 25),
  • makewindow(3, 7, 7, "Gives", 10, 27, 5,
    25),
  • makewindow(4, 7, 7, "", 17, 22, 5, 35).
  • do_sums - run(_), clear_windows, do_sums.

goal start
12
EX14EX02.pro SUM 2 NUMBERS (cont.)
  • run(Z) -
  • shiftwindow(1),
  • cursor(2, 1), readint(X),
  • shiftwindow(2),
  • cursor(2, 10), readint(Y),
  • shiftwindow(3), ZXY, cursor(2, 10),
    write(Z),
  • shiftwindow(4),
  • write(" Please press the space bar"),
  • readchar(_).
  • clear_windows -
  • shiftwindow(1), clearwindow,
  • shiftwindow(2), clearwindow,
  • shiftwindow(3), clearwindow,
  • shiftwindow(4), clearwindow.

13
LISTEX01.pro
goal ownes(X,Y)
  • domains
  • thing book(title,author) car(name) shoe
  • author author(firstname,lastname)
  • person,title,name,firstname,lastname,shoe
    symbol
  • predicates
  • ownes(person,thing)
  • clauses
  • ownes(siri,book("AI book",author(tom,brook))).
  • ownes(siri,car(honda)).
  • ownes(siri,shoe).

14
EX12EX07.PRO Read Person Information
  • domains
  • person p(name, age, telno, job)
  • age integer
  • telno, name, job string
  • predicates
  • readperson(person)
  • run
  • goal
  • run.
  • clauses
  • readperson(p(Name, Age, Telno, Job)) -
  • write("Which name ? "), readln(Name),
    write("Job ?"), readln(Job), write("Age
    ?"), readint(Age),
  • write("Telephone no ?"), readln(Telno).
  • run -
  • readperson(P), nl, write(P), nl, nl,
  • write("Is this compound object OK (y/n)"),
    readchar(Ch), Ch'y'.
  • run -
  • nl, nl, write("Alright, try again"), nl,
    nl, run.

15
EX07EX02.PRO Typewriter
  • / Uses repeat to keep accepting characters and
    printing them until the user presses Enter. /
  • predicates
  • repeat
  • typewriter
  • clauses
  • repeat.
  • repeat - repeat.
  • typewriter - repeat,
  • readchar(C), / Read a char, bind
    C to it /
  • write(C),
  • char_int(C,13). / Is C
    ASCII 13? /

goal typewriter
16
EX05EX09 Interactive window
predicates repeat action(integer,string)
test(string) goal makewindow(1,7,7,"int
eraction window",0,2,11,43), repeat,
shiftwindow(1), clearwindow, write("0.
Enter 0 to end\n"), write("1. Enter 1 to
create a window and input\n a new string\n"),
write("2. Enter 2 to remove the window and
text\n"), write("3. Enter 3 to write to
existing window\n\n"),
write("Selection? "),
readint(Int),nl, action(Int,Text),
Int 0,!, / this cut will
prevent backtracking even if you
have not created
a string / test(Text). clauses
action(0,"EXIT")- !, / this cut
prevents Turbo Prolog from looking
at
other options. / exit.
17
EX05EX09 Interactive window
action(1,Str)- existwindow(2),
write("You have a window that already
exists.\n"), write("Do you wish to clear
it.(y,n) "), readchar(Ans),!,
Ans'y', / If you answer yes to the question
this cut prevents the backtracking to the second
action(1) clause. / nl, shiftwindow(2),
clearwindow, write("Enter your string\n"),
readln(Str). action(1,Str)- !, /
this cut prevents Turbo Prolog from looking

at other options. / nl,
makewindow(2,7,7," simple window control ", 12,
3, 12, 40), write("Enter your string\n"),
readln(Str).
action(2,"window removed")-
existwindow(2), !, / If the window has
been input, this cut will prevent the second
action(2) clause from executing /
shiftwindow(2), removewindow,
clearwindow. ...... action(3,Str)-
existwindow(2),!, shiftwindow(2),
clearwindow, write("Enter your string\n"),
readln(Str). ....... action(_,"ERROR")-
write("not a valid option\n"),
write("press any key to continue").
test(Text)- write(Text). repeat.
repeat-repeat.
18
EX16EX02.PRO show time
  • predicates
  • timer
  • clauses
  • timer-
  • time(H1,M1,S1,D1),nl,
  • write("Start time is ",H1,"",M1,"",S1,"
    and ",D1,"/100 sec"),nl, / This is the activity
    that is being timed /
  • system("dir ."),
  • time(H2,M2,S2,D2),
  • Time (D2-D1) 100 ((S2 - S1) 60
    ((M2 - M1) 60 (H2 - H1))),
  • write("Elapsed time ",Time,"/100 sec"),nl,
  • time(H3,M3,S3,D3),
  • write("The time now is ",H3,"",M3,"",S3,"
    and ",D3,"/100 sec"),nl.
  • goal
  • makewindow(1,7,7," Timer ",8,10,12,60),
  • write("Press any key to start"),
  • readchar(_), timer.

19
EX05EX05
  • We can set the program to run automatic by using
    the command at .....
  • goal

lives(zebra, on_land). lives(frog,
on_land). lives(frog, in_water).
lives(shark, in_water). can_swim(Y) -
type(X, animal) , is_a(Y, X) ,
lives(Y,in_water).
  • predicates
  • type(symbol, symbol)
  • is_a(symbol, symbol)
  • lives(symbol, symbol)
  • can_swim(symbol)
  • goal
  • can_swim(What) ,
  • write("A ",What," can swim.\n").
  • clauses
  • type(ungulate, animal).
  • type(fish, animal).
  • is_a(zebra, ungulate).
  • is_a(herring, fish).
  • is_a(shark, fish).
  • write(.............. )
  • write(\n.......,Variable
  • ,..... \n\n\n)

20
cswim1
  • Fail
  • use to make the rule not true
  • prolog will find all of the solutions.
  • predicates
  • type(symbol, symbol)
  • is_a(symbol, symbol)
  • lives(symbol, symbol)
  • can_swim(symbol)
  • goal
  • can_swim(What) ,
  • write("A ", What," can
  • swim.\n"),fail.
  • clauses
  • type(ungulate, animal).
  • type(fish, animal).
  • is_a(zebra, ungulate).
  • is_a(herring, fish).
  • is_a(shark, fish).
  • is_a(tuna, fish).
  • lives(zebra, on_land).
  • lives(frog, on_land).
  • lives(frog, in_water).
  • lives(shark, in_water).
  • lives(herring, in_water).
  • lives(tuna, in_water).
  • can_swim(Y) -
  • type(X, animal) ,
  • is_a(Y, X) ,
  • lives(Y,in_water).

21
can_swim
  • We can set the program to run automatic by using
    the command at .....
  • goal
  • predicates
  • type(symbol, symbol)
  • is_a(symbol, symbol)
  • lives(symbol, symbol)
  • can_swim(symbol)
  • run.
  • goal
  • run.
  • clauses
  • type(ungulate, animal).
  • type(fish, animal).
  • is_a(zebra, ungulate).
  • is_a(tuna, fish).
  • is_a(shark, fish).

lives(zebra, on_land). lives(frog,
on_land). lives(frog,in_water).
lives(shark,in_water). lives(tuna,in_water). ca
n_swim(Y) - type(X, animal) ,
is_a(Y, X) , lives(Y,in_water). run -
write("\n Hello \n"),
can_swim(What), write("A ", What, " can
swim.\n"), fail.
22
EX05EX06 father
  • domains
  • name symbol
  • predicates
  • father(name, name)
  • everybody
  • clauses
  • father(leonard, katherine).
  • father(carl, jason).
  • father(carl, marilyn).
  • everybody -
  • father(X, Y),
  • write(X, " is ", Y, "'s father\n"),
  • fail.
  • Some predicate may not have attributes
  • Ex everybody
Write a Comment
User Comments (0)
About PowerShow.com