C程序,使用空格检测单词或句子,将莫尔斯电码解码为文本

时间:2018-07-29 15:23:21

标签: c whitespace morse-code

我有一项作业,要求我解码摩尔斯电码字符串。作业指定:

-字符之间没有空格(例如-之间的空格)

-每个字符之间用1个空格隔开

-每个单词用4个空格隔开

到目前为止,我有:

//Function to convert Morse code to text
char morsecode_to_text(char input[], int index){

int index_1 = 0, index_2 = 0, x = 0;
char output[100] = {'\0'};//Variable to keep output and later used to display text
char string[50] = {'\0'};//Variable to combine output

char *characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};


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


while(input[index] < 1000){
    if(input[index] != '\0'){
        if(input[index] == '.' || input[index] == '-') {
            //Compare if input Morse code is similar to Morse code in array
            while(input[index] == '.' || input[index] == '-'){
                string[index_1] = input[index];
                index += 1;
                ++index_1;
            }//end of while loop

            for(index_2=0;index_2<36;index_2++){
                if(strcmp(string, morsecode[index_2]) == 0){
                    strcat(output, characters[index_2]);
                    x = 1;}
            }//end of for loop

        memset(string,0,sizeof(string));
        //Check for spaces
        }else if(input[index] == ' '){
            if(input[index + 1] == ' ') {
                if((input[index + 2] == ' ') && (input[index + 3] == ' ')){
                    strcat(output," ");
                    index += 4;
                }else{
                    strcpy(output, "FAIL - WRONG MORSE CODE");
                    break;}
            }else
                index++;

        }else if(input[index] == '\0' || input[index] == '\n'|| input[index] != '.' || input[index] != '-'){
            break;}
    }else
        break;
}//end of while loop

if (x == 1){
printf("MORSE CODE TRANSLATED : SUCCESS - %s", output);
}else{
    strcpy(output,"FAIL - WRONG MORSE CODE");
    printf("%s", output);
}

}//end of Function to convert Morse code to text

void main()
{
    char input[1000] = {'\0'}; //User input
    char choice = 'y'; //Amount of time user wants to replay
    int index = 0;

do{
    system("cls");
    printf("MORSE CODE TRANSLATOR PROGRAM\n");
    printf("\nPlease enter a Morse code string: ");
    fgets(input,1000,stdin);

    morsecode_to_text(input, index);

printf("\n\nDo you want to try again? (Press 'y' for Yes and 'n' for No): ");
scanf("%c", &choice);
getchar();
}while(choice == 'y');//end of while loop
}//end of void main

我的输出应该看起来像这样: Example of Morse Code Translator Program execution

但是空格功能无法正常工作,因此显示以下内容:Output of code

提前谢谢!

2 个答案:

答案 0 :(得分:-1)

使用查找表的非常简单的

一个非.或'-'-分隔字符

多个空格-单词。

const char *getLetter(const char *morse, int *ch)
{
    char coded[10];
    const char *startpos = morse;

    while(*morse && *morse!= '.' && *morse != '-')
        morse++;
    if(!*morse) return NULL;
    startpos = morse;
    while(*morse && (*morse== '.' || *morse == '-'))
        morse++;
    if ((startpos - morse) > 9) return NULL;
    strncpy(coded, startpos, morse - startpos);
    coded[morse - startpos] = 0;
    *ch = -1;
    for(int index = 0; index < sizeof(morsecode) / sizeof(morsecode[0]); index++)
    {
        if(!strcmp(morsecode[index], coded))
        {
            *ch = characters[index][0];
            break;
        }
    }
    return morse;
}

int IsSpace(const char *morse)
{
    return *morse && *morse ==' ' && *(morse + 1) == ' ';
}

int main(void)
{
    char *morsestring = ".- -... -.-. -..     .- -... -.-. -.. ----------";
    int ch = 0;
    const char *nextpos = morsestring;
    while((nextpos = getLetter(nextpos, &ch)) != NULL)
    {
        if(ch == -1)
        {
            printf(" *ERR* ");
        }
        else
        {
            printf("%c%s", ch, IsSpace(nextpos) ? " " : "");
        }
    }
    printf("\n");

}

答案 1 :(得分:-1)

#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

//See Easy instructions and have Fun of the Program

