【 逆波兰表达式 】【 巧妙 】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/violinlove/article/details/81674726

巧妙题啊!!!

看完代码,你就秒懂了

分成两拨 处理 ,不断 递归 即可

atof 可以将 字符串 转化成 double 型 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

inline int wread(){
    char c(getchar ());int wans (0),flag(1);
    while (c<'0' ||c>'9'){if (c=='-') flag=-1; c=getchar ();}
    while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
    return wans*=flag;
} 

char s[100005];

double dfs (){
	scanf ("%s",s);
	if (s[0]=='*')	return dfs()*dfs();
	else if (s[0]=='-')	return dfs()-dfs();
	else if (s[0]=='/')	return dfs()/dfs();
	else if (s[0]=='+')	return dfs()+dfs();
	else return atof(s);
}

int main(){
	
	printf("%f\n",dfs ());
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/violinlove/article/details/81674726