肯德鸭


私信TA

用户名:dotcpp0673338

访问量:236

签 名:

等  级
排  名 7520
经  验 1255
参赛次数 0
文章发表 3
年  龄 23
在职情况 学生
学  校 渤海大学
专  业 软件工程

  自我简介:

既然选择远方,便只顾风雨兼程

TA的其他文章

解题思路:

逆波兰表达式的第一个字符一定是运算符,判断输入的字符串第一个字符是否为运算符,是则返回两个子逆波兰表达式相运算的结果,如不是则返回数值


注意事项:

  1. 不能使用gets读入字符串

  2. atof是将字符串转换为浮点数的函数 ,在头文件stdlib.h中 

  3. 本题还可利用栈的思想


参考代码:

#include #include double getResult() {
	char a[20];
	scanf("%s", a);
	switch(a[0]) {
		case '*':
			return getResult() * getResult();
		case '/':
			return getResult() / getResult();
		case '+':
			return getResult() + getResult();
		case '-':
			return getResult() - getResult();
		default:
			return atof(a);
	}
}

int main() {
	printf("%lf", getResult());
	return 0;
}


 

0.0分

3 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区