0

There is String a input from user. It's a polynom. How can I derive this polinom without using array?

ınput= 3x^4+5x^45-2+77x^100

output must be 12x^3 + 225x^44 + 7700x^99

How can i know that how many x statements are there in the polynom?

my code is :

String katsayi = polinom.substring(0, polinom.indexOf("x"));
String us = polinom.substring(a + 1);
int katSayi = Integer.parseInt(katsayi);
int uS = Integer.parseInt(us);
katSayi = katSayi * uS;
uS = uS - 1;
katsayi = Integer.toString(katSayi);
us = Integer.toString(uS);
yeniPolinom = katsayi + "x^" + us;
System.out.println(yeniPolinom);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Are you trying to get the derivative? – brso05 Feb 13 '15 at 21:01
  • Yes but i'll take the innput String from user. @brso05 –  Feb 13 '15 at 21:03
  • Give me a little bit and I will try to put together an answer for you... – brso05 Feb 13 '15 at 21:08
  • User will write the input. So the input can have many x statements. We don't know. I'm trying to get derivate of this input. if it is 3x^2+2x^33, output will be 6x+66x @brso05 –  Feb 13 '15 at 21:12
  • Will all parts be written in form `ax^n` or is it possible for input to be written like `x^2` instead of `1x^2`, or not to have `x` part like `1` instead of `1x^0`? – Pshemo Feb 13 '15 at 21:21
  • Sorry I didn't realize you didn't want to use an array... – brso05 Feb 13 '15 at 21:21
  • It can change but i have no problem with that. My problem is that i don't know how many x statements user will write. I cant use arrays. @Pshemo –  Feb 13 '15 at 21:42

3 Answers3

0

Parsing a String that looks like a polynom can be quite tedious because there are hundreds of ways of writing down the same polynom. However, if we stick to the format ax^n, you can take a look at this answer to extract the coefficients of the polynom : https://stackoverflow.com/a/13415745/1743880.

Consider making a Polynom class with a derivative method for this.

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

How about using regex to find parts in format [number1]x^[number2] then replacing this parts with [num1]*[num2]x^[num2 - 1]? Here is example which dynamically creates replacement part based on what found values.

String input = "3x^4+5x^45-2+77x^100";

Pattern p = Pattern.compile("(\\d+)x\\^(\\d+)");
//                            ^^^^      ^^^^
//                           group1    group2
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    int a = Integer.parseInt(m.group(1));
    int n = Integer.parseInt(m.group(2));
    if (n != 0)
        m.appendReplacement(sb, (a * n) + "x^" + (n - 1));
    else
        m.appendReplacement(sb, "0");
}
m.appendTail(sb);
String output = sb.toString();
System.out.println(output);

Output: 12x^3+225x^44-2+7700x^99

Notice that this solution is very limited. It assumes that all parts will be written in ax^n form, so it will not work correctly for data like x^2 instead of 1x^2, or -1 instead of -1x^0. I also assume that n will not be negative number.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0
 public static void main(String[] args) throws IOException {
        String polynomial = "3x^4+5x^45-2+77x^100";
        int lastNumber = 0;
        int temp = 0;
        String output = "";
        for(int i = 0; i < polynomial.length() - 1; i++)
        {
            if(polynomial.charAt(i) == 'x')
            {
                int counter = i + 1;
                int a = 0;
                int b = 0;
                String tempString1 = "";
                String tempString2 = "";
                String number = "";
                while((polynomial.charAt(counter) != '0') && (polynomial.charAt(counter) != '1') && (polynomial.charAt(counter) != '2') && (polynomial.charAt(counter) != '3') && (polynomial.charAt(counter) != '4') && (polynomial.charAt(counter) != '5') && (polynomial.charAt(counter) != '6') && (polynomial.charAt(counter) != '7') && (polynomial.charAt(counter) != '8') && (polynomial.charAt(counter) != '9'))
                {
                    tempString1 += polynomial.charAt(counter);
                    counter++;
                }
                while((counter < polynomial.length()) && ((polynomial.charAt(counter) == '0') || (polynomial.charAt(counter) == '1') || (polynomial.charAt(counter) == '2') || (polynomial.charAt(counter) == '3') || (polynomial.charAt(counter) == '4') || (polynomial.charAt(counter) == '5') || (polynomial.charAt(counter) == '6') || (polynomial.charAt(counter) == '7') || (polynomial.charAt(counter) == '8') || (polynomial.charAt(counter) == '9')))
                {
                    number += polynomial.charAt(counter);
                    counter++;
                }
                a = Integer.parseInt(number);
                temp = counter - 1;
                counter = i - 1;
                number = "";
                while((polynomial.charAt(counter) != '0') && (polynomial.charAt(counter) != '1') && (polynomial.charAt(counter) != '2') && (polynomial.charAt(counter) != '3') && (polynomial.charAt(counter) != '4') && (polynomial.charAt(counter) != '5') && (polynomial.charAt(counter) != '6') && (polynomial.charAt(counter) != '7') && (polynomial.charAt(counter) != '8') && (polynomial.charAt(counter) != '9'))
                {
                    tempString2 = polynomial.charAt(counter) + tempString2;
                    counter--;
                }
                while((counter >= 0) && ((polynomial.charAt(counter) == '0') || (polynomial.charAt(counter) == '1') || (polynomial.charAt(counter) == '2') || (polynomial.charAt(counter) == '3') || (polynomial.charAt(counter) == '4') || (polynomial.charAt(counter) == '5') || (polynomial.charAt(counter) == '6') || (polynomial.charAt(counter) == '7') || (polynomial.charAt(counter) == '8') || (polynomial.charAt(counter) == '9')))
                {
                    number = polynomial.charAt(counter) + number;
                    counter--;
                }
                b = Integer.parseInt(number);
                for(int j = lastNumber; j <= counter; j++)
                {
                    output += polynomial.charAt(j);
                }
                output += "" + (a * b) + "x^" + (a - 1);
                lastNumber = temp + 1;
                i = temp;
            }
        }
        System.out.println(output);
    }

Output:

12x^3+225x^44-2+7700x^99
brso05
  • 13,142
  • 2
  • 21
  • 40