C 语言教程 在线

1590c-examples-vowel-consonant

参考方法:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char c;
    printf("请输入一个字母:");
    scanf("%c",&c);
    if ((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
        if (c=='A'||c=='E'||c=='I'||c =='O'||c=='U'||c=='a'||c=='e'||c=='i'||c =='o'||c=='u')
            printf("%c为元音",c);
        else 
            printf("%c为辅音",c);
    else
        printf("Error input!");
    return 0;
}

1589c-examples-vowel-consonant

如果从键盘中输入的不是字母,例如,数字,标点符号,都会输出是辅音。所以我对这个做了一个简单的判定。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

int main()
{
    char cLetter;
    int iInRange, iCheck, i, iLetter;
    i = 1;
    while (i)
    {
        printf("输入一个字母, 判断元音与辅音. 如果想退出,请输入“0”。\n");
        scanf("%c", &cLetter);
        getchar();
        printf("----------------------------------------------------------------------\n");
        iLetter = (int)cLetter;
        iCheck = isalpha(iLetter);
        if (iCheck)
        {
            iInRange = (cLetter == 'a') || (cLetter == 'e') || (cLetter == 'i') || (cLetter == 'o') || (cLetter == 'u') || (cLetter == 'A') || (cLetter == 'E') || (cLetter == 'I') || (cLetter == 'O') || (cLetter == 'U');
            if (iInRange)
            {
                printf("字母 %c 是元音字母!\n", cLetter);
                printf("----------------------------------------------------------------------\n");
                continue;
            }
            else
            {
                printf("字母 %c 是辅音字母!\n", cLetter);
                printf("----------------------------------------------------------------------\n");
                continue;
            }
        }
        else if (iCheck == 0)
        {
            if (iLetter != 48)
            {
                printf("Error input!\n");
                printf("----------------------------------------------------------------------\n");
                continue;
            }
            else if ((int)cLetter == 48)
            {
                printf("Bye bye~~~!\n");
                printf("----------------------------------------------------------------------\n");
                i = (int)cLetter - 48;
            }
        }
    }
    return 0;
}

1588c-examples-even-odd

奇偶数判断其实有个更简单高效的办法,我们的整数,在计算机中存储的都是二进制,奇数的最后一位必是1,所以我们可以这样写:

#include <stdio.h>
 
int main()
{
    int number;
 
    printf("请输入一个整数: ");
    scanf("%d", &number);
 
    // 判断这个数最后一位是1这为奇数
    if(number&1)
        printf("%d 是奇数。", number);
    else
        printf("%d 是偶数。", number);
 
    return 0;
}

1587c-examples-swapping

参考方法:

#include<stdio.h>

void swap(int *a,int*b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
int main()
{
    int a=1,b=2;
    swap(&a,&b);
    printf("a=%d\nb=%d\n",a,b);
    return 0;
}

1586c-examples-int-data-compare

比较两数从键盘输入:

#include <stdio.h>

int main()
{
    int a,b;
    printf("请输入俩个整数以空格隔开: \n");
    scanf("%d %d",&a,&b);   // 从键盘输入两个数
    if(a>b){
        printf("%d>%d\n",a,b);
    }
    else
    {
        printf("%d<%d\n",a,b);
    }
}