我必须为二次类写一个read方法,其中以ax ^ 2 + bx + c的形式输入二次方。该课程的描述如下:
添加一个读取方法,询问用户标准格式的方程式并正确设置三个实例变量。 (因此,如果用户键入3x ^ 2 - x,则将实例变量设置为3,-1和0)。这将需要您之前完成的字符串处理。显示按原样输入的实际公式,并正确标记为预期输出。
我能够通过使用字符串操作和if else语句来完成ax ^ 2部分。但我不知道如何处理方程的bx和c部分,因为可能在bx和c前面的符号。以下是我如何使用方法的ax ^ 2部分。
public void read()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a quadratic equation in standard format.");
String formula = keyboard.next();
String a = formula.substring(0, formula.indexOf("x^2"));
int a2 = Integer.parseInt(a);
if (a2 == 0)
{
System.out.println("a = 0");
}
else if (a2 == 1)
{
System.out.println("a = 1");
}
else
{
System.out.println("a = " + a2);
}
}
随意编写任何代码作为示例。 任何帮助将不胜感激。
答案 0 :(得分:2)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Mini {
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
String formula = " -x^2 + 6x - 5";
formula = formula.replaceAll(" ", "");
if (!formula.startsWith("+") && !formula.startsWith("-"))
formula = "+" + formula;
String exp = "^((.*)x\\^2)?((.*)x)?([\\+\\s\\-\\d]*)?$";
Pattern p = Pattern.compile(exp);
Matcher m = p.matcher(formula);
System.out.println("Formula is " + formula);
System.out.println("Pattern is " + m.pattern());
while (m.find()) {
a = getDigit(m.group(2));
b = getDigit(m.group(4));
c = getDigit(m.group(5));
}
System.out.println("a: " + a + " b: " + b + " c: " + c);
}
private static int getDigit(String data) {
if (data == null) {
return 0;
}
else
{
if (data.equals("+"))
{
return 1;
}
else if (data.equals("-"))
{
return -1;
}
else
{
try
{
int num = (int) Float.parseFloat(data);
return num;
}
catch (NumberFormatException ex)
{
return 0;
}
}
}
}
}
答案 1 :(得分:1)
以下是如何使用正则表达式执行此操作的示例。到目前为止,只有当方程式以ax ^ 2 + bx + c格式给出时,这才能正常工作。它可以进一步调整,以允许更改子项的顺序,缺少术语等。为此,我可能会尝试为每个子项提出正则表达式。无论如何,这应该是为了给你一般的想法:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class ParseEquation {
static Pattern match = Pattern.compile("([\\+\\-]?[0-9]*)x\\^2([\\+\\-]?[0-9]*)x([\\+\\-]?[0-9]*)");
static String parseEquation(String formula) {
// remove all whitespace
formula = formula.replaceAll(" ", "");
String a = "1";
String b = "1";
String c = "0";
Matcher m = match.matcher(formula);
if (!m.matches()) return "syntax error";
a = m.group(1);
if (a.length() == 0) a = "1";
if (a.length() == 1 && (a.charAt(0) == '+' || a.charAt(0) == '-')) a += "1";
b = m.group(2);
if (b.length() == 0) b = "1";
if (b.length() == 1 && (b.charAt(0) == '+' || b.charAt(0) == '-')) b += "1";
c = m.group(3);
return a + "x^2" + b + "x" + c;
}
public static void main(String[] args) {
System.out.println(parseEquation("2x^2 + 3x - 25"));
System.out.println(parseEquation("-2x^2 + 3x + 25"));
System.out.println(parseEquation("+2x^2 + 3x + 25"));
System.out.println(parseEquation("x^2 + 3x + 25"));
System.out.println(parseEquation("2x^2 + x + 25"));
}
}
答案 2 :(得分:1)
通过正则表达式:
sub quadParse {
my ($inputStr) = @_;
my $str = "+".$inputStr; # as the first needn't have a sign
$str =~ s/\s+//g; # normalise
my $squared = $1 if ($str =~ m/([+-][0-9])*x\^2/);
my $ex = $1 if ($str =~ m/([+-][0-9]*)x(?!\^)/);
my $const = $1 if ($str =~ m/([+-][0-9]+)(?!x)/);
return "${squared}, ${ex}, ${const}";
}
对于字符串解析,Perl。
哦,继续吧:
public static String coeff(String str, String regex) {
Pattern patt = Pattern.compile(regex);
Matcher match = patt.matcher(str);
// missing coefficient default
String coeff = "+0";
if(match.find())
coeff = match.group(1);
// always have sign, handle implicit 1
return (coeff.length() == 1) ? coeff + "1"
: coeff;
}
public static String[] quadParse(String arg) {
String str = ("+" + arg).replaceAll("\\s", "");
String quad = coeff(str, "([+-][0-9]*)x\\^2" );
String ex = coeff(str, "([+-][0-9]*)x(?!\\^)");
String cnst = coeff(str, "([+-][0-9]+)(?!x)" );
return new String[] {quad, ex, cnst};
}
这些按任何顺序处理公式,在第一个术语上有或没有初始符号,并正确处理缺失的术语。 Perl版本没有将'+'修复为'+ 1'等,或者为缺少的术语提供明确的'0',因为我已经没时间了。