⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getint.c

📁 里面包含很多c语言的源码
💻 C
字号:
/* Using character test macros to create an integer */
/* input function. */

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

int get_int(void);

int main( void )
{
    int x;
    x =  get_int();

    printf("You entered %d.\n", x);
}

int get_int(void)
{
    int ch, i, sign = 1;

    /* Skip over any leading white space. */

    while ( isspace(ch = getchar()) )
        ;

    /* If the first character is nonnumeric, unget */
    /* the character and return 0. */

    if (ch != '-' && ch != '+' && !isdigit(ch) && ch != EOF)
    {
        ungetc(ch, stdin);
        return 0;
    }

    /* If the first character is a minus sign, set */
    /* sign accordingly. */

    if (ch == '-')
        sign = -1;

    /* If the first character was a plus or minus sign, */
    /* get the next character. */

    if (ch == '+' || ch == '-')
        ch = getchar();

    /* Read characters until a nondigit is input. Assign */
    /* values, multiplied by proper power of 10, to i. */

    for (i = 0; isdigit(ch); ch = getchar() )
        i = 10 * i + (ch - '0');

    /* Make result negative if sign is negative. */

    i *= sign;

    /* If EOF was not encountered, a nondigit character */
    /* must have been read in, so unget it. */

    if (ch != EOF)
        ungetc(ch, stdin);

    /* Return the input value. */

    return i;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -