About
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class reggix {
public static String derive(String formula)
{
String regex = "\\((.+)\\)\\^(\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(formula);
if(m.find())
{
String base = m.group(1);
double exponent = Double.parseDouble(m.group(2));
return exponent+ "("+base+")^"+(exponent - 1) + "(" + derive(base)+")";
}
return "d(" + formula + ")/dx";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(derive("(x+5)^5"));
}
}