Изучение основных способов представления математических проблем в символьном виде, пригодном для формальной обработки на компьютере, страница 7

strMULT(X,STR):-strMINUS(X,STR).

strMINUS(plus(X,Y),STR):-!,strPAR(plus(X,Y),STR).

strMINUS(X,STR):-strexp(X,STR).

strPAR(EXP,STR):strexp(EXP,STR1),

concat("(",STR1,STR2),

concat(STR2,")",STR).

/*

CLAUSES TO EDIT A STRING

*/

editstr(InString, OutString, Xpos, Ypos, Cpos) :cursor(Ypos,Xpos),

write(InString," "),

NXPos = Xpos+Cpos,

cursor(Ypos,NXpos),

readchar(Ch),

!,

editstr(Ch, InString, OutString, Xpos, Ypos, Cpos).

% Return -- Accept the current string.

editstr('\13',InString,InString,Xpos,Ypos,_) :cursor(Ypos,Xpos),

write(InString), nl.

% ESC -- Terminate the program.

editstr('\27',_,"",_,_,_) :- exit.

% HELP -- Display the help message

% taken from checkhelp.

editstr('?',InString,OutString,Xpos,Ypos,Cpos) :-

makewindow(4,23,7,"",10,3,14,73),

file_str("integr.hlp",I),

display(I),

removewindow,

!,

editstr(InString,OutString,Xpos,Ypos,Cpos).

% ^S -- Move Cursor left

editstr('\19',InString,OutString,Xpos,Ypos,Cpos) :Cpos > 0,

NewCpos = Cpos - 1,

!,

editstr(InString,OutString,Xpos,Ypos,NewCpos).

editstr('\19',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr(InString,OutString,Xpos,Ypos,Cpos).

% ^D -- Move Cursor left

editstr('\4',InString,OutString,Xpos,Ypos,Cpos) :str_len(InString,InStrLen),

Cpos < InStrLen,

NewCpos = Cpos + 1, !,

editstr(InString,OutString,Xpos,Ypos,NewCpos).

editstr('\4',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr(InString,OutString,Xpos,Ypos,Cpos).

% ^H -- Backspace, Delete the previous character.

editstr('\8',InString,OutString,Xpos,Ypos,1) :frontchar(InString,_,NewString),

!,

editstr(NewString,OutString,Xpos,Ypos,0).

editstr('\8',InString,OutString,Xpos,Ypos,Cpos) :Cpos > 1,

NewCpos = Cpos - 1,

!,

editstr('\21',InString,OutString,Xpos,Ypos,NewCpos). % Delete Char.

editstr('\8',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr(InString,OutString,Xpos,Ypos,Cpos).

% ^U -- Delete the current character.

editstr('\21',InString,OutString,Xpos,Ypos,Cpos) :frontstr(Cpos,InString,HeadString,TailStringP),

frontchar(TailStringP,_,TailString),

concat(HeadString,TailString,NewString),

!,

editstr(NewString,OutString,Xpos,Ypos,Cpos).

editstr('\21',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr(InString,OutString,Xpos,Ypos,Cpos).

% Insert a Character.

editstr(Ch,InString,OutString,Xpos,Ypos,Cpos) :makewindow(_,_,_,_,_,_,_,Width),

str_len(InString,InStrLen),

InStrLen < Width - 3 - Xpos,

Ch >= ' ', Ch <= '~',

frontstr(Cpos,InString,HeadString,TailStringP),

frontchar(TailString,Ch,TailStringP),

concat(HeadString,TailString,NewString),

NewCpos = Cpos + 1,

!,

editstr(NewString,OutString,Xpos,Ypos,NewCpos).

% Extended Key Hit.

editstr('\0',InString,OutString,Xpos,Ypos,Cpos) :readchar(Key), !,

editstr(extended,Key,InString,OutString,Xpos,Ypos,Cpos).

% Ignore All other keys pressed.

editstr(_,InString,OutString,Xpos,Ypos,Cpos) :editstr(InString,OutString,Xpos,Ypos,Cpos).

% Process extended keys.

% Del Key, map to the ^U key.

editstr(extended,'\83',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr('\21',InString,OutString,Xpos,Ypos,Cpos).

% Left Arrow, map to the ^S key.

editstr(extended,'\75',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr('\19',InString,OutString,Xpos,Ypos,Cpos).

% Right Arrow, map to the ^D key.

editstr(extended,'\77',InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr('\4',InString,OutString,Xpos,Ypos,Cpos).

% End.

editstr(extended,'\79',InString,OutString,Xpos,Ypos,_) :-

str_len(InString,InStrLen),

!,

editstr(InString,OutString,Xpos,Ypos,InStrLen).

% Home.

editstr(extended,'\71',InString,OutString,Xpos,Ypos,_) :- !,

editstr(InString,OutString,Xpos,Ypos,0).

% Ignore the rest.

editstr(extended,_,InString,OutString,Xpos,Ypos,Cpos) :- !,

editstr(InString,OutString,Xpos,Ypos,Cpos).

/*

UTILITY TO TRANSFORM A TOKANIZED LIST TO A LIST

*/

lstcat([],"").

lstcat([X|Xs],STR) :-

lstcat(Xs,STR1),

concat(X,STR1,STR).