2012-04-17 9 views
6

私はこの最後のステップに到達するまで私は飛んでいたいくつかの宿題を持っています、そして今私は困惑しています、私は本当にいくつかの助けに感謝します。電話のワードジェネレータのヘルプC++

プロジェクトの前提は、電話番号を与えられた可能な単語のファイルを作成することです。ユーザーは '### - ####'の形式で番号を入力することになります。その後、コードはハイフンを引き出し、電話番号をwordGeneratorに送信します。私はこの点まですべてが機能することを知っています。時間が出てきたら、言葉のさまざまな可能性が私の問題を抱えています。ここに私の方法は次のようになります。

// function to form words based on phone number 
void wordGenerator(const int * const n) 
{ 
    // set output stream and open output file 
    /* Write a declaration for an ofstream object called 
    outFile to open the file "phone.dat" */ 
ofstream outFile("phone.dat"); 
// letters corresponding to each number 

    /* Write a declaration for an array of 10 const char *'s 
    called phoneLetters. Use an initializer list to assign 
    each element of the array the corresponding string of 
    three letters. Use dummy characters for 0 and 1 */ 
    const char * phoneLetters[] = {"###", "###", "ABC", "DEF", "GHI", 
        "JKL", "MNO", "PRS", "TUV", "WXY"}; 
    // terminate if file could not be opened 
    /* Write code to check if the file was opened successfully, 
    and terminate if not */ 
    if(!outFile) 
    { 
    cerr << "The file could not be opened"; 
    exit(1); 
    } 

    int count = 0; // number of words found 

    // output all possible combinations 
    for (int i1 = 0; i1 <= 2; i1++) 
    { 
    for (int i2 = 0; i2 <= 2; i2++) 
    { 
     for (int i3 = 0; i3 <= 2; i3++) 
     { 
      for (int i4 = 0; i4 <= 2; i4++) 
      { 
       for (int i5 = 0; i5 <= 2; i5++) 
      { 
       for (int i6 = 0; i6 <= 2; i6++) 
       { 
        for (int i7 = 0; i7 <= 2; i7++) 
        { 
        /* Write a series of cascaded stream insertion 
       operations to output a set of seven letters 
      to outFile, followed by a space */ 

        outFile << phoneLetters[i7 + 2] << phoneLetters[i6 + 2] << phoneLetters[i5 + 2] << phoneLetters[i4 + 2] << phoneLetters[i3 + 2] << phoneLetters[i2 + 2] 
          << phoneLetters[i1 + 2] << ' '; 
        if (++count % 9 == 0) // form rows 
         outFile << '\n'; 
        } // end for 
       } // end for 
       } // end for 
      } // end for 
     } // end for 
     } // end for 
    } // end for 

    // output phone number 
    outFile << "\nPhone number is "; 

    for (int i = 0; i < 7; i++) 
    { 
     if (i == 3) 
     outFile << '-'; 

     outFile << n[ i ]; 
    } // end for 

    /* Write a statement to close the ouput file */ 
    outFile.close(); 
    system("pause"); 
    } // end function wordGenerator 

残念ながら、私は、コードの骨格を与えられ、割り当てを完了するために空白を埋めるように言われました。コメントがブロックされている場所(/ * * /)はどこにでも記入してください。

