


Implement the sum and product of two exponentially decreasing polynomials
Oct 31, 2016 pm 02:13 PMThere are two unary polynomials with decreasing exponentials. Write a program to first find the sum of these two polynomials, and then find their product.
[Tips] Use a singly linked list with a header node as the storage representation of a polynomial; you need to build two singly linked lists; polynomial addition means inserting the nodes in one singly linked list into another singly linked list. Pay attention to the correct modification of pointers during insertion and deletion operations.
#include<iostream> #include<cmath> using namespace std; /** 數(shù)據(jù)結(jié)構(gòu)習(xí)題1 多項(xiàng)式的相加和相乘 @劉輝 **/ struct Node{ int data; int index; Node* next; }; Node *insertList(Node* head,Node* p); //插入鏈表 Node *createList(); //創(chuàng)建鏈表 void printList(Node *head); //打印鏈表 Node *addList(Node *p1,Node *p2); //實(shí)現(xiàn)加法運(yùn)算 Node *createList() { int index,data; Node *p,*head,*q; head = new Node; p = head; cout<<"請輸入要輸入的多項(xiàng)式a的冪指數(shù):"; cin>>index; cout<<"請輸入該指數(shù)的參數(shù):"; cin>>data; while(index!=0) { q = new Node; q->index = index; q->data = data; p->next = q; p = q; cout<<"請輸入要輸入的多項(xiàng)式a的冪指數(shù):"; cin>>index; cout<<"請輸入該指數(shù)的參數(shù):"; cin>>data; } p->next = NULL; return head; } //多項(xiàng)式相加 Node *addList(Node *p1,Node *p2) { int add; Node *temp,*head,*p3; p1 = p1->next; p2 = p2->next; head = temp = new Node; head->next = NULL; while(p1&&p2) { if(p1->index==p2->index) { add = p2->data + p1->data; if(add!=0) { p3 = new Node; p3->index = p2->index; p3->data = add; p3->next = NULL; } p1 = p1->next; p2 = p2->next; } else if(p1->index<p2->index) { p3 = new Node; p3->data = p2->data; p3->index = p2->index; p3->next = NULL; p2 = p2->next; } else { p3 = new Node; p3->data = p1->data; p3->index = p1->index; p3->next = NULL; p1 = p1->next; } if(head->next ==NULL) { head->next = p3; temp = p3; } else { temp->next = p3; temp = p3; } } temp->next = p1?p1:p2; return head; } //多項(xiàng)式相乘 Node* mulList(Node *p1,Node *p2) { Node *head,*temp,*s,*r,*q; head = new Node; head->next = NULL; temp = new Node; temp->next = NULL; p1 = p1->next; p2 = p2->next; for(s=p1;s;s=s->next) { for(r=p2;r;r=r->next) { q = new Node; temp->next = q; q->data = s->data * r->data; q->index = s->index + r->index; q->next = NULL; head = addList(temp,head); } } return head; } //打印多項(xiàng)式 void printList(Node *head) { Node *p = NULL; p = head->next; if(p==NULL) { cout<<"文件為空"; } else { do { if(p->data>=0) cout<<p->data<<"x^"<<p->index; else cout<<p->data<<"x^"<<p->index; if(p->next!=NULL) cout<<"+"; p=p->next; }while(p != NULL); cout<<endl; } } //主函數(shù) int main() { int i; Node *p1=NULL,*p2=NULL,*p3=NULL,*p4=NULL; cout<<"創(chuàng)建第一個多項(xiàng)式的鏈表:"<<"\n"; p1 = createList(); cout<<"\n"; cout<<"創(chuàng)建第二個多項(xiàng)式的鏈表:"<<"\n"; p2 = createList(); cout<<"第一個多項(xiàng)式為:"; printList(p1); cout<<"\n"<<"第二個多項(xiàng)式為:"; printList(p2); p3 = addList(p1,p2); //實(shí)現(xiàn)多項(xiàng)式相加 cout<<"\n"<<"多項(xiàng)式相加后為:"; printList(p3); cout<<endl; p4 = mulList(p1,p2); //實(shí)現(xiàn)多項(xiàng)式相乘 cout<<"多項(xiàng)式相乘后為:"; printList(p4); cin>>i; return 0; }

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
