一、getint函数的介绍
getint是一个C标准库命令,用于读取下一个整数值。该函数从标准输入流(stdin)中读取下一个整数,并将其存储到指定的地址中。 函数原型:int getint(int *pn) 在读取输入时,该函数会忽略空格和制表符,并在遇到非数字字符时停止读取并返回。
二、getint函数的使用
下面是一个简单的示例代码:
#include <stdio.h>
int main()
{
int n = 0;
while(getint(&n) != EOF)
printf("%d\n", n);
return 0;
}
当用户在控制台中输入若干个数字时,该程序会逐个读取并输出这些整数。
三、getint函数的实现
下面是getint函数的实现代码:
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getint函数:读取下一个整数到*pn中 */
int getint(int *pn)
{
int c, sign;
while(isspace(c = getch())) /* 跳过空格和制表符 */
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); /* 不是数字 */
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if(c != EOF)
ungetch(c);
return c;
}
该实现代码中使用了ctype.h库中的isspace和isdigit函数。isspace用于判断一个字符是否为空格或制表符。isdigit用于判断一个字符是否为数字。同时,getch和ungetch函数也是由该例实现代码提供的。
四、getint函数的错误处理
在实际使用getint函数时,需要进行错误处理以确保程序的正确性。以下是几个可能出现的错误,并提供了对应的解决方案。
4.1 输入中包含非法字符
如果输入中包含非数字字符,getint函数将返回0,并将该字符推回到输入流中以供下次读取。在下面的示例程序中,非法字符会被打印:
#include <stdio.h>
int main()
{
int n = 0, rc;
while((rc = getint(&n)) != EOF)
{
if(rc == 0)
{
printf("非法字符\n");
break;
}
printf("%d\n", n);
}
return 0;
}
4.2 输入中包含多个数字
在输入中包含多个数字时,getint函数需要将它们分别读取,并且要避免多次将同一个数字读取到缓存中。下面的示例程序展示了如何正确处理这种情况:
#include <stdio.h>
int main()
{
int n = 0, rc;
while((rc = getint(&n)) != EOF)
{
if(rc == 0)
{
printf("非法字符\n");
break;
}
else if(rc > 0)
{
printf("%d\n", n);
n = 0; /* 重置n的值 */
}
}
return 0;
}
五、getint函数的扩展
为了满足不同的需求,getint函数还可以进行一些扩展。下面是几个可能有用的例子。
5.1 getfloat函数
getfloat函数是getint函数的扩展版本,用于读取浮点数。以下是getfloat函数的一种实现方式:
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getfloat函数:读取下一个浮点数到*pn中 */
int getfloat(float *pn)
{
int c, sign;
float power;
while(isspace(c = getch())) /* 跳过空格和制表符 */
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') {
ungetch(c); /* 不是数字 */
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0.0; isdigit(c); c = getch())
*pn = 10.0 * *pn + (c - '0');
if(c == '.')
c = getch();
for(power = 1.0; isdigit(c); c = getch())
{
*pn = 10.0 * *pn + (c - '0');
power *= 10.0;
}
*pn *= sign / power;
if(c != EOF)
ungetch(c);
return c;
}
5.2 不区分正负号的getint函数
由于getint函数返回值要么是EOF,要么表示读取到的字符,因此不能直接用来表示读取的整数值是否为正。以下是一种不区分正负的getint函数实现方法:
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getint函数:读取下一个整数到*pn中 */
int getint(int *pn)
{
int c, rc;
while(isspace(c = getch())) /* 跳过空格和制表符 */
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); /* 不是数字 */
return 0;
}
rc = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= rc;
if(c != EOF)
ungetch(c);
return c;
}
5.3 支持科学计数法的getfloat函数
getfloat函数还可以支持科学计数法。以下是getfloat函数的扩展实现:
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getfloat函数:读取下一个浮点数到*pn中 */
int getfloat(float *pn)
{
int c, sign, exp_sign, exp_value;
float power;
while(isspace(c = getch())) /* 跳过空格和制表符 */
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') {
ungetch(c); /* 不是数字 */
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0.0; isdigit(c); c = getch())
*pn = 10.0 * *pn + (c - '0');
if(c == '.')
c = getch();
for(power = 1.0; isdigit(c); c = getch())
{
*pn = 10.0 * *pn + (c - '0');
power *= 10.0;
}
if(c == 'e' || c == 'E')
{
exp_sign = 1;
c = getch();
if(c == '+' || c == '-')
{
exp_sign = (c == '-') ? -1 : 1;
c = getch();
}
for(exp_value = 0; isdigit(c); c = getch())
exp_value = 10 * exp_value + (c - '0');
if(exp_sign == 1)
while(exp_value-- > 0)
power *= 10.0;
else
while(exp_value-- > 0)
power /= 10.0;
}
*pn *= sign / power;
if(c != EOF)
ungetch(c);
return c;
}