我是菜鸟,这程序怎么运行不了,求帮助和完善,不胜感激!!!
// 一元多项式运算.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"
#include<iostream>
using namespace std;
#include<stdlib.h>
#include"stdio.h"
struct Term
{
int exp; //指数
double coef; //系数
};
struct Node
{
Term data; //结点内容,为TERM结构
struct Node *next;
};
template <typename T>
class Polynomial //多项式类
{
public:
Polynomial(Node* p) :front(p),n(0) //构造函数
{
p->data.coef=-1;
p->data.exp=-1;
p->next=NULL;
}
~Polynomial() { } //析构函数
void Print(); //显示
void Delete(Node *p); //销毁多项式值为0的项
int Getlength(); //获取多项式长度
void Setn();
void Getfront();
void Row(); //将多项式降序排序
T Power(T n, T x); //计算x^n,用于赋值
void Plus(Polynomial &B,Polynomial &R); //相加
void Minus(Polynomial &B,Polynomial &R); //相减
void Oppo(); //系数相反数,用于相减
void Evalute(double x); //赋值
void Derivate(Polynomial &R); //求导
void Multiply(Polynomial &B,Polynomial &R); //相乘
void destroyLink(); //销毁多项式
void menuPrint(); //菜单
void createLink(int n); //创建多项式
void Over(); //退出
int n;
double result;
private:
Node *front;
};
//计算x^n,用于赋值
template<class T>
T Polynomial<T>::Power(T n, T x)
{
double result=1;
for(int i=1;i<=n;i++)
result=result*x;
return result;
}
//显示
template<class T>
void Polynomial<T>::Print()
{
Row();
if((front==NULL)||(front->next==NULL))
{
cout<<"一元多项式为空"<<endl;
}
else
{
Node *p=front->next;
if(p->data.exp!=0)
cout<<"Poly= "<<p->data.coef<<"x^"<<p->data.exp;
else if(p->data.exp==0)
cout<<"Poly= "<<p->data.coef;
if(p->next!=NULL)
{
p=p->next;
while(1)
{
if((p->data.coef<0)&&(p->data.exp!=0))
cout<<p->data.coef<<"x^"<<p->data.exp;
else if((p->data.coef>0)&&(p->data.exp!=0))
cout<<"+"<<p->data.coef<<"x^"<<p->data.exp;
else if((p->data.coef<0)&&(p->data.exp==0))
cout<<p->data.coef;
else if((p->data.coef>0)&&(p->data.exp==0))
cout<<"+"<<p->data.coef;
if(p->next!=NULL)
p=p->next;
else
break;
}
}
}
cout<<endl;
}
//销毁多项式值为0的项
template<class T>
void Polynomial<T>::Delete(Node *p)
{
Node *q=front;
while(q->next!=p)
q=q->next;
p=q;
q=q->next;
delete q;
}
//获取多项式长度
template<class T>
int Polynomial<T>::Getlength()
{
Node *p=front;
if (p == NULL)
return 0;
while(p->next!=NULL)
{
n++;
p=p->next;
}
return n;
}
template<class T>
void Polynomial<T>::Setn()
{
n=Getlength();
}
template<class T>
void Polynomial<T>::Getfront()
{
Node *front=new Node;
}
//将多项式降序排序与合并同类项
template<class T>
void Polynomial<T>::Row()
{
Node *p=front->next;
Node *q=front->next;
Node *t=front->next;
while(1) //冒泡排序
{
p=t;
q=p->next;
while(q)
{
if(p->data.exp>q->data.exp)
{
if(q->next)
q=q->next;
else
break;
}
else if(p->data.exp==q->data.exp) //合并同类项
{
p->data.coef=p->data.coef+q->data.coef;
Node *temp=front;
while(temp->next!=q)
temp=temp->next;
if(q->next)
{
q=temp;
temp=temp->next;
q->next=temp->next;
delete temp;
q=q->next;
}
else
{
q=temp;
temp=temp->next;
delete temp;
q->next=NULL;
break;
}
}
else
{
Node *temp = new Node;
temp->data=q->data;
q->data=p->data;
p->data=temp->data;
delete temp;
if(q->next)
q=q->next;
else
break;
}
}
if(t->next)
{
t=t->next;
}
else
break;
}
Setn();
}
//系数相反数,用于相减
template<class T>
void Polynomial<T>::Oppo()
{
Row();
Node *p=front->next;
while(1)
{
p->data.coef=0-p->data.coef;
if(p->next)
p=p->next;
else
break;
}
}
//相减
template<class T>
void Polynomial<T>::Minus(Polynomial &B,Polynomial &R)
{
B.Oppo();
Plus(B,R);
B.Oppo();
}
//求导
template<class T>
void Polynomial<T>::Derivate(Polynomial &R)
{
Setn();
Node *p=front->next;
Node *r=R.front;
while(1)
{
r->next=new Node;
r=r->next;
r->data.coef=p->data.coef;
r->data.exp=p->data.exp;
r->next=NULL;
if(p->next)
p=p->next;
else
break;
}
r=R.front->next;
while(1)
{
if(r->data.exp==0)
Delete(p);
else
{
r->data.coef=r->data.coef*r->data.exp;
r->data.exp=r->data.exp-1;
}
if(r->next)
r=r->next;
else
break;
}
}
//相乘
template<class T>
void Polynomial<T>::Multiply(Polynomial &B,Polynomial &R)
{
Node* a=front->next;
Node* b=B.front->next;
Node* r=R.front;
if(R.front->next!=NULL)
R.destroyLink();
Row();
B.Row();
while(1)
{
while(1)
{
Node *rr=new Node;
rr->data.exp=a->data.exp+b->data.exp;
rr->data.coef=a->data.coef*b->data.coef;
rr->next=NULL;
r->next=rr;
r=rr;
if(b->next)
b=b->next;
else
break;
}
if(a->next)
a=a->next;
else
break;
b=B.front->next;
R.Row();
}
}
//赋值运算
template<class T>
void Polynomial<T>::Evalute(double x)
{
Setn();
Node *p=front->next;
double temp=0;
while(1)
{
temp+=(p->data.coef)*Power(p->data.exp,x);
if(p->next)
p=p->next;
else
break;
}
cout<<temp<<endl;
}
//相加
template<class T>
void Polynomial<T>::Plus(Polynomial &B,Polynomial &R)
{
Node* a=front->next;
Node* b=B.front->next;
Node* r=R.front;
while(1)
{
r->next=new Node;
r=r->next;
r->next=NULL;
r->data=a->data;
if(a->next)
a=a->next;
else
break;
}
while(1)
{
r->next=new Node;
r=r->next;
r->next=NULL;
r->data=b->data;
if(b->next)
b=b->next;
else
break;
}
R.Row();
}
//销毁多项式
template<class T>
void Polynomial<T>::destroyLink()
{
Node* a=front->next;
if(!a) throw "一元多项式已为空!";//抛出异常
while(1)
{
if(a->next)
{
Node* b=a;
a=a->next;
delete b;
}
else
{
delete a;
break;
}
}
front->next=NULL;
}
//菜单
template<class T>
void Polynomial<T>::menuPrint()
{
cout<<"\t***********一元多项式的简单运算*************"<<endl;
cout<<"\t\t 1创建要运算的两个一元多项式"<<endl;
cout<<"\t\t 2将两个一元多项式相加"<<endl;
cout<<"\t\t 3将两个一元多项式相减"<<endl;
cout<<"\t\t 4将两个一元多项式相乘"<<endl;
cout<<"\t\t 3将两个一元多项式求导"<<endl;
cout<<"\t\t 6显示两个一元多项式"<<endl;
cout<<"\t\t 7给两个一元多项式赋值"<<endl;
cout<<"\t\t 8销毁所创建的二个多项式"<<endl;
cout<<"\t\t 9退出"<<endl;
cout<<"请输入你要进行的操作(1-9)"<<endl;
}
//创建多项式
template<class T>
void Polynomial<T>::createLink(int n)
{
if(front->next!=NULL)
{
destroyLink();
}
Node *a=front;
for(int i=1;i<=n;i++)
{
a->next=new Node;
a=a->next;
a->next=NULL;
cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;
cout<<"系数:";
cin>>(a->data).coef;
cout<<"指数:";
cin>>(a->data).exp;
if(a->data.exp<0)
{
cout<<"您输入有误,指数不允许为负值!"<<endl;
Delete(a);
i--;
continue;
}
if(a->data.coef==0)
{
cout<<"系数为零,重新输入!"<<endl;
Delete(a);
i--;
continue;
}
}
}
//退出
template<class T>
void Polynomial<T>::Over()
{
exit(0);
}
//主函数
void main()
{
Node *p=new Node;
Node *q=new Node;
Node *r=new Node;
Polynomial<double> A(p); //操作多项式A
Polynomial<double> B(q); //操作多项式B
Polynomial<double> R(r); //操作多项式R(存储结果)
int m=0;
int n=0;
int choose;
while(1)
{
A.menuPrint();
cin>>choose;
switch(choose)
{
case 1: //创建多项式
cout<<"请输入你要运算的第一个一元多项式的项数:"<<endl;
cin>>m;
A.createLink(m);
cout<<"请输入你要运算的第二个一元多项式的项数:"<<endl;
cin>>n;
B.createLink(n);
break;
case 2: //相加
if(!m||!n)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl; //异常(以下省略)
break;
}
A.Plus(B,R);
cout<<"相加的两个一元多项式为:"<<endl;
A.Print();
B.Print();
cout<<"相加后的结果为:"<<endl;
R.Print();
R.destroyLink();
break;
case 3: //相减
if(!m||!n)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl;
break;
}
A.Minus(B,R);
cout<<"相减的两个一元多项式为:"<<endl;
A.Print();
B.Print();
cout<<"相减后的结果为:"<<endl;
R.Print();
R.destroyLink();
break;
case 4: //相乘
if(!n||!m)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl;
break;
}
A.Multiply(B,R);
cout<<"相乘的两个一元多项式为:"<<endl;
A.Print();
B.Print();
cout<<"相乘后的结果为:"<<endl;
R.Print();
R.destroyLink();
break;
case 5: //求导
if(!m)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl;
break;
}
cout<<"求导的一元多项式为:"<<endl;
A.Print();
B.Print();
cout<<"求导后的结果为:"<<endl;
A.Derivate(R);
R.Print();
R.destroyLink();
B.Derivate(R);
R.Print();
break;
case 6: //输出多项式
if(!n||!m)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl;
break;
}
cout<<"第一个一元多项式为:"<<endl;
A.Print();
cout<<"第二个一元多项式为:"<<endl;
B.Print();
break;
case 7: //多项式赋值
if(!n||!m)
{
cout<<"您的多项式创建有误,请重新选择……"<<endl;
break;
}
cout<<"第一个一元多项式为:"<<endl;
A.Print();
cout<<"第二个一元多项式为:"<<endl;
B.Print();
cout<<"您的X值为:"<<endl;
double x;
cin>>x;
cout<<"第一个一元多项式值为:";
A.Evalute(x);
cout<<"第二个一元多项式值为:";
B.Evalute(x);
break;
case 8:
try //捕捉异常
{
A.destroyLink();
B.destroyLink();
}
catch(const char *s)
{
cout<<s<<endl;
cout<<"请重新选择^^^"<<endl;
continue;
}
cout<<"销毁成功!"<<endl;
break;
case 9:
A.Over(); //退出函数
default:
cout<<"您的输入有误,请重新选择操作……"<<endl;
break;
}
}
}