我想实现一个多项式有序插入 (不论你怎么输入)
通过前一次输入位置左右滑动插入
输入的 次数分别是2 1 2 2 2 6
但是第二个就是很奇怪了:见下面的解释 好还有一个 下面的cout《无法输出y
#define inf -2147483647
class ele
{ public:
int x,y;
ele *next;
ele *pr;
};
class poly
{ public:
int n;
ele *h;
poly(){h=NULL;n=0;}
void poly_init( );
#include<iostream>using namespace std;
#include"dc.h" int main()
{
poly a; int n;ele *h;
;
cout<<"输入项数:";
cin>>n; / /我输入的是六
a.n=n;
h=new ele;
a.h=h;
h->x=inf; h->y=inf; //头结点为负无穷大
ele *p, *t=h;
int count=n;
h->pr=NULL;h->next=NULL;
for (int i(1);i<=n;++i)
{
p=new ele;
cout<<"第 "<<i<<" 个项的系数:";
cin>>p->x>>p->y;
cout<<p->x;cout<<p->y;
while (t->next && p->y >t->y)
t=t->next;
if (p->y==t->y)
{
t->x=p->x+t->x;
delete p;
if(t->x==0)
{
t->pr->next=t->next;
if(t->next)
{
t->next->pr=t->pr;
p=t->pr;
delete t; //销毁t的空间然后指向前面
}
t=p;
count--;
}
count--;
continue;
}
else if(!t->next && p->y >t->y)
{
t->next=p;
p->pr=t;
p->next=NULL;
t=p;
continue;
}
while(p->y <t->y)
t=t->pr;
if (p->y==t->y)
{
t->x=p->x+t->x;
delete p;
if(t->x==0)
{
t->pr->next=t->next;
if(t->next)
{
t->next->pr=t->pr;
p=t->pr;
delete t; //销毁t的空间然后指向前面
count--;
}
t=p;
}
count--;
continue;
}
//else //if(p->y>t->y)
p->next=t->next;
p->pr=t;
t->next=p;
t=p;
cout<<"____"<<t->y<<p->y<<endl;
}
p=NULL;t=NULL;
n=count;
cout<<endl<<"real lenghth is "<<n<<endl; p=a.h;
while(p->next!=NULL)
{ p=p->next;
cout<<'('<<p->x<<" ,"<<p->y<<cout<<')'<<endl;
}
//a.poly_init();
// cout<<a.h->next->x;
// show(a);
cin>>a.n;
return 0;
}
通过前一次输入位置左右滑动插入
输入的 次数分别是2 1 2 2 2 6
但是第二个就是很奇怪了:见下面的解释 好还有一个 下面的cout《无法输出y
#define inf -2147483647
class ele
{ public:
int x,y;
ele *next;
ele *pr;
};
class poly
{ public:
int n;
ele *h;
poly(){h=NULL;n=0;}
void poly_init( );
#include<iostream>using namespace std;
#include"dc.h" int main()
{
poly a; int n;ele *h;
;
cout<<"输入项数:";
cin>>n; / /我输入的是六
a.n=n;
h=new ele;
a.h=h;
h->x=inf; h->y=inf; //头结点为负无穷大
ele *p, *t=h;
int count=n;
h->pr=NULL;h->next=NULL;
for (int i(1);i<=n;++i)
{
p=new ele;
cout<<"第 "<<i<<" 个项的系数:";
cin>>p->x>>p->y;
cout<<p->x;cout<<p->y;
while (t->next && p->y >t->y)
t=t->next;
if (p->y==t->y)
{
t->x=p->x+t->x;
delete p;
if(t->x==0)
{
t->pr->next=t->next;
if(t->next)
{
t->next->pr=t->pr;
p=t->pr;
delete t; //销毁t的空间然后指向前面
}
t=p;
count--;
}
count--;
continue;
}
else if(!t->next && p->y >t->y)
{
t->next=p;
p->pr=t;
p->next=NULL;
t=p;
continue;
}
while(p->y <t->y)
t=t->pr;
if (p->y==t->y)
{
t->x=p->x+t->x;
delete p;
if(t->x==0)
{
t->pr->next=t->next;
if(t->next)
{
t->next->pr=t->pr;
p=t->pr;
delete t; //销毁t的空间然后指向前面
count--;
}
t=p;
}
count--;
continue;
}
//else //if(p->y>t->y)
p->next=t->next;
p->pr=t;
t->next=p;
t=p;
cout<<"____"<<t->y<<p->y<<endl;
}
p=NULL;t=NULL;
n=count;
cout<<endl<<"real lenghth is "<<n<<endl; p=a.h;
while(p->next!=NULL)
{ p=p->next;
cout<<'('<<p->x<<" ,"<<p->y<<cout<<')'<<endl;
}
//a.poly_init();
// cout<<a.h->next->x;
// show(a);
cin>>a.n;
return 0;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货