简体   繁体   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;
};

what if there is no blank space or tab than what value will s[0] will initialize .......& what is the use of s[1]='\\0' 如果没有空格或制表符,那么s [0]将初始化什么值呢?.......&s [1] ='\\ 0'的用途是什么

what if there is no blank space or tab than what value will s[0] will intialize 如果没有空格或制表符,那么s [0]将初始化什么值呢?

The following loop will continue executing until getch() returns a character that's neither a space nor a tab: 以下循环将继续执行,直到getch()返回的字符既不是空格也不是制表符:

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

what is the use of s[1]='\\0' s [1] ='\\ 0'的用途是什么

It converts s into a C string of length 1, the only character of which has been read by getch() . 它将s转换为长度为1的C字符串 ,该字符串的唯一字符已由getch()读取。 The '\\0' is the required NUL-terminator. '\\0'是必需的NUL终止符。

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

Read character until its not a tab or space. 读取字符,直到没有制表符或空格为止。

s[1] = '\0';

Convert char array s to a proper string in C format (all strings in c must be terminated with a null byte, which is represented by '\\0'. 将char数组s转换为C格式的适当字符串(c中的所有字符串都必须以空字节结尾,该空字节由'\\ 0'表示)。

  1. If there is no space or tab, you're stuck with an infinite loop. 如果没有空格或制表符,那么您将陷入无限循环。

  2. s[1]='\\0' is a way of marking the end so functions like strlen() know when to stop reading through c strings. s [1] ='\\ 0'是标记结束的一种方式,因此strlen()之类的函数知道何时停止读取c字符串。 it's called "Null-Terminating" a string: http://chortle.ccsu.edu/assemblytutorial/Chapter-20/ass20_2.html 它称为“ 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