题目:应用单链表实现一元多项式的相关操作。
[实验目的]
应用单链表解决实际应用问题。
实验内容及要求:
1、使用自己已定义的单链表模板类。
2、编写程序实现一元多项式的输入、输出、增加项、删除项和加法运算。
3、为便于观察程序的运行结果,设计的输出函数能在输出设备上以图形或表格或其它直观的形式展现、存储计算结果。
4、为程序制定测试方案,对所有输入变量取遍各种有代表性的值。
//SLList.h
//SLList.cpp
[实验目的]
应用单链表解决实际应用问题。
实验内容及要求:
1、使用自己已定义的单链表模板类。
2、编写程序实现一元多项式的输入、输出、增加项、删除项和加法运算。
3、为便于观察程序的运行结果,设计的输出函数能在输出设备上以图形或表格或其它直观的形式展现、存储计算结果。
4、为程序制定测试方案,对所有输入变量取遍各种有代表性的值。
5、为了增强程序的可读性,程序中要有适当的注释。
//main.cpp
#include <iostream>
#include <windows.h>
#include <conio.h>
#include "SLList.h"
using namespace std;
void Add(SLList a,SLList b,SLList& temp);
int main()
{
system("color 4f");
int n=0;
float coef=0;
int exp=0;
SLList a; //多项式输入
cout<<"请输入第一个多项式的项数 :\n\n";
cin>>n;
cout<<"\n\n请输入第一个多项式的各项系数和指数:"<<endl;
for(int i=0;i<n;i++)
{
cin>>coef>>exp;
a.Insert(coef,exp);
}
cout<<"\n\n\n\n按任意键继续......";
getch();
system("cls");
cout<<"第一个多项式为 :\n\n"; //多项式的输出
a.Sort();
a.Print();
cout<<"\n\n\n\n给第一个多项式增加一个1*X项 :"<<"\n\n" ; //多项式增加、删除
a.Insert(1,1);
a.Sort();
a.Print();
cout<<"\n\n\n\n按任意键继续......";
getch();
system("cls");
SLList b; //多项式输入
cout<<"请输入第二个多项式的项数 :\n\n";
cin>>n;
cout<<"\n\n请输入第二个多项式的各项系数和指数:"<<endl;
for(int i=0;i<n;i++)
{
cin>>coef>>exp;
b.Insert(coef,exp);
}
cout<<"\n\n\n\n按任意键继续......";
getch();
system("cls");
cout<<"第二个多项式为 :\n\n"; //多项式的输出
b.Sort();
b.Print();
cout<<"\n\n\n\n给第二个多项式删除最后一项 :"<<"\n\n" ;
b.DeleteFromTail(coef,exp);
b.Sort();
b.Print();
cout<<"\n\n\n\n按任意键继续......";
getch();
system("cls");
cout<<"把两个多项式相加 :\n\n"; //多项式相加
a.Print();
cout<<"\n\n+\n\n";
b.Print();
cout<<"\n\n=\n\n";
SLList temp;
Add(a,b,temp);
temp.Sort();
temp.Print();
cout<<"\n\n\n\n\n\n\n\n按任意键结束......";
getch();
system("cls");
return 0;
}
void Add(SLList a,SLList b,SLList& temp)
{
int k=0,p=0;
float c=0;
int e[30]={-1};
SLNode* ta=a.GetHead();
SLNode* tb=b.GetHead();
while(ta!=NULL)
{
tb=b.GetHead();
while(tb!=NULL)
{
if(ta->exp==tb->exp)
{
c=ta->coef+tb->coef;
e[k]=ta->exp;
temp.Insert(c,e[k]);
k++;
}
tb=tb->next;
}
ta=ta->next;
}
ta=a.GetHead();
while(ta!=NULL)
{
p=0;
for(int i=0;i<30;i++)
if(ta->exp!=e[i])
p++;
if(p==30)
temp.Insert(ta->coef,ta->exp);
ta=ta->next;
}
tb=b.GetHead();
while(tb!=NULL)
{
p=0;
for(int i=0;i<30;i++)
if(tb->exp!=e[i])
p++;
if(p==30)
temp.Insert(tb->coef,tb->exp);
tb=tb->next;
}
}
//SLList.h
#ifndef SLLIST_H
#define SLLIST_H
struct SLNode
{
public:
float coef;
int exp;
SLNode*next;
SLNode(float c=0,int e=0,SLNode*nextnode=NULL):coef(c),exp(e),next(nextnode) { };
} ;
class SLList
{
private:
SLNode*head,*tail,*currptr;
int length;
public:
SLList();
~SLList(){clear();}
int Length()const {return length;}
bool IsEmpty()const{return head->next==NULL;}
SLNode*GetHead()const{return head->next;}
void clear();
bool Pre();
bool Next();
void SetStart(){currptr=head;}
void SetEnd(){currptr=tail;}
bool Insert(const float& coef,const int& exp);
bool InsertFromHead(const float& coef,const int& exp){SetStart();Insert(coef,exp);}
bool InsertFromTail(const float& coef,const int& exp){SetEnd();Insert(coef,exp);}
bool Delete(float& coef,int& exp);
bool DeleteFromHead(float& coef,int& exp){SetStart();Delete(coef,exp);}
bool DeleteFromTail(float& coef,int& exp){SetEnd();Pre();Delete(coef,exp);}
void Sort();
void Print();
};
#endif
//SLList.cpp
#include<iostream>
#include"SLList.h"
using namespace std;
SLList::SLList()
{
head=tail=currptr=new SLNode();
length=0;
}
void SLList::clear()
{
while(!IsEmpty())
{
currptr=head->next;
head->next=currptr->next;
delete currptr;
}
delete head;
}
bool SLList::Pre()
{
if(IsEmpty()||currptr==head){cout<<"no previous node!"<<endl;return false;}
SLNode*temp=head;
while(temp->next!=currptr)
temp=temp->next;
currptr=temp;
return true;
}
bool SLList::Next()
{
if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}
currptr=currptr->next;
return true;
}
bool SLList::Insert(const float& coef,const int& exp)
{
currptr->next=new SLNode(coef,exp,currptr->next);
if(currptr==tail) tail=currptr->next;
currptr=currptr->next;
length++;
return true;
}
bool SLList::Delete(float& coef,int& exp)
{
if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}
SLNode*temp=currptr->next;
currptr->next=temp->next;
coef=temp->coef;
exp=temp->exp;
if(temp==tail) tail=currptr;
delete temp;
length--;
return true;
}
void SLList::Sort()
{
SLNode *p,*p1,*p2,*p3;
SLNode t;
p=head;
while (p->next!=NULL)
p=p->next;
p=p->next=&t;
while (p!=head->next)
{
p3=head;
p1=p3->next;
p2=p1->next;
while (p2!=p)
{
if ((p1->exp)<(p2->exp))
{
p1->next=p2->next;
p2->next=p1;
p3->next=p2;
p3=p2;
p2=p1->next;
}
else
{
p3=p1;
p1=p2;
p2=p2->next;
}
}
p=p1;
}
while (p->next!=&t)
p=p->next;
p->next=NULL;
}
/*void SLList::Sort()
{
SLNode* t1,* t2,* t3,* t4;
t3=t4=head->next;
t1->next=t3;
t2=t3->next;
for(int i=0;i<length;i++)
{
for(int j=0;j<length-i;j++)
{
if(t3->exp<t2->exp)
{
t1->next=t2;
t3->next=t2->next;
t2->next=t3;
t2=t3->next;
t1=t1->next;
}
else
{
t2=t2->next;
t1=t1->next;
}
}
t3=t4=t4->next;
t1->next=t3;
t2=t3->next;
}
}*/
void SLList::Print()
{
SLNode*temp=head->next;
for(int i=0;i<length-1;i++)
{
cout<<temp->coef<<"* X^"<<temp->exp<<" + ";
temp=temp->next;
}
cout<<temp->coef<<"* X^"<<temp->exp;
}