2017-02-11 207 views
0

我一直在製作一個將字母,數字和標點轉換爲莫爾斯電碼的程序。C++將ASCII轉換爲莫爾斯電碼

隨着字母和數字的工作,我希望它。

但與標點符號我不能讓它正常工作。我希望有人可以看看我的代碼並幫助我。

#include <iostream> 
#include <cstring> 
#include <sstream> 
using namespace std; 



    char ch; 
    string morseWord = ""; 

    for(unsigned int i=0; i < word.length(); i++) 
    { 
     if(isalpha(word[i])) 
     { 
      ch ; 
     } 
    } 
    return morseWord; 
} 


    char ch; 
    string morseWord = ""; 

    for(unsigned int i=0; i < word.length(); i++) 
    { 
     if(isdigit(word[i])) 
     { 
      ch = word[i]; 
      ch = toupper(ch); 
      morseWord += morseCode[ch - '0']; 
      morseWord += " "; 

    string morseWord = ""; 

    for(unsigned int i=0; i < word.length(); i++) 
    { 
     if(ispunct(word[i])) 
     { 
      ch = word[i]; 
      ch = toupper(ch); 
      morseWord += morseCode[ch - '.']; 
      morseWord += " "; 
     } 
    } 
    return morseWord; 
} 



int main() 
{ 
    stringstream ss; 
    string sentence; 
    string word = ""; 

    code: " << endl; 

    while(ss >> ToMorse(word) << endl; 
     cout << PunctuationToMorse(word) << endl; 
} 
+0

歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –

+1

_「但是標點符號不能讓它正常工作。」_你的實際問題是什麼?什麼是投入,預期產出和實際產出? –

+0

此刻我想輸入:點,問號和eksklamation標記。當我輸入點時,它可以很好地轉換它。 但是,當我輸入其他兩個它給我:?U ???。 –

回答

1

您的主要問題是,你錯過了,以提供while()循環括號中的main()功能:

while(ss >> word) { // <<<< Put an opening brace here 
    cout << EnglishToMorse(word) << endl; 
    cout << NumbersToMorse(word) << endl; 
    cout << PunctuationToMorse(word) << endl; 
} // <<<<< ... and a closing brace here 

一個通常更好的辦法是:

地圖所有已知可以使用std::map<char,std::string>轉換爲莫爾斯碼的字符,並具有處理這些字符的單一功能:

string CharToMorse(char c) { 
    static const std::map<char,std::string> morseCode = { 
     { 'A', ".-" } , 
     { 'B' , "-..." } , 
     { 'C', "-.-." } , 
     // ... 
     { 'Z', "--.." }, 
     { '0', ".----" } , 
     { '1', "..---" } , 
     { '2', "...--" } , 
     // ... 
     { '9', "-----" } , 
     { ' ', "......." } // Consider to support spaces between words 
     { '.', ".-.-.-" } , 
     { '!' , "..--.." } , 
     { '?' , "-.-.--"} 
    }; 

    auto morseString = morseCode.find(toUpper(c)); 
    if(morseString != morseCode.end()) { 
     return morseString->second; 
    } 
    return ""; 
} 

,並用它喜歡:

int main() { 
    stringstream ss; 
    string sentence; 

    cout << "Enter a English word, number or punctuation: "; 
    getline(cin, sentence); 
    ss << sentence; 
    cout << "Morse code: " << endl; 
    char c; 
    while(ss >> c) { 
     cout << CharToMorse(c); 
    } 
    cout << endl; 
} 

與您的實際代碼的問題是,它的前提依靠字符的ASCII碼錶的映射,以及'Z' - 'A' == 25
這不是由C++標準保證的,並且使您的代碼不可移植(也請參閱here)。

+1

下一步的演變步驟是將底層莫爾斯數據與其表示分離。而不是'std :: string','std :: map'的映射類型可以是一個類似'MorseCode'的類,它在內部存儲一系列「點」和「連字符」,例如。類型爲'std :: bitset'的'private'數據成員。然後提供一個像'std :: string ToString(MorseCode const&morse_code)'這樣的函數。 –

+0

@Christian當然。讓我們爲newb保持簡單。你是一個真正的愛好者;-)。不要忘記,對於這樣的課程,「std :: set 」應該足夠了。 –

+0

@LasseHedegard請停止編輯我的答案。改善你的問題。 –