-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp5-2.c
86 lines (69 loc) · 1.41 KB
/
cp5-2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<ctype.h>
#define SIZE 10
main()
{
int getfloat(float *);
float array[SIZE]={0.0};
int i;
for(i=0;i<SIZE&&getfloat(&array[i])!=EOF;i++)
;
for(i=0;i<SIZE;i++)
printf("%f\n",array[i]);
}
int getch(void);
void ungetch(int c);
int getfloat(float *pn)/*返回的仍然是int,*pn才是浮点*/
{
int c,sign;
float pow=1;/*float*/
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;isdigit(c);c=getch())/*收集整数部分*/
*pn=(*pn)*10.0+(c-'0');
if(c=='.')
{
c=getch();/*此处需要先读一个,才能有下面的isdigit*/
for(pow=1;isdigit(c);c=getch())
{
*pn=(*pn)*10+(c-'0');
pow*=10;
}
}
*pn= sign* *pn/pow;/*通过*pn修改,而不是return*/
if(c!=EOF)
ungetch(c);
return c;
}
#define BUFSIZE 10
char buf[BUFSIZE];
int bufp=0;
int getch(void)
{
return (bufp>0)?buf[--bufp]:getchar();
}
void ungetch(int c)
{
if(bufp>=BUFSIZE)
printf("error:buf is full\n");
else
buf[bufp++]=c;
}
/*
if(c=='.')
c=getch();
for(pow=1;isdigit(c);c=getch())
{
*pn=(*pn)*10+(c-'0');
pow*=10;
}
*/
/*这样写也是可以的,因为上面的for遇到不是数字跳出,判断该字符是不是.,如果不是,那么不会再getch,下面的for语句isdigit(c),仍然不会执行。,此时会把该符号压回buf区并返回*/