I am trying to write a simple code for polynomial multiplication. I think it is logical to take inputs as strings such as "3x^2".
Problem is I don't know what to do with the 'x's in it. How can I turn them into something that can be multiplied? What should the logic be? I am totally new in Java and I really need help.
For example:
String s = "x^2";
String s2 = "3x^5";
//(Multiply them)
result = 3x^7
public static int[] multiply(String term1, String term2)
{
int[] t1 = parse(term1);
int[] t2 = parse(term2);
int[] ret = new int[2];
ret[0] = t1[0] * t2[0];
ret[1] = t1[1] + t2[1];
return ret;
}
public static int[] parse(String term)
{
int pos = term.indexOf("x^");
String coeffStr = term.substring(0, pos);
int coeff = Integer.parseInt(coeffStr);
String powerStr = term.substring(pos + 2);
int power = Integer.parseInt(powerStr);
int[] ret = new int[2];
ret[0] = coeff;
ret[1] = power;
return ret;
}