189 8069 5689

多项式计算java代码 编程实现多项式的求和运算

谁知道用java怎么写出两个多项式相除的程序啊。。求解啊。。速度啊。。

网上有用单链表来实现多项式的加减乘,你可以参考一下,代码如下

公司主营业务:网站制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出通城免费做网站回馈大家。

public class Polynomial {

private Monomial first; // 首项

//添加单项式

public void append(Monomial monomial) {

if (monomial == null) {

// do nothing

} else if (first == null) {

first = monomial;

} else {

Monomial current = first;

while (current != null) {

// Examda提示:如果指数相同,则相加

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();

}

// 两个多项式相加

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;

}

// 两个多项式相减 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 实现相除

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);

}

}

/**

* 单项式

*/

class Monomial {

double coefficient; // 系数

int index; // 指数

Monomial next; // 后继结点

public Monomial() {

}

public Monomial(double c, int i) {

this.coefficient = c;

this.index = i;

}

}

java中完成一元稀疏多项式的基本运算

除以上功能外,还有乘法和除法的计算和导数计算呢。

这是我以前做的数据结构课程设计。希望能帮上你的忙。

#includestdio.h

#includemalloc.h

typedef struct Polynomial{

float coef;

int expn;

struct Polynomial *next;

}*Polyn,Polynomial; //Polyn为结点指针类型

void Insert(Polyn p,Polyn h){

if(p-coef==0) free(p); //系数为0的话释放结点

else{

Polyn q1,q2;

q1=h;q2=h-next;

while(q2p-expnq2-expn){ //查找插入位置

q1=q2;

q2=q2-next;

}

if(q2p-expn==q2-expn){ //将指数相同相合并

q2-coef+=p-coef;

free(p);

if(!q2-coef){ //系数为0的话释放结点

q1-next=q2-next;

free(q2);

}

}

else{ //指数为新时将结点插入

p-next=q2;

q1-next=p;

}

}

}//Insert

Polyn CreatePolyn(Polyn head,int m){//建立一个头指针为head、项数为m的一元多项式

int i;

Polyn p;

p=head=(Polyn)malloc(sizeof(struct Polynomial));

head-next=NULL;

for(i=0;im;i++){

p=(Polyn)malloc(sizeof(struct Polynomial));//建立新结点以接收数据

printf("请输入第%d项的系数与指数:",i+1);

scanf("%f %d",p-coef,p-expn);

Insert(p,head); //调用Insert函数插入结点

}

return head;

}//CreatePolyn

void DestroyPolyn(Polyn p){//销毁多项式p

Polyn q1,q2;

q1=p-next;

q2=q1-next;

while(q1-next){

free(q1);

q1=q2;//指针后移

q2=q2-next;

}

}

void PrintPolyn(Polyn P){

Polyn q=P-next;

int flag=1;//项数计数器

if(!q) { //若多项式为空,输出0

putchar('0');

printf("\n");

return;

}

while (q){

if(q-coef0flag!=1) putchar('+'); //系数大于0且不是第一项

if(q-coef!=1q-coef!=-1){//系数非1或-1的普通情况

printf("%g",q-coef);

if(q-expn==1) putchar('X');

else if(q-expn) printf("X^%d",q-expn);

}

else{

if(q-coef==1){

if(!q-expn) putchar('1');

else if(q-expn==1) putchar('X');

else printf("X^%d",q-expn);

}

if(q-coef==-1){

if(!q-expn) printf("-1");

else if(q-expn==1) printf("-X");

else printf("-X^%d",q-expn);

}

}

q=q-next;

flag++;

}//while

printf("\n");

}//PrintPolyn

int compare(Polyn a,Polyn b){

if(ab){

if(!b||a-expnb-expn) return 1;

else if(!a||a-expnb-expn) return -1;

else return 0;

}

else if(!ab) return -1;//a多项式已空,但b多项式非空

else return 1;//b多项式已空,但a多项式非空

}//compare

Polyn AddPolyn(Polyn pa,Polyn pb){//求解并建立多项式a+b,返回其头指针

Polyn qa=pa-next;

Polyn qb=pb-next;

Polyn headc,hc,qc;

hc=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点

hc-next=NULL;

headc=hc;

while(qa||qb){

qc=(Polyn)malloc(sizeof(struct Polynomial));

switch(compare(qa,qb)){

case 1:

{

qc-coef=qa-coef;

qc-expn=qa-expn;

qa=qa-next;

break;

}

case 0:

{

qc-coef=qa-coef+qb-coef;

qc-expn=qa-expn;

qa=qa-next;

qb=qb-next;

break;

}

case -1:

{

qc-coef=qb-coef;

qc-expn=qb-expn;

qb=qb-next;

break;

}

}//switch

if(qc-coef!=0){

qc-next=hc-next;

hc-next=qc;

hc=qc;

}

else free(qc);//当相加系数为0时,释放该结点

}//while

return headc;

}//AddPolyn

Polyn SubtractPolyn(Polyn pa,Polyn pb){//求解并建立多项式a+b,返回其头指针

Polyn h=pb;

Polyn p=pb-next;

Polyn pd;

while(p){ //将pb的系数取反

p-coef*=-1;

p=p-next;

}

pd=AddPolyn(pa,h);

for(p=h-next;p;p=p-next) //恢复pb的系数

p-coef*=-1;

return pd;

}//SubtractPolyn

float ValuePolyn(Polyn head,float x){//输入x值,计算并返回多项式的值

Polyn p;

int i;

float sum=0,t;

for(p=head-next;p;p=p-next){

t=1;

for(i=p-expn;i!=0;){

if(i0){t/=x;i++;}//指数小于0,进行除法

else{t*=x;i--;}//指数大于0,进行乘法

}

sum+=p-coef*t;

}

return sum;

}//ValuePolyn

Polyn Derivative(Polyn head){//求解并建立a的导函数多项式,并返回其头指针

Polyn q=head-next,p1,p2,hd;

hd=p1=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点

hd-next=NULL;

while(q){

if(q-expn!=0){ //该项不是常数项时

p2=(Polyn)malloc(sizeof(struct Polynomial));

p2-coef=q-coef*q-expn;

p2-expn=q-expn-1;

p2-next=p1-next;//连接结点

p1-next=p2;

p1=p2;

}

q=q-next;

}

return hd;

}//Dervative

Polyn MultiplyPolyn(Polyn pa,Polyn pb){//求解并建立多项式a*b,返回其头指针

Polyn hf,pf;

Polyn qa=pa-next;

Polyn qb=pb-next;

hf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点

hf-next=NULL;

for(;qa;qa=qa-next){

for(qb=pb-next;qb;qb=qb-next){

pf=(Polyn)malloc(sizeof(struct Polynomial));

pf-coef=qa-coef*qb-coef;

pf-expn=qa-expn+qb-expn;

Insert(pf,hf);//调用Insert函数以合并指数相同的项

}

}

return hf;

}//MultiplyPolyn

void DevicePolyn(Polyn pa,Polyn pb){//求解并建立多项式a*b,返回其头指针

Polyn hf,pf,af,temp1,temp2,q;

Polyn qa=pa-next;

Polyn qb=pb-next;

hf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点,存储商

hf-next=NULL;

pf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点,存储余数

pf-next=NULL;

temp1=(Polyn)malloc(sizeof(struct Polynomial));

temp1-next=NULL;

temp2=(Polyn)malloc(sizeof(struct Polynomial));

temp2-next=NULL;

temp1=AddPolyn(temp1,pa);

while(qa!=NULLqa-expn=qb-expn){

temp2-next=(Polyn)malloc(sizeof(struct Polynomial));

temp2-next-coef=(qa-coef)/(qb-coef);

temp2-next-expn=(qa-expn)-(qb-expn);

Insert(temp2-next,hf);

pa=SubtractPolyn(pa,MultiplyPolyn(pb,temp2));

qa=pa-next;

temp2-next=NULL;

}

pf=SubtractPolyn(temp1,MultiplyPolyn(hf,pb));

pb=temp1;

printf("商是:");

PrintPolyn(hf);

printf("余数是:");

PrintPolyn(pf);

}//DevicePolyn

