Morse Code Translating-disk file / manipulation
Author |
Message |
fenix_128
|
Posted: 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.
Description: |
This is the message decoded |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
DecodedMsg.txt |
Filesize: |
1.26 KB |
Downloaded: |
90 Time(s) |
Description: |
Message that is suppose to be decoded |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
CodedMsg.txt |
Filesize: |
4.34 KB |
Downloaded: |
104 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
gitoxa
![](http://compsci.ca/v3/uploads/user_avatars/125344263047f801d546bcb.jpg)
|
Posted: 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.
|
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: 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
|
|
|
|
|
|
![](images/spacer.gif) |
fenix_128
|
Posted: 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
|
|
|
|
|
|
![](images/spacer.gif) |
|
|