无法显示结果

时间:2013-07-05 09:57:14

标签: c function getchar

我自己练习c编程。我写的程序是一个getfloat,它将一个字符流转换为浮点数(来自K& R练习5-2)。我使用了书中的代码片段,它使用getch和ungetch从缓冲区或输入中获取下一个字符。我的代码的问题是我正在编写的Visual Studio在正确执行计算和转换后无法打印出值。我进入程序,发现我的函数中的变量c在转换结束时变为-1而不是10,如所说的那样。这是我的代码:

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

int getch(void);
void ungetch(int c);
int getfloat(float *pn);
int main(void)
{
int ret;

    float f;
    printf("enter a float number:\n");
    ret=getfloat(&f);
    if(ret>0)
        printf("you've entered: %f",f);

if (ret == EOF) 
{
      puts("Stopped by EOF.");
} 
else 
{
      puts("Stopped by bad input.");
}

return 0;
}

int getfloat(float *pn)
{
 char c,sign,dec;
 float pow;
 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();
 if(!isdigit(c))
 {
     ungetch(c);
     return -1;
 }
 for(*pn=0;c!=EOF && isdigit(c);c=getch())
     *pn=10* (*pn)+ (c-'0');  //calculate the integer part//

 if((c=getch())=='.')
 {
     for(*pn,dec=1;c!=EOF && isdigit(c);c=getch())
     {
         *pn=10* (*pn)+ (c-'0');  //calculate the decimal part//
         dec=dec*10;
     }
     *pn=*pn*sign/dec;
 }

 if((c=getch())=='e')
 {
      if((c=getch())=='-')
         for(*pn,pow=0.1;c!=EOF && isdigit(c);c=getch())
     {
         *pn=10* (*pn)+ (c-'0');  
         dec=dec/10;
     }
     else 
     {
         ungetch(c);
         for(*pn,pow=1.0;c!=EOF && isdigit(c);c=getch())
         {
             *pn=10* (*pn)+ (c-'0');  
             dec=dec*10;
         }
     }
*pn=*pn*sign*dec;
 }
if(c!=EOF)
    ungetch(c);
return c;
 }




#define BUFSIZE 100

char buf[BUFSIZE];      /* bufer for ungetch */
int bufp = 0;           /* next free position in buf */

 int getch(void)         /* get a (possibly pushed-back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)     /* push character back on input */
{
       if (bufp >= BUFSIZE)
              printf("ungetch: too many characters\n");
      else
              buf[bufp++] = c;
}

3 个答案:

答案 0 :(得分:2)

您的计划中存在太多问题。以下是一些:

dec=dec*10;

您的程序中有一些有效的代码路径dec未初始化。

char c,sign,dec;

/* ... */ 

if(c!=EOF)

EOF为否定int,因此c必须声明为int,而不是char

答案 1 :(得分:0)

int getfloat(float *pn){
    int c,sign,dec;
    float pow, tmp;

    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();
    if(!isdigit(c)){
        ungetch(c);
        return -1;
    }
    for(*pn=0;c!=EOF && isdigit(c);c=getch())
        *pn = 10* (*pn)+ (c-'0');  //calculate the integer part//
    *pn *= sign;

    if(c=='.'){
        c = getch();
        tmp=0;
        for(dec=1;c!=EOF && isdigit(c);c=getch()){
            tmp=10 * tmp + (c-'0');  //calculate the decimal part//
            dec=dec*10;
        }
        *pn = *pn + sign * tmp / dec;
    }

    if(c=='e'){
        c=getch();
        sign=(c=='-')?-1:1;
        if(c=='-')
            c=getch();
        for(tmp=0;c!=EOF && isdigit(c);c=getch())
            tmp=10 * tmp + (c-'0');
        printf("debug:%f\n", sign*tmp);
        *pn=*pn*powf(10.0f, sign*tmp);
    }
    if(c!=EOF)
        ungetch(c);
    return c;
}

可能还有问题

答案 2 :(得分:0)

for (*pn, ...)意味着什么?这是循环的初始化阶段,但*pn只获得pn所指向的内容并将其抛弃。

相关问题