单循环链表计算多项式乘积

御风天 2011-09-13 07:23:26

#include <iostream>
#include <cmath>
using namespace std;
class Node
{
public:
double coef; //---系数
int expo; //---指数
Node * next;
Node():coef(0),expo(0),next(NULL){} //---默认构造函数
Node(double pa,int pb):coef(pa),expo(pb){} //---构造函数
};

class Multinomial //---多项式类 单循环链表
{
private:
int length; //链表长度

Node * front; //头指针

static double sum; //累计和
public:
Multinomial():length(0),front(new Node){ front->next = NULL ; } //构造

Multinomial(Node pmul[],int plength);

~Multinomial(); //析构

double Algorithm(const double x,const Multinomial & P) const; //计算两多项式乘积

Node * Getfront() const; //获得头指针
};

double Multinomial::sum =0;

Multinomial::Multinomial(Node pmul[],int plength)
{
front = new Node;
front->next = front; //单循环
for(int i = 0;i<plength; i++)
{
Node * s = new Node;
s->coef = pmul[i].coef;
s->expo = pmul[i].expo;
s->next = front->next;
front->next = s;
}
length = plength;
}

Multinomial::~Multinomial()
{
Node * p = front->next;
Node * temp;
while(p != front)
{
temp = p;
p = front->next;
delete temp;
}
delete p;
}

Node * Multinomial::Getfront() const
{
return front;
}

double Multinomial::Algorithm(const double x,const Multinomial & P) const //---计算多项式乘积
{
Node * pa = front->next;
Node * pb = NULL;

while(pa != front)
{
pb = P.Getfront()->next;
while(pb != P.Getfront())
{
int temp = pa->expo+pb->expo;
sum += (pa->coef)*(pb->coef)*pow(x,temp);
pb = pb->next;
}
pa = pa->next;
}

return sum;
}

void main()
{
Node ea(3,4); //两节点
Node eb(4,5);

Node a[1]={ea}; //储存节点的数组
Node b[1]={eb};

Multinomial x(a,1);
Multinomial y(b,1);
double aa;

aa=x.Algorithm(1,y);
cout<<aa<<endl;

}


输出的结果是对的,但是出现了一个对话框 Debug Assertion Failed ,怎么会这样???谁能帮一下忙,谢谢!!!
...全文
143 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
御风天 2011-09-14
  • 打赏
  • 举报
回复
谢谢!!!
御风天 2011-09-14
  • 打赏
  • 举报
回复
这么简单的错误,靠!!!晕,浪费了好多时间,太粗心了!!!唉
newfarmerchi 2011-09-14
  • 打赏
  • 举报
回复

Multinomial::~Multinomial()
{
Node * p = front->next;
Node * temp;
while(p != front)
{
temp = p;
//p = front->next;改成下面的, 试一试!
p = p->next;
delete temp;
}
delete p;
}



qyxqyxqyx 2011-09-13
  • 打赏
  • 举报
回复
这是我们实验课做的一个题目,我写了如下代码实现多项式的加减乘除和求模

#include<iostream>
#include<vector>
#include <algorithm>
#include<string>

using namespace std;

char c[100];
int t=0;

struct ploy
{
double xishu;
int zhishu;
};

int first_kh(int i)
{
int r=i;
while((c[r]!=')')&&(r<t))
{
r++;
}
return r;
}

int first_aom(int i,int n)
{
int r=i+1;
we:
while((c[r]!='+')&&(c[r]!='-')&&(r<n))
{
r++;
}
if((c[r]=='-')&&(c[r-1]=='^'))
{
r++;
goto we;
}
return r;
}

vector<ploy> get_vect(int v,int n)
{
vector<ploy> head;
int w;
for(int i=v;i<n;)
{
struct ploy p;
p.xishu=1;
p.zhishu=0;
w=first_aom(i,n);
bool flag=true;
for(int j=i;j<w;j++)
{
if(((c[j]=='-')||(isdigit(c[j])))&&flag)
{
char*s=&c[j];
p.xishu=strtod(s,NULL);
if((c[j+1]=='x')&&(c[j]=='-'))
{
p.xishu=-1;
}
flag=false;
}
if(c[j]=='x')
{
p.zhishu=1;
}
if(c[j]=='^')
{
char*s=&c[j+1];
p.zhishu=strtod(s,NULL);
break;
}
}
if((w-v)!=1)
{
head.push_back(p);
}
i=w;
}
return head;
}

void print1(const ploy& v)
{
int i=0;
if(v.xishu<0)
{
if(v.xishu==-1)
{
if(v.zhishu<0)
{
i=1;
}
if(v.zhishu==0)
{
i=2;
}
if(v.zhishu==1)
{
i=3;
}
if(v.zhishu>1)
{
i=4;
}
}
if(v.xishu!=-1)
{
if(v.zhishu<0)
{
i=5;
}
if(v.zhishu==0)
{
i=6;
}
if(v.zhishu==1)
{
i=7;
}
if(v.zhishu>1)
{
i=8;
}
}
}
if(v.xishu>0)
{
if(v.xishu==1)
{
if(v.zhishu<0)
{
i=9;
}
if(v.zhishu==0)
{
i=10;
}
if(v.zhishu==1)
{
i=11;
}
if(v.zhishu>1)
{
i=12;
}
}
if(v.xishu!=1)
{
if(v.zhishu<0)
{
i=13;
}
if(v.zhishu==0)
{
i=14;
}
if(v.zhishu==1)
{
i=15;
}
if(v.zhishu>1)
{
i=16;
}
}
}
switch(i)
{
case 1:
cout<<"x^"<<v.zhishu;
break;
case 2:
cout<<1;
break;
case 3:
cout<<"x";
break;
case 4:
cout<<"x^"<<v.zhishu;
break;
case 5:
cout<<-v.xishu<<"x^"<<v.zhishu;
break;
case 6:
cout<<-v.xishu;
break;
case 7:
cout<<-v.xishu<<"x";
break;
case 8:
cout<<-v.xishu<<"x^"<<v.zhishu;
break;
case 9:
cout<<"x^"<<v.zhishu;
break;
case 10:
cout<<1;
break;
case 11:
cout<<"x";
break;
case 12:
cout<<"x^"<<v.zhishu;
break;
case 13:
cout<<v.xishu<<"x^"<<v.zhishu;
break;
case 14:
cout<<v.xishu;
break;
case 15:
cout<<v.xishu<<"x";
break;
case 16:
cout<<v.xishu<<"x^"<<v.zhishu;
break;
}
}

void print(vector<ploy>& v)
{
if(v.empty()){cout<<0;return;}
vector<ploy>::iterator it=v.begin();
if((*it).xishu<0)
{
cout<<"-";
}
else
{
cout<<"\0";
}
print1(*it);
it++;
for(;it!=v.end();it++)
{
if((*it).xishu<0)
{
cout<<"-";
}
else
{
cout<<"+";
}
print1(*it);
}
}

vector<ploy> operator+(vector<ploy>& op1,vector<ploy>& op2){
if(op1.empty())return op2;if(op2.empty())return op1;
vector<ploy> answer;
vector<ploy>::iterator it1=op1.begin();
vector<ploy>::iterator it2=op2.begin();
while(it1!=op1.end() && it2!=op2.end()){
if(((*it1).zhishu)>((*it2).zhishu)){
answer.push_back(*it1);
it1++;
continue;
}
else{
if(((*it1).zhishu<(*it2).zhishu)){
answer.push_back(*it2);
it2++;
continue;
}
else{
if((*it1).xishu!=-(*it2).xishu){
ploy temp;
temp.xishu=(*it1).xishu+(*it2).xishu;temp.zhishu=(*it1).zhishu;
answer.push_back(temp);
}
it1++;it2++;
continue;
}
}
}
if(it1!=op1.end()){
while(it1!=op1.end()){
answer.push_back(*it1);
it1++;
}
}
else{
if(it2!=op2.end()){
while(it2!=op2.end()){
answer.push_back(*it2);
it2++;
}
}
}
return answer;
}

vector<ploy> operator-(vector<ploy>& op1,vector<ploy>& op2){
vector<ploy> answer;
vector<ploy>::iterator it1=op1.begin();
vector<ploy>::iterator it2=op2.begin();
if(op2.empty()){return op1;}
if(op1.empty()){
for(;it2!=op2.end();it2++)
{
ploy temp;
temp.xishu=-(*it2).xishu;
temp.zhishu=(*it2).zhishu;
answer.push_back(temp);
}
return answer;
}
it2=op2.begin();
while(it1!=op1.end() && it2!=op2.end()){
if(((*it1).zhishu)>((*it2).zhishu)){
answer.push_back(*it1);it1++;
continue;
}
else{
if(((*it1).zhishu<(*it2).zhishu)){
ploy temp;temp.zhishu=(*it2).zhishu;temp.xishu=-(*it2).xishu;
answer.push_back(temp);it2++;
continue;
}
else{
ploy temp;
if((temp.xishu=(*it1).xishu-(*it2).xishu)!=0){
temp.zhishu=(*it1).zhishu;
answer.push_back(temp);
}
it1++;it2++;
continue;
}
}
}
if(it1!=op1.end()){
while(it1!=op1.end()){
answer.push_back(*it1);
it1++;
}
}
else{
while(it2!=op2.end()){
ploy temp;
temp.xishu=-(*it2).xishu;temp.zhishu=(*it2).zhishu;
answer.push_back(temp);
it2++;
}
}
return answer;
}

