'* Morse. bas - codes and decodes messages. '* '* Justin Keswick '* Qbasic Developer '* Business Consultant '* 89 Beachview Crescent '* Toronto, Ontario '* cell 416-707-8904 '* home 416-691-9956 '* email jkeswick@interlog.com '* website www.interlog.com/~jkeswick '* '* If you require services, please call today. DEFINT A-Z CONST NumMorseChars = 44 CONST Speed = 3 DECLARE SUB Encode (Message$, Code$) DECLARE SUB Decode (Code$, Message$) DECLARE SUB SendCode (Code$) REDIM SHARED Text(1 TO 50) AS STRING REDIM SHARED Morse(1 TO 50) AS STRING Text(1) = "A": Morse(1) = ". -" Text(2) = "B": Morse(2) = "- . . ." Text(3) = "C": Morse(3) = "- . - ." Text(4) = "D": Morse(4) = "- . ." Text(5) = "E": Morse(5) = "." Text(6) = "F": Morse(6) = ". . - ." Text(7) = "G": Morse(7) = "- - ." Text(8) = "H": Morse(8) = ". . . ." Text(9) = "I": Morse(9) = ". ." Text(10) = "J": Morse(10) = ". - - -" Text(11) = "K": Morse(11) = "- . -" Text(12) = "L": Morse(12) = ". - . ." Text(13) = "M": Morse(13) = "- -" Text(14) = "N": Morse(14) = "- ." Text(15) = "O": Morse(15) = "- - -" Text(16) = "P": Morse(16) = ". - - ." Text(17) = "Q": Morse(17) = "- - . -" Text(18) = "R": Morse(18) = ". - ." Text(19) = "S": Morse(19) = ". . ." Text(20) = "T": Morse(20) = "-" Text(21) = "U": Morse(21) = ". . -" Text(22) = "V": Morse(22) = ". . . -" Text(23) = "W": Morse(23) = ". - -" Text(24) = "X": Morse(24) = "- . . -" Text(25) = "Y": Morse(25) = "- . - -" Text(26) = "Z": Morse(26) = "- - . ." Text(27) = "": Morse(27) = ". - - . -" Text(28) = "": Morse(28) = ". - . -" Text(29) = "": Morse(29) = ". . - . ." Text(30) = "": Morse(30) = "- - . - -" Text(31) = "": Morse(31) = "- - - ." Text(32) = "": Morse(32) = ". . - -" Text(33) = "1": Morse(33) = ". - - - -" Text(34) = "2": Morse(34) = ". . - - -" Text(35) = "3": Morse(35) = ". . . - -" Text(36) = "4": Morse(36) = ". . . . -" Text(37) = "5": Morse(37) = ". . . . ." Text(38) = "6": Morse(38) = "- . . . ." Text(39) = "7": Morse(39) = "- - . . ." Text(40) = "8": Morse(40) = "- - - . ." Text(41) = "9": Morse(41) = "- - - - ." Text(42) = "0": Morse(42) = "- - - - -" Text(43) = ",": Morse(43) = "- - . . - -" Text(44) = ".": Morse(44) = ". - . - . -" 'Text(45) = "Interrogition": Morse(45) = ". . - - . ." 'Text(46) = "Wait": Morse(46) = ". - . . ." 'Text(47) = "Understand": Morse(47) = ". . . - ." 'Text(48) = "Don't Understand": Morse(48) = "- . . . . - ." 'Text(49) = "Call": Morse(49) = ". - . -" 'Text(50) = "Transmission Finished": Morse(50) = ". . . - . -" CLS COLOR 11 PRINT "MORSE CODER -- Use only capital letters and only one space at a time." PRINT " Only the period and comma are availabe in the " PRINT " international system." PRINT LINE INPUT "Do you want to enter a message or encode morse.txt( e or t )? ", Response$ IF Response$ = "t" THEN OPEN "morse.txt" FOR INPUT AS #1 PRINT "Encoded message: " DO WHILE NOT EOF(1) LINE INPUT #1, Message$ Encode Message$, Code$ PRINT Code$; SendCode Code$ SLEEP 1 LOOP CLOSE #1 ELSE LINE INPUT "Enter a message to be encoded: ", Message$ PRINT Encode Message$, Code$ PRINT "Encoded message: " PRINT PRINT Code$ PRINT SendCode Code$ Decode Code$, Message$ PRINT "Decoded message: " PRINT PRINT Message$ END IF END SUB Decode (Code$, Message$) Message$ = "" DO IF MID$(Code$, 1, 1) = " " THEN Code$ = MID$(Code$, 2, LEN(Code$) - 1) Message$ = Message$ + " " END IF j = 0 DO j = j + 1 LOOP UNTIL (MID$(Code$, j, 1) = " ") AND (MID$(Code$, j + 1, 1) = " ") Ch$ = MID$(Code$, 1, j - 1) Code$ = MID$(Code$, j + 3, LEN(Code$) - j - 3 + 1) j = 0 DO j = j + 1 LOOP UNTIL Ch$ = Morse(j) Message$ = Message$ + Text(j) LOOP UNTIL Code$ = "" END SUB SUB Encode (Message$, Code$) Code$ = "" FOR i = 1 TO LEN(Message$) IF MID$(Message$, i, 1) = " " THEN Code$ = Code$ + " " ELSE j = 0 DO j = j + 1 LOOP UNTIL MID$(Message$, i, 1) = Text(j) Code$ = Code$ + Morse(j) + SPACE$(3) END IF NEXT i END SUB SUB SendCode (Code$) Dot$ = "O5 L" + STR$(Speed * 2) + " G#" Dash$ = "O5 L" + STR$(Speed) + " G#" Pause$ = "P" + STR$(Speed * 2) FOR i = 1 TO LEN(Code$) IF MID$(Code$, i, 1) = "." THEN PLAY Dot$ ELSEIF MID$(Code$, i, 1) = "-" THEN PLAY Dash$ ELSEIF MID$(Code$, i, 1) = " " THEN PLAY Pause$ END IF NEXT i END SUB