-1

This is my attempting to fix the coefficient of any math equation variable for sum coefficients of the same variables like "x^2+x^2+2x-x-25" to be "+1x^2+1x^2+2x-1x-25" and then make the summation to be "2x^2+x-25", Notice that i have done the summation process in another method.

private static String fixCoeff(String equ)
{
    equ=equ.toLowerCase();//change equation variables to lower case
    equ=equ.trim();//remove white spaces
    String []characters={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    int index=-1;
    String fixedCoeff="";
    for (int i = 0; i < characters.length; i++)
    {
        if(!equ.contains(characters[i]))
        {
            continue;
        }
        //if a not found in equ i++ 
        //if true execute this
        while(equ.indexOf(characters[i],++index)!=-1)
        {


            index=equ.indexOf(characters[i]);
            if(index==0)
            {
                fixedCoeff+="+1"+equ;
                equ=fixedCoeff;
                index=2;
                break;

            }
            else
            {

                if (equ.charAt(index-1)=='+'||equ.charAt(index-1)=='-')
                {
                    fixedCoeff=equ.substring(-1,index-1);
                    fixedCoeff+="1"+equ.substring(index-1,equ.length()-1);
                    equ=fixedCoeff;
                    index++;
                    break;
                }

            }

        //  if (index==equ.length()-1) {//if we found last element in equ is a variable
                //break;
            //}
        }//end while

    }//end for loop 


    return equ;

}//end fixCoeff

input cases :

  1. a
  2. x
  3. x^2
  4. x^2+x^2

output cases :

  1. +1a
  2. +1x
  3. +1x^2
  4. +1x^2+1x^2
Hossam Hassan
  • 665
  • 1
  • 8
  • 22

2 Answers2

2

just to add up to @brso05 answer,

it is another way of doing this:

    String s ="x^2+x^2+2x-x-25";
    s=s.replaceAll("(?<!\\d)([a-z])", "1$1");    // <-- replaces any lower letters with 1 concatanted with the same letter
    if(!s.startsWith("-")){
        s="+"+s;  //<-- if the first character is not negative add a + to it.
    }
    System.out.println(s);

output:

+1x^2+1x^2+2x-1x-25
nafas
  • 5,283
  • 3
  • 29
  • 57
1

You can do this much easier using String.replaceAll() like this:

for (int i = 0; i < characters.length; i++)
{
    if(!equ.contains(characters[i]))
    {
        continue;
    }
    equ = equ.replaceAll(("(?<!\\d)" + characters[i]), ("1" + characters[i]));
}
if(!equ.startsWith("-") && !equ.startsWith("+"))
{
    equ = "+" + equ;
}

This will replace the character where there is no digit in front of it with 1x(or whatever the current character is). This uses regex with a negative lookbehind to make sure there are no digits in front of the character then it will replace with 1(character).

Here is a self-contained example:

String line = "x-x^2+3x^3";
line = line.replaceAll("(?<!\\d)x", "1x");
System.out.println("" + line);

This will output 1x-1x^2+3x^3.

brso05
  • 13,142
  • 2
  • 21
  • 40