#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. udp接收

    char receive_buffer[500] = {0}; std::vector<std::string> mysplit(std::string str,std::string p ...

  2. java自动装箱的陷阱

    自动装箱和拆箱是java的一颗语法糖,在给我们带来使用便利的同时也带来一些疑惑,请看下面的代码: public class TestClass { public static void main(St ...

  3. LeetCode 213. House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  4. Ubuntu14.04安装PHP5

    因为任务需要在Ubuntu14.04的server版本下安装PHP5,所以总结一下   使用root进行安装  要么在前面加上sudo进行安装. apt-get install php5-cgi ap ...

  5. CODE[VS]-寻找子串位置-字符串处理-天梯青铜

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...

  6. Asp.net简单代码设置GridView自适应列宽不变形

    动态绑定的GridView由于列数不固定,而列又太多,是要自定设置gridView的宽度 //在GridView的行数据绑定完的事件中设置 protected void gvObjectList_Ro ...

  7. NEUQ1051: 谭浩强C语言(第三版)习题6.7

    //C代码简直难看到家,求大神知道如何写出复用性好的,维护性强的代码... //格式错误了好几次,最后发现是are和数字之间多了个空格......本来一直以为是最后的换行多了,费劲搞掉了. #incl ...

  8. js检测对象中是否存在某个属性

    1.使用in关键字.该方法可以判断对象的自有属性和继承来的属性是否存在. 2.使用对象的hasOwnProperty()方法.该方法只能判断自有属性是否存在,对于继承属性会返回false. 3.用un ...

  9. open("","r")与open("","rb")的效率区别

    fd = open("C:\Users\william\Desktop\dup_file - Copy (3).txt","r")for i in fd: pr ...

  10. 阿里云-对象储存OSS

    最近 需要搞一个阿里云的文件上传 手机端主要做了图片上传 pod安装:pod 'AliyunOSSiOS', '~> 2.5.2' 主要就是需要四个参数:accessKey secretKey  ...