• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

实现两个指数递减多项式的和与积

c# 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。

【提示】 用带表头结点的单链表作为多项式的存储表示;要建立两个单链表;多项式相加就是要把一个单链表中的结点插入到另一个单链表中去,要注意插入、删除操作中指针的正确修改。

#include#includeusing namespace std;/**数据结构习题1多项式的相加和相乘@刘辉**/struct Node{    int data;    int index;    Node* next;};Node *insertList(Node* head,Node* p);  //插入链表Node *createList();           //创建链表void printList(Node *head);  //打印链表Node *addList(Node *p1,Node *p2); //实现加法运算Node *createList(){    int index,data;    Node *p,*head,*q;    head = new Node;    p = head;    cout<>index;    cout<>data;    while(index!=0)    {        q = new Node;        q->index = index;        q->data = data;        p->next = q;        p = q;        cout<>index;        cout<>data;    }    p->next = NULL;    return head;}//多项式相加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->indexindex)        {            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;<mark>来源gaodaimacom搞#^代%!码网</mark>            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;    }//多项式相乘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;}//打印多项式 void printList(Node *head) {     Node *p = NULL;     p = head->next;     if(p==NULL)     {         cout<data>=0)                cout<data<<"x^"<index;            else                cout<data<<"x^"<index;            if(p->next!=NULL)                cout<next;        }while(p != NULL);    cout<<endl;     } }  //主函数int main(){    int i;    Node *p1=NULL,*p2=NULL,*p3=NULL,*p4=NULL;    cout<<"创建第一个多项式的链表:"<<"\n";    p1 = createList();    cout<<"\n";    cout<<"创建第二个多项式的链表:"<<"\n";    p2 = createList();    cout<<"第一个多项式为:";    printList(p1);    cout<<"\n"<<"第二个多项式为:";    printList(p2);    p3 = addList(p1,p2);        //实现多项式相加    cout<<"\n"<<"多项式相加后为:";    printList(p3);    cout<<endl;    p4 = mulList(p1,p2);        //实现多项式相乘    cout<>i;    return 0;}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:实现两个指数递减多项式的和与积
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址