#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 计算完全版的更多相关文章

  1. Infix expression 计算 without '(' and ')'

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

  2. Python科学计算发行版—Anaconda

    Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些模块可能又依赖 ...

  3. 目前比较流行的Python科学计算发行版

    经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...

  4. PAT1130:Infix Expression

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

  5. A1130. Infix Expression

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

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

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

  7. PAT 甲级 1130 Infix Expression

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

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

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

  9. 复利计算--web版--总结--软件工程

    复利计算项目 估计用时 实际用时 时间(小时) 5.5小时  6.5小时 总共代码行 500  550 功能包含 单利/复利计算,本金计算,求投资年限,求投资项目利率估计 (计算利息和,计算时间,计算 ...

随机推荐

  1. 时间序列 预测分析 R语言

    在对短期数据的预测分析中,我们经常用到时间序列中的指数平滑做数据预测,然后根据不同. 下面我们来看下具体的过程 x<-data.frame(rq=seq(as.Date('2016-11-15' ...

  2. JavaEE XML DOM解析

    DOM解析XML @author ixenos XML解析方式(原理) a)     DOM 解析树 b)     SAX  流事件 DOM解析对应主流工具 i.          DOM(官方) i ...

  3. spring boot maven 插件

    spirng boot 需要使用专用maven插件打包,才能打包.引入如下. <build>        <plugins>            <plugin> ...

  4. php dday1... web服务器的搭建 数据库的安装....

  5. niceScroll接口大全

    Query滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollIn ...

  6. C#下在图片文件本地

    //C#下载图片文件到本地,c#,c#下载,下载图片,下载文件,下载函数// 从图片地址下载图片到本地磁盘// 将二进制文件保存到磁盘 using System;using System.Drawin ...

  7. [NEUQ-OJ] 1012 SZ斐波拉契数列

    一道水题,让我看清基础我的基础是多么薄弱. 递归,数组清零,数组名/变量名重复层出不穷...路漫漫啊.......... http://ncc.neuq.edu.cn/oj/problem.php?i ...

  8. mysql 数据库知识

    order by 字段    将查到的list集合按指定字段升序排序 order by 字段 DESC   将查到的list集合按指定字段降序排序 GROUP BY 语句用于结合合计函数,根据一个或多 ...

  9. 容易忘记的几个Linux命令

    #查看文件或者目录的属性ls -ld filenamels -ld directory #vi编辑器输入:.,$d #清除全部内容 #修改管理员.用户密码passwd user #("use ...

  10. Django ORM操作

    ORM 常用操作进阶操作 #!/usr/bin/env python #_*_ coding:utf8 _*_ from __future__ import unicode_literals from ...