0

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

  • 用 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]++;
    }
}

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

4

1 回答 1

1

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

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

为了可读性,第 2 部分应该使用第 1 部分,而不是包含它。

这更是如此,因为您似乎使用了很长的代码行。所以读者必须使用水平滑块,在这种情况下,不可能同时看到长代码行的结尾和后面的较短代码行。这使得查找错误变得更加困难。

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

CharTester 对象可以将所有繁重的工作委托给字符串查找方法

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

    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 字符)。

于 2019-10-19T18:34:41.493 回答