该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
以下是源码
package jfc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception{
List fcss = new ArrayList();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请以ax+b=c的形式输入方程,输入end表示结束!");
String line = br.readLine();
while(!"end".equals(line)) {
fcss.add(FCS.analysis(line));
System.out.println("请以ax+b=c的形式输入方程,输入end表示结束!");
line = br.readLine();
}
for(FCS f:fcss) {
System.out.println(f.getX()+":"+f.getR());
}
}}
package jfc;
public class FCS {
//ax+b = c;
private int a;
private int b;
private int c;
private String r;
private String x;
public FCS(int a,int b,int c,String x,String r) {
this.a = a;
this.b = b;
this.c = c;
this.x = x;
this.r = r;
}
public static FCS analysis(String line) {
int a,b,c;
String astr = null;
line = line.replaceAll(" ", "");
String[] lrs = line.split("=");
c = Integer.parseInt(lrs[1]);
String left = lrs[0];
if(left.indexOf("+") > 0) {
int index = left.indexOf("+");
try {
b = Integer.parseInt(left.substring(0,index));
astr = left.substring(index+1);
}catch(Exception e) {
b = Integer.parseInt(left.substring(index+1));
astr = left.substring(0,index);
}
}else if(left.indexOf("-") > 0){
String[] nvs = left.split("-");
try {
b = Integer.parseInt(nvs[0]);
astr = "-"+nvs[1];
}catch(Exception e) {
b = Integer.parseInt(nvs[1])*-1;
astr = nvs[0];
}
}else {
b = 0;
astr = left;
}
a = Integer.parseInt(astr.substring(0,astr.length() -1));
String x = astr.substring(astr.length() -1);
int fm = c-b;
String r = "";
if(fm % a == 0) {
r = fm/a + "";
}else {
boolean flag = false;
if(a < 0 && fm > 0) {
flag = true;
a = a*-1;
}
if(fm < 0 && a > 0) {
flag = true;
fm = fm * -1;
}
int max = fm > a ? fm:a;
int min = fm < a ? fm:a;
int index = min/2;
int maxgy = 1;
for(int i=index;i>1;i--) {
if(min % i == 0 && max % i == 0) {
maxgy = i;
break;
}
}
r = (fm/maxgy)+"/" + (a/maxgy);
if(flag) {
r = "-"+r;
}
}
return new FCS(a,b,c,x,r);
}
public int getA() {
return a;
} public void setA(int a) {
this.a = a;
} public int getB() {
return b;
} public void setB(int b) {
this.b = b;
} public int getC() {
return c;
} public void setC(int c) {
this.c = c;
} public String getX() {
return x;
} public void setX(String x) {
this.x = x;
} public String getR() {
return r;
} public void setR(String r) {
this.r = r;
}
}