V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zmaibbs7
V2EX  ›  C

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

  •  
  •   zmaibbs7 · 2015-08-31 23:15:00 +08:00 · 1010 次点击
    这是一个创建于 3157 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个是老师的代码,解决的是逆波兰表达式。
    我想问的是
    代码如下,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 条回复    2015-09-01 22:56:43 +08:00
    zmaibbs7
        1
    zmaibbs7  
    OP
       2015-08-31 23:15:46 +08:00
    我写的是这样: 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  
       2015-08-31 23:22:52 +08:00 via iPhone   ❤️ 1
    你理解错了吧。。
    + 36 25
    相当于
    +
    36
    25
    三次 cin
    sandideas
        3
    sandideas  
       2015-08-31 23:23:53 +08:00 via iPhone
    str[0]就是判断是不是符号。如果不是,就说明是数字,所以整个 str 转换成数字
    jedihy
        4
    jedihy  
       2015-09-01 02:54:23 +08:00
    因为它本来就没有指向下一字节。。,只是每次递归你都在输入。。
    linux40
        5
    linux40  
       2015-09-01 07:34:09 +08:00 via Android
    没有 break
    linux40
        6
    linux40  
       2015-09-01 07:35:18 +08:00 via Android
    @linux40 我理解错你的意思了, 36 和 25 要读两次,因为有空格。。。
    jimzhong
        7
    jimzhong  
       2015-09-01 07:54:05 +08:00
    @linux40 return 了不需要 break
    但是你每个 f 里面都在读入字符串,感觉怪怪的。
    zmj1316
        8
    zmj1316  
       2015-09-01 08:18:00 +08:00
    都用了 devcpp 了 跟踪一下不就知道了?
    ljbha007
        9
    ljbha007  
       2015-09-01 08:25:15 +08:00
    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
       2015-09-01 10:12:13 +08:00
    @sandideas 懂了 非常感谢!
    zmaibbs7
        11
    zmaibbs7  
    OP
       2015-09-01 10:13:47 +08:00
    @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  
       2015-09-01 17:53:03 +08:00
    @jimzhong 嗯,对。。。
    cheng007
        13
    cheng007  
       2015-09-01 22:55:13 +08:00
    一个函数只有一个返回值呀,你要打印出来,倒序,你需要在递归函数里面去打印,而不是在在外面 cout
    cheng007
        14
    cheng007  
       2015-09-01 22:56:43 +08:00
    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;
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5065 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.