V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zmaibbs7
V2EX  ›  C

C/C++ 递归调用 小问题。求指点!

  •  
  •   zmaibbs7 · Aug 31, 2015 · 1619 views
    This topic created in 3892 days ago, the information mentioned may be changed or developed.

    这个是老师的代码,解决的是逆波兰表达式。
    我想问的是
    代码如下,ide 是 dervC++:

    include<iostream>

    include <stdlib.h>

    using namespace std;
    double f ()
    {
    char str[10];
    cin >> str;

    switch (str[0])
    {
        case '+': return f () ;
        case '-': return f () ;
        case '*': return f () ;
        case '/': return f () ;
        default: return atof (str );
    }
    

    }
    int main ()
    {
    cout << f ();
    return 0;
    }

    为什么每次递归调用 str[0]都会自动指向下一个字符
    还有为什么 atof (str ) 这个 str 是指的整个字符数组吗?为什么至转化了一部分 比如 输入:+ 36 25
    第一次调用 atof ( str )返回的是 36 不是 36 25 。。
    求大神指点!!!!!!!!!
    为什么像我这样写递归 str[0]不会自动指向下一个字符。。。
    求大神指点!!!!!!!!!

    14 replies    2015-09-01 22:56:43 +08:00
    zmaibbs7
        1
    zmaibbs7  
    OP
       Aug 31, 2015
    我写的是这样: str[0]不会自动指向下一个字符 为什么啊?
    #include<iostream>
    using namespace std;
    void f (){
    char a[101];
    cin >> a ;
    if (a[0] == ')') cout << a[0] <<endl;
    else f ();
    cout << a[0] <<endl;
    }
    int main ()
    {
    f ();
    return 0;
    }
    sandideas
        2
    sandideas  
       Aug 31, 2015 via iPhone   ❤️ 1
    你理解错了吧。。
    + 36 25
    相当于
    +
    36
    25
    三次 cin
    sandideas
        3
    sandideas  
       Aug 31, 2015 via iPhone
    str[0]就是判断是不是符号。如果不是,就说明是数字,所以整个 str 转换成数字
    jedihy
        4
    jedihy  
       Sep 1, 2015
    因为它本来就没有指向下一字节。。,只是每次递归你都在输入。。
    linux40
        5
    linux40  
       Sep 1, 2015 via Android
    没有 break
    linux40
        6
    linux40  
       Sep 1, 2015 via Android
    @linux40 我理解错你的意思了, 36 和 25 要读两次,因为有空格。。。
    jimzhong
        7
    jimzhong  
       Sep 1, 2015
    @linux40 return 了不需要 break
    但是你每个 f 里面都在读入字符串,感觉怪怪的。
    zmj1316
        8
    zmj1316  
       Sep 1, 2015
    都用了 devcpp 了 跟踪一下不就知道了?
    ljbha007
        9
    ljbha007  
       Sep 1, 2015
    http://www.cplusplus.com/reference/cstdlib/atof/

    The function first discards as many whitespace characters (as in isspace ) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below ), and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.

    注意 takes as many characters as possible that are valid following a syntax resembling that of floating point literals 这一句
    The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.
    zmaibbs7
        10
    zmaibbs7  
    OP
       Sep 1, 2015
    @sandideas 懂了 非常感谢!
    zmaibbs7
        11
    zmaibbs7  
    OP
       Sep 1, 2015
    @jimzhong 我简化了
    本来 switch 是这样的 可以做+ - * / 运算
    switch (str[0])
    {
    case '+': return f () + f ();
    case '-': return f () - f ();
    case '*': return f () * f ();
    case '/': return f () / f ();
    default: return atof (str );
    }
    linux40
        12
    linux40  
       Sep 1, 2015
    @jimzhong 嗯,对。。。
    cheng007
        13
    cheng007  
       Sep 1, 2015
    一个函数只有一个返回值呀,你要打印出来,倒序,你需要在递归函数里面去打印,而不是在在外面 cout
    cheng007
        14
    cheng007  
       Sep 1, 2015
    using namespace std;
    void f ()
    {
    char str[10];
    cin >> str;

    switch (str[0])
    {
    double ret;
    case '+': ret = f () ;
    case '-': ret = f () ;
    case '*': ret = f () ;
    case '/': ret = f () ;
    default: ret = atof (str );
    cout << ret;
    }
    }
    int main ()
    {
    f ();
    return 0;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   954 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 21:50 · PVG 05:50 · LAX 14:50 · JFK 17:50
    ♥ Do have faith in what you're doing.