如何在C++中计算彼此相邻的元音?

How to calculate vowels next to each other in C++?

本文关键字:C++ 计算      更新时间:2023-10-16

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

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

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

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

  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 字符,如 á、à、ê、ẞ. 等。(统一码字符(。