电话单词生成器帮助C++

Telephone Word Generator Help C++

本文关键字:帮助 C++ 单词生 电话      更新时间:2023-10-16

我有一些家庭作业,我一直在飞奔,直到我到达最后一步,我现在被难住了,我真的很感激一些帮助。

该项目的前提是创建一个给定电话号码的可能单词的文件。用户需要输入一个格式为"###-####"的数字。然后,代码将连字符拉出,并将电话号码发送到方法 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

不幸的是,我得到了一个代码框架,并被告知填写空白以完成作业。 注释被阻止的任何地方(/* */)都是我必须填写代码的地方。

我不确定我需要做什么才能输出可能单词的正确格式。我尝试搜索谷歌,但我找到的所有结果都使用更简单(在我看来)switch 语句来实现这一点,我受限于模板:(

感谢所有的帮助,即使是朝着正确的方向推动。

编辑: 我只是想到了另一件事。我觉得如果有人能帮助我弄清楚如何单独而不是块遍历phoneLetters[]的字符,那将是向前迈出的重要一步。 例: 当读取电话号码的数字"2"而不是打印"ABC"时,为所有可能的组合打印"A",然后转到"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-xxxxn";
   // 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

如果你不能摆脱可怕的嵌套语句,那么你可以使用以下行:

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]
    << ' ';

关于代码的其他一些说明:

  • 避免在C++中使用exit,因为可以防止析构函数运行

  • 使用 using namespace 语句避免命名空间污染(我假设上面有一个 using namespace std; 语句)

  • 如果您没有使用exit并删除了暂停(或添加了范围),则 fstream 的显式关闭是多余的

希望这有帮助。

首先,更改这些嵌套的 for-s。它们看起来很糟糕,如果我说你现在要生成15位数字,那么你的处境会非常糟糕。

你在那里看到的是某种带有模 3 的排列生成器,正如我想的那样......由于没有特殊情况(如真实手机上的"PQRS"),只需计算从 0 到 3^7 的所有可能组合。

然后,使用一个 for 循环来循环访问它们。编写自定义迭代器函数可能很难理解,所以考虑一下就像进行所有这些组合一样,例如:

// 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;
    } 
}

该代码将在三元系统;)中生成数字排列然后可用于输出正确的字母

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

如果您确实需要使用"级联"写入文件,请将内部循环替换为所有 7 种可能性,例如:

/*1.*/   i % 3
/*2.*/   (i / 3) % 3
/*3.*/   (i / 9) % 3

等等...如果我应该更详细地解释一些事情,请对此发表评论;如果我帮忙,请:)点赞祝作业好运。

如果

电话号码在遵守模板时只有两位数的解决方案。

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

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

输出:兆瓦MX我西北尼克纽约安省社会福利金牛唰唰��