2017-02-11 18 views
0

私は、文字、数字、句読点をモールスコードに変換するプログラムを作っています。C++からASCIIコードをモールスコードに変換

文字と数字は私が望むように働いています。

しかし、句読点では、私はそれを正しく動作させることはできません。誰かが自分のコードを見て助けてくれることを願っていました。

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 

A一般的により良いアプローチは、次のようになります:既知の

地図すべて

#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

ようこそスタックオーバーフロー。 [The Tour](http://stackoverflow.com/tour)を読み、[ヘルプセンター](http://stackoverflow.com/help/asking)の資料を参考にしてください。ここに聞いてください。 –

+1

_ "句読点では、私はそれを正しく働かせることはできません" _実際の問題は何ですか?入力、期待される出力、実際の出力は何ですか? –

+0

現時点で私は入力できるようにしたい:ドット、疑問符とeksklamationマーク。私はドットを入力すると、それは細かい変換します。 しかし、私は他の2つを入力すると、私には:?U ??? –

答えて

1

あなたの主な問題は、あなたがあなたのmain()機能にwhile() forループブレースを提供する逃したということです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'のようなクラスであり、内部的に一連の" dots "と" hyphens "を例えば。 'std :: bitset'型の' private'データメンバです。そして 'std :: string ToString(MorseCode const&morse_code)'のような関数を提供します。 –

+0

@Christianもちろん。 newbのためにそれを簡単に保ちましょう。あなたは本当の愛好家です;-)。そのようなクラス 'std :: set 'で十分であることを忘れないでください。 –

+0

@LasseHedegard私の答えを編集しないでください。代わりにあなたの質問を改善してください。 –

関連する問題