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 功能包含 单利/复利计算,本金计算,求投资年限,求投资项目利率估计 (计算利息和,计算时间,计算 ...
随机推荐
- CFRound#379(div2)
题目链接:http://codeforces.com/contest/734 A:SB题. #include<cstdio> #include<cstring> #includ ...
- java 的序列化
(1) 首先是java自己内部实现的对象序列化机制 其实就是ObjectInputStream 和 ObjectOutputStream 首先实现一个实体对象 记住必须实现Serializable ...
- HTML form的一些属性(第一版)
p,li { white-space: pre-wrap } HTML表单属性总结(第一版) 基本格式为:<input type="类型" name="名字[唯一, ...
- Spark 源码解读 -- 依赖
窄依赖 所谓窄依赖就是说子RDD中的每个分区(partition)只依赖于父RDD中有限个数的partition.在API中解释如下: 窄依赖在代码中有两种具体实现,一种是一对一的依赖:OneToOn ...
- AJAX应用的五个步骤
1.建立xmlHttpRequest对象 if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); if(xmlHttp.ov ...
- ios监听ScrollView/TableView滚动的正确姿势
主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题六 最小生成树 POJ 1251 Jungle Roads
题意: 有n个点 每个点上有一些道路 求最小生成树 解释下输入格式 A n v1 w1 v2 w2 A点上有n条边 A到v1权值是w1 A到v2权值是w2 思路: 字符串处理之后跑kruskal求最小 ...
- ibaits的一个简单的完整的例子
ibaits的简单介绍: iBatis 是apache 的一个开源项目,一个O/R Mapping(对象/关系映射) 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,i ...
- java调用wsdl xfire和cxf两种方式
xfire 如下: String spID = ""; String password = ""; String accessCode = "&quo ...
- 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...