Skip to content

Latest commit

 

History

History
62 lines (40 loc) · 1.82 KB

File metadata and controls

62 lines (40 loc) · 1.82 KB

C++ 程序:检查字符是元音还是辅音

原文: https://www.programiz.com/cpp-programming/examples/vowel-consonant

在此示例中,if...else语句用于检查用户输入的字母是元音还是辅音。

要理解此示例,您应该了解以下 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

如果LowerowerVowelisUppercaseVowel均为true,则输入的字符是元音,如果不是,则该字符是辅音。