当前位置:网站首页>Calculate the result of a string formula
Calculate the result of a string formula
2022-06-09 12:05:00 【N. LAWLIET】
problem
Given a string str,str Represents a formula
There may be integers in the formula , Add, subtract, multiply and divide , Left and right parentheses and other symbols
Returns the formula calculation result , The difficulty is that parentheses can have many layers ?
Example
str= 48*((70-65)-43)+8*1 Return results -1816
thought
Use the stack to add elements to the stack , The start of the stack starts with the left parenthesis , Stop again , Do not include parentheses , When there is no bracket, the end of the left bracket is encountered , Then the inner use in brackets creates a new stack , And give priority to .
Code
public class Code01_ExpressionCompute {
public static int computenum(String str) {
return process(str.toCharArray(),0)[0];
}
private static int[] process(char[] charArray, int i) {
LinkedList<String> que = new LinkedList<String>();
int num = 0;
int[] bar = null;
while(i<charArray.length&&charArray[i]!=')') {
if(charArray[i]>='0'&&charArray[i]<='9') {
num = num*10 +charArray[i++]-'0';
}else if(charArray[i]!='(') {
addnum(que,num);
que.addLast(String.valueOf(charArray[i++]));
num = 0;
}else {
bar = process(charArray, i+1);
num = bar[0];
i = bar[1]+1;
}
}
addnum(que, num);
return new int[] {
getnum(que),i};
}
private static void addnum(LinkedList<String> que, int num) {
if(!que.isEmpty()) {
String top = que.pollLast();
int cur = 0;
if(top.equals("+")||top.equals("-")) {
que.addLast(top);
}else {
cur = Integer.valueOf(que.pollLast());
num = top.equals("*")?2cur*num:(cur/num);
}
}
que.addLast(String.valueOf(num));
}
private static int getnum(LinkedList<String> que) {
int res = 0;
int num = 0;
String cur = null;
boolean add = true;
while(!que.isEmpty()) {
cur = que.pollFirst();
if(cur.equals("+")) add = true;
else if(cur.equals("-")) add = false;
else {
num = Integer.valueOf(cur);
res += add? num:(-num);
}
}
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str = scanner.nextLine();
int ans = computenum(str);
System.out.println(ans);
}
}
}
边栏推荐
- Win10 20h2 was officially released, and the list of old and new features was compared
- 5.括号生成
- tag回溯-刷题预备知识-1. 回溯法模板, + lt.46. 全排列
- 使用U盘一比一拷贝核心板系统镜像的方法
- Redis数据结构与介绍
- Do you dare to deliver your microservices independently?
- Open IPv6 access on the website to test whether it supports IPv6
- Iphone5s display disabled solution
- 【无标题】
- 还有新产品?丰田GR系列或将扩充产品线
猜你喜欢

12. < tag binary tree and BST foundation > lt.701 Insert operation DBC in binary search tree

tag贪心-刷题预备知识-贪心解题方法 + lt.455. 分发饼干 + lt.376. 摆动序列

How to solve the existing 1px problem?

7.<tag-回溯和子集问题>lt.70.子集 + lt.90.子集 II
![[patch analysis] cve-2016-8610: patch analysis of](/img/cb/77578a38c2c907c6ee83ac0f70df88.png)
[patch analysis] cve-2016-8610: patch analysis of "SSL death alert" vulnerability leading to denial of service

07 | 中台落地第二步:企业数字化全景规划(Define)

Win11正式发布新功能

UDP可靠性实践

15 Win32 class library name in WMI

09 | the fourth step: the construction and delivery of the middle office
随机推荐
还有新产品?丰田GR系列或将扩充产品线
04 | everything must be done in advance: four issues that must be clearly considered before the construction of China Taiwan Relations
Learning notes of segmentation, paging, page table and quick table
2. < tag backtracking, combination and pruning > lt.216 Total number of combinations|||
10.<tag-二叉树和BST基础>lt.700. 二叉搜索树中的搜索 + lt.98. 验证二叉搜索树 + lt.530. 二叉搜索树的最小绝对差(同lt.783)
计算字符串公式的结果
Real questions and answers of comprehensive knowledge of system integration project management engineer in the second half of 2021
Range方法返回对象转换办法
5.括号生成
H3C Certified Wireless senior engineer
What is the difference between a fire engineer and a fireman?
Several ways of traversing map
11.<tag-二叉树和BST基础>lt.501. 二叉搜索树中的众数
03 | 中台定义:当我们谈中台时到底在谈些什么?
8. < tag backtracking and full arrangement > lt.46 Full Permutation + lt.47 Full arrangement II
U8g2 graphics library and STM32 migration (I2C, software and hardware)
How to insert the video monitoring memory card into the Yuntai pro of Xiaomi smart camera
08 | 中台落地第三步:中台的规划与设计(Design)
5. < tag backtracking and cutting problems > lt.93 Restore IP address
01 | context: why is Zhongtai so popular?