簡體   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