如何通过不同操作计算包含整数的字符串的总和 (+ - * /) java
How to calculate the sum of string that contains integer with different operations (+ - * /) java
我有一个字符串包含操作 (+,-) 和其他操作 (+, -, *, /)
链条如下:
String myString = "-18+65-14+78"
=> 111
String myString3 = "-69*18+14-22*-75"
=> 422
我用这个代码拆分
public class Test2 {
public static void main(String[] args) {
String myString = "18+65-14+78";
String[] str = myString.split("[-\+]");
int s = 0;
for (int i = 0; i < str.length; i++) {
s += Double.parseDouble(str[i]);
System.out.println(str[i]);
}
System.out.println("the sum is : " + s);
}
}
当我在链的开头使用 - 时,错误是 empty String
如何解决(+和-)运算符和另一个运算符(*)的问题?
如果您只想求解 (+,-),这很简单。只需拆分数字并添加或减去它们。源代码应该是这样的:
import java.io.*;
import java.util.*;
public class Main {
public static void main (String[] args) {
String str = "-18+65-14+78";
String temp = "0";
double sum = 0.0;
char prev = '+';
for(char ch: str.toCharArray()) {
if(ch == '+' || ch == '-') {
if(prev == '+') sum = sum + Double.parseDouble(temp);
else sum = sum - Double.parseDouble(temp);
prev = ch;
temp = "0";
}
else temp = temp + ch;
}
if(prev == '+') sum = sum + Double.parseDouble(temp);
else sum = sum - Double.parseDouble(temp);
System.out.println(sum);
}
}
现在,如果您必须为 (+,-,*,/) 执行此操作,则有点复杂。好吧,让我们看看我是否可以为您分解它。我希望您之前对 Postfix Notation 有过介绍。如果你还没有,你可以在这里看到 Reverse Polish notation or Postfix Notation.
In a simple description:
(1) Infix: A + B
Postfix: A B +
(2) Infix: A + B * C
Postfix: A B C * +
如您所知,(*,/) 的优先级高于(+,-),因此我们无法像计算(+,-) 那样计算它。
Let us take an equation A+B*C-D*Q
which is actually A+(B*C)-(D*Q)
因此,我们必须以一种没有括号的方式有序地表示它,这样我们才能正确计算它。
Let's observe the part A+B*C i.e. A+(B*C)
If we represent it in Postfix, it would be like: A B C * +
Now, the algorithm to calculate it
(1) Take a stack
(2) When we see a number, we push it to stack
(3) When we see a operator, we pop two numbers out of stack and calculate them with help of operator and push the result into stack again
(4) We do it till the end
(5) At last, only a number would be left in stack, that is our answer.
Let's visualise it:
(1) [A]
(2) [A, B]
(3) [A, B, C]
(4) [A, R1] where R1 = B*C
(5) [R2] where R2 = A+R1
现在,主要问题是后缀 A B C * + D Q * -
中的 A+(BxC)-(DxQ)
我希望,你可以自己做,并创建一个算法来解决问题。您将获得大量用于将中缀符号转换为后缀符号的源代码,或者您可以自己编写一个。
我有一个字符串包含操作 (+,-) 和其他操作 (+, -, *, /)
链条如下:
String myString = "-18+65-14+78"
=> 111
String myString3 = "-69*18+14-22*-75"
=> 422
我用这个代码拆分
public class Test2 {
public static void main(String[] args) {
String myString = "18+65-14+78";
String[] str = myString.split("[-\+]");
int s = 0;
for (int i = 0; i < str.length; i++) {
s += Double.parseDouble(str[i]);
System.out.println(str[i]);
}
System.out.println("the sum is : " + s);
}
}
当我在链的开头使用 - 时,错误是 empty String
如何解决(+和-)运算符和另一个运算符(*)的问题?
如果您只想求解 (+,-),这很简单。只需拆分数字并添加或减去它们。源代码应该是这样的:
import java.io.*;
import java.util.*;
public class Main {
public static void main (String[] args) {
String str = "-18+65-14+78";
String temp = "0";
double sum = 0.0;
char prev = '+';
for(char ch: str.toCharArray()) {
if(ch == '+' || ch == '-') {
if(prev == '+') sum = sum + Double.parseDouble(temp);
else sum = sum - Double.parseDouble(temp);
prev = ch;
temp = "0";
}
else temp = temp + ch;
}
if(prev == '+') sum = sum + Double.parseDouble(temp);
else sum = sum - Double.parseDouble(temp);
System.out.println(sum);
}
}
现在,如果您必须为 (+,-,*,/) 执行此操作,则有点复杂。好吧,让我们看看我是否可以为您分解它。我希望您之前对 Postfix Notation 有过介绍。如果你还没有,你可以在这里看到 Reverse Polish notation or Postfix Notation.
In a simple description:
(1) Infix: A + B
Postfix: A B +
(2) Infix: A + B * C
Postfix: A B C * +
如您所知,(*,/) 的优先级高于(+,-),因此我们无法像计算(+,-) 那样计算它。
Let us take an equation A+B*C-D*Q
which is actually A+(B*C)-(D*Q)
因此,我们必须以一种没有括号的方式有序地表示它,这样我们才能正确计算它。
Let's observe the part A+B*C i.e. A+(B*C)
If we represent it in Postfix, it would be like: A B C * +
Now, the algorithm to calculate it
(1) Take a stack
(2) When we see a number, we push it to stack
(3) When we see a operator, we pop two numbers out of stack and calculate them with help of operator and push the result into stack again
(4) We do it till the end
(5) At last, only a number would be left in stack, that is our answer.
Let's visualise it:
(1) [A]
(2) [A, B]
(3) [A, B, C]
(4) [A, R1] where R1 = B*C
(5) [R2] where R2 = A+R1
现在,主要问题是后缀 A B C * + D Q * -
中的 A+(BxC)-(DxQ)我希望,你可以自己做,并创建一个算法来解决问题。您将获得大量用于将中缀符号转换为后缀符号的源代码,或者您可以自己编写一个。