getint和getch [英] getint and getch

查看:135
本文介绍了getint和getch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照书面规定,getint会将+或-而不是后跟数字视为零的有效表示形式.修复该问题可将这样的字符推回输入端."

"As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input."

好吧,这是原始版本:

int getint2(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;
    ungetch(c);
    return c;
}

我这样

int getint2(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();

       if(!isdigit(c)) {
         ungetch(c);
         return 0;
       }
    }

    for(*pn = 0; isdigit(c); c = getch())
        *pn = 10 * *pn + (c - '0');

    *pn *= sign;
    ungetch(c);
    return c;
}

所以我不确定作者想要什么.我应该同时删除+/-还是后面的字符?如果在+/-或-1之后没有数字,我应该返回0吗?

So im not sure what did the author want. Should i unget the +/- aswell, or just the character after it? Should i return 0 in case there's no digits after +/- or -1?

我也有关于getch和ungetch函数的问题: 由于系统上的EOF为-1,因此这就是我编写getch和ungetch的方式:

I also have a question about getch and ungetch functions: since EOF on my sistem is -1, this is how i wrote getch and ungetch:

int buf = EOF;

int getch()
{
    int temp;

    if(buf == -2)
        return EOF;

    if(buf == EOF) 
        temp = getchar();
    else
    {
        temp = buf;
        buf = EOF;
    }

    return temp;
}

void ungetch(int c)
{
     if(c == EOF) 
        buf = -2;

     buf = c;
}

所以有人告诉我,EOF可以为-2.我应该怎么做才能避免这种问题".

So i was told by some people that EOF can be -2. What should i do to avoid this sort of 'problem'.

推荐答案

要回答关于EOF-2的第二个问题,我可以想到一些解决方案:

To answer your second question, about EOF and -2, I can think of a couple of solutions:

  • 您可以在缓冲区"中包含一个字符数组,并在该位置中包含一个索引"(即,具有基本的堆栈).我相信那时候在K& R中引入了数组.这将使您具有更大的灵活性,因为您可以ungetch()多个字符.此方案的另一个优点是,第1部分中的代码(可能需要两次ungetch())也变得更加容易.
  • 您可以将状态"存储在另一个int变量中,例如int buffered = 0;,然后getch()仅在buffered == 1时返回buf. ungetch()设置buffered = 1getch()设置buffered = 0:
  • you could have an array of characters in the "buffer", and an "index" in the position (i.e., have a rudimentary stack). I believe that arrays are introduced in K&R by that point. That will give you flexibility in that you can ungetch() more than one character. An added advantage of this scheme is that your code in part 1, where you might need to ungetch() twice, becomes easier as well.
  • you could store the 'state' in another int variable, such as int buffered = 0;, and then getch() would return buf only when buffered == 1. ungetch() sets buffered = 1, getch() sets buffered = 0:
static int buf;
static int buffered = 0;

int getch()
{
    if (buffered) {
        buffered = 0;
        return buf;
    }
    return getchar();
}

void ungetch(int c)
{
    buffered = 1;
    buf = c;
}

这篇关于getint和getch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