using namespace std;
void  convert(char s[]);
void clrscr()
{
    system("@cls||clear");

}

int main()
{
    clrscr();

    char string[100];

    cout<<"INSTRUCTIONS : \n\n";
    cout<<"1. Enter leaving single SPACE     (like) : To enter MAN -> -- .- -. ";
    cout<<endl<<"2. To Give SPACE, Enter s (small s) with Space (like) : To Enter MR A -> -- .-. s .-";
    cout<<endl<<endl<<"SIMPLE !";
    getch();
    cout<<endl;
    cout<<"Enter the Code to Convert into STRING  : \n";
    gets(string);


    cout<<endl<<endl;

    cout<<"Converting ....";
    getch();
    clrscr();

    //for(int i =0 ; i < strlen(string) ; i++)

    convert(string);



    getch();
    return 0;

}


void  convert(char s[])
{

    int j=0;
    int i=0;
    int c=0;
    int add=0;

    char a[10];
    char f[100];


    while(s[j] != '\0')
    {   

        c=0;

        while (c != 5)
        {
            a[c]='\0';
            c++;
        }

        i=0;
        while( s[j] != ' ' && s[j] != '\0')
        {
            a[i] = s[j];
            j++;
            i++;
        }
        if(s[j]== ' ' ){j++;}


        a[i+1]='\0';

        if(strcmp(a,".-") == 0)
        {
            f[add]='A';
            add++;
            f[add]='\0';
        }
        else if(strcmp(a,"-...") == 0){f[add]= 'B'; add++; f[add]='\0';}
        else if(strcmp(a,"-.-.") == 0){f[add]= 'C'; add++; f[add]='\0';}
        else if(strcmp(a,"-..") == 0){f[add]= 'D'; add++; f[add]='\0';}

        else if(strcmp(a,".") == 0){f[add]= 'E'; add++; f[add]='\0';}
        else if(strcmp(a,"..-.") == 0){f[add]= 'F'; add++; f[add]='\0';}
        else if(strcmp(a,"--.") == 0){f[add]= 'G'; add++; f[add]='\0';}
        else if(strcmp(a,"....") == 0){f[add]= 'H'; add++; f[add]='\0';}
        else if(strcmp(a,"..") == 0){f[add]= 'I'; add++; f[add]='\0';}

        else if(strcmp(a,".---") == 0){f[add]= 'J'; add++; f[add]='\0';}
        else if(strcmp(a,"-.-") == 0){f[add]= 'K'; add++; f[add]='\0';}
        else if(strcmp(a,".-..") == 0){f[add]= 'L'; add++; f[add]='\0';}
        else if(strcmp(a,"--") == 0){f[add]= 'M'; add++; f[add]='\0';}

        else if(strcmp(a,"-.") == 0){f[add]= 'N'; add++; f[add]='\0';}
        else if(strcmp(a,"---") == 0){f[add]= 'O'; add++; f[add]='\0';}
        else if(strcmp(a,".--.") == 0){f[add]= 'P'; add++; f[add]='\0';}
        else if(strcmp(a,"--.-") == 0){f[add]= 'Q'; add++; f[add]='\0';}

        else if(strcmp(a,".-.") == 0){f[add]= 'R'; add++; f[add]='\0';}
        else if(strcmp(a,"...") == 0){f[add]= 'S'; add++; f[add]='\0';}
        else if(strcmp(a,"-") == 0){f[add]= 'T'; add++; f[add]='\0';}
        else if(strcmp(a,"..-") == 0){f[add]= 'U'; add++; f[add]='\0';}

        else if(strcmp(a,"...-") == 0){f[add]= 'V'; add++; f[add]='\0';}
        else if(strcmp(a,".--") == 0){f[add]= 'W'; add++; f[add]='\0';}
        else if(strcmp(a,"-..-") == 0){f[add]= 'X'; add++; f[add]='\0';}
        else if(strcmp(a,"-.--") == 0){f[add]= 'Y'; add++; f[add]='\0';}
        else if(strcmp(a,"--..") == 0){f[add]= 'Z'; add++; f[add]='\0';}
        else if(strcmp(a,"s") == 0 ){f[add]=' '; add++; f[add]='\0';}








    }


    cout<<f;


}