我的一项家庭作业有问题。我的任务如下:


用C ++编写程序
输入:一个字符串
输出:相邻元音的数量
例如:输入->计算机;输出-> 1 2 1 2(因为c,mp,t,rs)


我已经尝试了几件事,但是没有用。
这是我的尝试之一:

cout << "Type your word: ";
cin >> YourWord;
cout << "Your word is: " << YourWord << endl;

//maganhangzók
char a = 'a';
char e = 'e';
char i = 'i';
char o = 'o';
char u = 'u';

//massalhangzok
char b = 'b';
char c = 'c';
char d = 'd';
char f = 'f';
char g = 'g';
char h = 'h';
char j = 'j';
char k = 'k';
char l = 'l';
char m = 'm';
char n = 'n';
char p = 'p';
char q = 'q';
char r = 'r';
char s = 's';
char t = 't';
char v = 'v';
char w = 'w';
char x = 'x';
char y = 'y';
char z = 'z';

int counter[YourWord.length()];
int nothing = 0;

for(int i=1; i<YourWord.length(); i++) {
    if (((YourWord[i] = a) || (YourWord[i] = e) || (YourWord[i] =  i) || (YourWord[i] = o) || (YourWord[i] = u)) && ((YourWord[i-1] = a) || (YourWord[i-1] = e) || (YourWord[i-1] =  i) || (YourWord[i-1] = o) || (YourWord[i-1] = u))) {
        nothing++;
    } else if (((YourWord[i] = a) || (YourWord[i] = e) || (YourWord[i] =  i) || (YourWord[i] = o) || (YourWord[i] = u)) && ((YourWord[i-1] = b) || (YourWord[i-1] = c) || (YourWord[i-1] = d) || (YourWord[i-1] = f) || (YourWord[i-1] = g) || (YourWord[i-1] = j) || (YourWord[i-1] = k) || (YourWord[i-1] = l) || (YourWord[i-1] = m) || (YourWord[i-1] = n) || (YourWord[i-1] = p) || (YourWord[i-1] =  q) || (YourWord[i-1] = r) || (YourWord[i-1] = s) || (YourWord[i-1] = t) || (YourWord[i-1] =  v) || (YourWord[i-1] =  w) || (YourWord[i-1] = x) || (YourWord[i-1] = y) || (YourWord[i-1] = z))) {
        counter[i] = counter[i];
    } else if (((YourWord[i] = b) || (YourWord[i] = c) || (YourWord[i] =  d) || (YourWord[i] = f) || (YourWord[i] = g) || (YourWord[i] = j) || (YourWord[i] =  k) || (YourWord[i] = l) || (YourWord[i] = m) || (YourWord[i] = n) || (YourWord[i] = p) || (YourWord[i] = q) || (YourWord[i] = r) || (YourWord[i] = s) || (YourWord[i] = t) || (YourWord[i] = v) || (YourWord[i] =  w) || (YourWord[i] = x) || (YourWord[i] = y) || (YourWord[i] = z)) && ((YourWord[i-1] = b) || (YourWord[i-1] = c) || (YourWord[i-1] =  d) || (YourWord[i-1] = f) || (YourWord[i-1] = g) || (YourWord[i-1] = j) || (YourWord[i-1] = k) || (YourWord[i-1] = l) || (YourWord[i-1] = m) || (YourWord[i-1] = n) || (YourWord[i-1] = p) || (YourWord[i-1] = q) || (YourWord[i-1] = r) || (YourWord[i-1] = s) || (YourWord[i-1] = t) || (YourWord[i-1] =  v) || (YourWord[i-1] =  w) || (YourWord[i-1] = x) || (YourWord[i-1] = y) || (YourWord[i-1] = z))) {
        counter[i]++;
    } else {
            counter[i+1]++;
    }
}


不幸的是,它不必要地复杂并且绝对无法使用。
谁能帮我提示我应该从哪里开始?

最佳答案

您的代码有一个问题,那就是它似乎试图同时做两件事:


