Entwickler-Ecke

Sonstiges (Delphi) - Text =>> Morsecode Probleme


JRNewers - Fr 08.03.13 20:06
Titel: Text =>> Morsecode Probleme
Guten Abend,
ich muss für die Schule ein Programm entwickeln das den Text eines Edit Feldes in einer Funktion in das Morsealphabet umwandelt. Jedoch habe ich ein Problem und zwar dieses:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
procedure TForm1.FormCreate(Sender: TObject);
var
    myfile:textfile;
    i:integer;
begin
  AssignFile(myFile, 'morse_alpha.txt');
  Rewrite(myFile);
  for i := 65 to 90 do
  begin
    writeln(myfile, chr(i));
     case chr(i) of
        'A' : writeln(myfile, '. -');
        'B' : writeln(myfile, '- ...');
        'C' : writeln(myfile, '- . - .');
        'D' : writeln(myfile, '- . .');
        'E' : writeln(myfile, '.');
        'F' : writeln(myfile, '. . - .');
        'G' : writeln(myfile, '- - .');
        'H' : writeln(myfile, '. . . .');
        'I' : writeln(myfile, '. .');
        'J' : writeln(myfile, '. - - -');
        'K' : writeln(myfile, '- . -');
        'L' : writeln(myfile, '. - . .');
        'M' : writeln(myfile, '- -');
        'N' : writeln(myfile, '- .');
        'O' : writeln(myfile, '- - -');
        'P' : writeln(myfile, '. - - .');
        'Q' : writeln(myfile, '- - . -');
        'R' : writeln(myfile, '. - .');
        'S' : writeln(myfile, '. . .');
        'T' : writeln(myfile, '-');
        'U' : writeln(myfile, '. . -');
        'V' : writeln(myfile, '. . . -');
        'W' : writeln(myfile, '. - -');
        'X' : writeln(myfile, '- . . -');
        'Y' : writeln(myfile, '- . - -');
        'Z' : writeln(myfile, '- - . .');
      end;
  end;
    for i := 97 to 122 do
  begin
    writeln(myfile, chr(i));
     case chr(i) of
        'a' : writeln(myfile, '. -');
        'b' : writeln(myfile, '- ...');
        'c' : writeln(myfile, '- . - .');
        'd' : writeln(myfile, '- . .');
        'e' : writeln(myfile, '.');
        'f' : writeln(myfile, '. . - .');
        'g' : writeln(myfile, '- - .');
        'h' : writeln(myfile, '. . . .');
        'i' : writeln(myfile, '. .');
        'j' : writeln(myfile, '. - - -');
        'k' : writeln(myfile, '- . -');
        'l' : writeln(myfile, '. - . .');
        'm' : writeln(myfile, '- -');
        'n' : writeln(myfile, '- .');
        'o' : writeln(myfile, '- - -');
        'p' : writeln(myfile, '. - - .');
        'q' : writeln(myfile, '- - . -');
        'r' : writeln(myfile, '. - .');
        's' : writeln(myfile, '. . .');
        't' : writeln(myfile, '-');
        'u' : writeln(myfile, '. . -');
        'v' : writeln(myfile, '. . . -');
        'w' : writeln(myfile, '. - -');
        'x' : writeln(myfile, '- . . -');
        'y' : writeln(myfile, '- . - -');
        'z' : writeln(myfile, '- - . .');
      end;
  end;
  CloseFile(myFile);


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
function Translateinttomorse(S:string):string;
var
    Res:string;
    myfile:textfile;
    Alpha,Morse:string;
    I:integer;
begin
AssignFile(myFile, 'morse_alpha.txt');
Reset(myFile);
Res:='';                         
while not Eof(myFile) do
  begin
     ReadLn(myFile, Alpha);
     ReadLn(myFile, Morse);
  for I := 1 to length(S) do          
   if  S[I] = Alpha  then
   begin
      Res:= Res +  Morse + '        ';
   end;
  end;
    CloseFile(myFile);
  Translateinttomorse:=Res;
end;


Wenn ich zum Beispiel hallo eingebe gibt er mir den Morse Code aus und zwar ahllo, das ist natürlich falsch. Mir ist Bewusst was abläuft im Code(Er nimmt die Reihenfolge des Alphabetes, jedoch fehlt mir einfach die Ueberlegung um das Problem zu lösen, sodass sich das richtige Ergebnis bildet.

Ich hoffe ihr könnt mir bei der Ueberlegung helfen.
Danke im Voraus. :)

PS: Ich muss eine externe Textdatei nehmen und eine Funktion (Wird später in eine eigne Unit eingesetzt) gehört zur Aufgabe für die Schule.


Mathematiker - Fr 08.03.13 20:24

Hallo,
die Logik Deiner Routine ist nicht korrekt.
Bei Deiner Lösung wirst Du bei jedem Wort die Buchstaben alphabetisch sortiert bekommen.

Du musst für jeden Buchstaben die Datei von vorn(!) durchsuchen und zwar in der Reihenfolge der Zeichen des Wortes, z.B.

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
function Translateinttomorse(S:string):string;
var
    Res:string;
    myfile:textfile;
    Alpha,Morse:string;
    I:integer;
begin
  AssignFile(myFile, 'morse_alpha.txt');
  Res:='';                         

  for I := 1 to length(S) do          
  begin
    Reset(myFile);
    Repeat
      ReadLn(myFile, Alpha);
      ReadLn(myFile, Morse);
    until (s[i]=alpha) or eof(myfile);
    if s[i]=alpha then
      Res:= Res +  Morse + '        ';
  end;
  CloseFile(myFile);
  Translateinttomorse:=Res;
end;

Allerdings ist die Lösung mit der Datei wirklich nicht sehr elegant.

Beste Grüße
Mathematiker


JRNewers - Fr 08.03.13 20:37

Vielen Dank für die Lösung klappt, habe gar nicht mehr daran gedacht, habe viel zu weit gedacht, Lösung ist so einfach. Aergerlich. ;)

Ja die Lösung mit der Datei ist nicht elegant jedoch muss ich sie benutzen. Aufgabe in der Schule halt.