こんにちは。現在、英文、もしくは英単語、英字を国際符号(モールス信号)に訳すプログラムを書いているのですが、もしユーザーが二つの英単語を入力し、その単語がスペースで離れている場合(例として、SOS SOSもしくはsos sos)、翻訳された単語のモールス信号の間にスペースを三つ、文字の間に一つづつスペースを入れたいのですが(sos sosであれば、... --- ... ... --- ...)コツがつかめなくて困っています(文字の間に一つスペースを入れる事に成功はしたのですが...)
C++言語で経験者の方、いらっしゃいましたら是非アドバイスを御願い致します。
これが自分が今まで書き上げたコードなのですが、
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string engtomol (string, string[]);
int main ()
{
char alpha[26] = {'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'};
string morse[81] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
string text, morsecode;
char choice;
char repeat='y';
cout << "Select 1 to decode Morse code to English text. Select 2 to decode English text to Morse code" << endl;
cin >> choice;
if (choice=='1')
{
cout << "NOTE. DO NOT INPUT A NON ENGLISH CHARACTER. THIS TRANSLATOR EXCLUSIVELY ONLY TRANSLATES ENGLISH ALPHABETS (CAPITALIZED AND NON CAPITALIZED) ONLY.\n";
cout << "Enter a text to translate, each word seperated by spaces if you want to translate more than one word:";
cin.get();
getline(cin,text);
cout << "TEXT: " << text << endl;
cout << "MORSE CODE: " << engtomol(text, morse) << endl;
}
else if (choice=='2')
{
cout << "lol";
}
return 0;
}
string engtomol (string text, string morse[])
{
int textlength = text.length();
string morsevalue;
string spacesbtwletters= " ";
string spacesbtwwords = " ";
for (int k=0; k<textlength; k++)
{
if (text[k]!= ' ') //if the word(s) did not encounter a space
{ text[k]=toupper(text[k]); //upper case letters and lower case letters are the same hence have the same appropriate morse code.
morsevalue=spacesbtwletters+=morse[text[k]-'A']+" ";//A is the first value of the array. by subtracting its finding the appropriate morse code for each letters
}
if (text[k]==' ')
{
morsevalue+spacesbtwwords;
}
}
return morsevalue;
}
プログラムをコンパイルすると、(また例としてsos sosを入力したとします)。--- ... --- --- ... ---と、単語の間にスペースが一つしか表れません。どの様に変えたら、希望通りの結果が出ますか?
よろしく御願い致します。
補足
hitomuraさん、 ご回答ありがとうございます。 何とか、希望通りにできましたが、今度はモールスを逆に英文(または英字、英単語)に変換したいのですが、自分の書いたコードだと"AAA"しか出てきません。どの様にコードを書き換えたらよいでしょうか? #include <iostream> #include <string> #include <cctype> using namespace std; string engtomol (string, string[]); string moltoeng (string, char[]); int main () { char alpha[26] = {'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'}; string morse[81] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; string text, morsecode; char choice; char repeat='y'; while (repeat=='y') { cout << "Select 1 to decode Morse code to English text.\nSelect 2 to decode English text to Morse code" << endl; cin >> choice; if (choice=='1') { cout << "NOTE. DO NOT INPUT A NON ENGLISH CHARACTER. THIS TRANSLATOR EXCLUSIVELY TRANSLATES ENGLISH TEXTS (CAPITALIZED AND NON CAPITALIZED).\n"; cout << "Enter a text to translate, each word seperated by a space if you want to translate more than one word: "; cin.get(); getline(cin,text); cout << "TEXT: " << text << endl; cout << "MORSE CODE: " << engtomol(text, morse) << endl; } else if (choice=='2') { cout << "Enter a morsecode to translate, each letter code seperated by a space. If you want to translate more than one word, have 3 spaces between each word (for example, ... --- ... ... --- ...): "; cin.get(); getline(cin,morsecode); cout << "MORSECODE: " << morsecode << endl; cout << "TEXT: " << moltoeng (morsecode, alpha) << endl; } cout << "Would you like to continue? Press y to repeat. Press any other key to exit. "; cin >> repeat; } return 0; } string engtomol (string text, string morse[]) { int textlength = text.length(); string morsevalue; string spacesbtwletters= " "; string spacesbtwwords = " ";//2 spaces because im going to add it with spacesbtwletters so that it will = 3 for (int k=0; k<textlength; k++) { if (text[k]!= ' ') //if the word(s) did not encounter a space { text[k]=toupper(text[k]); //upper case letters and lower case letters are the same hence have the same appropriate morse code. morsevalue=spacesbtwletters+=morse[text[k]-'A']+" ";//A is the first value of the array. by subtracting its finding the appropriate morse code for each letters } if (text[k]==' ') { spacesbtwletters+=spacesbtwwords;//adds 3 spaces when there is a space between words } } return morsevalue; } string moltoeng (string morsecode, char alpha[]) { int morselength = morsecode.length(); string tran; string spacesbtwletters= " "; string spacesbtwwords = " "; for (int k=0; morsecode[k]; k++) { if (morsecode[k]!= ' ') //if the word(s) did not encounter a space { tran=spacesbtwletters+=alpha[morsecode[k]-morsecode[1]];//A is the first value of the array. by subtracting its finding the appropriate morse code for each letters } } return tran; } 現在あるコードです。逆の場合はモールス信号を入力した場合、文字同士の間には1スペースを、単語同士の間には3スペース、入力し、変換した英文は、英単語同士の間だけに1スペース開ける様に出力したいのですが(... --- ... ... --- ...を SOS(もしくはsos) SOS等) 長文で申し訳ありませんが、どうかアドバイスをよろしく御願い致します。