我必须使用TI MSP430G2553微控制器完成莫尔斯电码项目,以刷新莫尔斯电码。我试图让程序在弄乱微控制器的闪光灯之前为每个字母打印出点和破折号。我不确定如何将数组与2D数组进行比较。我希望进行比较,以便在查看数组中的相应字母时检查是否找到了点,破折号或空格。这是我到目前为止所拥有的。
我尝试通过执行“ if(str [i] == morse_array [j] [k])”将数组设置为另一个数组,但是我得到的消息是我无法比较指针和整数。有关解决此问题的步骤的任何指导?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void time_delay(int sec);
void main(void)
{
unsigned int i = 0;
unsigned int j, k;
char str [] = "Go Knights";
char* morse_array [36][2] = {{".-", "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"}};
for(i=0; i< strlen(str); i++){
for (j=0; j <= i; j++) {
if (strncmp(morse_array[i][j], &str[i], strlen(str)) == 0){
if(str[i] == '.'){
printf("DOT");
time_delay(200);
}
else if (str[i] == '-'){
printf("DASH");
time_delay(600);
}
else if (str[i] == ' '){
printf("SPACE");
time_delay(1400);
}
}
}
time_delay (1400);
}
printf("start program again");
return;
}
void time_delay(int sec) {
// Converting time into milli_seconds
int milliSeconds = 1000 * sec;
clock_t start = clock();
while (clock() < start + milliSeconds)
;
}