繁体   English   中英

关于计算器程序的困惑

[英]Confusion about calculator program

int getop(char s[])
{
    int i = 0, c, next;
    /* Skip whitespace */
    while((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';    
    /* Not a number but may contain a unary minus. */
    if(!isdigit(c) && enter code herec != '.' && c != '-')
        return c;
    if(c == '-')
    {
        next = getch();
        if(!isdigit(next) && next != '.')
           return c;
        c = next;
    }
    else
        c = getch();    

    while(isdigit(s[++i] = c)) //HERE
            c = getch();
    if(c == '.')                     /* Collect fraction part. */
        while(isdigit(s[++i] = c = getch()))
                        ;
    s[i] = '\0';
    if(c != EOF)
        ungetch(c);
    return NUMBER;
};

如果没有空格或制表符,那么s [0]将初始化什么值呢?.......&s [1] ='\\ 0'的用途是什么

如果没有空格或制表符,那么s [0]将初始化什么值呢?

以下循环将继续执行,直到getch()返回的字符既不是空格也不是制表符:

while((s[0] = c = getch()) == ' ' || c == '\t')
    ;

s [1] ='\\ 0'的用途是什么

它将s转换为长度为1的C字符串 ,该字符串的唯一字符已由getch()读取。 '\\0'是必需的NUL终止符。

while((s[0] = c = getch()) == ' ' || c == '\t')

读取字符,直到没有制表符或空格为止。

s[1] = '\0';

将char数组s转换为C格式的适当字符串(c中的所有字符串都必须以空字节结尾,该空字节由'\\ 0'表示)。

  1. 如果没有空格或制表符,那么您将陷入无限循环。

  2. s [1] ='\\ 0'是标记结束的一种方式,因此strlen()之类的函数知道何时停止读取c字符串。 它称为“ Null-Terminated”字符串: http : //chortle.ccsu.edu/assemblytutorial/Chapter-20/ass20_2.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM