开发者

Trying to convert morse code to english. struggling

开发者 https://www.devze.com 2023-01-25 02:08 出处:网络
I\'m trying to create a function to read Morse code from one file, convert it to English text, print the converted text to the terminal, and write it to an output file. Here\'s a rough start...

I'm trying to create a function to read Morse code from one file, convert it to English text, print the converted text to the terminal, and write it to an output file. Here's a rough start...

#define TOTAL_MORSE 91 #define MORSE_LEN 6

void 
morse_to_english(FILE* inputFile, FILE* outputFile, char morseStrings[TOTAL_MORSE][MORSE_LEN])
{ int i = 0, compare = 0;
 char convert[MORSE_LEN] = {'\0'}, *buffer = '\0';
 //read in a line of morse string from file
// fgets(buffer, //then what?
 while(((convert[i] = fgetc(inputFile)) != ' ') && (i < (MORSE_LEN - 1)))
 { i++;
 }
 if (convert[i + 1] == ' ')
  convert[i + 1] = '\0';
 //compare read-in string w/morseStrings
 for (i = 48, compare = strcmp(convert, morseStrings[i]); //48 is '0'
  i < (TOTAL_MORSE - 1) && compare != 0;
  i++) 
 { compare = strcmp(convert, morseStrings[i]);
 }
 printf("%c", (char)i);
}

I have initialized morseStrings to the morse code. That's my function right now. It does not work, and I'm not really sure what approach to take.

My original algorithm plan was something like this:

1. Scan Morse code in from file, character by character, until a space is reached

1.1 save to a temporary buffer (convert)

2. loop while i < 91 && compare != 0

    compare = strcmp(convert, morseString[i])

3. if (test ==0) print ("%c", i); 4. loop through this until eof

but.. I can't seem to think of a good way to test if the next char in the file is a space. So this has made it very difficult for me.

I got pretty frustrated and googled for ideas, and found a suggestion to use this algorithm

  1. Read a line
  2. Loop

    -strchr() for a SPACE or EOL -copy characters before the space to another string

    -Use strcm开发者_开发技巧p() and loop to find the letter -Test the next character for SPACE.

    -If so, output another space -Skip to next morse character

  3. List item

Endloop

But, this loops is kind of confusing. I would use fgets() (I think), but I don't know what to put in the length argument.

Anyways, I'm tired and frustrated. I would appreciate any help or insight for this problem. I can provide more code if necessary.


Your original plan looks fine. You're off by 1 when you check for the ' ' in the buffer, though. It's at convert[i], not convert[i + 1]. The i++ inside the loop doesn't happen when a space is detected.


I wouldn't use strchr(), to complicated.

  1. Loop through the Inputfile reading a line
  2. tokenize line with [strtok][1]
  3. loop through tokens and save(best append) the single Letters to a Buffer
  4. close looops and print

a bit of pseudocode for u

while(there is a next line){

tokens = strtok(line);
int i = 0;

while(tokens hasnext){
save to buffer}}


If you are concerned about the CPU time you can write a lookup table to find the values, something as a switch like this:

case '.-': code =  "A";  break;
case '-...': code =  "B";  break;
case '-.-.': code =  "C";  break;

After you split the morse code by the spaces and send the diferent . and - combinations to the switch to get the original character.

I hope this help. Best regards.

0

精彩评论

暂无评论...
验证码 换一张
取 消