Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Lista de cadenas (https://www.clubdelphi.com/foros/showthread.php?t=46253)

Khronos 27-07-2007 02:21:40

Lista de cadenas
 
Estoy trabajando con una {$APPTYPE CONSOLE} y estoy haciendo una especie de cmd pero con mis propios comandos.
Lo que estoy haciendo es que vaya leyendo las lineas hasta que una coincida con un comando que tengo asignado y que realice esa accion.
Por ejemplo(les pego un pedazo de codigo):

Código:

var
Comando: string;
 
Procedure DetectarComando;
var
repetir: boolean;
begin
 
repetir:= true;
Repeat
    Readln(Comando);
      If Comando='edit' then
          begin
              repetir:=false;
          end;
      If Comando='mail' then
          begin
              repetir:=false;
          end;
      If Comando='quit' then
          begin
              repetir:=false;
          end;
until repeat=false;
end;
 
 
begin
If comando='edit' then
  begin
    Edit;
  end;
 
end;

Espero que se entienda. La lista de comandos cada vez se me hace mas grande y es poco "elegante" lo que estoy haciendo yo :D
¿Como podria hacerlo de otra forma?
Probe con una variable de tipo TStringList con todos los comandos registrados en los items y haciendo una busqueda, pero se me cierra.

Salu2

seoane 27-07-2007 02:44:36

A mi no me parece mal lo que estas haciendo, pero si buscas formas "originales" de hacerlo:

Código Delphi [-]
program Project1;

{$APPTYPE CONSOLE}

uses SysUtils, Classes;

type
  THandler = procedure(Cmd: String);

procedure Ping(Cmd: String);
begin
  Writeln('Pong');
end;

procedure Salir(Cmd: String);
begin
  Halt;
end;

var
  i: Integer;
  Commands: TStringList;
  Str: String;

begin
  Commands:= TStringList.Create;
  try
    // Creamos la lista de comandos
    Commands.AddObject('PING',TObject(@Ping));
    Commands.AddObject('SALIR',TObject(@Salir));
    while TRUE do
    begin
      Readln(Str);
      i:= Commands.IndexOf(Uppercase(Str));
      if i <> -1 then
        THandler(Commands.Objects[i])(Str);
    end;
  finally
    Commands.Free;
  end;
end.

Khronos 28-07-2007 02:41:46

Gracias por responder seoane, ya lo consegui. Resulta que declaraba una variable de tipo TStringList, pero se me olvidaba crearla :D
Creo que te complicas demasiado, casi no entendi nada de tu codigo, yo lo hice mas sencillito:


Código Delphi [-]
 
program Phoenix;
 
{$APPTYPE CONSOLE}
 
uses
  SysUtils, Classes;
 
var
Comando: string= '';
respuesta: boolean;
Repetir: string='';
Lista: TStringList;
ID: integer;
 
procedure SearchCommand;
var
i: integer;
begin
for i:=0 to 5 do
begin
Readln(Comando);
if lista.Find(Comando, i)=true then
begin
Exit;
end else
Writeln('Please enter a valid command');
end;
end;
 
begin
Lista:= Tstringlist.Create;
Lista.Insert(0, 'edit');
Lista.Insert(1, 'quit');
Lista.Insert(2, 'encrypt');
Lista.Insert(3, 'mail');
Lista.Insert(4, 'execute');
Writeln('Welcome to the application in MSDOS Phoenix');
Writeln('Ver.-[26/07/2007] - CopyLeft 2007 by Khronos');
repeat
SearchCommand;
ID:=Lista.IndexOf(comando);
case ID of
0: CommandEdit;
1: exit;
2: CommandEncrypt;
end;
 
comando:='';
ID:=-1;
until repetir='no';
end.


Salu2


La franja horaria es GMT +2. Ahora son las 16:52:41.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi