#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. Ubuntu中Qt5.7.0无法输入中文

    把libfcitxplatforminputcontextplugin.so复制到安装的Qt目录下的两个文件夹中 sudo apt install fcitx-frontend-qt5 sudo cp ...

  2. Python基础知识学习_Day5

    一.生成器和迭代器 1.列表生成 >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a = map(lambda x:x+1, a ...

  3. string,stringbuilder和stringbuffer的区别

    String.StringBuffer.StringBuilder之间的区别(网上收集) StringBuffer.StringBuilder和String一样,也用来代表字符串.String类是不可 ...

  4. CODE[VS]-最小数和最大数-整数处理-天梯青铜

    题目描述 Description 输入n个数,n<=100,找到其中最小的数和最大的数 输入描述 Input Description 第一行一个整数n 接下来一行n个整数,每个整数不超过231 ...

  5. js中setTimeout()的使用

    setTimeout()在js类中的使用方法   setTimeout (表达式,延时时间)setTimeout(表达式,交互时间)延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTi ...

  6. Tiny6410之重定位代码到SRAM+4096

    重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...

  7. 从键盘或文件中获取标准输入:read命令

    文件的描述符和重定向 文件描述符是和文件的输入.输出相关联的非负整数,Linux内核(kernel)利用文件描述符(file descriptor)来访问文件.打开现存文件或新建文件时,内核会返回一个 ...

  8. mysql 数据库知识

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

  9. 12.04 css小测div+css...

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. MPMoviePlayerController

    属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...