1

I have a string containing the operation (+,-) and other with (+, -, *, /)

The chain is as follows:

String myString = "-18+65-14+78" => 111

String myString3 = "-69*18+14-22*-75" => 422

i used the split by this code

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);
 }
}

When i used - at the beginning of the chain an error is empty String

How to solve the problem with (+ and -) operators and with the other operator (*)?

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
wajihov
  • 23
  • 2

1 Answers1

1

If you want the solve for just (+,-), it's easy. Just split the numbers and add or sub them. A source code would be like this:

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);
    }
}

Now, if you have to do this for (+,-,*,/), then its a bit complicated. Well, let's see if I can break it down for you. I hope you have a previous introduction with Postfix Notation. If you have not, you can see here 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 * +

As you know (*,/) has higher priority than (+,-), so we can't calculate it as we've for (+,-).

Let us take an equation A+B*C-D*Q
which is actually A+(B*C)-(D*Q)

So, we have to represent it in a way without the parenthesis and in an orderly fashion, so that we may calculate it properly.

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

Now, the main problem was A+(BxC)-(DxQ) which in Postfix A B C * + D Q * -

I hope, you could do it by yourself and create an algorithm to solve the problem. You'll get a lot of source code out there for converting infix to postfix notation, or you may write one yourself.

reyad
  • 1,392
  • 2
  • 7
  • 12