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

2015-08-31 23:15:00 +08:00
 zmaibbs7

这个是老师的代码,解决的是逆波兰表达式。
我想问的是
代码如下,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]不会自动指向下一个字符。。。
求大神指点!!!!!!!!!

1018 次点击
所在节点    C
14 条回复
zmaibbs7
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
2015-08-31 23:22:52 +08:00
你理解错了吧。。
+ 36 25
相当于
+
36
25
三次 cin
sandideas
2015-08-31 23:23:53 +08:00
str[0]就是判断是不是符号。如果不是,就说明是数字,所以整个 str 转换成数字
jedihy
2015-09-01 02:54:23 +08:00
因为它本来就没有指向下一字节。。,只是每次递归你都在输入。。
linux40
2015-09-01 07:34:09 +08:00
没有 break
linux40
2015-09-01 07:35:18 +08:00
@linux40 我理解错你的意思了, 36 和 25 要读两次,因为有空格。。。
jimzhong
2015-09-01 07:54:05 +08:00
@linux40 return 了不需要 break
但是你每个 f 里面都在读入字符串,感觉怪怪的。
zmj1316
2015-09-01 08:18:00 +08:00
都用了 devcpp 了 跟踪一下不就知道了?
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
2015-09-01 10:12:13 +08:00
@sandideas 懂了 非常感谢!
zmaibbs7
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
2015-09-01 17:53:03 +08:00
@jimzhong 嗯,对。。。
cheng007
2015-09-01 22:55:13 +08:00
一个函数只有一个返回值呀,你要打印出来,倒序,你需要在递归函数里面去打印,而不是在在外面 cout
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;
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/217443

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX