Prolog; Testing 2 variables
Page 1 of 17 Replies - 771 Views - Last Post: 14 September 2012 - 09:04 PM
#1
Prolog; Testing 2 variables
Posted 13 September 2012 - 06:50 PM
Like for example,
M=Month, D=Day.
If M=1, D<=19, write("Capricorn").
Something like that.
My code was something like this,
write("Month: "), readint(M), write("Day: "), readint(D), try(M), try(D).
M=1,D<=19,write("Capricorn"); and so on...
My code isn't working yet. That was just to explain what I want.
Hope you help me:)
Replies To: Prolog; Testing 2 variables
#2
Re: Prolog; Testing 2 variables
Posted 13 September 2012 - 08:10 PM
write_sign(M, D) :- M=1, D <=19, write("Capricorn").
You might be able to use facts.
star_sign(1, "Capricorn"). star_sign(2, "Aquarius"). write_sign(M, D) :- star_sign(M, SIGN), D<=19, write(SIGN).
#3
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 02:17 AM
#4
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 05:57 AM
Like for example, there are only 30 days in April,June,September,November and February 29 only exist every 4 years.
predicates
go
test(integer)
try(char)
try_first
write_zodiac(integer,integer)
clauses
go:- clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year : "), readint(Y),
YT=2012, Age=YT-Y,
write_zodiac(M,D), test(Y),
write("\nAge : ",Age," years old\n").
write_zodiac(M, D) :-
M=1, D <=19, write("Zodiac Sign : Capricorn\n");
M=1, D >=20, write("Zodiac Sign : Aquarius\n");
M=2, D <=18, write("Zodiac Sign : Aquarius\n");
M=2, D >=19, D<=28, write("Zodiac Sign : Pisces\n");
M=3, D <=20, write("Zodiac Sign : Pisces\n");
M=3, D >=21, write("Zodiac Sign : Aries\n");
M=4, D <=19, write("Zodiac Sign : Aries\n");
M=4, D >=20, write("Zodiac Sign : Taurus\n");
M=5, D <=20, write("Zodiac Sign : Taurus\n");
M=5, D >=21, write("Zodiac Sign : Gemini\n");
M=6, D <=20, write("Zodiac Sign : Gemini\n");
M=6, D >=21, write("Zodiac Sign : Cancer\n");
M=7, D <=22, write("Zodiac Sign : Cancer\n");
M=7, D >=23, write("Zodiac Sign : Leo\n");
M=8, D <=22, write("Zodiac Sign : Leo\n");
M=8, D >=23, write("Zodiac Sign : Virgo\n");
M=9, D <=22, write("Zodiac Sign : Virgo\n");
M=9, D >=23, write("Zodiac Sign : Libra\n");
M=10, D <=22, write("Zodiac Sign : Libra\n");
M=10, D >=23, write("Zodiac Sign : Scorpio\n");
M=11, D <=21, write("Zodiac Sign : Scorpio\n");
M=11, D >=22, write("Zodiac Sign : Sagittarius\n");
M=12, D <=21, write("Zodiac Sign : Sagittarius\n");
M=12, D >=22, write("Zodiac Sign : Capricorn\n");
M=2, D>=30, write("There are only maximum of 29 days in February"),go;
M=4, D=31, write("There are only 30 days in April!"),go;
M=6, D=31, write("There are only 30 days in June!"),go;
M=9, D=31, write("There are only 30 days in September!"),go;
M=11, D=31, write("There are only 30 days in November!"),go.
test(Y):- Y=1980, write("Chinese Zodiac : Monkey"),try_first;
Y=1992, write("Chinese Zodiac : Monkey"),try_first;
Y=2004, write("Chinese Zodiac : Monkey"),try_first;
Y=1981, write("Chinese Zodiac : Rooster"),try_first;
Y=1993, write("Chinese Zodiac : Rooster"),try_first;
Y=2005, write("Chinese Zodiac : Rooster"),try_first;
Y=1982, write("Chinese Zodiac : Dog"),try_first;
Y=1994, write("Chinese Zodiac : Dog"),try_first;
Y=2006, write("Chinese Zodiac : Dog"),try_first;
Y=1983, write("Chinese Zodiac : Boar"),try_first;
Y=1995, write("Chinese Zodiac : Boar"),try_first;
Y=2007, write("Chinese Zodiac : Boar"),try_first;
Y=1984, write("Chinese Zodiac : Rat"),try_first;
Y=1996, write("Chinese Zodiac : Rat"),try_first;
Y=2008, write("Chinese Zodiac : Rat"),try_first;
Y=1985, write("Chinese Zodiac : Ox"),try_first;
Y=1997, write("Chinese Zodiac : Ox"),try_first;
Y=2009, write("Chinese Zodiac : Ox"),try_first;
Y=1986, write("Chinese Zodiac : Tiger"),try_first;
Y=1998, write("Chinese Zodiac : Tiger"),try_first;
Y=2010, write("Chinese Zodiac : Tiger"),try_first;
Y=1987, write("Chinese Zodiac : Rabbit"),try_first;
Y=1999, write("Chinese Zodiac : Rabbit"),try_first;
Y=2011, write("Chinese Zodiac : Rabbit"),try_first;
Y=1988, write("Chinese Zodiac : Dragon"),try_first;
Y=2000, write("Chinese Zodiac : Dragon"),try_first;
Y=2012, write("Chinese Zodiac : Dragon"),try_first;
Y=1989, write("Chinese Zodiac : Snake"),try_first;
Y=2001, write("Chinese Zodiac : Snake"),try_first;
Y=1990, write("Chinese Zodiac : Horse"),try_first;
Y=2002, write("Chinese Zodiac : Horse"),try_first;
Y=1991, write("Chinese Zodiac : Ram"),try_first;
Y=2003, write("Chinese Zodiac : Ram"),try_first.
test(Y):- YT=2012, Y>YT, write("Invalid Year "),go.
test(Y):- Y<1980, write("Invalid Year "),go.
try_first:-
write("\nWould you like to repeat Again? [Y/N] : "),readchar(A),try(A).
try(A):-A='y',go;
A='Y',go;
A='n',write("\nThank You");
A= 'N',write("\nThank You");
A<>'Y',write("\nInvalid Entry"),try_first;
A<>'y',write("\nInvalid Entry"),try_first;
A<>'N',write("\nInvalid Entry"),try_first;
A<>'n',write("\nInvalid Entry"),try_first.
goal
go.
Hope you could help me. I'm just new in Prolog.
This post has been edited by k3nnt0ter0: 14 September 2012 - 05:59 AM
#5
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 07:52 PM
If a number, eg 2012 is divided by 4 the remainder is 0, then the number is exactly divisible by 4.
2012 mod 4 = 0
Here are some ideas :
You wish to check a date. To check the date you need to find the number of days in a month and for that, a test whether a year is a leap year is required.
domains days,months,years = integer predicates daysinmonth(months, days, years) isleapyear(years) check_date(months, days, years) clauses daysinmonth( 1, 31, _). /* etc */
I'll let you have a go at writing some of the code, since that is half the fun.
Leap Year : Wikipedia
This post has been edited by #define: 14 September 2012 - 08:07 PM
#6
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 08:02 PM
#7
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 08:37 PM
I don't know how am I going to use the daysinmonth.
domains
days, months, years = integer
predicates
zod
testleap(integer,integer,integer)
daysinmonth(months, days, years)
isleapyear(years)
clauses
daysinmonth(1, 31, _).
daysinmonth(2, 29, _).
daysinmonth(3, 31, _).
daysinmonth(4, 30, _).
daysinmonth(5, 31, _).
daysinmonth(6, 30, _).
daysinmonth(7, 31, _).
daysinmonth(8, 31, _).
daysinmonth(9, 30, _).
daysinmonth(10, 31, _).
daysinmonth(11, 30, _).
daysinmonth(12, 31, _).
isleapyear(1980).
isleapyear(1984).
isleapyear(1988).
isleapyear(1992).
isleapyear(1996).
isleapyear(2000).
isleapyear(2004).
isleapyear(2008).
isleapyear(2012).
zod :- clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year: "), readint(Y),
YT=2012, Age=YT-Y,
write("\nAge: ",Age,"years old\n"),testleap(M,D,Y).
goal
zod.
This post has been edited by k3nnt0ter0: 14 September 2012 - 08:41 PM
#8
Re: Prolog; Testing 2 variables
Posted 14 September 2012 - 09:04 PM
daysinmonth(2, 29, YEAR) :- isleapyear(YEAR), !.
daysinmonth(2, 28, _).
To use daysinmonth a variable is used to get the number of days.
zod :- clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year: "), readint(Y),
YT=2012, Age=YT-Y,
write("\nAge: ",Age,"years old\n"),
daysinmonth(M, MAXDAYS, Y),
write("Days in the Month : ", MAXDAYS, "\n").
This post has been edited by #define: 14 September 2012 - 09:07 PM
|
|

New Topic/Question
Reply




MultiQuote




|