You are on page 1of 2

# Brža verzija:

operator('-').
operator('+').
operator('*').
operator('/').

mojbr(T, K, L):-duzina(T, D), u_intervalu(D1, 0, D), podlista(T1, T, D1), mojbr1(T1, _, K, L1), !, rpn_u_infix(L1, L).

## mojbr1(R, X, K, L1) :- duzina(R, D1), duzina(X, D2), D2+1=:=D1, !, operatori_liste(X),

permutacija(R, T), spojinak2(T, X, L), rpn_jednako(L, K), L1 = L.

## spojinak2([G|R], L, [G|K]) :- spojinak21(R, L, K).

spojinak21([], L, L) :- !.
spojinak21([G1|R], [X|L], [G1, X|K]) :- spojinak21(R, L, K).
spojinak21([G1, G2|R], L, [G1, G2|K]) :- spojinak21(R, L, K).

permutacija([], []).
permutacija([X], [X]) :- !.
permutacija([T|H], X) :- permutacija(H, H1), dodaj(L1, L2, H1), dodaj(L1, [T], X1), dodaj(X1, L2, X).

dodaj([], X, X).
dodaj([T|H], X, [T|L]) :- dodaj(H, X, L).

operatori_liste([X]) :- operator(X).
operatori_liste([X|Y]) :- operator(X), operatori_liste(Y).

## rpn_jednako(L, K) :- rpn1(L, [], K1), abs(K-K1)<0.000001.

podlista([], _, 0) :- !.
podlista([X|X1], [X|X2], K) :- K>0, K1 is K-1, podlista(X1, X2, K1).
podlista([X|X1], [_|X2], K) :- K>0, podlista([X|X1], X2, K).

rpn1([], [X], X) :- !.
rpn1(['+'|R], [G1, G2|R1], K) :- !, G is G2+G1,
rpn1(R, [G|R1], K).
rpn1(['-'|R], [G1, G2|R1], K) :- !, G is G2-G1,
rpn1(R, [G|R1], K).
rpn1(['*'|R], [G1, G2|R1], K) :- !, G is G2*G1,
rpn1(R, [G|R1], K).
rpn1(['/'|R], [G1, G2|R1], K) :- !, G1=\=0, G is G2/G1,
rpn1(R, [G|R1], K).
rpn1([G|R], S, K) :- !, not(G=='+'; G=='-'; G=='/'; G=='*'),
rpn1(R, [G|S], K).

## rpn_u_infix(R, L):-rui(R, [], L).

rui([], [G], G).
rui([G|R], S, L):-not(operator(G)), rui(R, [G|S], L).

rui(['+'], [G1, G2| S], L) :- !, atomic_list_concat([G2, '+', G1], '', L1), rui([], [L1|S], L).
rui(['-'], [G1, G2| S], L) :- !, atomic_list_concat([G2, '-', G1], '', L1), rui([], [L1|S], L).
%ui(['/'], [G1, G2| S], L) :- atomic_list_concat([G2, '/', G1], '', L1), rui([], [L1|S], L).
rui(['+'|R], [G1, G2| S], L) :- atomic_list_concat(['(', G2, '+', G1, ')'], '', L1), rui(R, [L1|S], L).
rui(['-'|R], [G1, G2| S], L) :- atomic_list_concat(['(', G2, '-', G1, ')'], '', L1), rui(R, [L1|S], L).
rui(['*'|R], [G1, G2| S], L) :- atomic_list_concat([G2, '*', G1], '', L1), rui(R, [L1|S], L).
rui(['/'|R], [G1, G2| S], L) :- atomic_list_concat([G2, '/', G1], '', L1), rui(R, [L1|S], L).

duzina([], 0).
duzina([_|K], D):-duzina(K, D1), D is D1+1.

u_intervalu(X, L, H) :- X is L + 1, X =< H.
u_intervalu(X, L, H) :- L1 is L + 1, L1 < H, u_intervalu(X, L1, H).
Sporija verzija:

