递归程序,包括 C 中的 scanfs

问题描述 投票:0回答:1

我正在尝试编写这样的递归函数

int nestedExpression(){
    
    char expCh;
    scanf(" %c", &expCh);
  

    if(expCh == '\n'){
        return 0;
    }
 
    nestedExpression();


    \\deal with the return
    return 0;

现在我的输入是 (1 + 1) 并且当它到达角色时 ) 它正在按原样对待它 ' '.

c recursion scanf
1个回答
0
投票

想想这一点

scanf(" %c", &expCh);


if(expCh == '\n'){
    return 0;
}

当 expCh 不是 ' ',以后你总是

return 0;

我想这就是为什么你认为每个角色都被视为“ '.

此外,您对递归调用的结果不做任何事情

nestedExpression();

您需要将其分配给变量。

© www.soinside.com 2019 - 2024. All rights reserved.