我正在开发一个程序,该程序将作为莫尔斯电码提供的消息解码为命令行参数。如果提供的字符串包含 7 个空格,则意味着我需要为解码的字符串打印一个空格。目前,我的程序显示所有正确的字母,但我无法克服在字符串中定位 7 个空格并打印一个空格的问题。
这是问题以及如何传递编码字符串:
e3r2p2% ./morse "..- -.. .-. .. .-- --. ..- ....."
udriwg zsh: segmentation fault ./morse "..- -.. .-. .. .-- --. ..- ....."
程序工作正常并打印所有解密的字母 (udriwg(,直到它满足 7 中的第一个(或第二个(空格;
这是我的程序代码:
int find_morse(const char *s);
bool count_spaces(char *s);
bool is_valid_char(char *s);
bool is_valid_params(int argc, char *argv[]);
void mx_printchar(char c);
void mx_printerr(const char *s);
int mx_strcmp(const char *first, const char *second);
char* mx_strcpy(char* dest, const char* source);
int mx_strlen(const char *s)
int main(int argc, char *argv[]){
if (!is_valid_params(argc, argv)){
mx_printerr("usage: ./morse "str"n");
exit(1);
}
char *output[27]= {"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","."};
char *copy;
copy = malloc(mx_strlen(argv[1]) + 1);
if (copy != NULL) {
mx_strcpy(copy, argv[1]);
} else {
mx_printerr("usage: ./morse "str"n");
exit(1);
}
int buff = 0;
char temp[5];
int index;
int space = 0;
int j;
for (int i = 0, n = mx_strlen(copy); i < n; i++) {
if (copy[i] == '.' && copy[i + 1] == '.' && copy[i + 2] == '.'
&& copy[i + 3] == '.' && copy[i + 4] == '.') {
mx_printchar(*output[26]);
}
if (copy[i] != ' ') {
temp[buff] = copy[i];
buff++;
}
if (copy[i] == ' ') {
index = find_morse(temp);
//print letter converted from morse to ENG
mx_printchar(*output[index]);
j = i;
while (copy[j] == ' ') {
space++;
j++;
if (space == 7) {
mx_printchar(' ');
space = 0;
break;
}
}
temp[buff] = ' ';
buff = 0;
for(int j = 0; j <4; j++) {
temp[j] =' ';
}
}
}
mx_printchar('n');
free(copy);
return 0;
}
int find_morse(const char *s) {
char *input[27]={".-","-...","-.-.","-..",".","..-.","--.", "....", "..",
".---","-.-",".-..","--","-.", "---",".--.","--.-",".-.",
"...","-","..-", "...-",".--","-..-","-.--","--..","....."};
for (int i = 0; i < 27; i++) {
if (mx_strcmp(s, input[i]) == 0) {
return i;
}
}
return -1;
}
bool count_spaces(char *s) {
int space = 0;
for (int i = 0; s[i] != ' '; i++) {
if (s[i] == ' ') {
space++;
if (s[i + 1] != ' ') {
if (space != 1 && space != 7) {
return false;
} else {
space = 0;
}
}
}
}
return true;
}
bool is_valid_char(char *s){
int i = 0;
while (s[i] != ' '){
if (s[i] != ' ' && s[i] != '-' && s[i] != '.') {
return false;
}
i++;
}
return true;
}
bool is_valid_params(int argc, char *argv[]){
if (argc != 2) {
return false;
}
if (!is_valid_char(argv[1])){
return false;
}
if (!count_spaces(argv[1])){
return false;
}
return true;
}
void mx_printchar(char c){
write(1, &c, 1);
}
void mx_printerr(const char *s){
write(2, s, mx_strlen(s));
}
int mx_strcmp(const char *first, const char *second) {
while (*first) {
if (*first != *second) break;
first++;
second++;
}
return *(const unsigned char*)first - *(const unsigned char*)second;
}
char* mx_strcpy(char* dest, const char* source){
if (dest == NULL) {
return NULL;
}
char *ptr = dest;
while (*source != ' ') {
*dest = *source;
dest++;
source++;
}
*dest = ' ';
return ptr;
}
int mx_strlen(const char *s){
int length = 0;
while (s[length] != ' '){
length++;
}
return length;
}
我相信问题从这里开始:
j = i;
while (copy[j] == ' ') {
space++;
j++;
if (space == 7) {
mx_printchar(' ');
space = 0;
break;
}
}
程序打印去脉的字母,然后尝试检查这些空格,但有些会出错。 我将不胜感激任何有关如何解决它的建议
从命令行使用程序时,始终在程序名称与参数和其他参数之间放置一个空格作为分隔符。因此,在您的情况下,如果有 7 个空格,则会在您的程序名称和 7 个空格之间添加一个额外的空格作为分隔符,使其像这样 8 个空格./morse
。所以,我宁愿建议使用任何其他符号,如!或任何其他符号。 现在对于Segmentation fault
错误,我认为这不会发生,因为由于这一行copy = malloc(mx_strlen(argv[1]) + 1);
而发生的空格计数器部分,因为您必须知道malloc()
返回分配给空间的void *
。现在我不知道那些mx_ functions
是什么。在调试您的代码时,我的脑海中也出现了一些问题,这些问题包含在我放在下面的修改后的代码中(在有点令人满意的水平上(。请在评论中回答。
法典:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Questions
** 1. What are these mx_ functions?
** 2. What is is_valid_params?
** 3. Can't we define *output[27] as *output = "abcdef...z." ?
** 4. In " for(j = 0; j < 4; j++) temp[j] =' '; " why j < 4 instead of j < 5?
*/
// Function Prototype
int find_morse(const char *s);
//main()
int main(int argc, char *argv[])
{
char *output[27]= {"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","."};
char *copy, temp[5];
int buff = 0, index, space = 0, i, j, n;
// Checking for valid parameters.
if (argc != 2)
{
printf("usage: ./morse "Invalid Parameters."n");//mx_printerr("usage: ./morse "str"n");
exit(1);
}
copy = (char *)malloc(strlen(argv[1]) + 1);
if (copy != NULL)
strcpy(copy, argv[1]);
else
{
printf("usage: ./morse "Memory Unavailable."n");
exit(2);
}
for (i = 0, n = strlen(copy); i < n; i++)
{
if (copy[i] == '.' && copy[i + 1] == '.' && copy[i + 2] == '.' && copy[i + 3] == '.' && copy[i + 4] == '.')
putchar(*output[26]);
if (copy[i] != ' ')
temp[buff++] = copy[i];
else
{
j = i;
while (copy[j] == ' ')
{
space++;
j++;
if (space == 7)
{
putchar(' ');
space = 0;
break;
}
}
temp[buff] = ' ';
buff = 0;
// Emptying temp[5]
//for(j = 0; j < 5; j++)
temp[0] = ' '; // Rest elements will automatically be assigned to by compiler.
}
if(index = find_morse(temp) != -1)
putchar(*output[index]); //print letter converted from morse to ENG
}
putchar('n');
free(copy);
return 0;
}
int find_morse(const char *s)
{
char *input[27] = {".-","-...","-.-.","-..",".","..-.","--.", "....", "..",".---","-.-",".-..","--","-.", "---",".--.","--.-",".-.","...","-","..-", "...-",".--","-..-","-.--","--..","....."};
for (int i = 0; i < 27; i++)
{
if (strcmp(s, input[i]) == 0)
return i;
}
return -1;
}
在这个程序中,我不明白用户将如何一个接一个地或以字符串的形式输入摩尔斯电码。因为,当匹配这些莫尔斯电码条目时,我确信会发生逻辑错误。