试图莫尔斯code转换为英语。奋斗的 [英] Trying to convert morse code to english. struggling
问题描述
我试图创建一个函数来从一个文件中读取莫尔斯code,将其转换为英文文本,打印转换的文本到终端,并将其写入到输出文件。这里有一个粗略的开始......结果的#define TOTAL_MORSE 91
的#define MORSE_LEN 6
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);
}
我已经初始化morseStrings到莫尔斯code。
这是我的功能现在。它不工作,我真的不知道采取什么办法。
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.
我原来的算法的计划是这样的:结果 1。扫描莫尔斯code从文件中的,每个字符,直到达到一个空格
结果
1.1保存到一个临时缓冲区(转换)
结果 2。循环while I&LT; 91安培;&安培;比较!= 0
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。如果(测试== 0)打印(%C,我);
4。通过这个循环,直到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.
我得到了pretty沮丧,一派的想法,并找到了一个建议使用此算法
I got pretty frustrated and googled for ideas, and found a suggestion to use this algorithm
- 阅读行
-
循环
- Read a line
Loop
-strchr()为空间或EOL
空间到另一个字符串之前-copy字符结果
- 使用的strcmp()和环路找到信
- 测试空间下一个字符。结果
- 如果是这样,输出另一个空间
-Skip下一莫尔斯字符
-strchr() for a SPACE or EOL
-copy characters before the space to another string
-Use strcmp() and loop to find the letter
-Test the next character for SPACE.
-If so, output another space
-Skip to next morse character
项目
ENDLOOP
不过,这种循环是一种混乱。我会使用与fgets()(我认为),但我不知道要放什么东西在长度参数。
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.
不管怎么说,我累了,沮丧。我想AP preciate任何帮助或洞察力这个问题。如果需要的话我可以提供更多的code。
Anyways, I'm tired and frustrated. I would appreciate any help or insight for this problem. I can provide more code if necessary.
推荐答案
您最初的计划看起来不错。你关了1,当您检查在缓冲区中,虽然。这是一个在
转换[I]
,而不是转换[I + 1]
。在我++
里面当检测到空间循环不会发生。
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.
这篇关于试图莫尔斯code转换为英语。奋斗的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!