Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

program now working correclty

Status
Not open for further replies.

adienn

Programmer
Jun 11, 2013
1
PL
hi i have to write a program which solves problem
B+G=D
B+C=A
C+E+G=F
if D<A then C=2 and if D>=A then E=2
each letter equals different number from 0 to 9 and have to find solution with lowest max(max is highest number in current solution)
i wrote program but some parts seem to not be used and cant figure out why
Code:
domains
Intlist = Integer*


predicates
nondeterm rozwiazanie
nondeterm sprawdz(Intlist)
nondeterm wariacja(Integer,Intlist,Intlist)
nondeterm usun(Integer,Intlist,Intlist)
nondeterm max(Integer,Intlist,Intlist)
nondeterm przy(Intlist)
nondeterm writte
database - mydb
max2(Integer)
max3(Integer)
rozw(Symbol,Integer)
goal
rozwiazanie.
clauses
rozwiazanie:-
wariacja(10,[0,1,2,3,4,5,6,7,8,9],[A,B,C,D,F,E,G]),
sprawdz([A,B,C,D,F,E,G]),
 write("E= ",E," A= ",A," B= ",B," C= ",C," D= ",D," F= ",F," G= ",G),nl,%if needed writes every solution
 max(0,[A,B,C,D,E,F,G],[A,B,C,D,E,F,G]),nl,fail.
rozwiazanie:-writte.
 

sprawdz([A,B,C,D,F,E,G]):-D>=A,B+G=D,B+C=A,C+E+G=F,E=2;
D<A,B+G=D,B+C=A,C+E+G=F,C=2.

wariacja(3,_,[]):-!. 
wariacja(N,L,[H|T]):-		usun(H,L,LW),		NN=N-1,			wariacja(NN,LW,T).

usun(E,[E|T],T). 
usun(E,[H|T1],[H|T2]):-		usun(E,T1,T2).


max(T,[],[A,B,C,D,E,F,G]):-T=7,    %<-part 1 starts here
max3(X),write("x=  ",X),nl,
max2(Y),X<Y,
asserta(max2(X),mydb),       
przy([A,B,C,D,E,F,G]),write("pra"),nl,
asserta(max3(0),mydb), 
!.                                   %<-part 1 ends here

max(T,[GG|TT],RR):-                 $part2 starts here
max3(X),T>1,TTT=T+1,
GG>X,
asserta(max3(GG),mydb),write("aktualne max =",GG),nl,
max(TTT,TT,RR).                                            %<ends here

max(T,[GG|TT],RR):-       % part 3 starts here
T=0,TTT=T+1,
GG>T,
asserta(max3(GG),mydb),write("t=",T,"   max=",GG),nl,
max(TTT,TT,RR).   $part 3 ends here

przy([A,B,C,D,E,F,G]):- 
asserta(rozw("A",A),mydb),write("przyp A"),nl,
asserta(rozw("B",B),mydb),
asserta(rozw("C",C),mydb),
asserta(rozw("D",D),mydb),
asserta(rozw("E",E),mydb),
asserta(rozw("F",F),mydb),
asserta(rozw("G",G),mydb).


writte:- %write solution on screen using facts made by program
rozw("A",A),
rozw("B",B),
rozw("C",C),
rozw("D",D),
rozw("E",E),
rozw("F",F),
rozw("G",G),
max3(T),
write("best solution a=",A," b=",B," c=",C," d=",D," e=",E," f=",F," g=",G,"  max =",T).

max2(100).
max3(1).
rozw("A",1).
rozw("B",1).
rozw("C",1).
rozw("D",1).
rozw("E",1).
rozw("F",1).
rozw("G",1).
main problem is with parts 1-3 (think rest of program is fine) i trying things and think part 3 is working but if i use "write" i can see part 1 and 2 is not even executed

hope someone can help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top