

-----------------------------------

fenix_128

Thu May 15, 2008 2:54 am


Morse Code Translating-disk file / manipulation

-----------------------------------

I am trying to code a Morse Code translator that takes morse code from a file (previously made) and translates it to user. 
I read from the file and translate the code but I need a way recognize multiple spaces spaces and find out where it is so I can enter a space in the translated output. 
(1 space between letters and 3 between words)

This is what I have so far
var alpha : array 1 .. 42 of char :=
    init ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", chr(34), "1","2","3","4","5","6","7","8","9","0",".",",","'","(",")")
var morse : array 1 .. 42 of string :=
    init (".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-..-.",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----",".-.-.-","--..--",".----.","-.--.","-.--.-")
var morseLetter : string
var codeFile:int

function translate (morseLetter : string) : char
    var position : int
    for i : 1 .. 42

        if morseLetter = morse (i) then
            position := i
            exit
        end if
    end for
    
    
    result alpha (position)
end translate

open :codeFile, "file location", get
assert codeFile not = 0
loop
exit when eof (codeFile)
get: codeFile, morseLetter

put translate (morseLetter)..

end loop
close :codeFile 

The morse that is suppose to be decoded is attached as well as the decoded code.


-----------------------------------

gitoxa

Thu May 15, 2008 4:27 pm


RE:Morse Code Translating-disk file / manipulation

-----------------------------------

for i : 1 .. 42

    if morseLetter = morse (i) then
        position := i
        exit
    end if
end for

I'm sorry, but that's definately an awesome for loop.


-----------------------------------

CodeMonkey2000

Thu May 15, 2008 8:46 pm


RE:Morse Code Translating-disk file / manipulation

-----------------------------------

Make a new code for spaces. Either that or collect the input character by character into a new string and check it from there. Remember:
var a : char := "a"
var b : char := "b"

var ab : string := a + b
put ab



-----------------------------------

fenix_128

Mon May 26, 2008 1:36 pm


Re: Morse Code Translating-disk file / manipulation

-----------------------------------

SO I finally found it out!!!!
here is the code

/*Sets the alphabet and other characters in an array of characters paraelling
 the morse code array*/
var alpha : array 1 .. 42 of char := init ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
    "R", "S", "T", "U", "V", "W", "X", "Y", "Z", chr (34), "1", "2", "3", "4", "5", "6", "7",
    "8", "9", "0", ".", ",", "'", "(", ")")
/*Sets the morse code equvalent to the alphabet etc paraelling to the array
 alpha*/
var morse : array 1 .. 42 of string := init (".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
    "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
    "-.--", "--..", ".-..-.", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..",
    "----.", "-----", ".-.-.-", "--..--", ".----.", "-.--.", "-.--.-")
%Declare variab;es
var morseChar : string (1) %Each word
var morseLetter : string := "" %Each character
var codeFile : int %File variable
var outFile : int %File variable
var old : string (1) := "" %old character

%function to compare the letters and morse code
function translate (morseLetter : string) : char
    var position : int
    for i : 1 .. 42
        if morseLetter = morse (i) then
            position := i
            exit
        end if
    end for
    result alpha (position)
end translate

%opens file to read from
open : codeFile, "filelocation", get
assert codeFile not= 0
%opens file to write to
open : outFile, "filelocation", mod, put
assert codeFile not= 0
%Main program
loop
    exit when eof (codeFile)
    % reads 1 character from the file
    get : codeFile, morseChar : 1
    %compares the read character to the the one before it
    if morseChar = " " then
        if old = " " then
            put " " ..
            put : outFile, " " ..
        else
            %Translates the morsecode
            put translate (morseLetter) ..
            put : outFile, translate (morseLetter) ..
            morseLetter := ""
        end if
    else

        morseLetter := morseLetter + morseChar
    end if
    %Sets the new character to the old character
    old := morseChar
end loop
%Closes files
close : codeFile
close : outFile


