![](images/smilies/emot/em11.gif)
[此贴子已经被作者于2006-3-25 10:14:47编辑过]
[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]
#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();
}