我只是出于学习目的解决简单的任务。 任务:用摩尔斯电码对给定字符串进行编码,返回编码后的字符串。 问题:当字符串被分割成标记时,莫尔斯电码中表示单词之间 1 个空格的 3 个空格会丢失。
结果应该是:“HEY JUDE” 当前结果:“HEYJ U D E”
在解决方案期间我的方法改变了3次
但问题是,在使用 strtok 进行分割期间,我丢失了 3 个空格,这应该正是“指针”,指向我在编码过程中应该将空格放置在何处。 我想出了这个问题的解决方案 - 查看 find_space_positions 函数。 但它的工作并不完全正确,我不明白为什么。
请帮助我理解为什么 find_space_positions 没有按应有的方式工作?如何让它按照我想要的方式工作?
在 C 中做此类事情的更好方法是什么?
这是代码:
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include<stdio.h>
const char *morse[55] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-.-.-", "--..--", "..--..", ".----.", "-.-.--", "-..-.", "-.--.", "-.--.-", ".-...", "---...", "-.-.-.", "-...-", ".-.-.", "-....-", "..--.-", ".-..-.", "...-..-", ".--.-.", "...---..."};
const char *ascii[55] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", ",", "?", "'", "!", "/", "(", ")", "&", ":", ";", "=", "+", "-", "_", "\"", "$", "@", "SOS"};
char input[] = ".... . -.-- .--- ..- -.. .";
static uint16_t msg_iter = 0;
static uint16_t voc_word_iter = 0;
static uint16_t voc_word_chr_iter = 0;
static uint16_t iter = 0;
static char buf[20] = "";
bool find_space_positions (const char* morse_code, uint16_t token_counter, uint16_t len ) {
uint16_t space_counter = 0;
for (uint16_t i = 0; i <len; i++){
printf("%c\n", morse_code[i]);
if (morse_code[i] == ' ') {
space_counter++;
printf("%d+++ \n", space_counter);
}
else if (space_counter == token_counter) {
if (morse_code[i + 1] == ' ' && morse_code[i + 2] == ' ') {
return true;
}
else {
return false;
}
}
}
}
void decode_morse(const char *morse_code) {
uint16_t input_len = strlen(morse_code);
char *word = strtok(morse_code, " ");
uint16_t token_number = 1;
while (word != NULL) {
for (int voc_word_iter = 0; voc_word_iter < 55; voc_word_iter++) {
if (strcmp(word, morse[voc_word_iter]) == 0) {
if (find_space_positions(input,token_number, input_len )) {
buf[iter] = ' ';
iter++;
}
buf[iter] = *ascii[voc_word_iter];
iter++;
break;
}
}
word = strtok(NULL, " ");
token_number++;
}
printf("%s\n", buf);
}
int main () {
decode_morse(input);
return 777;
}
“..” v
“但它的工作并不完全正确,我不明白为什么。”
很难诊断以错误方式做错误事情的乐观代码。然而,值得注意的是,辅助函数
find_space_positions()
努力寻找曾经是3个连续空间的吸烟痕迹。不幸的是,这个辅助函数在搜索仅 1 的 token_num
时将剩余的 SP 对计为 2(对于每个单词...)。这一切都有点太错误了,无法纠正。
按照“在 C 中执行此类操作的更好方法是什么?”的指导,提供以下内容作为实现评论中概述的方法:
// Your #includes and arrays of morse[] & ascii not repeated here
char decode_morse_char( char *cp ) {
// Find a match in the array of Morse strings
// Return the single ASCII character (or '#' for not found)
for( int i = 0; i < sizeof morse / sizeof morse[0]; i++ )
if( strcmp( cp, morse[i] ) == 0 )
return ascii[i][0];
return '#'; // not in alphabet,
}
void decode_morse_word( char *cp ) {
// break the "word" into individual Morse strings for lookup
for( char *ltr = cp; (ltr = strtok( ltr, " " ) ) != NULL; ltr = NULL )
putchar( decode_morse_char( ltr ) ); // decode one Morse character code
}
void decode_morse_text( char *cp ) {
char *wordSep = " "; // break into "words" on 3 SP's together
char *ep;
while( ( ep = strstr( cp, wordSep ) ) != NULL ) {
*ep = '\0'; // "tokenise" the word in the buffer
decode_morse_word( cp ); // decode one "word"
putchar( ' ' ); // and output a separating SP
cp = ep + 1;
}
decode_morse_word( cp ); // the final "word"
putchar( '\n' );
}
int main( void ) {
char input[] = ".... . -.-- .--- ..- -.. .";
decode_morse_text( input ); // entire string
return 0;
}
请注意,每个函数只做一件事。层次结构方法遵循 KISS 原则。
这确实输出“HEY JUDE”。
- .- -.- . .- ... .- -.. -.-. --- -.. . .- -. -.. -- .- -.- . .. - -... . - - . .-.