私は可能な単語の正しいフォーマットを出力するために何をする必要があるのか​​分かりません。私はGoogleを検索しようとしましたが、私が見つけたすべての結果は、これを達成するためにはるかに単純な(私の意見では)switch文を使用しており、テンプレートに制約されています:(

すべてのヘルプは、

編集: 私はちょうど他のものを考えました。誰かがブロックの代わりにphoneLetters []の文字を反復する方法を理解する助けをすることさえできたら、それは重要なステップです 例: 「ABC」を印刷する代わりに電話番号の番号「2」を読み取った場合、すべての組み合わせについて「A」を印刷し、「B」に移動してから「B」に移動します。

編集:すべての

int main() 
{ 
    int phoneNumber[ 7 ] = { 0 }; // holds phone number 

    // prompt user to enter phone number 
    cout << "Enter a phone number (digits 2 through 9) " 
    << "in the form: xxx-xxxx\n"; 

    // loop 8 times: 7 digits plus hyphen; 
    // hyphen is not placed in phoneNumber 
    for (int u = 0, v = 0; u < 8; u++) 
    { 
    int i = cin.get(); 

    // test if i is between 0 and 9 
    if (i >= '0' && i <= '9') 
    phoneNumber[ v++ ] = i - '0'; 
    } // end for 

    wordGenerator(phoneNumber); // form words from phone number 
    return 0; 
} // end main 
+0

、それが簡単になりますループのネストされた各文字 –

+5

7をループへのあなたのため通常は、タスクを簡素化するために... – AJG85

+0

@Msonicを愚かな何かをやっている良い兆候です。 –

答えて

6

あなたは、あなたが次の行に使用できるステートメントのための恐ろしい入れ子のを取り除くことができない場合は、次のコードで

outFile 
    << phoneLetters[n[0]][i1] 
    << phoneLetters[n[1]][i2] 
    << phoneLetters[n[2]][i3] 
    << phoneLetters[n[3]][i4] 
    << phoneLetters[n[4]][i5] 
    << phoneLetters[n[5]][i6] 
    << phoneLetters[n[6]][i7] 
    << ' '; 

他のいくつかの注意事項:中exitを使用して

は、この情報がお役に立てば幸いです。

+0

これは私が必要としていたものです!助けをありがとう、あなたの知識は非常に高く評価されています! – Kyle

2

まず、これらのため-sのネストされた変更: はここに私のメインは()です。彼らはひどく見えて、15桁の数字を生成すると言ったら、あなたは本当に悪い立場になるでしょう。

あなたが見ているのは、3を法とする何らかのパーミュテーションジェネレータです。実際の電話では「PQRS」のような特別なケースはありませんので、 0〜3^7の組み合わせ。

次に、for-loopを使用して反復処理を行います。同様に、カスタムイテレータ機能、すなわち理解することはおそらく難しい概念も、そう考えるだろうただ、これらすべての組み合わせを作るとされて執筆:このコードは、トライ進システムの数字の順列を生成します

// 3^7 = 2187 

for (int i = 0; i < 2187; ++i) 
{ 
    int Div = 1; 
    // now, 7 digits 
    for (int d = 0; d < 7; ++d) 
    { 
     outFile << (i/Div) % 3; 
     Div *= 3; 
    } 
} 

;)どのその後、することができます

/*1.*/ i % 3 
/*2.*/ (i/3) % 3 
/*3.*/ (i/9) % 3 
:あなたは本当になど、すべての7つの可能性と内部ループを置き換え、ファイルへの書き込み「カスケード」を使用する必要がある場合

outFile << phoneLetters[ActualReadNumber][ (i/Div)%3 ]; 

で出力右の文字を使用すること

それで... もっと詳しく説明する必要がある場合は、これについてコメントしてください。 私が手伝ってくれたら、宿題をして、幸運を祈る。

+0

それは間違いなくエレガントなソリューションです。私はあなたにそれを書いていただき、ありがとうございます。これらのループはそこに滞在しますので、しかし、私は、これらのばかげたループ – Kyle

+0

ああを含め、私は与えられたテンプレートコードを変更することになっていませんよ。私はあなたの痛みを知っている、私も勉強していると教師は独自の "方法"があります。私はすでにあなたに手紙を通して反復する方法を書いたと信じています。 ( 'phoneLetters [num] [letter]')。 –

+0

ああ私はそれを見る。あなたは私のためにそれを詳しく教えていただけますか?私はそれをどのように実装できるかわかりません。元の投稿を編集して、メインと残りのデータメンバーを見ることができます。 – Kyle

0

ソリューションあなたにテンプレートをobyingながら、電話番号は2桁のみを持っていた場合。

int pN[] = {6,9}; 

const char * phoneLetters[] = {"###", 
    "###", "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"}; // 


for (const char *c1 = phoneLetters[pN[0]]; *c1 != '\0'; c1++) 
{ 
    for (const char *c2 = phoneLetters[pN[1]]; *c2 != '\0'; c2++) 
    { 
     printf("%c%c\n" , *c1 , *c2); 
    } 
} 

OUTPUT: MW あなたはシャアの2次元配列にごphoneLetters配列を変更してみてください可能性がありMX MY NW NX NY OW OX OY