Created
April 27, 2025 11:15
-
-
Save OEIRU/45f1574eb2e279e23928838671668823 to your computer and use it in GitHub Desktop.
Defence_lab3_II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| domains | |
| name = symbol | |
| i = integer | |
| list = i* | |
| database | |
| toy(name, i, i, i) | |
| counter(i) | |
| predicates | |
| nondeterm choice(char) | |
| menu | |
| nondeterm query_a(name, i, i) | |
| nondeterm query_b(name, i, i) | |
| nondeterm query_c(i) | |
| nondeterm query_d(name, name, i) | |
| nondeterm query_e(name) | |
| nondeterm max(list, i) | |
| nondeterm max(list, i, i) | |
| nondeterm init_counter | |
| nondeterm count_toys | |
| nondeterm collect_prices(list) | |
| clauses | |
| toy("doll", 350, 3, 8). | |
| toy("blocks_blue", 200, 2, 6). | |
| toy("blocks_green", 150, 2, 6). | |
| toy("blocks_red", 210, 2, 6). | |
| toy("ball", 150, 4, 10). | |
| toy("robot", 500, 7, 12). | |
| toy("puzzle", 400, 5, 9). | |
| toy("constructor", 450, 6, 11). | |
| toy("car", 250, 3, 7). | |
| toy("pyramid", 180, 1, 4). | |
| toy("chess", 600, 4, 10). | |
| count_toys :- | |
| retract(counter(_)), | |
| fail. | |
| count_toys :- | |
| assert(counter(0)), | |
| fail. | |
| count_toys :- | |
| toy(_, _, _, _), | |
| retract(counter(Current)), | |
| NewCount = Current + 1, | |
| assert(counter(NewCount)), | |
| fail. | |
| count_toys. | |
| init_counter :- | |
| counter(_), !. | |
| init_counter :- | |
| count_toys, !. | |
| collect_prices([]) :- !. | |
| collect_prices([Price|Rest]) :- | |
| toy(_, Price, _, _), | |
| collect_prices(Rest), !. | |
| menu :- | |
| write("\n*********************************\n"), | |
| write("Make your choice:\n"), | |
| write("1 - Toys <= price and min age\n"), | |
| write("2 - Toys for age range\n"), | |
| write("3 - Total price of blocks\n"), | |
| write("4 - Toys combinable with another\n"), | |
| write("5 - Toys near max price\n"), | |
| write("6 - Add toy\n"), | |
| write("7 - Show all toys\n"), | |
| write("s - Save database\n"), | |
| write("l - Load database\n"), | |
| write("0 - Exit\n"), | |
| write("*********************************\n"), | |
| readchar(Choice), | |
| choice(Choice), | |
| Choice <> '0', | |
| menu. | |
| menu. | |
| choice('6') :- | |
| write("Name: "), readln(Name), | |
| write("Price: "), readint(Price), | |
| write("Min age: "), readint(MinAge), | |
| write("Max age: "), readint(MaxAge), | |
| assert(toy(Name, Price, MinAge, MaxAge)), | |
| retract(counter(Current)), | |
| NewCount = Current + 1, | |
| assert(counter(NewCount)), | |
| write("Added. Total toys now: ", NewCount, "\n"), !. | |
| choice('7') :- | |
| toy(Name, Price, MinAge, MaxAge), | |
| write("Name: ", Name, ", Price: ", Price, ", Ages: ", MinAge, "-", MaxAge, "\n"), | |
| fail. | |
| choice('7') :- !. | |
| choice('1') :- | |
| write("Price limit: "), readint(Price), | |
| write("Min age: "), readint(MinAge), | |
| query_a(_, Price, MinAge), | |
| fail. | |
| choice('1') :- !. | |
| choice('2') :- | |
| write("Min age: "), readint(ReqMin), | |
| write("Max age: "), readint(ReqMax), | |
| query_b(_, ReqMin, ReqMax), | |
| fail. | |
| choice('2') :- !. | |
| choice('3') :- | |
| query_c(Total), | |
| write("Total for blocks: ", Total, "\n"), !. | |
| choice('4') :- | |
| write("Toy name: "), readln(ReqName), | |
| write("Max total price: "), readint(MaxPrice), | |
| query_d(_, ReqName, MaxPrice), | |
| fail. | |
| choice('4') :- !. | |
| choice('5') :- | |
| query_e(Name), | |
| write(Name, "\n"), | |
| fail. | |
| choice('5') :- !. | |
| choice('s') :- | |
| save("toys.dat"), | |
| write("Saved.\n"), !. | |
| choice('l') :- | |
| existfile("toys.dat"), | |
| consult("toys.dat"), | |
| init_counter, | |
| write("Loaded.\n"), !. | |
| choice('0') :- !. | |
| choice(_) :- | |
| write("Invalid option.\n"), !. | |
| query_a(Name, ReqPrice, ReqMinAge) :- | |
| toy(Name, Price, MinAge, MaxAge), | |
| Price <= ReqPrice, | |
| MinAge <= ReqMinAge, | |
| MaxAge >= ReqMinAge, | |
| write(Name, "\n"), | |
| fail. | |
| query_a(_, _, _). | |
| query_b(Name, ReqMin, ReqMax) :- | |
| toy(Name, _, ToyMin, ToyMax), | |
| ReqMin <= ToyMax, | |
| ReqMax >= ToyMin, | |
| write(Name, "\n"), | |
| fail. | |
| query_b(_, _, _). | |
| query_c(Total) :- | |
| toy("blocks_red", P1, _, _), | |
| toy("blocks_blue", P2, _, _), | |
| toy("blocks_green", P3, _, _), | |
| Total = P1 + P2 + P3, !. | |
| query_d(Name, ReqName, MaxPrice) :- | |
| toy(ReqName, Price1, _, _), | |
| toy(Name, Price2, _, _), | |
| Name <> ReqName, | |
| Price1 + Price2 <= MaxPrice, | |
| write(Name, "\n"), | |
| fail. | |
| query_d(_, _, _). | |
| query_e(Name) :- | |
| collect_prices(Prices), | |
| max(Prices, Max), | |
| toy(Name, Price, _, _), | |
| Max - Price <= 1, | |
| fail. | |
| query_e(_). | |
| max([H|T], Max) :- max(T, H, Max), !. | |
| max([], Max, Max) :- !. | |
| max([H|T], CurrMax, Max) :- | |
| H > CurrMax, !, | |
| max(T, H, Max). | |
| max([_|T], CurrMax, Max) :- | |
| max(T, CurrMax, Max), !. | |
| goal | |
| init_counter, | |
| menu. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment