新聞中心
誰知道用java怎么寫出兩個(gè)多項(xiàng)式相除的程序啊。。求解啊。。速度啊。。
網(wǎng)上有用單鏈表來實(shí)現(xiàn)多項(xiàng)式的加減乘,你可以參考一下,代碼如下

為安達(dá)等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及安達(dá)網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、安達(dá)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
public class Polynomial {
private Monomial first; // 首項(xiàng)
//添加單項(xiàng)式
public void append(Monomial monomial) {
if (monomial == null) {
// do nothing
} else if (first == null) {
first = monomial;
} else {
Monomial current = first;
while (current != null) {
// Examda提示:如果指數(shù)相同,則相加
if (current.index == monomial.index) {
current.coefficient += monomial.coefficient;
break;
} else if (current.next == null) { // 否則直接扔到最后
current.next = monomial;
break;
}
current = current.next;
}
}
}
public void append(double c, int i) {
append(new Monomial(c, i));
}
public String toString() {
StringBuffer sb = new StringBuffer();
Monomial current = first;
while (current.next != null) {
sb
.append("(" + current.coefficient + "x^" + current.index
+ ") + ");
current = current.next;
}
sb.append("(" + current.coefficient + "x^" + current.index + ")");
return sb.toString();
}
// 兩個(gè)多項(xiàng)式相加
public Polynomial add(Polynomial p2) {
Polynomial result = new Polynomial();
Monomial current = this.first;
while (current != null) {
result.append(current.coefficient, current.index); // Examda提示:注意這里
current = current.next;
}
current = p2.first;
while (current != null) {
result.append(current.coefficient, current.index);
current = current.next;
}
return result;
}
// 兩個(gè)多項(xiàng)式相減 this- p2
public Polynomial substract(Polynomial p2) {
Polynomial result = new Polynomial();
Monomial current = this.first;
while (current != null) {
result.append(current.coefficient, current.index); // 注意這里
current = current.next;
}
current = p2.first;
while (current != null) {
result.append(-current.coefficient, current.index);
current = current.next;
}
return result;
}
/**
* this * p2
*
* @return
*/
public Polynomial multiply(Polynomial p2) {
Polynomial result = new Polynomial();
Monomial c1 = this.first;
Monomial c2 = p2.first;
while (c1 != null) {
while (c2 != null) {
result.append(c1.coefficient * c2.coefficient, c1.index
+ c2.index);
c2 = c2.next;
}
c1 = c1.next;
c2 = p2.first;
}
return result;
}
public Polynomial divide(Polynomial p2) {
// todo 實(shí)現(xiàn)相除
return null;
}
public static void main(String[] args) {
Polynomial p1 = new Polynomial();
p1.append(2.2, 1);
p1.append(3.3, 2);
p1.append(4.111, 7);
System.out.println("p1: " + p1);
Polynomial p2 = new Polynomial();
p2.append(2.232, 5);
p2.append(3.444, 6);
p2.append(5.777, 1);
System.out.println("p2: " + p2);
Polynomial result = p1.add(p2);
System.out.println("加: " + result);
result = p1.substract(p2);
System.out.println("減: " + result);
result = p1.multiply(p2);
System.out.println("乘: " + result);
}
}
/**
* 單項(xiàng)式
*/
class Monomial {
double coefficient; // 系數(shù)
int index; // 指數(shù)
Monomial next; // 后繼結(jié)點(diǎn)
public Monomial() {
}
public Monomial(double c, int i) {
this.coefficient = c;
this.index = i;
}
}
關(guān)于java加減除的代碼問題
import java.util.Scanner;
public class P {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
P prm=new P();
int a,b;
System.out.println("請輸入兩個(gè)整數(shù)(以空格分隔):");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("兩數(shù)相加后等于:"+prm.add(a,b));
System.out.println("兩數(shù)相減后等于:"+prm.sub(a,b));
System.out.println("兩數(shù)相除后等于:"+prm.divide(b,a));
sc.close();
}
int add(int a,int b) {
return a+b;
}
int sub(int a,int b) {
if(ab)
return a-b;
else
return b-a;
}
int divide(int a,int b) {
if(0==b)
System.out.println("錯誤");
if(0==a)
System.out.println("除數(shù)不能為0");
return b/a;
}
}
用java語言從鍵盤輸入兩個(gè)數(shù),求這兩個(gè)數(shù)相除的結(jié)果.處理所有可能的異常情況
import?java.util.Scanner;
public?class?HelloJava?{
public?static?void?main(String[]?args)?{
int?num1?=?0;
int?num2?=?0;
Scanner?scanner?=?new?Scanner(System.in);
try?{
System.out.println("請輸入除數(shù)?按回車確認(rèn)");
num1?=?scanner.nextInt();
System.out.println("請輸入被除數(shù)?按回車確認(rèn)");
num2?=?scanner.nextInt();
}catch?(InputMismatchException?e){
System.out.println("輸入的類型不合法");
e.printStackTrace();
}?catch?(Exception?e)?{
e.printStackTrace();
}
if(num2?==?0)?{
System.out.println("num2?的值?不能是?0");
}?else?{
System.out.println(num1?+?"?/?"?+?num2?+?"?=?"?+?(num1?/?num2));
}
}
}
怎么編寫一個(gè)JAVA程序,從鍵盤輸入任意兩個(gè)整數(shù)進(jìn)行相除操作并輸出結(jié)果,使用2?
利用百Java應(yīng)用程序編寫要求輸入兩個(gè)整度數(shù)并計(jì)算兩個(gè)數(shù)據(jù)之和具知體操作步驟道如下:內(nèi)
import java.util.Scanner
public class Test {
public static void main(String[] args) {
System.out.print("請輸容入第一個(gè)整數(shù):")
Scanner s=new Scanner(System.in)
int a=s.nextInt()
System.out.print("請輸入第二個(gè)整數(shù):")
int b=s.nextInt()
System.out.println(a+b)
}
}
文章標(biāo)題:java兩個(gè)參數(shù)相除代碼 java兩數(shù)相除取整數(shù)部分
標(biāo)題URL:http://www.dlmjj.cn/article/dohddpi.html


咨詢
建站咨詢
