| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 990 人关注过本帖
标题:给个多项式加法的代码(详细的程序C++写的)
只看楼主 加入收藏
lijianbo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-3-25
收藏
 问题点数:0 回复次数:4 
给个多项式加法的代码(详细的程序C++写的)
大家帮帮忙啊!写了好久也写不好!要是c++写的

[此贴子已经被作者于2006-3-25 10:14:47编辑过]


搜索更多相关主题的帖子: 多项式 加法 代码 
2006-03-25 10:10
hewking
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-3-15
收藏
得分:0 

[QUOTE]#include <stdio.h>
#include <stdlib.h>
void input(struct poly **);
void poly_add(void);
void show_ans(void);
struct poly {
int coef;
int exp;
struct poly *next;
};
struct poly *sum, *eq_h1, *eq_h2, *ans_h;
void main(void)
{
printf("\n\n");
printf("%c %c %c %c %c %c %c %c %c %c %c %c \n\n",6,5,4,3,6,5,4,3,6,5,4,3);
printf(" Multinomial For Example: AxB+CxD \n\n");
printf("%c %c %c %c %c %c %c %c %c %c %c %c \n\n\n",3,4,5,6,3,4,5,6,3,4,5,6);
printf("Input the first multinomial: ");
input(&eq_h1);
printf("Input the second multinomial: ");
input(&eq_h2);
poly_add();
show_ans();
getch();
}
void input(struct poly **eq_h)
{
struct poly *prev = NULL;
char symbol = '+';
do
{
sum = (struct poly *) malloc(sizeof(struct poly));
sum->next = NULL;
scanf("%dx%d", &sum->coef, &sum->exp);
if(*eq_h == NULL)
*eq_h = sum;
else
{
if(symbol == '-') sum->coef = -(sum->coef);
prev->next = sum;
}
prev = sum;
scanf("%c", &symbol);
} while(symbol != '\n');
}
void poly_add(void)
{
struct poly *this_n1, *this_n2, *prev;
this_n1 = eq_h1;
this_n2 = eq_h2;
prev = NULL;
while(this_n1 != NULL || this_n2 != NULL)
{
sum = (struct poly *) malloc(sizeof(struct poly));
sum->next = NULL;
if(this_n1 != NULL && (this_n2 == NULL || this_n1->exp > this_n2->exp))
{
sum->coef = this_n1->coef;
sum->exp = this_n1->exp;
this_n1 = this_n1->next;
}
else
if(this_n1 == NULL || this_n1->exp < this_n2->exp)
{
sum->coef = this_n2->coef;
sum->exp = this_n2->exp;
this_n2 = this_n2->next;
}
else
{
sum->coef = this_n1->coef + this_n2->coef;
sum->exp = this_n1->exp;
if(this_n1 != NULL) this_n1 = this_n1->next;
if(this_n2 != NULL) this_n2 = this_n2->next;
}
if(sum->coef != 0)
{
if(ans_h == NULL) ans_h = sum;
else prev->next = sum;
prev = sum;
}
else free(sum);
}
}
void show_ans(void)
{
struct poly *this_n;
this_n = ans_h;
printf("The Result: ");
while(this_n != NULL)
{
printf("%dx%d", this_n->coef, this_n->exp);
if(this_n->next != NULL && this_n->next->coef >= 0)
printf("+");
this_n = this_n->next;
}
printf("\n");
}[/QUOTE]

2006-04-21 10:13
linzhaoyang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-4-21
收藏
得分:0 

请问怎么提问?
谢谢

2006-04-21 10:18
hewking
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-3-15
收藏
得分:0 

Not cpp
C

2006-04-21 10:34
两个人的笨笨
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-29
收藏
得分:0 

#include<iostream.h>
#include<assert.h>
#define NULL 0
class listnode{
public:
int coef;
int exp;
listnode *link;
listnode(int coefval,int expval,listnode * linkval=NULL)
{coef=coefval;exp=expval;link=linkval;}
listnode(listnode* linkval=NULL)
{link=NULL;}
~listnode(){}
};
class list{
private:
listnode *head;
listnode *tail;
listnode *curr;
public:
list()
{head=tail=curr=new listnode;}
~list();
void append(const int& coefval,const int& expval,listnode * linkval=NULL)
{tail=tail->link=new listnode(coefval,expval,linkval);}
void print()
{
listnode *p;
p=head;
p=p->link;
while(p!=NULL)
{
cout<<p->coef<<"x^"<<p->exp<<"+";
p=p->link;
}
cout<<endl;
}
list(const list & o)
{
head=tail=curr=new listnode;
listnode *p;

p=((o.head)->link);
if(p==NULL)
{
assert(0);
}
while(p!=NULL){
tail=tail->link=new listnode(p->coef,p->exp,NULL);
p=p->link;
}

}
friend list padd(list&,list&);
};
list::~list()
{
// cout<<head->link->coef<<":"<<head->link->exp<<"+"<<head->link->link->coef<<":"<<head->link->link->exp<<endl;
while(head!=NULL){
curr=head;

head=head->link;

delete curr;
}
// cout<<"It'over"<<endl;

}

list padd(list& A,list& B)
{
list C;
listnode *r,*p,*q;
int x=0;
r=C.tail;
p=A.head->link;
q=B.head->link;
while(p!=NULL&&q!=NULL){
if(p->exp==q->exp)
{
x=p->coef+q->coef;
if(x!=0)
C.append(x,p->exp,NULL);
p=p->link;
q=q->link;
}
else if(p->exp>q->exp)//
{
C.append(p->coef,p->exp,NULL);
p=p->link;
}
else
{
C.append(q->coef,q->exp,NULL);
q=q->link;

}
}
while(p!=NULL){
C.append(p->coef,p->exp,NULL);
p=p->link;
}
while(q!=NULL){
C.append(q->coef,q->exp,NULL);
q=q->link;
}

return C;
}

void main()
{
list A;
list B;
A.append(3,14);
A.append(2,8);
A.append(1,0);
B.append(8,14);
B.append(-3,10);
B.append(4,6);
B.append(-5,0);
//A=3x^14+2x^8+1;B=8x^14-3x^10+4x^6-5;结果应该为:C=11x^14-3x^10+2x^8+4x^6-4;
// A.print();
// B.print();
list D=padd(A,B);
D.print();

}

2006-04-22 01:15
快速回复:给个多项式加法的代码(详细的程序C++写的)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025207 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved