一个c++程序,关于一元多项式的表示以及合并同类项,有错,帮助修改

#include<iostream>
using namespace std;
class term{
float coef;
int expn;
term *link;//coef为系数,expn为指数
public:
term();
term(const float &data1,const int &data2);
friend class list;
};
term::term(){ link=NULL;}
term::term(const float &data1,const int &data2){
coef=data1;
expn=data2;
link=NULL;
}

class list{
term *head,*tail;
public:
list();
void makeempty();
void deleteterm(term *p);
~list();
term *find(int data);//查找指数相同的项,并返回该项地址
void printlist();
void insertrear(term *p);//向后生成链表,不排序
void insertorder(term *p);//按升序排列
term *creatterm(float data1,int data2);//生成孤立项
};
list::list(){
head=tail=new term();
}
list::~list(){
makeempty();
delete head;
}
void list::makeempty(){
term *tempP;
while(head->link!=NULL){
tempP=head->link;
head->link=tempP->link;
delete tempP;
}
tail=head;
}
term *list::find(int data){
term *tempP=head->link;
while(tempP!=NULL&&tempP->expn!=data) tempP=tempP->link;
return tempP;
}//查找成功返回该地址,不成功则返回NULL
void list::printlist(){
term *tempP=head;
cout<<tempP->coef<<"x^"<<tempP->expn;
tempP=tempP->link;
while(tempP!=NULL){
if(tempP->coef<0) {
cout<<tempP->coef<<"x^"<<tempP->expn;
}
if(tempP->coef>0){
cout<<"+"<<tempP->coef<<"x^"<<tempP->expn;
}
tempP=tempP->link;
}
cout<<endl;
}
void list::deleteterm(term *p){
term *tempP=head;
while(tempP->link!=NULL&&tempP->link!=p) tempP=tempP->link;
if(tempP->link==tail) tail=tempP;
else tempP->link=p->link;
}
term *list::creatterm(float data1,int data2){
term *tempP=new term(data1,data2);
return tempP;
}
void list::insertrear(term *p){
term *tempP=list::find(p->expn);
if(tempP!=NULL) {
if((p->coef+tempP->coef)==0) list::deleteterm(tempP);
else tempP->coef=p->coef+tempP->coef;
}
else{
p->link=tail->link;
tail->link=p;
tail=p;
}
}
void list::insertorder(term *p){
term *tempP=list::find(p->expn);
if(tempP!=NULL) {
if((p->coef+tempP->coef)==0) list::deleteterm(tempP);
else tempP->coef=p->coef+tempP->coef;
}

else{
term *tempQ=head;
term *tempP=head->link;
while((p->expn)>(tempP->expn)&&tempP->link!=NULL){
tempQ=tempP;
tempP=tempP->link;
}
if(p->expn<tempP->expn){
tempQ->link=p;
p->link=tempP;
}
if(p->expn>tempP->expn&&tempP->link==NULL){
tempP->link=p;
p->link=NULL;
}
}
}

int main(){
term *P1;
list list1;
cout<<"请输入一元多项式的项数"<<endl;

float a;
int b;
int n;
int m;
cin>>n;
for(m=0;m<n;m++){
cin>>a>>b>>endl;

P1=list1.creatterm(a,b);
list1.insertorder(P1);
}
list1.printlist();
cout<<"程序结束";
return 0;
}

兄弟!你写程序难道没有注释……
前几天,俺们老师也留了这道题,把我的发给你看看!说实话,让别人读你的程序,不如你读别人的程序……
#include<iostream>
using namespace std;
struct PloyNode
{
double coef;//系数域
int exp;//指数域
};
typedef PloyNode DataType;

class Node
{
friend class LinkList;
private:
DataType Data; //数据域
Node *next; //指针域
public:
Node() //构造函数
{
next = NULL;
}
Node(DataType x) //构造函数
{
Data = x;
next = NULL;
}
};

