取出双字母

时间:2013-12-16 01:03:36

标签: c++

我的程序会生成用户选择的随机辅音或元音。然后它在字典中搜索单词并显示它们。我想要的是改变双字母,因此不再需要再次询问用户它们不再相同。

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <ctime>
#include <algorithm>
#include <string>
#include <array>
using namespace std;


char response;
int letter;
int letter2;
int n;
char  result[8];
string a = " ";

string array1 [70549];
string array2[70549] ;
string array3[70549] ;
string array4[70549] ;
string array5[70549] ;
string array6[70549] ;
string array7[70549] ;
string array8[70549] ;
string array9[70549] ;
string array10[70549] ;

char test = 'a';
int j = 0;
int k = 0;

  int main()
{
for (int a =0; a<8; a++)
{
cout<< "Consonant (c) or vowel(v)?" << endl;
cin >> response; 


{


if ( 'c' == response ) 
{
    srand (time(0));
    letter =  (rand() %21);
    n ++;


        switch (letter)         //seleccts random const
        {
        case 0: 
            cout << "b" << endl;
            result[n] = 'b';
            break;

        case 1: 
            cout << "c" << endl;
            result[n] = 'c';
            break;

        case 2:  
            cout << "d" << endl;
            result[n] = 'd';
            break;

        case 3: 
            cout << "f" << endl;
            result[n] = 'f';
            break;

        case 4:
            cout << "g" << endl;
            result[n] = 'g';
            break;

        case 5: 
            cout << "h"<< endl;
            result[n] = 'h';
            break;

        case 6: 
            cout << "j" << endl;
            result[n] = 'j';
            break;

        case 7: 
            cout << "k" << endl;
            result[n] = 'k';
            break;

        case 8: 
            cout << "l" << endl;
            result[n] = 'l';
            break;

        case 9: 
            cout << "m" << endl;
            result[n] = 'm';
            break;

        case 10: 
            cout << "n" << endl;
            result[n] = 'n';
            break;

        case 11: 
            cout << "p" << endl;
            result[n] = 'p';
            break;

        case 12: 
            cout << "q" << endl;
            result[n] = 'q';
            break;

        case 13: 
            cout << "r" << endl;
            result[n] = 'r';
            break;

        case 14: 
            cout << "s"<< endl;
            result[n] = 's';
            break;

        case 15: 
            cout << "t" << endl;
            result[n] = 't';
            break;

        case 16: 
            cout << "v"<< endl;
            result[n] = 'v';
            break;

        case 17: 
            cout << "w"<< endl;
            result[n] = 'w';
            break;

        case 18: 
            cout << "x" <<endl;
            result[n] = 'x';
            break;

        case 19: 
            cout << "y"<< endl;
            result[n] = 'y';
            break;

        case 20: 
            cout << "z"<< endl;
            result[n] = 'z';
            break;
        }
}

   else if ('v' == response)
{
    srand (time(0));
    letter2 = ( rand() %4);
    n++;

    switch (letter2)                                      //selects random vowel
        {
        case 0: 
            cout <<  "a"<< endl;
            result[n] = 'a';
            break;

        case 1: 
            cout << "e" <<endl;
            result[n] = 'e';
            break;

        case 2: 
            cout << "i"<< endl;
            result[n] = 'i';
            break;

        case 3: 
            cout << "o"<< endl;
            result[n] = 'o';
            break;

        case 4: 
            cout << "u" << endl;
            result[n] = 'u';

        }   
    }
     else if (  response != 'c' || 'v')
        {
            cout << "Invalid, please choose 'c' or 'v'"<< endl;
            cin >> response;
        }

}


}



cout<<endl<< "Your letters are";




for(int i=0; i<9; i++)
{
    cout << result[i];
    cout << " ";
}



 ifstream file("C:\\Users\\Chris\\Documents\\words.txt");
if(file.is_open())
{

    for(int i = 0; i < 70549; ++i)
    {
        file >> array1[i];
    }
}

    int l = 0;




    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array1[i].find(result[1]);

            if (found != std::string::npos)
                array2[i] = array1[i];              
        }   


    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array2[i].find(result[2]);

            if (found!=std::string::npos)

                array3[i] = array2[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array3[i].find(result[3]);

            if (found!=std::string::npos)

                array4[i] = array3[i];
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array4[i].find(result[4]);

            if (found!=std::string::npos)

                array5[i] = array4[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array5[i].find(result[5]);

            if (found!=std::string::npos)

                array6[i] = array5[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array6[i].find(result[6]);

            if (found!=std::string::npos)

                array7[i] = array6[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array7[i].find(result[7]);

            if (found!=std::string::npos)

                array8[i] = array7[i];              
        }

    for (int i =0; i < 70549; i ++)
        {
            std::size_t found = array8[i].find(result[8]);

            if (found!=std::string::npos)

                array9[i] = array8[i];              
        }


cout << "Your words are: ";
    for ( int i=0; i< (sizeof(array2)/sizeof(*array2));i ++)
        if (!array9[i].empty())
        {
        cout << array9[i] << ", ";
        }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

一些一般建议:

  • 使用像string sa[10][70549];
  • 这样的二维字符串数组
  • 不要反复调用srand()。你只需要播种一次。 (来自Lie Ryan)
  • 使用char数组而不是那些令人讨厌的开关块。 (瑞安再次纠正)
  • 你有一个元音选择的错误(称为%4,而不是%5,所以你永远不会看到'你')。
  • 使用an作为位置跟踪器很糟糕,看起来像上面的错误。
  • else if ( response != 'c' || 'v')也是一个错误;你想说if (response != 'c' && response != 'v'),但考虑到你的测试,这实际上是多余的。只需使用else { ... }
  • 为防止2个相同的连续字母(懒惰方式),在每个字母生成块中超过a / n == 0,循环使用随机生成器,直到它得到与前一个字符不同的结果。

由于只是扫描代码的负担,这个程序非常臃肿到人们不愿意帮助你的程度。这看起来像是入门编程课的作业。你对事情的运作方式有足够的误解,你应该花一些时间阅读一些初级水平的书籍,并从TA获得一对一的帮助。

答案 1 :(得分:0)

我不确定它是否与您的问题有关,但这段代码可能不正确:

 else if (  response != 'c' || 'v')
    {
        cout << "Invalid, please choose 'c' or 'v'"<< endl;
        cin >> response;
    }

此处的if语句将始终返回true,因为它说的如下:

if ((response != 'c') || 'v')

在C ++中,非零的变量始终被视为true,零被视为false。在这种情况下,'v'具有非零字符值(请记住,字符基本上只是格式化(通常)8位数字)。你应该写:

else if (response != 'c' && response != 'v')

else if (!(response == 'c' || response == 'v'))

或者,您可以将整个if('c')/ else if('v')/ else if(!('c'||'v'))块转换为switch语句,这可能是无论如何都更容易阅读。