operator('-').
operator('+').
operator('*').
operator('/').

mojbr(T, K, L):-duzina(T, D), u_intervalu(D1, 0, D), podlista(T1, T, D1), mojbr1(T1, _, K, L1), !, rpn_u_infix(L1, L).

mojbr1(R, X, K, L1) :- duzina(R, D1), duzina(X, D2), D2+1=:=D1, !, operatori_liste(X), permutacija(R, T),
spojinak2(T, X, L), rpn_jednako(L, K), L1 = L.

## spojinak2([G, G1|R], L, [G, G1|K]) :- spojinak21(R, L, K, -1).

spojinak21([], L, L, _) :- !.
spojinak21([G1|R], L, [G1|K], P) :- P1 is P-1, spojinak21(R, L, K, P1).
spojinak21(R, [X|L], [X|K], P) :- P =< -1, P1 is P+1, spojinak21(R, L, K, P1).

permutacija([], []).
permutacija([X], [X]) :- !.
permutacija([T|H], X) :- permutacija(H, H1), dodaj(L1, L2, H1), dodaj(L1, [T], X1), dodaj(X1, L2, X).

dodaj([], X, X).
dodaj([T|H], X, [T|L]) :- dodaj(H, X, L).

operatori_liste([X]) :- operator(X).
operatori_liste([X|Y]) :- operator(X), operatori_liste(Y).

podlista([], _, 0) :- !.
podlista([X|X1], [X|X2], K) :- K>0, K1 is K-1, podlista(X1, X2, K1).
podlista([X|X1], [_|X2], K) :- K>0, podlista([X|X1], X2, K).

## rpn_jednako(L, K) :- rpn1(L, [], K1), abs(K-K1)<0.000001.

rpn1([], [X], X) :- !.
rpn1(['+'|R], [G1, G2|R1], K) :- !, G is G2+G1,
rpn1(R, [G|R1], K).
rpn1(['-'|R], [G1, G2|R1], K) :- !, G is G2-G1,
rpn1(R, [G|R1], K).
rpn1(['*'|R], [G1, G2|R1], K) :- !, G is G2*G1,
rpn1(R, [G|R1], K).
rpn1(['/'|R], [G1, G2|R1], K) :- !, G1=\=0, G is G2/G1,
rpn1(R, [G|R1], K).
rpn1([G|R], S, K) :- !, not(G=='+'; G=='-'; G=='/'; G=='*'),
rpn1(R, [G|S], K).

## rpn_u_infix(R, L):-rui(R, [], L).

rui([], [G], G).
rui([G|R], S, L):-not(operator(G)), rui(R, [G|S], L).

rui(['+'], [G1, G2| S], L) :- !, atomic_list_concat([G2, '+', G1], '', L1), rui([], [L1|S], L).
rui(['-'], [G1, G2| S], L) :- !, atomic_list_concat([G2, '-', G1], '', L1), rui([], [L1|S], L).
%ui(['/'], [G1, G2| S], L) :- atomic_list_concat([G2, '/', G1], '', L1), rui([], [L1|S], L).
rui(['+'|R], [G1, G2| S], L) :- atomic_list_concat(['(', G2, '+', G1, ')'], '', L1), rui(R, [L1|S], L).
rui(['-'|R], [G1, G2| S], L) :- atomic_list_concat(['(', G2, '-', G1, ')'], '', L1), rui(R, [L1|S], L).
rui(['*'|R], [G1, G2| S], L) :- atomic_list_concat([G2, '*', G1], '', L1), rui(R, [L1|S], L).
rui(['/'|R], [G1, G2| S], L) :- atomic_list_concat([G2, '/', G1], '', L1), rui(R, [L1|S], L).

duzina([], 0).
duzina([_|K], D):-duzina(K, D1), D is D1+1.

u_intervalu(X, L, H) :- X is L + 1, X =< H.
u_intervalu(X, L, H) :- L1 is L + 1, L1 < H, u_intervalu(X, L1, H).