关于单链表c++实现一元多项式求和的程序错误问题

qq596437780 2017-11-28 10:55:29
#include<iostream>
using namespace std;
struct elem
{
int coef;
int exp;
};
template<class T>
struct Node
{
T data;
Node<T>*next;
};
template<class T>
class Linklist
{
public:
Linklist();
Linklist(T a[],int n);//尾插法建立单链表
Linklist(int n,T a[]);//头插法建立单链表
~Linklist();
int Length();
T Get(int i);
int Locate(T x);
void Insert(int i,T x);
T Delete(int i);
void Printlist();
friend void Add(Linklist<elem> A,Linklist<elem> B);
private:
Node<T>*frist;
};
template<class T>
void Linklist<T>::Printlist()
{
T *p;
p=frist->next;
while (p!=NULL)
{
cout<<p->data;
p=p->next;
}
}
template<class T>
int Linklist<T>::Length()
{
Node<T> *p;
p=frist->next;int count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
template<class T>
T Linklist<T>::Get(int i)
{
Node<T> *p;
p=frist->next;int count=1;
while(p!=NULL&&count<i)
{
p=p->next;
count++;
}
if(p==NULL)cout<<"查找元素位置有误!"<<endl;
else return p->data;
}
template<class T>
int Linklist<T>::Locate(T x)
{
Node<T> *p;
p=frist->next;int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class T>
void Linklist<T>::Insert(int i,T x)
{
Node<T> *p,*s;
p=frist;int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)cout<<"插入元素位置有误!"<<endl;
else
{
s=new Node<T>;s->data=x;
s->next=p->next;p->next=s;
}
}
template<class T>
Linklist<T>::Linklist()
{
frist=new Node<T>;
frist->next=NULL;
}
template<class T>
Linklist<T>::Linklist(int n,T a[])
{
Node<T>*s;
frist=new Node<T>;
frist->next=NULL;
for(int i=1;i<n;i++)
{
s=new Node<T>;s->data=a[i];
s->next=frist->next;frist->next=s;
}
}
template<class T>
Linklist<T>::Linklist(T a[],int n)
{
Node<T> *r,*s;
frist=new Node<T>;
r=frist;
for(int i=0;i<n;i++)
{
s=new Node<T>;s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class T>
T Linklist<T>::Delete(int i)
{
Node<T> *p,*q;
T x;
p=frist;int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
cout<<"删除元素位置有误!"<<endl;
else
{
q=p->next;x=q->data;
p->next=q->next;
delete q;
return x;
}
}
template<class T>
Linklist<T>::~Linklist()
{
Node<T> *q;
while(frist!=NULL)
{
q=frist;
frist=frist->next;
delete q;
}
}
void Add(Linklist<elem> A,Linklist<elem> B)
{
Node<elem>*p,*pre,*q,*qre,*v;
pre=A.frist;p=pre->next;
qre=B.frist;q=qre->next;
while(p!=NULL&&q!=NULL)
{
if(p->exp<q->exp)
{
pre=p;
p=p->next;
}
else if(p->exp>q->exp)
{
v=q->next;
pre->next=q;
q->next=p;
q=v;
}
else
{
p->coef=p->coef+q->coef;
if(p->coef==0)
{
pre->next=p->next;
delete p;
p=pre->next;
}
else
{
pre=p;
p=p->next;
}
qre->next=q->next;
delete q;
q=qre->next;
}
}
if(q!=NULL)
pre->next=q;
delete B.frist;
}
int main()
{
cout<<"测试"<<endl;
}

error: 'struct Node<elem>' has no member named 'exp'
error: 'struct Node<elem>' has no member named 'coef'
...全文
149 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-11-28
  • 打赏
  • 举报
回复
仅供参考:
//链表实现一元多项式的加法减法乘法
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
    float coef;   //系数
    int expn;     //指数
    struct node *next;
}
PolyNode;      //多项式节点 polynomial node
typedef PolyNode* Polynomial;
Polynomial createPolynomial() {  //创建多项式
    PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点
    head->next = NULL;
    float coef;
    int expn;
    printf("输入该多项式每一项的系数和指数,每项一行,输入0 0结束!\n");
    while (scanf("%f %d", &coef, &expn) && coef) {   // 默认,按指数递减排列
        if (head->next) {
            p = head;
            while (p->next && expn < p->next->expn)
                p = p->next;
            if (p->next) {
                if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式
                    p->next->coef += coef;
                    if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0,则舍弃该节点
                        q = p->next;
                        p->next = q->next;
                        free(q);
                    }
                } else {
                    q = (PolyNode*)malloc(sizeof(PolyNode));
                    q->coef = coef;
                    q->expn = expn;
                    q->next = p->next;
                    p->next = q;
                }
            } else {
                p->next = (PolyNode*)malloc(sizeof(PolyNode));
                p = p->next;
                p->coef = coef;
                p->expn = expn;
                p->next = NULL;
            }
        } else {
            head->next = (PolyNode*)malloc(sizeof(PolyNode));
            head->next->coef = coef;
            head->next->expn = expn;
            head->next->next = NULL;
        }
    }
    return head;
}
Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘,该单项式为 coefx^expn
    PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));
    p = Poly;
    q = poly->next;
    while (q) {
        p->next = (PolyNode*)malloc(sizeof(PolyNode));
        p = p->next;
        p->coef = (q->coef*coef);
        p->expn = (q->expn + expn);
        q = q->next;
    }
    p->next = NULL;
    return Poly;
}
void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上
    PolyNode *p, *q, *r;
    r = poly1;
    p = poly1->next;  //指向第一个节点
    q = poly2->next;
    poly2->next = NULL;
    while (p && q) {
        if (p->expn > q->expn) {
            r->next = p;
            p = p->next;
            r = r->next;
        } else if (p->expn < q->expn) {
            r->next = q;
            q = q->next;
            r = r->next;
        } else {
            PolyNode *t;
            p->coef += q->coef;
            if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0
            {
                r->next = p;
                r = r->next;
                p = p->next;
            } else {
                t = p;
                p = p->next;
                free(t);
            }
            t = q;
            q = q->next;
            free(t);
        }
    }
    if (p)
        r->next = p;
    if (q)
        r->next = q;
}
Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式
    //把poly2的系数取相反数,形成一个新的多项式
    Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点
    PolyNode *p, *q;
    p = poly;
    q = poly2->next;
    while (q) {
        p->next = (PolyNode*)malloc(sizeof(PolyNode));
        p = p->next;
        p->coef = -(q->coef);  //系数取反
        p->expn = q->expn;
        q = q->next;
    }
    p->next = NULL;
    add(poly, poly1);  //利用加法
    return poly;
}
Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式
    Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点
    poly->next = NULL;
    PolyNode *p, *q, *r;
    r = poly;
    p = poly1->next;
    q = poly2->next;
    while (p&&q) {
        if (p->expn > q->expn) {
            r->next = (PolyNode*)malloc(sizeof(PolyNode));
            r = r->next;
            r->coef = p->coef;
            r->expn = p->expn;
            p = p->next;
        } else if (p->expn < q->expn) {
            r->next = (PolyNode*)malloc(sizeof(PolyNode));
            r = r->next;
            r->coef = q->coef;
            r->expn = q->expn;
            q = q->next;
        } else {
            float m = p->coef + q->coef;
            if (!(m > -0.000001 && m < 0.000001)) {
                r->next = (PolyNode*)malloc(sizeof(PolyNode));
                r = r->next;
                r->coef = m;
                r->expn = p->expn;
            }
            q = q->next;
            p = p->next;
        }
    }
    while (p) {
        r->next = (PolyNode*)malloc(sizeof(PolyNode));
        r = r->next;
        r->coef = p->coef;
        r->expn = p->expn;
        p = p->next;
    }
    while (q) {
        r->next = (PolyNode*)malloc(sizeof(PolyNode));
        r = r->next;
        r->coef = q->coef;
        r->expn = q->expn;
        q = q->next;
    }
    r->next = NULL;
    return poly;
}
Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘
    Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点
    poly->next = NULL;
    PolyNode *p;
    p = poly2->next;
    while (p) {
        add(poly, multiply(poly1, p->coef, p->expn));
        p = p->next;
    }
    return poly;
}
void printPoly(Polynomial poly) {  //打印多项式
    if (poly && poly->next) {
        PolyNode *p = poly->next;  //p指向第一个节点
        while (p->next) {
            printf("%gx^%d", p->coef, p->expn);
            p = p->next;
            if (p && (p->coef > 0))
                printf("+");
        }
        if (p->expn == 0)
            printf("%g", p->coef);   //打印常数项
        else
            printf("%gx^%d", p->coef, p->expn);
        printf("\n");
    }
}
void freePoly(Polynomial poly) {  //释放内存
    if (poly && poly->next) {
        PolyNode *p, *q;
        p = poly;
        while (p) {
            q = p->next;
            free(p);
            p = q;
        }
    }
    poly = NULL;
}
int main() {
    printf("用链表实现多项式的加减法\n");
    Polynomial poly1, poly2, poly3;
    printf("创建多项式一\n");
    poly1 = createPolynomial();
    printf("多项式一:\n");
    printPoly(poly1);
    printf("创建多项式二\n");
    poly2 = createPolynomial();
    printf("多项式二:\n");
    printPoly(poly2);
    printf("两多项式相加,和为:\n");
    poly3 = polyAdd(poly1, poly2);
    printPoly(poly3);
    freePoly(poly3);
    printf("两个多项式相乘,积为:\n");
    poly3 = polyMultiply(poly1, poly2);
    printPoly(poly3);
    freePoly(poly3);
    printf("两多项式相减,差为:\n");
    poly3 = polySubtract(poly1, poly2);
    printPoly(poly3);
    freePoly(poly1);
    freePoly(poly2);
    freePoly(poly3);
    system("pause");
    return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