infix expression 计算完全版
#include<iostream>
#include<stack>
#include<string>
using namespace std; char compare(char tp, char op)
{
if (((tp == '+' || tp == '-') && (op == '*' || op == '/')) || (tp == '#'))
return '<';
else if (tp == '('&&op != ')')
return '<';
else if ((tp == '*' || tp == '/'||tp=='+'||tp=='-') && (op == '('))
return '<';
return '>';
} int compute(int n1, int n2, char op)
{
if (op == '+')
return n1 + n2;
else if (op == '-')
return n1 - n2;
else if (op == '*')
return n1*n2;
else if (op == '/')
return n2 / n1;
} int main()
{
stack<char>num;
stack<char>oper; oper.push('#'); string s;
cin >> s; for (int i = 0; i<s.length(); i++)
{
if (s[i] == '0' || s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '4' || s[i] == '5' || s[i] == '6' || s[i] == '7' || s[i] == '8' || s[i] == '9')
num.push(s[i]);
else
{
char comp = compare(oper.top(), s[i]);
if (comp == '<')
oper.push(s[i]);
else if (comp == '>')
{
if (s[i] == ')')
{
if (num.size() != 1)
{
while (oper.top() != '(')
{
int num1 = num.top() - '0';
num.pop();
int num2 = num.top() - '0';
num.pop();
int result = compute(num1, num2, oper.top());
num.push(result + '0');
oper.pop();
}
}
oper.pop();
}
else
{
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
char result = compute(num1, num2, oper.top());
num.push(result);
oper.pop();
oper.push(s[i]);
}
}
}
} if (num.size() != 1)
{
while (oper.top() != '#')
{
int num1 = num.top()-'0';
num.pop();
int num2 = num.top()-'0';
num.pop();
int result = compute(num1, num2, oper.top());
num.push(result+'0');
oper.pop();
}
} cout << num.top() << endl; return 0;
}
infix expression 计算完全版的更多相关文章
- Infix expression 计算 without '(' and ')'
#include<iostream> #include<stack> #include<string> using namespace std; char comp ...
- Python科学计算发行版—Anaconda
Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些模块可能又依赖 ...
- 目前比较流行的Python科学计算发行版
经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...
- PAT1130:Infix Expression
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- A1130. Infix Expression
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT 甲级 1130 Infix Expression
https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- 复利计算--web版--总结--软件工程
复利计算项目 估计用时 实际用时 时间(小时) 5.5小时 6.5小时 总共代码行 500 550 功能包含 单利/复利计算,本金计算,求投资年限,求投资项目利率估计 (计算利息和,计算时间,计算 ...
随机推荐
- 根据相同的字段创建或者修改一个Model
public static class ObjectUtils { /// <summary> /// 根据source创建一个强类型的Object,并根据相同属性名进行赋值. /// & ...
- nohup及/dev/null使用
1.nohup ----后台执行,执行记录默认输出到当前目录下的nohup.out文件 nohup find /etc -name passwd 2./dev/null介绍 把/ ...
- spring+springmvc+mybatis整合框架搭建
由于例子是基于Maven搭建的,所以首先是pom.xml文件的依赖信息: <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- 爬虫代码实现五:解析所有分页url并优化解析实现类
如图,我们进入优酷首页,可以看到电视剧列表,我们称这个页面为电视剧列表页,而点击进入某个电视剧,则称为电视剧详情页.那么如何获取所有分页以及对应的详情页呢,通过下面的分页得到. 因此,首先,我们将St ...
- urllib模块 | Python 2.7.11
官方文档: https://docs.python.org/2/library/urllib.html 某博客对官方文档较全的翻译: http://h2byte.com/post/tech/relat ...
- NGUI中的Tween的委托使用
public TweenPosition tweenIn; public TweenScale tweenOut; EventDelegate In = new EventDelegate(this, ...
- jquery+ajax 实现text框模糊搜索并可利用listbox实时显示模糊搜索列表结果
功能描述: text框中输入,text框下面的listbox中实时显示依据输入的内容进行模糊搜索的结果 js代码 $j(function() { $j("input[id='txtCos'] ...
- 页面加载与iframe加载函数
<head> <script> $(document).ready(function(){ alert("a"); var wait = documen ...
- Openjudge-NOI题库-垂直直方图
题目描述 Description 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...
- 最受欢迎的iOS第三方SDK
http://www.raywenderlich.com/forums/viewtopic.php?t=4496