java 解方程_【求助帖】Java怎么解方程啊

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

f3cedc61c0f4ad728ddd74732822a274.png

以下是源码

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值