int main(){

int m,n,flag=0;

float x;

Polyn pa=0,pb=0,pc,pd,pe,pf;//定义各式的头指针,pa与pb在使用前付初值NULL

printf("请输入a的项数:");

scanf("%d",m);

pa=CreatePolyn(pa,m);//建立多项式a

printf("请输入b的项数:");

scanf("%d",n);

pb=CreatePolyn(pb,n);//建立多项式a

//输出菜单

printf("**********************************************\n");

printf("操作提示:\n\t1.输出多项式a和b\n\t2.建立多项式a+b\n\t3.建立多项式a-b\n");

printf("\t4.计算多项式a在x处的值\n\t5.求多项式a的导函数\n\t6.建立多项式a*b\n");

printf("\t7.建立多项式a/b\n\t8.退出\n**********************************************\n");

for(;;flag=0){

printf("执行操作");

scanf("%d",flag);

if(flag==1){

printf("多项式a:");PrintPolyn(pa);

printf("多项式b:");PrintPolyn(pb);continue;

}

if(flag==2){

pc=AddPolyn(pa,pb);

printf("多项式a+b:");PrintPolyn(pc);

DestroyPolyn(pc);continue;

}

if(flag==3){

pd=SubtractPolyn(pa,pb);

printf("多项式a-b:");PrintPolyn(pd);

DestroyPolyn(pd);continue;

}

if(flag==4){

printf("输入x的值:x=");

scanf("%f",x);

printf("多项式a的值%g\n",ValuePolyn(pa,x));continue;

}

if(flag==5){

pe=Derivative(pa);

printf("多项式a的导函数:");PrintPolyn(pe);

DestroyPolyn(pe);continue;

}

if(flag==6){

pf=MultiplyPolyn(pa,pb);

printf("多项式a*b:");PrintPolyn(pf);

DestroyPolyn(pf);continue;

}

if(flag==7){

DevicePolyn(pa,pb);

continue;

}

if(flag==8) break;

if(flag1||flag8) printf("Error!!!\n");continue;

}//for

DestroyPolyn(pa);

DestroyPolyn(pb);

return 0;

}

用java单链表实现一元多项式相加的算法?

public class Test {

public static void main(String[] args) {

try{

LinkList list1 = new LinkList();

LinkList list2 = new LinkList();

LinkList list3 = null;

list1.addAt(0, new Item(1, 5));

list1.addAt(1, new Item(-1.5, 3));

list1.addAt(2, new Item(1, 1));

list2.addAt(0, new Item(0.5, 5));

list2.addAt(1, new Item(0.5, 4));

list2.addAt(2, new Item(1.5, 3));

list2.addAt(3, new Item(3, 0));

list3 = mergeLinkList(list1, list2);

System.out.println("一元多项式的相加过程:");

list1.listAll();

System.out.println(" + ");

list2.listAll();

System.out.println(" = ");

list3.listAll();

}

catch(Exception e){

e.printStackTrace();

}

}

/**

* 一元多项式的一般项类

*/

class Item{

private double coef;  //一元多项式的一般项的系数

private int exp;   //一元多项式的一般项的指数

public Item(){

this.coef = 0.0;

this.exp = 0;

}

public Item(double coef, int exp){

this.coef = coef;

this.exp = exp;

}

public double getCoef(){

return this.coef;

}

public void setCoef(double coef){

this.coef = coef;

}

public int getExp(){

return this.exp;

}

public void setExp(int exp){

this.exp = exp;

}

}

/**

* 链表结点类

*/

class Node{

private Item data;

private Node next;   //链表结点的指针域,指向直接后继结点

public Node(){

data = null;

next = null;

}

public Node(Item data, Node next){

this.data = data;

this.next = next;

}

public Item getData(){

return this.data;

}

public void setData(Item data){

this.data = data;

}

public Node getNext(){

return this.next;

}

public void setNext(Node next){

this.next = next;

}

}

/**

* 链表类

*/

class LinkList{

private Node head = null; //头结点指针

private int size = 0;

public LinkList(){

head = new Node();

size = 0;

}

//在i位置插入元素elem

public boolean addAt(int i, Item elem) {

if(i 0 || i size){

return false;

}

Node pre,curr;

int pos;

for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());

curr = new Node(elem, pre.getNext());

pre.setNext(curr);

size++;

return true;

}

//删除i位置的元素

public boolean removeAt(int i) {

if(i 0 || i = size){

return false;

}

Node pre,curr;

for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());

curr = pre.getNext();

pre.setNext(curr.getNext());

size--;

return true;

}

java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

用Java语言实现求一个矩阵的特征多项式的程序代码怎么写?跪求高人指点~~~

矩阵规模如何?

如果矩阵比较小 (~10), 可以直接按照定义来计算, P[x] = det( x*I - A); 其中det是一个矩阵的行列式; 可以按照定义直接迭代求;

如果矩阵比较大(100), 可以先求特征值, 然后构造矩阵特征多项式 P[x] = (x-x1)*(x-x2)... (x-xn), 其中xi为矩阵的第i个特征值.


文章题目:多项式计算java代码 编程实现多项式的求和运算
网站链接:http://cdxtjz.com/article/hipois.html

其他资讯