原文: https://www.programiz.com/cpp-programming/examples/vowel-consonant
要理解此示例,您应该了解以下 C++ 编程主题:
五个字母 a,e,i,o 和 u 被称为元音。 除了这 5 个字母外,其他所有字母都是辅音。
该程序假定用户将始终输入字母。
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
cout << "Enter an alphabet: ";
cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}输出
Enter an alphabet: u
u is a vowel.用户输入的字符存储在变量c中。
如果c是小写元音,则isLowerCaseVowel的计算结果为true,而对于其他任何字符,则为false。
同样,如果c是大写元音,则isUpperCaseVowel的计算结果为true,而对于其他任何字符,则为false。
如果LowerowerVowel和isUppercaseVowel均为true,则输入的字符是元音,如果不是,则该字符是辅音。