class LinkList //链表类
{
private:
Node *head1; //头指针1
Node *head2; //头指针2
int Size1; //链1的节点个数
int Size2; //链2的节点个数
public:
LinkList() //构造函数
{
head1 = new Node();
head2 = new Node();
Size1 = Size2 = 0;
}
~LinkList() //析构函数
{
Node *p, *q;

p = head1;
while(p != NULL)//析构表1
{
q = p;
p = p->next;
delete q;
}
Size1 = 0;
head1 = NULL;

p = head2;
while(p != NULL)//析构表2
{
q = p;
p = p->next;
delete q;
}
Size2 = 0;
head2 = NULL;
}
void Insert()//插入有序链表
{
int i, n1, n2;
Node *tail1, *tail2, *p, *r, *t;
PloyNode temp;

cout<<"请输入第一个多项式的项数:";
cin>>n1;
Size1 = n1;
for(i=1; i<=n1; i++)
{
cout<<"输入第一个多项式的第"<<i<<"项的系数:";
cin>>temp.coef;
cout<<"输入第一个多项式的第"<<i<<"项的指数:";
cin>>temp.exp;
p = new Node(temp); //新的构造节点
if(head1 ->next == NULL) //如果构造的是第一个节点
{
head1 ->next = tail1 = p;//就将尾指针和头指针都指向p
}
else //如果构造的不是第一个节点
{
r = head1; //将指针r指向头结点
t = r ->next; //将指针t指向第一个节点
while( (t != NULL) && (p ->Data.exp > t ->Data.exp) )//如果p指针所指向的新节点的指数 比 t指针指向的第一个节点的指数大
{ //并且这条链不止一个节点,那就将指针r和指针t向后移
r = t;
t = t ->next;
}
if(t == NULL) //如果p指针指向的新节点的指数 最大,那就将它插入到链尾
{
tail1 ->next = p;
tail1 = p; //将尾指针tail1指向p
}
else if(p ->Data.exp == t ->Data.exp) //假如p指针指向的新节点的指数 等于 链表中一项的指数,就合并
{
t ->Data.coef += p ->Data.coef;
Size1 --; //链表节点减一
}
else //如果p指针指向的新节点的指数 小于 t指针所指向的节点,就插入到它的前面
{
p ->next = t;
r ->next = p;
}
}
}

cout<<"请输入第二个多项式的项数:"; //以下同理
cin>>n2;
Size2 = n2;
for(i=1; i<=n2; i++)
{
cout<<"输入第一个多项式的第"<<i<<"项的系数:";
cin>>temp.coef;
cout<<"输入第一个多项式的第"<<i<<"项的指数:";
cin>>temp.exp;
p = new Node(temp); //新的构造节点
if(head2 ->next == NULL) //如果构造的是第一个节点
{
head2 ->next = tail2 = p;//就将尾指针和头指针都指向p
}
else //如果构造的不是第一个节点
{
r = head2; //将指针r指向头结点
t = r ->next; //将指针t指向第一个节点
while( (t != NULL) && (p ->Data.exp > t ->Data.exp) )//如果p指针所指向的新节点的指数 比 t指针指向的第一个节点的指数大
{ //并且这条链不止一个节点,那就将指针r和指针t向后移
r = t;
t = t ->next;
}
if(t == NULL) //如果p指针指向的新节点的指数 最大,那就将它插入到链尾
{
tail2 ->next = p;
tail2 = p; //将尾指针tail1指向p
}
else if(p ->Data.exp == t ->Data.exp) //假如p指针指向的新节点的指数 等于 链表中一项的指数,就合并
{
t ->Data.coef += p ->Data.coef;
Size2 --; //链表节点减一
}
else //如果p指针指向的新节点的指数 小于 t指针所指向的节点,就插入到它的前面
{
p ->next = t;
r ->next = p;
}
}
}
}
void print1() //输出链表一
{
Node *p = NULL;

cout<<"共有"<<Size1<<"项,结果如下:"<<endl;
p = head1 ->next;
while(p != NULL)
{
cout<<"["<<p ->Data.coef<<" "<<p ->Data.exp<<"] ";
p = p ->next;
}
cout<<endl;
}
void print2() //输出链表二
{
Node *p = NULL;
cout<<"共有"<<Size2<<"项,结果如下:"<<endl;
p = head2 ->next;
while(p != NULL)
{
cout<<"["<<p ->Data.coef<<" "<<p ->Data.exp<<"] ";
p = p ->next;
}
cout<<endl;
}
void Add() //两链表相加
{
Node *per = head1;
Node *p = per ->next;

Node *q = head2 ->next;
Node *del = NULL;

while((p != NULL) && (q != NULL))//当p或q有一个为空时
{
if(p ->Data.exp < q ->Data.exp) //p结点是和多项式中的一项 p后移,q不动
{
per = p;
p = p ->next;
}
else if(p ->Data.exp > q ->Data.exp)//q结点是和多项式中的一项 将q插在p之前,q后移,p不动

{
per ->next = q;
per = q;
q = q ->next;
per ->next = p;
Size1++;
Size2--;
}
else
{
p ->Data.coef += q ->Data.coef;//修改p系数域, 释放q,p,q后移

if(p ->Data.coef == 0)//当系数为零时,从1表中删去p, 释放p,q,p,q后移
{
del = p;
p = p ->next;
delete del;
per ->next = p;
Size1--;
}
del = q;
q = q ->next;
delete del;
Size2--;
}
}
if(p == NULL)//将表2中剩余部分连到表1上
{
per ->next = q;
Size1 += Size2;
}
head2 = NULL;
}
};
int main()
{
LinkList myList;

myList.Insert();
system("cls");
cout<<"\n单项式的格式:[系数 指数]\n"<<endl;
cout<<"\n合并同类项后……"<<endl;
cout<<"-----------------------------------------------------------"<<endl;
cout<<"\n多项式一";
myList.print1();
cout<<"\n多项式二";
myList.print2();
cout<<"-----------------------------------------------------------"<<endl;
myList.Add();
cout<<"\n多项式的和";
myList.print1();
system("PAUSE");

return 0;
}
哎!缩进也没了!你还是把它复制运行一下看看!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-25
要写好注释,这么长的程序,变量的命名也不尽标准,要看明白很头疼的
相似回答