问题1.单链表的应用求多项式和.
说明:输入两个多项式的数目(结点数),每项系数和指数(结点内容),输出第3个多项式(结果多项式)
思路:书上用重载加法,我不想照书上的所以只用链表来完成.鉴于指数比较时的困难,我的程序规定只能降冥输入
运行结果:通过编译,但运行时有错,而且仅一个函数出错
求改内容:如何能完成要实现的功能- -
程序代码如下:
头文件
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream.h>
struct Node
{
int core;
int exp;
Node *next;
Node():core(NULL),exp(NULL),next(NULL){}
Node(const int& c, const int& e,Node *n=NULL):core(c),exp(e),next(n){}
};
class Linklist
{
public:
friend void add(Linklist a,Linklist b);
Linklist()
{
head=new Node();
}
Linklist(int kind)
{
kind=1;
head=NULL;
Node *p;
cout<<"please enter the amounts of the polys"<<endl;
int nodes;
cin>>nodes;
int *a=new int[nodes];
int *b=new int[nodes];
cout<<"plese enter "<<nodes<<"cores and exps"<<endl;
for(int i=0;i<nodes;i++)
{
cin>>a;
cin>>b;
}
for(int k=0;k<nodes;k++)
{
if (b[k]<b[k+1])
{
cerr<<"your poly isn't on rule"<<endl;
break;
}
else
head=p=new Node(a[k],b[k],head);
}
}
void showList()
{
Node *p=head;
for(;p;p=p->next)
{
if(p!=NULL)
cout<<p->core<<'x'<<'^'<<p->exp<<'+';
else
cout<<p->core<<'x'<<'^'<<p->exp<<endl;
}
}
protected:
Node *head;
int currentsize;
};
#endif
说明:单链表类定义,包括结点,类中的构造函数(空表的和有值表的),输出表元素的函数.
主函数:
#include<iostream.h>
#include"poly.h"
void add(Linklist a,Linklist b)
{
Linklist c;
Node *p=a.head;
Node *q=b.head;
Node *r=c.head;
while(p->next!=NULL||q->next!=NULL)
{
if(p->next==NULL)
{
for(;q;q=q->next)
{
r->next=q;
r=r->next;
}
}
else if(q->next==NULL)
{
for(;p;p=p->next)
{
r->next=p;
r=r->next;
}
}
else if(p->exp<q->exp)
{
r->next=p;
r=r->next;
p=p->next;
}
else if(p->exp>q->exp)
{
r->next=q;
r=r->next;
q=q->next;
}
else if(p->core+q->core==0)
{
p=p->next;
q=q->next;
}
else
{
Node *s=new Node(p->core+q->core,p->exp,NULL);
r=s;
r=r->next;
p=p->next;
q=q->next;
}
}
c.showList();
}
void main()
{
cout<<"please enter the first poly"<<endl;
Linklist polya(1);
cout<<"please enter the second poly"<<endl;
Linklist polyb(2);
cout<<"the first poly is"<<endl;
polya.showList();
cout<<"the second poly is"<<endl;
polyb.showList();
cout<<"now adding..."<<endl;
add(polya,polyb);
}
说明:第1个函数定义求多项式和.
拜一下各位大虾看看
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
struct Node
{
int core;
int exp;
Node *next;
Node():core(0),exp(0),next(NULL){}
Node(const int c, const int e,Node *n=NULL):core(c),exp(e),next(n){}
};
class Linklist
{
public:
friend void add(Linklist& a,Linklist& b);
Linklist()
{
head=new Node();
}
Linklist(int kind)
{
kind=1;
head=NULL;
Node *p;
std::cout<<"please enter the amounts of the polys"<<std::endl;
int nodes;
std::cin>>nodes;
int *a=new int[nodes];
int *b=new int[nodes];
std::cout<<"plese enter "<<nodes<<"cores and exps"<<std::endl;
for(int i=0;i<nodes;i++)
{
std::cin>>a[i];
std::cin>>b[i];
}
head = p = new Node();
for(int k=0;k<nodes;k++)
{
if (k < nodes-1 && b[k]<b[k+1])
{
std::cerr<<"your poly isn't on rule"<<std::endl;
break;
}
else {
p->next=new Node(a[k],b[k],NULL);
p = p->next;
}
}
delete [] a;
delete [] b;
}
void showList()
{
Node *p=head->next;
if (p == NULL)
return ;
for(; p->next;p=p->next)
{
//if(p!=NULL)
std::cout<<p->core<<'x'<<'^'<<p->exp<<'+';
//else
//std::cout<<p->core<<'x'<<'^'<<p->exp<<std::endl;
}
std::cout<<p->core<<'x'<<'^'<<p->exp<<std::endl;
}
protected:
Node *head;
int currentsize;
};
#endif
#include<iostream>
#include"poly.h"
void add(Linklist& a,Linklist& b)
{
Linklist c;
Node *p=a.head->next;
Node *q=b.head->next;
Node *r=c.head;
while(p!=NULL||q!=NULL)
{
if(p==NULL)
{
for(;q;q=q->next)
{
r->next=q;
r=r->next;
}
}
else if(q==NULL)
{
for(;p;p=p->next)
{
r->next=p;
r=r->next;
}
}
else if(p->exp<q->exp)
{
r->next=p;
r=r->next;
p=p->next;
}
else if(p->exp>q->exp)
{
r->next=q;
r=r->next;
q=q->next;
}
else if(p->core+q->core==0)
{
p=p->next;
q=q->next;
}
else
{
Node *s=new Node(p->core+q->core,p->exp,NULL);
r->next=s;
r=r->next;
p=p->next;
q=q->next;
}
}
c.showList();
}
using namespace std;
int main()
{
cout<<"please enter the first poly"<<endl;
Linklist polya(1);
cout<<"please enter the second poly"<<endl;
Linklist polyb(2);
cout<<"the first poly is"<<endl;
polya.showList();
cout<<"the second poly is"<<endl;
polyb.showList();
cout<<"now adding..."<<endl;
add(polya,polyb);
return 0;
}
发觉你的程序的逻辑极其混乱,哎,差点都改不下去了
设计不合理,两条链接相加,应该生成第三条链表,你把原来的链表破坏了,并且有些节点摘了,有些节点直接扔了(不用释放吗?)
其它不想说了,LZ还需加油啊