来自用户的String输入。这是一个多项式。如何在不使用数组的情况下派生此polinom ?
ınput= 3x^4+5x^45-2+77x^100
输出必须为12x^3 + 225x^44 + 7700x^99
我怎么知道多项式中有多少个x语句?
我的代码是:
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);
答案 0 :(得分:0)
解析看起来像多项式的字符串可能非常繁琐,因为有数百种写下相同多项式的方法。但是,如果我们坚持格式ax^n
,您可以查看此答案以提取多项式的系数:https://stackoverflow.com/a/13415745/1743880。
考虑使用Polynom
方法制作derivative
课程。
答案 1 :(得分:0)
如何使用regex
查找格式为[number1]x^[number2]
的部分,然后用[num1]*[num2]x^[num2 - 1]
替换此部分?以下是根据找到的值动态创建替换零件的示例。
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);
输出:12x^3+225x^44-2+7700x^99
请注意,此解决方案非常有限。它假设所有部分都将以ax^n
形式编写,因此对于x^2
而不是1x^2
或-1
而不是{{1}这样的数据无效}。我还假设-1x^0
不是负数。
答案 2 :(得分: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);
}
输出:
12x^3+225x^44-2+7700x^99