Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Morse Code Translating-disk file / manipulation
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
fenix_128




PostPosted: Thu May 15, 2008 2:54 am   Post subject: 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
Turing:
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.



DecodedMsg.txt
 Description:
This is the message decoded

Download
 Filename:  DecodedMsg.txt
 Filesize:  1.26 KB
 Downloaded:  90 Time(s)


CodedMsg.txt
 Description:
Message that is suppose to be decoded

Download
 Filename:  CodedMsg.txt
 Filesize:  4.34 KB
 Downloaded:  104 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
gitoxa




PostPosted: Thu May 15, 2008 4:27 pm   Post subject: RE:Morse Code Translating-disk file / manipulation

code:
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




PostPosted: Thu May 15, 2008 8:46 pm   Post subject: 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:
code:
var a : char := "a"
var b : char := "b"

var ab : string := a + b
put ab
fenix_128




PostPosted: Mon May 26, 2008 1:36 pm   Post subject: Re: Morse Code Translating-disk file / manipulation

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

/*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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: