📄 tools.pro
字号:
/*****************************************************************************
Copyright (c) Phd 301 Workshop
Project: FORMULA
FileName: TOOLS.PRO
Purpose: Developed by Chen Hongbiao, All Rights Reserved. Granted to be used by Xuwang Company
Written by: Dr. Chen Hongbiao (陈鸿标 )
Comments:
******************************************************************************/
include "formula.inc"
include "formula.con"
include "hlptopic.con"
/*
domains
slist=string*
rlist=real*
include "tools.pre"
PREDICATES
convert_str_list(string InpStr,string Marker,slist OutSList)-(i,i,o)
replace_str(string Instr,slist TobeRpList,slist RpList, string Tstr, string Outstr)-(i,i,i,i,o)
slist_member_pos(string Mem,slist MemLst, real N,real N)-(i,i,i,o)
nth_Slist_member(string Mem,slist Rplist, real Nth,real C)-(o,i,i,i)
str_slist_member(STRING InStr,SLIST StrList, STRING Member, INTEGER Pos)-(i,i,o,o)
*/
CLAUSES
%%%%%begin: tool: str_slist_member(InStr, StrList, Member, Pos)- (i,i,o,o): check if InStr contains
%%%% a member of StrList, output what Member, where it is in InStr. If not found, member="", Pos=0
str_slist_member(_InStr, [], "", 0):-!.%finished, and not found
str_slist_member(InStr, [M|List], Member, Pos):- %loop
not(searchstring(InStr, M,_)), !,%if found,fail, go to below clause
str_slist_member(InStr, List, Member, Pos).
str_slist_member(InStr, [M|_List], M, Pos):- %found one, not finished
searchstring(InStr,M,Pos),!.
%%%%%end: tool: str_slist_member(InStr, StrList, Member, Pos)- (i,i,o,o): check if InStr contains
%%%begin: concat slist member if it is not included in one slist already
PREDICATES
check_concat(STRING Str, slist SLIst2,slist SList3)
CLAUSES
check_concat_slists([],Slist2,SList2):-!.
check_concat_slists([Str|RSList1],SList2,Slist3):-
check_concat_slists(RSList1,SList2,TSlist3),
check_concat(Str, TSLIst3,SList3).
%
check_concat(Str, SLIst2,[Str|SList2]):-
str_slist_member(Str, SList2, _Member, Pos),
% write("Str=",Str, ", Slist2=",Slist2, ", Pos= ",POs),nl,
Pos=0,%not existed
!.
check_concat(_Str, SLIst2,SList2):-!. % already existed , no concat there
%%%end: concat slist member if it is not included in one slist already
%%%find out where the specified member is in a slist
slist_member_pos(Mem,[Mem|_RLst],N,N):-!.
slist_member_pos(_Mem,[],_N,0):-!.
slist_member_pos(Mem,[_|RLst],TN,N):-
NTN=TN+1,
slist_member_pos(Mem,RLst,NTN,N).
%%%count slist member's number
slist_length([], 0):-!.
slist_length([_|InfUnitList], ListN):-
slist_length(InfUnitList, TListN),
ListN=TListN+1.
%%%pick out the specified seq of a list member
nth_Slist_member("",[], _Nth,_C):-!.
nth_Slist_member(Mem,[Mem|_Rlist], Nth,Nth):-!.
nth_Slist_member(Mem,[_|Rlist ],Nth,C):-
Nc=C+1,
nth_Slist_member(Mem,Rlist, Nth,NC).
%%%replace the vars in expr with values
PREDICATES
check_replace(string T,slist TobeRpList,slist RpList, string ThisStr)
CLAUSES
replace_str("",_TobeRpList, _RpList, Expr,Expr):-
!.
replace_str(Instr,TobeRpList,RpList, Texpr, Expr):-
fronttoken(Instr,T,NInstr),
check_replace(T,TobeRpList,RpList, ThisStr),
concat(Texpr,ThisStr,NTexpr),
replace_str(NInstr,TobeRpList,RpList, NTexpr,Expr).
%%%replace if the token is one of TobeRplist
check_replace(T,TobeRpList,_RpList, ThisStr):- %not varialble
str_slist_member(T, TobeRpList, _Member, Pos),
Pos=0,
ThisStr=T,
!.
check_replace(T,TobeRpList,RpList, ThisStr):- %T is var, get his value
str_slist_member(T, TobeRpList, _Member, Pos),
Pos<>0,!,
slist_member_pos(T,TobeRplist,1,Nth),
nth_Slist_member(ThisStr,Rplist, Nth,1),
fronttoken(ThisStr, _,_),
!.
check_replace(_T,_TobeRpList,_RpList, _ThisStr):- %T is var, get his value
% dlg_note("用数值取代公式中的变量时出错。"),
fail.
%%% begin: by using the marker, convert a string to a list, e.g. "NP,VP" will be [NP,VP]
predicates
chk_addtolist(string Token,string Marker,slist Tlist,slist Tlist)
clauses
%%% begin: by using the marker, convert a string to a list, e.g. "NP,VP" will be [NP,VP]
convert_str_list(InpStr,Marker,OutSlist):-
not(searchstring(InpStr,Marker,_)),
fronttoken(Inpstr,_,_),
OutSList=[InpStr],% write(InpStr, ", ",OutSList),nl,
!.
convert_str_list(InpStr,Marker,NTList):-
% fronttoken(Inpstr,T,RestStr),!,
searchstring(InpStr,Marker,Pos), %if fail, go to below
L=Pos-1,
frontstr(L,Inpstr,T,NextStr0),!,
% write("Inpstr= ", Inpstr, " \n Element0= ", Element0, " \nNextStr0= ", NextStr0),nl,
% del_a_char(Element0,' ', Element),
concat(Marker,RestStr,NextStr0), % delete the "," in",VP"
convert_str_list(RestStr,Marker,Tlist),
chk_addtolist(T,Marker,Tlist,NTlist).
convert_str_list(Reststr,_Marker,[]):-%if RestStr is almost empty
not(fronttoken(RestStr,_,_)),
!.
convert_str_list(Reststr,_Marker,[]):-%if RestStr is almost empty
fronttoken(RestStr,_,_),!.
chk_addtolist(T,Marker,Tlist,[T|Tlist]):-%the token is not the marker itself
T<>Marker,
fronttoken(T,_,_),
!.
chk_addtolist(_T,_Marker,Tlist,Tlist):-%the token is the marker itself, do not put it to list
!.
%%% end: by using the marker, convert a string to a list, e.g. "NP,VP" will be [NP,VP]
%%%begin: del a delstr from a Instr
del_str(Instr,_DelStr,OutStr):-
not(searchstring(Instr,_Delstr,_)),
OutStr=Instr,
!.
del_str(Instr,_DelStr,OutStr):-
not(fronttoken(Instr,_,_)),
OutStr=Instr,
!.
del_str(Instr,DelStr,OutStr):-
searchstring(Instr, DelStr, Pos),
Length=Pos-1,
% write("L=",Length),nl,
frontstr(Length,Instr,PureStr,RestStr0),
concat(DelStr, RestStr,RestStr0),
del_str(RestStr, Delstr, TOutStr),
concat(PureStr, TOutstr,OutStr).
%%%end: del a str1 from a Instr
%%%begin: replace last char of all mems of a slist with ReplChar: ["A1","B1"] becomes ["A2","B2"]
repl_slist_lastChar([],_Withstr, []):-
!.
repl_slist_lastChar([ToBe|RList],WithStr, [Replaced|TOutList]):-
repl_slist_lastChar(RList,WithStr, TOutList),
str_len(ToBe,L),
Len=L-1,
frontstr(Len,ToBe,PureStr,_LastChar),
concat(PureStr,WithStr, Replaced).
%%%end: replace last char of all mems of a slist with ReplChar: ["A1","B1"] becomes ["A2","B2"]
%%% begin: check whether an element is a member of a string list
slist_member(_,[],b_false):-!.
slist_member(M,[M|_],b_true):-!.
slist_member(M,[_|T],S):- slist_member(M,T,S).
%%% end: check whether an element is a member a string list
%%%begin: put slist1 and slist2 together. ie: slist1+slist2=slist3
concat_slists([],Slist2,SList2):-!.
concat_slists([Str|RSList1],SList2,[Str|TSlist3]):-
concat_slists(RSList1,SList2,TSlist3).
%%%test a string is a letter StrYN=yes /no
is_letter(Letterstr, yes):-
upper_lower(Letterstr1,Letterstr),
frontchar(Letterstr1,FrontChar,_RestString),
FrontChar>='A',
FrontChar<='Z',
!.
is_letter(_Letterstr, no):-!.
/*
goal
convert_str_list("我门",",",Out).
%slist_member_pos("2",["1","2","3","4"],1,N).
nth_Slist_member(Mem,["1","2","3","4"],5,1).
replace_str("A*2+B",["A","B"],["13","24"], "", Expr).
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -