您的位置:首页 > 编程语言 > C语言/C++

取读浮点数

2015-11-26 17:34 323 查看

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

int getInt(int *);
int getFloat(double *);

#define     SIZE        100
#define     MAX_FLT     2147483648          //double 8字节,但2^63太大,此处用2^31作为阈值,其实是不太准确的
//#define       MAX_FLT     2.1474836e+009  //当arr_flt定义为float型数组时,if(arr_flt
== MAX_FLT)会失效,
//而定义为double型数组时,则有效(?),如果如上句那样定义MAX_FLT=
//2147483648则不会出现失效这个问题
int main()
{
int i;
int n;
int arr_int[SIZE];
double arr_flt[SIZE];
int dim;
int reNum;

//for(n = 0; n < SIZE && ((reNum = getInt(&arr_int
)) != EOF); ++n)          //当getInt返回值是EOF或0或n >= SIZE时,退出循环
//      ;
for(i = 0; i < SIZE; ++i)
arr_int[i] = MAX_FLT;
//for(n = 0; n < SIZE && ((reNum = getFloat(&arr_flt
)) != EOF); ++n)
//if(arr_flt
== MAX_FLT)
//continue;     //for循环中continue语句是直接跳到++n处执行,如果不想跳转到此处而是直接
//跳到条件测试部分,应该用while循环(如下代码)
n = 0;
while(n < SIZE && ((reNum = getInt(&arr_int
)) != EOF))
{
if(arr_int
== MAX_FLT)
continue;
++n;
}
dim = n - 1;
n = 0;
while(dim-- >= 0)
printf("%d ", arr_int[n++]);
printf("/n");

return 0;
}

int getch();
void ungetch(int );

/***************************************************************************
**名称:getInt
**功能:从输入流中读取若干字符将他们转化成一个int型数据,存储在实参所指的
内存空间中
**参数:pNum:指向一个int数据的指针
**返回:1:如果从输入流中读取的字符不是EOF,'0' ~ '9','+','-'时,返回0;
2:如果从输入流中读取的字符时EOF,则返回EOF(-1);
3:如果从输入流中读取的若干连续字符能转化成一个有意义的数字时,
返回一个正值;
***************************************************************************/
int getInt(int *pNum)
{
int c;
int d;
int sign;

sign = 1;
while(isspace(c = getch()))                         //用库函数isspace更全面,不会发生遗漏情况
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-') //当不满足这4种情况时,返回0,下面对这4种情况分别处理
{
//ungetch(c);
//if(!isspace(c))
*pNum = c;
//else
//ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
{
d = c;
if(!isdigit(c = getch()))
{
if(c != EOF)
ungetch(c);
//ungetch(d);
*pNum = d;
return d;
}
}
for(*pNum = 0; isdigit(c); c = getch())             //将若干连续数字字符转化成int型值
*pNum = 10 * *pNum + (c - '0');                 //实参是指针的好处在于能改变其所指向的值
*pNum *= sign;
if(c != EOF)
ungetch(c);

return c;
}

int getFloat(double *pFlt)      //从输入流中获取浮点型数据,不符合要求的字符,全部丢弃
{
int c;
int sign;
int pow;

pow = 1;
while(isspace(c = getch()));
if(!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.')
return 0;
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
if(!isdigit(c = getch()))
return 0;
for(*pFlt = 0; isdigit(c); c = getch())
*pFlt = 10 * *pFlt + (c - '0');
if(c == '.')
if(isdigit(c = getch()))
{
for(; isdigit(c); c = getch())
{
*pFlt = 10 * *pFlt + (c - '0');
pow *= 10;
}
*pFlt /= pow;
*pFlt *= sign;
}
if(c != EOF)
ungetch(c);

return c;
}

#define     BUFSIZE     100     //自定义的输入缓冲区容量

int buf[BUFSIZE];               //自定义的输入缓冲区
int bufp;                       //指向输入缓冲区的指针

int getch()                     //从自定义的输入缓冲区或OS定义的输入缓冲区中读入一个字符
{
return (bufp != 0) ? buf[--bufp] : getchar();
}

void ungetch(int num)           //将一个字符压回到自定义的输入缓冲区中
{
if(bufp >= BUFSIZE)
printf("error: buffer full!");
else
buf[bufp++] = num;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 函数 编程