zmaibbs7
V2EX  ›  C

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

  •  
  •   zmaibbs7 · Aug 31, 2015 · 1889 views
    This topic created in 4082 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
    zmaibbs7
    OP
    Aug 31, 2015
    1
    我写的是这样: 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
    sandideas
    Aug 31, 2015 via iPhone❤️1
    2
    你理解错了吧。。
    + 36 25
    相当于
    +
    36
    25
    三次 cin
    sandideas
    sandideas
    Aug 31, 2015 via iPhone
    3
    str[0]就是判断是不是符号。如果不是,就说明是数字,所以整个 str 转换成数字
    jedihy
    jedihy
    Sep 1, 2015
    4
    因为它本来就没有指向下一字节。。,只是每次递归你都在输入。。
    linux40
    linux40
    Sep 1, 2015 via Android
    5
    没有 break
    linux40
    linux40
    Sep 1, 2015 via Android
    6
    @linux40 我理解错你的意思了, 36 和 25 要读两次,因为有空格。。。
    jimzhong
    jimzhong
    Sep 1, 2015
    7
    @linux40 return 了不需要 break
    但是你每个 f 里面都在读入字符串,感觉怪怪的。
    zmj1316
    zmj1316
    Sep 1, 2015
    8
    都用了 devcpp 了 跟踪一下不就知道了?
    ljbha007
    ljbha007
    Sep 1, 2015
    9
    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
    zmaibbs7
    OP
    Sep 1, 2015
    10
    @sandideas 懂了 非常感谢!
    zmaibbs7
    zmaibbs7
    OP
    Sep 1, 2015
    11
    @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
    linux40
    Sep 1, 2015
    12
    @jimzhong 嗯,对。。。
    cheng007
    cheng007
    Sep 1, 2015
    13
    一个函数只有一个返回值呀,你要打印出来,倒序,你需要在递归函数里面去打印,而不是在在外面 cout
    cheng007
    cheng007
    Sep 1, 2015
    14
    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   ·   Privacy   ·   Solana   ·   2951 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 12:44 · PVG 20:44 · LAX 04:44 · JFK 07:44
    ♥ Do have faith in what you're doing.