如何计算C++中的元音?
我的一个作业有问题。我的任务如下: 用C++编写程序 输入:字符串 输出:相邻元音的数量 示例:输入->计算机;输出->12因为c、mp、t、rs 我已经试过几次了,但都不管用。 这是我的一个尝试:如何计算C++中的元音?,c++,arrays,string,C++,Arrays,String,我的一个作业有问题。我的任务如下: 用C++编写程序 输入:字符串 输出:相邻元音的数量 示例:输入->计算机;输出->12因为c、mp、t、rs 我已经试过几次了,但都不管用。 这是我的一个尝试: cout << "Type your word: "; cin >> YourWord; cout << "Your word is: " << YourWord << endl; //maganhangzók char a = 'a';
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部分,而不是包括它 当您使用很长的代码行时,情况更是如此。因此,读者必须使用水平滑块,在这种情况下,不可能同时看到长代码行的结尾和随后的短代码行。这使得查找bug变得更加困难 <>这是C++,因此面向对象的编程,我们可以委托一个字符是否是所选的一部分到一个特定的对象,例如一个字符测试类的决定。ChartTester对象在创建时接收包含所有选定字符的字符串。这样,计算请求的数字的算法就可以短得多 ChartTester对象可以将所有繁重的工作委托给字符串 创建此辅助对象的源代码如下所示:
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;
}
旁注:
以上,我使用了char类型和相关的经典C++字符串类型,因为这是你在代码中的方式。但是,对于生产代码,您可能必须使用更通用的宽wchar__t和wstring类型,以便允许非ASCII字符,例如á、á、ê,ẞ., 等Unicode字符。
y呢?那不是元音吗?猜它不是匈牙利语的元音。方便阅读:YourWord[i]=a将a的值赋给YourWord[i]。YourWord[i]==a比较这两个值。在你的问题中,你说的是输出彼此相邻的元音数。然而,似乎你指的是辅音的数量,而不是元音。你可以使用包含所有元音或所有辅音的字符串变量listOfGoodChars,正如上面Andreas所暗示的。所有元音的列表都是数据而不是代码!然后使用findstring方法的表单4,如下所述:;like pos=listOfGoodChars.findch;如果位置!=是的,这是一个很好的性格。。。// 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