用于将文本转换为莫尔斯电码的C函数

问题描述 投票:0回答:1

我正在尝试编写一个程序,在该程序中,系统需要给定文本的莫尔斯电码。关于将文本转换为莫尔斯电码,我主要是将它们全部编写出来(与程序文件本身分开)。现在,我的目标是将其编写为一个函数,以便在程序的其他函数中使用它。每当我尝试时,都会出现细分错误。谁能帮助我从头开始构建函数本身?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdint.h>
#include<ctype.h>
#include <time.h>

char * fileName1 = NULL;
char * fileName2 = NULL;

int main(int argc, char * argv[]) {
  int n;
  for (n = 0; n < argc; n++) {
    // printf("Argument %s\n",argv[n]); // prints options  delete this in the end,just for debugging
    if (strcmp(argv[n], "-text") == 0) {
      //text to morsecode
      int c, v = 0;
      char * str = (char * ) malloc(v);
      str = (char * ) realloc(str, (c + strlen(argv[n + 1])));
      strcat(str, argv[n + 1]);
      strcat(str, " ");

      char *alphamorse[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
      char *nummorse[]={"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};

      int i;
      char str1[1000];
      i = 0;
      while (str[i] != '\0') {
        if (str[i] != ' ' && (!isdigit(str[i]))) {
          printf("%s ", alphamorse[toupper(str[i]) - 65]);
        }
        if (str[i] == ' ') {
          printf(" ");
        }
        if (isdigit(str[i]) && str[i] != ' ') {
          printf("%s ", nummorse[str[i] - 48]);
        }
        i++;
      }
      printf("\n");
      // end of text to morsecode
    }
    if (strcmp(argv[n], "-o") == 0) {
      //output = concat(output, argv[n + 1]);
      n++;
      continue;
    }
    if (strcmp(argv[n], "--") == 0) {
      if (n + 1 <= argc) {
        fileName1 = argv[++n];
        printf("    fileName1=%s\n", fileName1);
      }
      if (n + 1 <= argc) {
        fileName2 = argv[++n];
        printf("    fileName2=%s\n", fileName2);
      }
    }
  }
  return 0;
}
c function segmentation-fault main morse-code
1个回答
0
投票

我不知道这是否是导致问题的错误,但这是一个错误:

int c, v = 0;
char *str = (char *)malloc(v);
    str = (char *)realloc(str, (c + strlen(argv[n+1])));

首先,c未初始化。它可以是任何值,包括负值。因此,程序中的行为未定义。

而且,不需要在malloc之后进行realloc调用。只需分配一次即可完成。

我认为这是您打算做的

size_t len = strlen(argv[n+1]);
str = (char*)malloc(len + 1 + 1); // +1 for space char to be appended, +1 again for null char
strcpy(str, argv[n+1]); // copy string
strcat(str, " ");       // append a space

我会发现更多,会写更多...

这看起来可疑;

  i = 0;
  while (str[i] != '\0') {
    if (str[i] != ' ' && (!isdigit(str[i]))) {
      printf("%s ", alphamorse[toupper(str[i]) - 65]);
    }
    if (str[i] == ' ') {
      printf(" ");
    }
    if (isdigit(str[i]) && str[i] != ' ') {
      printf("%s ", nummorse[str[i] - 48]);
    }
    i++;
  }

您正在重复调用isdigit并进行评估以确保str[i]不是空格。它是数字,字母或无法转换的内容。更好:

  i = 0;
  while (str[i] != '\0') {

    if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
      printf("%s ", alphamorse[toupper(str[i]) - 'A']);
    }
    else if (isdigit(str[i])) {
      printf("%s ", nummorse[str[i] - '0']);
    }
    else {
        printf(" ");
    }
    i++;
  }

此后的一切,我都不知道它是干什么的。但这看起来很可疑:

if (strcmp(argv[n], "-o") == 0) {
  //output = concat(output, argv[n + 1]);
  n++;
  continue;

为什么再次增加n?

© www.soinside.com 2019 - 2024. All rights reserved.