/*************************************************************************** TRABAJO CON STRING, CHAR y EDITOR Inteligencia Artificial Gonzalo Villarreal Farah Para Turbo Prolog 2.0 de Borland ****************************************************************************/ DOMAINS i = integer st = string PREDICATES pres lec_esc frente_str largo_str verifica(st,i,st) lee_largo(i) fren_char muestra_corte(i,st) verifica_char(st,char,st,st) fren_str muestra_pal(i,st) verifica_str(st,st,st,st) editor CLAUSES /********************************************************************** IMPORTANTE ========== Las Declaraciones que a Continuaci¢n Veremos son proporcionadas por Turbo Prolog. Adem s de su utilizaci¢n directa, que luego detallaremos, el lenguaje da la posivilidad de considerar los par metros de esta en m s de un sentido (Input,Output), maximizando el potencial de cada declaraci¢n. ***********************************************************************/ % field_str permite a partir de la Fila y Columna de la ventana % actival eer o escribir (input/output) un String de longitud Largo % field_str(Fila,Columna,Largo,String) % (integer,integer,integer,string) - (i,i,i,i) (i,i,i,o) % Ejemplo 1 % Goal: lec_eje lec_esc:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7,"ESCRITURA/LECTURA",6,10,14,60), field_str(2,3,10,"Procesamiento de Datos"), field_str(5,7,17,"Presione [Enter]"), cursor(5,24),readln(_), field_str(5,10,8,OUT), field_str(7,5,8,OUT), cursor(7,13),readln(_), removewindow(1,1),removewindow(2,1). %---------------------------------------------------------------------- % Ejemplo 2 % Goal: frente_str % frontstr entrega en la variable ComienzoString los primeros caracteres % de longitud Largo y en RestoString el resto del String %frontstr(Largo,InpString,ComienzoString,RestoString) % (integer,string,string,string) - (i,i,o,o) frente_str:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7," FRONTSTR ",6,10,14,60), write("\n\tIngrese un String: "), readln(String), write("\n\tIngrese el Largo donde cortar el String: "), readint(Largo), frontstr(Largo,String,ComienzoString,RestoString), clearwindow, write("\n Strin de ingreso : ",String), write("\n Largo a cortar el String: ",Largo), write("\n Comienzo del String : ",ComienzoString), write("\n Resto del String : ",RestoString), readln(_),removewindow(1,1),removewindow(2,1). %---------------------------------------------------------------------- % Ejemplo 3 % Goal: largo_str % str_len permite: % 1.- Ver si el largo del String que se ingresa corresponde % al valor (entero) que se ingresa (i,i) % 2.- Obtenel el largo de un String ingresado (i,o) % 3.- Obtene un String bacio de longitud Largo % str_len(String,Largo) % (string,integer) - (i,i) (i,o) (o,i) largo_str:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7," STR_LEN ",6,10,14,60), write("\n Ingresa un String : "),readln(String), write("\n Ingresa un Numero : "),readint(Largo), verifica(String,Largo,SN), clearwindow, write("\n El Numero ",Largo," ",SN," al Largo"), write("\n del String \"", String,"\""),readln(_),fail; clearwindow, write("\n Ingresa un String : "),readln(String), clearwindow, str_len(String,Largo), write("\n El Largo del String \" ",String), write("\"\n es: ",Largo),readln(_),fail; clearwindow, write("\n Ingresa un Numero entre 1 y 5: "),lee_largo(Largo), clearwindow,str_len(String,Largo), write("\n\n Aqui hay ",Largo,String," espacio(s) en blanco."), readln(_),removewindow(1,1),removewindow(2,1). verifica(String,Largo,"corresponde"):-str_len(String,Largo),!. verifica(_,_,"no corresponde"):-!. lee_largo(Largo):-readint(Largo),Largo>=1,Largo<=5,!. lee_largo(Largo):-!,lee_largo(Largo). %------------------------------------------------------------------- % frontchar : % Como permite varias posibilidades de entrada/salida (input/output), % para los tres par metros (String,FrontChar,RestoString), dependiendo de % esto la interpretaci¢n de lo que haga. % Ej.: El caso de mayor uso de frontchar(String,FrontChar,RestoString) % es cuando String es de entrada y FrontChar, RestoString son de salida, % con esto se consigue obtener el primer caracter (FrontChar) del String, % adem s del resto de este £ltimo, que es el String ingresado sin el primer % caracter. % frontchar(String,FrontChar,RestoString) % (string,char,string) - (i,o,o) (i,i,o) % (i,o,i) (i,i,i) (o,i,i) % Ejemplo 4 % Goal: fren_char fren_char:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7," FRONTCHAR ",6,10,14,60), write("\n Ingresa un String : "),readln(String), clearwindow, write("\n Este es un ejemplo de frontchar : "), muestra_corte(1,String),readln(_),fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa un Caracter: "),readchar(FrontChar), verifica_char(String,FrontChar,SN,RestoStrig), clearwindow, write("\n El caracter '",Frontchar,"'",SN,"corresponde al"), write("\n primer caracter de \"",String,"\""),readln(_),fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa otro String: "),readchar(RestoString), verifica_char(String,FrontChar,SN,RestoStrig), clearwindow, write("\n El segundo String \"",RestoString,"\"",SN,"corresponde al"), write("\n resto del Strig \"",String,"\", donde"), write("\n su primer caracter es '",FrontChar," '"),readln(_),fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa un Caracter: "),readchar(FrontChar), write("\n Ingresa otro String: "),readln(RestoString), verifica_char(String,FrontChar,SN,RestoStrig), clearwindow, write("\n El caracter '",Frontchar,"' unido (concatenado)"), write("\n con el £timo Sting \"",SN,"corresponde al"), write("\n primer al String \"",String,"\""),readln(_),fail; removewindow(1,1),removewindow(2,1). muestra_corte(_,""):-!. muestra_corte(Cont,String):-frontchar(String,FrontChar,RestoString), write("\n Caracter N¤",Cont,": ",FrontChar), Cont2=Cont+1,!,muestra_corte(Cont2,RestoString). verifica_char(String,FrontChar," ",RestoString):- frontchar(String,FrontChar,RestoString),!. verifica_char(_,_," no ",""):-!. %---------------------------------------------------------------------- % fronttoken: Esta instrucci¢n es similar al frontchar, pero aqui % en una de las formas a utilizar esta sentencia, por su variedad de % interpretaciones seg£n sea el input u output, se puede obtener % la primera secuencia de caracteres, hasta el primer blanco, de un String % % Ej: fronttoken(" Este es un ejemplo de fronttoken",Frente,RestoString) % (i,o,o) % donde se instanciar n: % Frente =Este % RestoString = es un ejemplo de fronttoken % fronttoken(String,Frente,RestoString) % (string,string,string) - (i,o,o) (i,i,o) % (i,o,i) (i,i,i) (o,i,i) % Ejemplo 5 % Goal: fren_str fren_str:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7," FRONTTOKEN ",6,10,14,60), write("\n Ingresa un String : "),readln(String), clearwindow, write("\n Este es un ejemplo de fronttoken : "), muestra_pal(1,String),pres,fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa una Palabra: "),readln(Frente), verifica_str(String,Frente,SN,RestoStrig), clearwindow, write("\n La Palabra \"",Frente,"\"",SN,"corresponde a la"), write("\n primer palabra de \"",String,"\""),pres,fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa otro String: "),readln(RestoString), verifica_str(String,Frente,SN,RestoStrig), clearwindow, write("\n El segundo String \"",RestoString,"\"",SN,"corresponde al"), write("\n resto del Strig \"",String,"\", donde"), write("\n su primera palabra es \"",Frente,"\""),pres,fail; clearwindow, write("\n Ingresa un String : "),readln(String), write("\n Ingresa una Palabra: "),readln(Frente), write("\n Ingresa otro String: "),readln(RestoString), verifica_str(String,Frente,SN,RestoStrig), clearwindow, write("\n La Palabra \"",Frente,"\" unida (concatenado)"), write("\n con el £timo Sting ",SN,"corresponde al"), write("\n primer al String \"",String,"\""),pres,fail; removewindow(1,1),removewindow(2,1). muestra_pal(_,""):-!. muestra_pal(Cont,String):-fronttoken(String,Frente,RestoString), write("\n String N¤",Cont,": ",Frente), Cont2=Cont+1,!,muestra_pal(Cont2,RestoString). verifica_str(String,Frente," ",RestoString):- fronttoken(String,Frente,RestoString),!. verifica_str(String,Frente," no ",RestoString):- fronttoken(String,Frente,RestoString),!. pres:-cursor(11,30),write(" Presione [Enter]"),beep,readln(_). /***************************************************************** Editor ====== Prolog proporciona una importante herramienta en relaci¢n a Editores para esto se cuenta con: edit(StringIngreso,StringSalida) (string,string) - (i,o) Proporciona un ambiente, donde uno puede digitar lo que desee. Si ingresa un String (StringIngreso), este aparecer en la pantalla que se active para el editor, dando la posibilidad de digitar algo m s y teniendo una salida es StringSalida un String que es la conca- tenaci¢n entre el StringIngreso y lo que se digite. La manera dee abandonarlo es con la tecla F10 o [Esc]. ------------------------------------------------------------------------- Aquella personas que se interesen en algo mas que el simple editor anterior, se les recomienda que estudien el siguiente editor, el cual da grandes ventajas que pueden ser interesantespara tus futuras aplicasiones hechas en Turbo Prolog, este editor es: edit(InputString,OutputString,Headstr,Headstr2,Msg,Pos,Helpfilename, EditMode,Indent,Insert,TextMode,RetPos,RetStatus) (string,string,string,string,string,integer,string, integer,integer,integer,integer,integer,integer) - (i,o,i,i,i,i,i,i,i,i,i,o,o) If the user saves the text from the editor, HeadStr2 will be used as the file name. EditMode = 0 means display mode = 1 means read/overwrite mode Indent = 0 no indentation = 1 auto indentation on Insert = 0 overwrite mode = 1 insert mode TextMode = 0 Textmode off = 1 Textmode on (automatic linefeed) RetPos = Cursor position related to OutPutString at return. RetStatus = 0 F10 editor ended by F10 1 F10 editor ended by Esc *****************************************************************/ % Ejemplo 6 % Goal: editor editor:-makewindow(1,112,0,"",0,0,25,80), makewindow(2,7,7,"",1,10,12,60), edit(" Este es un ejemplo del editor de Prolog, con este INPUT\n ",S), makewindow(3,7,7," ESTA ES LA SALIDA DEL EDITOR ",14,10,10,60), window_str(S), % Despliega en la ventana activa el String S % desde el comienzo, hasta lo que se alcance a % visualizar, dependiendo de la ventana activa. % window_str(StringVentanaActiva) % (String) (i) (o) shiftwindow(1),cursor(24,60), write(" Presione [Enter]"), shiftwindow(2),shiftwindow(3), readchar(_),fail; removewindow(1,1), removewindow(2,1),removewindow(3,1), makewindow(1,112,0,"",0,0,25,80), cursor(10,10), write(" Veremos un Ejemplo en donde se activa un editor\n"), write("\t\t SIN un Ingreso (StrinIngreso)\n\n\t\t\t\t[Enter]"), readchar(_),clearwindow, makewindow(2,7,7,"",1,10,12,60), edit("",S), concat(S," \n\n\t\t\t\t [Esc]",S2), makewindow(3,7,7," ESTA ES LA SALIDA DEL EDITOR ",14,10,10,60), display(S2), % Despliega en la ventana que este activa el String % S2, dando la posibilidad de recorrer el escrito, % sin poder modificarlo, s¢lo se sale con [Esc]. % display(String) % (string) - (i) removewindow(1,1), removewindow(2,1),removewindow(3,1).