#include<iostream>
#include<stack>
#include<string>
using namespace std; char compare(char tp, char op)
{
if (((tp == '+' || tp == '-') && (op == '*' || op == '/')) || (tp == '#'))
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 == '>')
{
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.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 计算 without '(' and ')'的更多相关文章

  1. infix expression 计算完全版

    #include<iostream> #include<stack> #include<string> using namespace std; char comp ...

  2. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  3. A1130. Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  4. PAT A1130 Infix Expression (25 分)——中序遍历

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  5. PAT 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  6. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  7. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  8. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  9. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

随机推荐

  1. Fatal error: Call to undefined function oci_connect()

    http://stackoverflow.com/questions/22478387/call-to-undefined-function-oci-connect Whenever you conn ...

  2. [Q]自定义快捷键

    打开CAD批量打图精灵主界面可以使用以下三个命令其一:“QuickPlot”.“QPlot”.“QP”.“PP”,其中“PP”可以更改, 方法如下:进入AutoCAD传统界面,点“工具”-“自定义”- ...

  3. 仿QQ空间视差效果,ListView.setHeader( )

    根据listview的手指移动事件,动态设置listview上面的图片的宽高,并在手指放开的时候 实现图片的动画(随时间变化的动画值) ValueAnimator.ofInt ( ) import a ...

  4. Python快捷键

    IDLE默认不能显示行号,使用ALT+G 跳到对应行号,在右下角有显示光标所在行.列. ALT+P  上一个历史输入内容. ALT+N 下一个历史输入内容. IDLE中按F5可以运行代码.

  5. linux标准输入输出2>&1

    linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1,2.     STDIN是标准输入,默认从键盘读取信息:STDOUT是标准输出,默认将输出结果输出至终 ...

  6. Python基础知识学习_Day6

    一.time&datetime模块 常用选项如下: import time print(time.asctime()) #返回时间格式 print(time.localtime())#返回本地 ...

  7. 利用before、after制作提示框

    提示框由两部分组成,框+箭头,箭头则利用伪元素before.after写成. 根据提示框的样式可以看出,上面的箭头由两部分组成:灰色大箭头+蓝色小箭头,蓝色嵌套在灰色里面,于是箭头就有了边框,整体搭配 ...

  8. C#在客户端验证数字证书(Certificate)

    ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallback;//Init时执行,用于 ...

  9. [SOJ] 导游

    Description Mr. G. 在孟加拉国的一家旅游公司工作.他当前的任务是带一些游客去一些遥远的城市.和所有国家一样,一些城市之间有双向道路.每对相邻城市之间都有一条公交路线,每条路线都规定了 ...

  10. 推荐几个好的 Maven 常用仓库网址

    注意,以下内容转载自:推荐几个好的 Maven 常用仓库网址 Maven 确确实实是个好东西,用来管理项目显得很方便,但是如果是通过 Maven 来远程下载 JAR 包的话,我宿舍的带宽是4兆的,4个 ...