泰州网站的建设,项目网络由箭线和节点构成,徐州服饰网站建设,安吉网站设计C程序设计语言 #xff08;第二版#xff09; 练习 5-2
练习 5-2 模仿函数getint的实现方法#xff0c;编写一个读取浮点数的函数getfloat。getfloat函数的返回值应该是什么类型#xff1f;
注意#xff1a;代码在win32控制台运行#xff0c;在不同的IDE环境下#xf…C程序设计语言 第二版 练习 5-2
练习 5-2 模仿函数getint的实现方法编写一个读取浮点数的函数getfloat。getfloat函数的返回值应该是什么类型
注意代码在win32控制台运行在不同的IDE环境下有部分可能需要变更。
IDE工具Visual Studio 2010 代码块
#include stdio.h
#include stdlib.h
#include ctype.h
#include string.h#define BUFSIZE 100
#define SIZE 20char buf[BUFSIZE];
int bufp 0;int getch(void){return (bufp 0) ? buf[--bufp] : getchar();
}void ungetch(int c){if(bufp BUFSIZE){printf(Ungetch! Too many characters!\n);}else{buf[bufp] c;}
}int getfloat(float *pn){int c, sign, t;float p;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.0; isdigit(c); c getch()){*pn *pn * 10.0 (c - 0);}if(c .){c getch();}for(p 10.0; isdigit(c); c getch(), p * 10.0){*pn (c - 0) / p;}*pn * sign;if(c ! EOF){ungetch(c);}return c;
}int main(){int n;float arr[SIZE];for(n 0; n SIZE getfloat(arr[n]) ! EOF; n);for(int i 0; i n; i){printf(%f , arr[i]);}printf(\n);system(pause);return 0;
}