决定角色是否在所选集合中
根据这些决定,计算请求的数字。


出于可读性考虑,第2部分应使用第1部分,而不包括第1部分。

在您似乎使用很长的代码行的情况下,甚至更是如此。因此,读者必须使用水平滑块,在这种情况下,无法同时看到长代码行的末端和随后的较短代码行。这使得查找错误更加困难。

这是C ++,因此是面向对象的编程,因此我们可以将某个字符是否属于所选字符的决定委托给某个临时对象,例如CharTester类。 CharTester对象在创建时接收包含所有“选择”字符的字符串。这样,用于计算请求数字的算法可以更短。

CharTester对象可以将所有艰苦的工作委托给字符串find method

创建此辅助对象的源代码将如下所示:

    static const char  EnglishConsonantList[] =
        "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ";
    CharTester  cht(EnglishConsonantList);


上面的编码风格的好处是,如果某个正式的语法委员会决定从现在开始,Y将是辅音而不是元音,则源代码中所需的更改很小。而且您的老师可能还没有说过这样的话:«现在您已经完成了辅音,请使用元音。 »-这是为了迫使您意识到源代码的灵活性。

因此,我们进入以下代码,其中最有趣的部分是goodCharCounts()函数,该函数返回包含所请求数字的std::vector对象。

#include  <vector>
#include  <string>
#include  <iostream>

using  std::string;
using  std::cout;


class CharTester {
public:
    CharTester(const string& list) : goodCharList(list)
    {};
    bool isGoodChar(char ch) const;

private:
    string  goodCharList;    // list of "chosen" characters
};

bool CharTester::isGoodChar(char ch) const
{
    // hard work there:
    bool isGood = (goodCharList.find(ch) != string::npos);

    return isGood;
}


// THE ALGORITHM:

std::vector<int>  goodCharCounts(const CharTester& cht, const std::string& str)
{
    std::vector<int>  posVec;

    int  counter = 0;
    // loop on all characters of the input string
    for (char ch : str) {
        bool isGood = cht.isGoodChar(ch);

        if (isGood) {
            counter++;
        }
        else if (counter > 0) {
            // end of current "chosen" group, so must register it
            posVec.push_back(counter);
            counter = 0;
        }
    }

    if (counter > 0) {
        // register last "chosen" group
        posVec.push_back(counter);
        counter = 0;
    }

    return  posVec;
}


我们可以通过添加以下主程序来测试算法:

// test one string and print the results:
void doUnitTest(const CharTester& cht, const string& str)
{
    std::vector<int>  counts = goodCharCounts(cht, str);

    cout << str << " --> ";
    // loop on all numbers:
    for (int n : counts) {
        cout << n << ' ';
    }
    cout << std::endl;
}


// possible lists of "chosen" characters :

//-- const wchar_t FrenchVowelList[]      = L"AEIOUYÀÂÉÊÈÏÔÛÙaeiouyàâéêèïôûù";
//-- const wchar_t RussianVowelList[]     = L"АЕИОЭЙЫЮЯЁУаеиоэйыюяёу";

static const char  EnglishVowelList[] =  "aeiouyAEIOUY";
static const char  EnglishConsonantList[] =
    "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ";


int main()
{
    CharTester  cht(EnglishConsonantList);

    doUnitTest(cht, "computers");
    doUnitTest(cht, "Some computers are cheap. Aeiuo-XrZT.");

    return EXIT_SUCCESS;
}


执行:

computers --> 1 2 1 2
Some computers are cheap. Aeiuo-XrZT. --> 1 1 1 2 1 2 1 2 1 4


边注:

上面,我使用了char类型和相关的经典C ++ string类型,因为这是您进行代码的方式。但是,对于生产代码,您可能必须使用更通用的宽域wchar_twstring类型,以便允许使用非ASCII字符,例如á,à,ê,ẞ等。 Unicode字符)。

关于c++ - 如何在C++中计算相邻的元音?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58458315/

10-12 21:41