|
|
#2
wangwang1682007-09-17 23:08
|
|
|
#2
wangwang1682007-09-17 23:08
可以有多种方法,具体就是建立一个类,在这个类的私有数据中设置一个数组来存储系数和指数,也可以用链表来实现这个多项式
|
|
|
#3
wxzyn1232007-09-18 13:26
就是想用链表来实现两个多项式相加的 |
|
|
#4
aipb20072007-09-19 09:33
链表,结点中包含一个指数一个系数。
对应指数项相加。 |
|
|
#5
ibiancheng2007-10-27 09:18
#include<stdio.h> Node *Creat() void Read(Node *head) //读取链表中的数据 Node *Add(Node *head1,Node *head2) void main() 自己写的! |
|
|
#6
wingyip2007-10-27 16:30
头文件
#include<iostream> using namespace std; typedef struct linklist //自定义一个struct作为多项式的节点 { int xi; int zhi; struct linklist *next; }node; class Poly { public: Poly(); //初始化 void sort(); void produce(); //建立多项式 void add(Poly p2); //多项式的相加 void print(); //输出多项式 int getlong(); void destroy(); void multiply(Poly p2); private: node *head; }; Poly::Poly() { head=new node; //把head的后继指向空 head->next=NULL; } void Poly::add(Poly p2) { node *p,*q,*pre; p=head->next; pre=head; q=p2.head->next; while(p&&q) { if(p->zhi==q->zhi) //当遇到指数相等的情况就进行系数的相加 { p->xi+=q->xi; p=p->next; pre=pre->next; q=q->next; } else if(p->zhi<q->zhi) //当指数不相等的时候,较小的指数的多项式的指针指向后一位 { pre->next=q; q->next=p; pre=pre->next; } else { pre=pre->next; p=p->next; } } if(q) pre->next=q; } void Poly::produce() // 客户进行多项式的输入 { int temp1,temp2; node *p,*q; q=head; cout<<"请分别输入系数和指数(两个都为零时输入完毕)"<<endl; cin >>temp1>>temp2; while(temp1!=0||temp2!=0) { p=new node; //新建一个节点 p->xi=temp1; p->zhi=temp2; p->next=q->next; //将新的节点插入到链表里面 q->next=p; q=p; cout<<"请分别输入系数和指数(两个都为零时输入完毕)"<<endl; cin >>temp1>>temp2; } } /******************************************************************** 输入要分情况讨论 1 当系数等于零的时候,不输入 2 当系数等于1的时候直接输出+ 3 当系数小于零的时候直接输出系数 4 当指数等于零的时候不用输出y ********************************************************************/ void Poly::print() { node *p; bool first=true; p=head->next; while(p) { if(first) { if(p->xi==1) cout<<"y^"<<p->zhi; else if(p->xi==-1) cout<<'-'<<"y^"<<p->zhi; else if(p->xi>0) cout<<p->xi<<"y^"<<p->zhi; else if(p->xi<0) cout<<p->xi<<"y^"<<p->zhi; else if(p->xi==0) {} first=false; } else if(p->zhi==0) { if(p->xi>0) cout<<'+'<<p->xi; else cout<<p->xi; } else { if(p->xi==1) cout<<'+'<<"y^"<<p->zhi; else if(p->xi==-1) cout<<'-'<<"y^"<<p->zhi; else if(p->xi>0) cout<<'+'<<p->xi<<"y^"<<p->zhi; else if(p->xi<0) cout<<p->xi<<"y^"<<p->zhi; else if(p->xi==0) {} } p=p->next; } } void Poly::sort() { node *p,*q,*pre,*preP,*preQ; pre=head; while(pre->next) { p=pre->next; q=p; preP=pre; while(p) { if(q->zhi<=p->zhi) { q=p; preQ=preP; } p=p->next; preP=preP->next; } preQ->next=preQ->next->next; q->next=pre->next; pre->next=q; pre=q; } } int Poly::getlong() { node *p; int len=0; p=head->next; while(p) { len++; p=p->next; } return len; } void Poly::multiply(Poly p2) { node *p,*pre,*q,*l,*head3,*preQ; p=head->next; head3=new node; pre=head3; while(p) { q=p2.head->next; while(q) { l=new node; l->zhi=p->zhi+q->zhi; l->xi=p->xi*q->xi; pre->next=l; pre=l; q=q->next; } p=p->next; } pre->next=NULL; p=head3->next; while(p->next) { q=p->next; preQ=p; while(q) { if(p->zhi==q->zhi) { p->xi+=q->xi; preQ->next=preQ->next->next; delete(q); q=preQ->next; } else { q=q->next; preQ=preQ->next; } } p=p->next; } destroy(); head=head3; } void Poly::destroy() { node *p; p=head->next; while(p) { head->next=head->next->next; delete(p); p=head->next; } p=head; delete(p); } 测试文件 // 测试文件 #include<iostream> #include"poly.h" using namespace std; //void printOut(node *head); main() { Poly p1,p2; //新建两个多项式 p1.produce(); //建立多项式p1 cout<<endl; p1.print(); //输出多项式p1 cout<<endl; p1.sort(); //对刚刚的多项式进行降幂排序 cout<<endl; cout<<"降幂排序整理后得:"<<endl; //重新输出多项式 p1.print(); cout<<endl; p2.produce(); //建立多项式p2 cout<<endl; p2.print(); //输出多项式p2 cout<<endl; p2.sort(); cout<<endl; cout<<"降幂排序整理后得:"<<endl; p2.print(); cout<<endl; //进行多项式的乘法运算 p1.multiply(p2); cout<<endl; cout<<"两个多项式相乘后得"<<endl; p1.print(); cout<<endl; return 0; } |
|
|
#7
老虎没打到2012-06-20 09:46
#include <stdio.h>
#include <stdlib.h> #include<malloc.h> #define OVERFLOW -2 #define ERROR 0 #define OK 1 typedef struct Node { int coeff;//系数 int expo;//指数 Node *next; }listNode,*list; list createList() { list head; head = (list)malloc(sizeof(listNode)); head = NULL; printf("want to create a new node?y/n\n"); char ch; fflush(stdin);//送入缓存 scanf("%c",&ch); while(ch=='Y' || ch== 'y') { list p; p = (list)malloc(sizeof(listNode)); printf("input coeff\n"); int coeff; scanf("%d",&coeff); p->coeff = coeff; printf("input expo\n"); int expo; scanf("%d",&expo); p->expo = expo; p->next = NULL; //链表为空的时候,即添加首个元素 if(head == NULL) { head=p; //添加代码 } else { list prev,curr;//prev是前驱结点curr当前节点 curr = head; prev = NULL; //找到添加位置 while(curr!=NULL && curr->expo>p->expo) { prev=curr; curr=curr->next;//添加代码 } if(curr!=NULL && curr->expo == p->expo) { curr->coeff = curr->coeff + p->coeff; printf("want to create a new node?y/n\n"); fflush(stdin); scanf("%c",&ch); if(ch=='Y' || ch== 'y') continue; else return head; } //插入结点,结点非首 if(prev != NULL) { prev->next=p; p->next=curr;//添加代码 } //插入结点,结点为首 else { p->next=curr; head=p;//添加代码 } } printf("want to create a new node?y/n\n"); fflush(stdin); scanf("%c",&ch); } return head; } list add(list head1,list head2) { list head,newNode,ptr1,ptr2,ptr3; ptr1 = head1; ptr2 = head2; head = NULL; while(ptr1 != NULL && ptr2 != NULL) { newNode = (list)malloc(sizeof(listNode)); if(ptr1->expo > ptr2->expo) { newNode->coeff = ptr1->coeff; newNode->expo = ptr1->expo; newNode->next = NULL; ptr1 = ptr1->next; } else if(ptr1->expo < ptr2->expo) { newNode->coeff = ptr2->coeff; newNode->expo = ptr2->expo; newNode->next = NULL; ptr2 = ptr2->next;//添加代码 } else { newNode->coeff = ptr1->coeff + ptr2->coeff; newNode->expo = ptr1->expo; newNode->next = NULL; ptr1 = ptr1->next; ptr2 = ptr2->next; } if(head==NULL) { head = newNode; } else { ptr3 = head; while(ptr3->next!=NULL) ptr3=ptr3->next;//添加代码 ptr3->next=newNode; } } while(ptr1 != NULL) { newNode = (list)malloc(sizeof(listNode)); newNode->coeff = ptr1->coeff; newNode->expo = ptr1->expo; newNode->next = NULL; ptr3 = head; if(ptr3 == NULL) head = ptr3 = newNode; else { while(ptr3->next != NULL) ptr3 = ptr3->next; ptr3->next = newNode;//添加代码 } ptr1 = ptr1->next; } while(ptr2 != NULL) { newNode = (list)malloc(sizeof(listNode)); newNode->coeff = ptr2->coeff; newNode->expo = ptr2->expo; ptr3 = head; if(ptr3 == NULL) head = ptr3 = newNode; else { while(ptr3->next != NULL) ptr3 = ptr3->next; ptr3->next = newNode; } ptr2 = ptr2->next; } return head; } void display(list head) { list ptr = head; while(ptr != NULL) { if(ptr != head ) printf("+"); printf("%d",ptr->coeff); printf("x^"); printf("%d",ptr->expo); ptr = ptr->next; } } int main(int argc, char* argv[]) { list head,head1,head2; printf("input the first list\n"); head1 = createList(); printf("input the second list\n"); head2 = createList(); head = add(head1,head2); display(head); return 0; } |