vector<ploy> operator*(vector<ploy>& op1,ploy& op2){
vector<ploy> answer;
vector<ploy>::iterator it1=op1.begin();
for(;it1!=op1.end();it1++){
ploy temp;
temp.xishu=(*it1).xishu*op2.xishu;temp.zhishu=(*it1).zhishu+op2.zhishu;
answer.push_back(temp);
}
return answer;
}
vector<ploy> operator*(ploy& op1,vector<ploy>& op2){
vector<ploy> answer;
vector<ploy>::iterator it2=op2.begin();
for(;it2!=op2.end();it2++){
ploy temp;
temp.xishu=(*it2).xishu*op1.xishu;temp.zhishu=(*it2).zhishu+op1.zhishu;
answer.push_back(temp);
}
return answer;
}

vector<ploy> operator*(vector<ploy>& op1,vector<ploy>& op2){
vector<ploy> answer;
vector<ploy>::iterator it1=op1.begin();
vector<ploy>::iterator it2=op2.begin();
for(;it2!=op2.end();it2++){
answer=answer+op1*(*it2);
}
return answer;
}

vector<ploy> operator/(vector<ploy>& op1,vector<ploy>& op2){
vector<ploy> answer;
if(op1.empty())return answer;if(op2.empty())return op1;
if(op1[0].zhishu<op2[0].zhishu){return answer;}
vector<ploy> v1=op1;
vector<ploy>::iterator it1=v1.begin();
vector<ploy>::iterator it2=op2.begin();
while(!v1.empty()){
ploy temp;temp.zhishu=v1[0].zhishu-op2[0].zhishu;temp.xishu=v1[0].xishu/op2[0].xishu;
answer.push_back(temp);
v1=v1-op2*temp;
if(v1[0].zhishu<op2[0].zhishu)break;
}
return answer;
}

vector<ploy> operator%(vector<ploy>& op1,vector<ploy>& op2){
vector<ploy> answer;
if(op1.empty())return answer;if(op2.empty())return op1;
answer=op1-op2*(op1/op2);
return answer;
}

vector<ploy> operator*(double d,vector<ploy>& v){
vector<ploy> answer;
ploy temp;temp.xishu=d;temp.zhishu=0;
answer=temp*v;
return answer;
}

vector<ploy> operator*(vector<ploy>& v,double d){
vector<ploy> answer;
ploy temp;temp.xishu=d;temp.zhishu=0;
answer=temp*v;
return answer;
}

int cmp(const ploy &a, const ploy &b){
if(a.zhishu >=b.zhishu )
return 1;
else
return 0;
}

int main(){
while(1)
{
vector<ploy> head1,head2;
int ch,i=0;
while((ch=getchar())!=EOF&&(ch!='\n'))
c[t++]=ch;
int m=first_kh(0);
head1=get_vect(0,m);
char s=c[m+1];
head2=get_vect(m+2,t-1);
sort(head1.begin(),head1.end(),cmp);
sort(head2.begin(),head2.end(),cmp);
#if 0
for(int k=0;k<head1.size();k++)
{
cout<<head1[k].xishu<<" "<<head1[k].zhishu<<endl;
}
for(int t=0;t<head2.size();t++)
{
cout<<head2[t].xishu<<" "<<head2[t].zhishu<<endl;
}
#endif
switch(s)
{
case '+':
{
vector<ploy> answer=head1+head2;
print(answer);cout<<endl;
}
break;
case '-':
{
vector<ploy> answer2=head1-head2;
print(answer2);cout<<endl;
}
break;
case '*':
{
vector<ploy> answer3=head1*head2;
print(answer3);cout<<endl;
}
break;
case '/':
{
vector<ploy> answer4=head1/head2;
print(answer4);cout<<endl;
}
break;
case '%':
{
vector<ploy> answer5=head1%head2;
print(answer5);cout<<endl;
}
break;
}

memset(c,'\0',sizeof(c));
}
return 0;
}

输入格式是:(x+1)+(1-x)前面一个多项式,后面一个多项式,中间为符号(+-*/%).
你的这个我估计是你默认的有错误。
assertion是断言的意思。
assert(1==2);就会出现异常。你再查查看看,应该是初始化的地方出问题了。
用dev运行完了没有自动关掉哦。不知道怎么搞的

64,637

社区成员

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

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