Sunteți pe pagina 1din 3

Conversor de Texto a C�digo Morse (C++)

16776960 16711680 3348860 1FEFF00

/*
** Text to Morse Code Converter
**
** Copyright (C) 2005 Ram�n E. Torres Salom�n
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef _MSC_VER
#include <windows.h>
#else
#include <dos.h>
#endif

#define MORSE_BEEP_FREQ 800 // 0.8KHz

// Timers
#define MORSE_DOT_TIME 70
#define MORSE_DASH_TIME 210
#define MORSE_CHARACTER_SEPARATION MORSE_DOT_TIME*3
#define MORSE_WORD_SEPARATION MORSE_DOT_TIME*7

// Functions prototipes
void playsound(int note, int duration);
void morse(char c);

int main(void)
{
char buffer[256];

printf("Raymondjavaxx Text to Morse Code Converter\n");


printf("------------------------------------------\n");
printf("Type a text and press enter: ");
gets(buffer);

int x=0;
while(buffer[x])
{
if(buffer[x] == ' ')
{
printf("\n");
while(buffer[x] == ' ')
x++;

if(buffer[x] == NULL)
break;

#ifdef _MSC_VER
Sleep(MORSE_WORD_SEPARATION);
#else
delay(MORSE_WORD_SEPARATION);
#endif
}
morse(buffer[x]);
x++;
}

system("PAUSE");
return 0;
}

/*********
* func: morse
* desc: morse parsing function
********************************************/
void morse(char c)
{
char code[8] = {0};

switch(toupper(c))
{
// Letter Morse
case 'A': strcpy(code, ".-"); break;
case 'B': strcpy(code, "-..."); break;
case 'C': strcpy(code, "-.-."); break;
case 'D': strcpy(code, "-.."); break;
case 'E': strcpy(code, "."); break;
case 'F': strcpy(code, "..-."); break;
case 'G': strcpy(code, "--."); break;
case 'H': strcpy(code, "...."); break;
case 'I': strcpy(code, ".."); break;
case 'J': strcpy(code, "-.-."); break;
case 'K': strcpy(code, "-.-"); break;
case 'L': strcpy(code, ".-.."); break;
case 'M': strcpy(code, "--"); break;
case 'N': strcpy(code, "-."); break;
case 'O': strcpy(code, "---"); break;
case 'P': strcpy(code, ".--."); break;
case 'Q': strcpy(code, "--.-"); break;
case 'R': strcpy(code, ".-."); break;
case 'S': strcpy(code, "..."); break;
case 'T': strcpy(code, "-"); break;
case 'U': strcpy(code, "..-"); break;
case 'V': strcpy(code, "...-"); break;
case 'W': strcpy(code, ".--"); break;
case 'X': strcpy(code, "-..-"); break;
case 'Y': strcpy(code, "-.--"); break;
case 'Z': strcpy(code, "--.."); break;

// Digit Morse
case '0': strcpy(code, "-----"); break;
case '1': strcpy(code, ".----"); break;
case '2': strcpy(code, "..---"); break;
case '3': strcpy(code, "...--"); break;
case '4': strcpy(code, "....-"); break;
case '5': strcpy(code, "....."); break;
case '6': strcpy(code, "-...."); break;
case '7': strcpy(code, "--..."); break;
case '8': strcpy(code, "---.."); break;
case '9': strcpy(code, "----."); break;

// Others
case '.': strcpy(code, ".-.-.-"); break;
case ',': strcpy(code, "--..--"); break;
case '?': strcpy(code, "..--.."); break;

default: printf("[invalid character '%c']\n", c); break;


}

for(int i=0; i<8; i++)


{
if(code[i] == '-')
{
printf("%c", code[i]);
playsound(MORSE_BEEP_FREQ, MORSE_DASH_TIME);
}
else if(code[i] == '.')
{
printf("%c", code[i]);
playsound(MORSE_BEEP_FREQ, MORSE_DOT_TIME);
}
}

printf("\n");
}

/*********
* func: playsound
* desc: play a note with pc internal speaker
********************************************/
void playsound(int note, int duration)
{
#ifdef _MSC_VER
Beep(note, duration);
Sleep(MORSE_CHARACTER_SEPARATION);
#else
sound(note);
delay(duration);
nosound();
delay(MORSE_CHARACTER_SEPARATION);
#endif
}

El c�digo est� preparado para ser compilado en Turbo C++ o en Visual C++ 6.

S-ar putea să vă placă și