多项式计算

在学习了链栈和链队列以后,试着完成了多项式计算的问题,除法的计算有些问题,还望高人指点.

以下是源代码:

link.h//链栈及链队列,扩展链队列模板类定义

enum Error_code{success,fail,overflow,underflow};
#define NULL 0
//节点定义开始
template
struct Node
{
 Node_entry entry;
 Node *next;
 Node();
 Node(Node_entry item,Node *add_on=NULL);
};
template
Node::Node()
{
 next=NULL;
}
template
Node::Node(Node_entry item,Node *add_on)
{
 entry=item;
 next=add_on;
}
//节点定义结束

//链栈模板定义开始
template
class Stack
{
public:
 Stack();
 bool empty()const;
 Error_code push(const Stack_entry &item);
 Error_code pop();
 Error_code top(Stack_entry &item)const;
 ~Stack();
 Stack(const Stack &original);
 void operator=(const Stack &original);
protected:
 Node *top_node;
};
template
Stack::Stack()
{
 top_node=NULL;
 
}
template
bool Stack::empty()const
{
 if (top_node==NULL)
  return true;
 else return false;
}
template
Error_code Stack::push(const Stack_entry &item)
{
 Node *new_top=new Node(item,top_node);
 if(new_top==NULL)return overflow;
 top_node =new_top;
 return success;
}
template
Error_code Stack::pop()
{
 Node *old_top=top_node;
 if (top_node==NULL)return underflow;
 top_node=old_top->next;
 delete old_top;
 return success;
}
template
Error_code Stack::top(Stack_entry &item)const
{
 if (top_node==NULL)return fail;
 item=top_node->entry;
 return success;

}
template
Stack::~Stack()
{
 while(pop()==success);
}
template
Stack::Stack(const Stack &original)
{
 Node *new_copy,*original_node=original.top_node;
 if (original_node==NULL)top_node=NULL;
 else{
  top_node=new_copy=new Node(original_node->entry);
  while (original_node->next!=NULL)
  {
   original_node=original_node->next;
   new_copy->next=new Node(original_node->entry);
   new_copy=new_copy->next;
  }
 }

}
template
void Stack::operator=(const Stack &original)
{
 Node *new_top,*new_copy,*original_node=original.top_node;
 if (original_node==NULL)new_top=NULL;
 else
 {
  new_copy=new_top=new Node(original_node->entry);
  while (original_node->next!=NULL)
  {
   original_node=original_node->next;
   new_copy->next=new Node(original_node->entry);
   new_copy=new_copy->next;

  }

 }
 while (pop()==success);
 top_node=new_top;

  
}
//链栈定义结束.


//链队列定义开始
template
class Queue
{
public:
 Queue();
 bool empty()const;
 Error_code append(const Queue_entry &item);
 Error_code serve();
 Error_code retrieve(Queue_entry &item)const;
 ~Queue();
 Queue(const Queue &original);
 void operator=(const Queue &original);
protected:
 Node *front,*rear;
};
template
Queue::Queue()
{
 front=rear=NULL;
}
template
bool Queue::empty()const
{
 if (front==NULL)
  return true;
 else return false;
}
template
Error_code Queue::append(const Queue_entry &item)
{
 Node *new_rear=new Node(item);
 if (new_rear==NULL)return overflow;
 if (rear==NULL)front=rear=new_rear;
 else
 {
  rear->next=new_rear;
  rear=new_rear;
 }
 return success;
}
template
Error_code Queue::serve()
{
 if (front==NULL)return underflow;
 Node *old_front=front;
 front=old_front->next;
 if (front==NULL)rear=NULL;
 delete old_front;
 return success;
}
template
Error_code Queue::retrieve(Queue_entry &item)const
{
 if (front==NULL)return fail;
 item=front->entry;
 return success;
}

template
Queue::~Queue()
{
 while (serve()==success);
}
template
Queue::Queue(const Queue &original)
{
 Node *new_copy,*original_node=original.front;
 if (original_node== NULL) front=NULL;
 else
 {
  front=new_copy=new Node(original_node->entry);
  while (original_node->next!=NULL)
  {
   original_node=original_node->next;
   new_copy->next=new Node(original_node->entry);
   new_copy=new_copy->next;

  }

 }
 rear=new_copy;
}
template
void Queue::operator =(const Queue &original)
{
 Node *new_front,*new_copy,*original_node=original.front;
 if (original_node==NULL)new_front=NULL;
 else
 {
  new_copy=new Node(original_node->entry);
  new_front=new_copy;
  while (original_node->next!=NULL)
  {
   original_node=original_node->next;
   new_copy->next=new Node(original_node->entry);
   new_copy=new_copy->next;

  }

 }
 while (serve()==success);
 front=new_front;
 rear=new_copy;
}
//链队列定义结束.


//扩展链队列定义开始.
template
class Extened_queue:public Queue
{
public:
 int size()const;
 void clear();
 Error_code serve_and_retrieve(Queue_entry &item);
};
template
int Extened_queue::size()const
{
 Node *window=front;
 int count=0;
 while (window!=NULL)
 {
  window=window->next;
  count++;
 }
 return count;

}
template
void Extened_queue::clear()
{
 while (serve()==success);

}
template
Error_code Extened_queue::serve_and_retrieve(Queue_entry &item)
{
 if (front==NULL)return fail;

 retrieve(item);
 serve();
 return success;
}

Polynomial.h//多项式类的定义.

#include "link.h"
#include
#include
#include

struct Term
{
 int degree;
 double coefficient;
 Term(int exponent=0,double scalar=0);
};
Term::Term(int exponent,double scalar)
{
 degree=exponent;
 coefficient=scalar;
}
//template
class Polynomial:private Extened_queue
{
public:
 void read();
 void print()const;
 void equals_sum(Polynomial p,Polynomial q);
 void equals_difference(Polynomial p,Polynomial q);
 void equals_product(Polynomial p,Polynomial q);
 Error_code equals_quotient(Polynomial p,Polynomial q,Polynomial &y);
 int degree()const;
 bool isempty()const;
 void quyu();
private:
 void mult_term(Polynomial p,Term t);
 
 
};
//template
bool Polynomial::isempty()const//不空返回假,空返回真
{
 if (!empty())
  return false;
 else return true;
}


void Polynomial::mult_term(Polynomial p,Term t)
{
 Term p_term;
 clear();
 while (!p.empty())
 {
  p.serve_and_retrieve(p_term);
  Term answer(p_term.degree+t.degree,p_term.coefficient*t.coefficient);
  append(answer);

 }
}

void Polynomial::read()
{
 clear();
 double coefficient;
 int last_exponent,exponent;
 bool first_term=true;
 cout<<"请输入多项式的系数和X的指数,"
  <<"依次输入每一项,并且按指数递减输入."<  <<"输入系数0或指数0来完成一个多项式的输入"< do
 {
  cout<<"请输入系数:"<  cin>>coefficient;
  if (coefficient!=0.0)
  {
   cout<<"请输入指数:"<   cin>>exponent;
   if ((!first_term&&exponent>=last_exponent)||exponent<0)
   { 
    exponent=0;
    cout<<"错误的指数输入,将丢失最近输入的一项."
     <   }
   else
   {
    Term new_term(exponent,coefficient);
    append(new_term);
    first_term=false;

   }
   last_exponent=exponent;
  }
  
 }while (coefficient!=0.0&&exponent!=0);
}
//template
void Polynomial::print()const
{
 Node *print_node=front;
 bool first_term=true;
 while (print_node!=NULL)
 {
  Term &print_term=print_node->entry;
  if (first_term)
  {
   first_term=false;
   if (print_term.coefficient<0)cout<<"-";
  }
  else if (print_term.coefficient<0)cout<<"-";
  else cout<<"+";
  //if (print_term.coefficient=0.0)break;
  double r=(print_term.coefficient>0)
   ?print_term.coefficient:-(print_term.coefficient);
  
  if (r!=1)cout<  if (print_term.degree>1||print_term.degree<0)cout<<"X^"<  if (print_term.degree==1)cout<<"X";
  if (r==1&&print_term.degree==0)cout<<"1";
  print_node=print_node->next;
 }
 if (first_term)
  cout<<"0";
 cout<}
//template //加法
void Polynomial::equals_sum(Polynomial p,Polynomial q)
{
 clear();
 while (!p.empty()||!q.empty())
 {
  Term p_term,q_term;
  if (p.degree()>q.degree())
  {
   p.serve_and_retrieve(p_term);
   append(p_term);
  }
  else if (q.degree()>p.degree())
  {
   q.serve_and_retrieve(q_term);
   append(q_term);
  }
  else
  {
   p.serve_and_retrieve(p_term);
   q.serve_and_retrieve(q_term);
   if (p_term.coefficient+q_term.coefficient!=0)
   {
    Term answer_term(p_term.degree,p_term.coefficient+q_term.coefficient);
    append(answer_term);
   }
  }
 }
}

void Polynomial::equals_difference(Polynomial p,Polynomial q)//减法

 Term p_term,q_term;
 clear();
 while (!p.empty()||!q.empty())
 {
  if (p.degree()>q.degree())
  {
   p.serve_and_retrieve(p_term);
   append(p_term);
  }
  else if (q.degree()>p.degree())
  {
   q.serve_and_retrieve(q_term);
   append(q_term);
  }
  else
  {
   p.serve_and_retrieve(p_term);
   q.serve_and_retrieve(q_term);
   if (p_term.coefficient-q_term.coefficient!=0)
   {
    Term answer_term(p_term.degree,p_term.coefficient-q_term.coefficient);
    append(answer_term);
   }

}}}
void Polynomial::equals_product(Polynomial p,Polynomial q)//乘法
{
 Term p_term,q_term;
 clear();
 Polynomial y;
 y=q;
 //Node *old_front=front;
 while (!p.empty())
 {
  p.retrieve(p_term);
  while (!q.empty())
  {
   q.retrieve(q_term);
   Term answer_term(p_term.degree+q_term.degree,p_term.coefficient*q_term.coefficient);
   append(answer_term);
   q.serve();
   
  }
  q=y;
  p.serve();
 }
 q.clear();

 Node *f=front;
 Node *r;
 Node *del_r,*front_f,*front_r;
 while (f->next!=NULL)
 {
  r=f->next;
  front_r=f;
  while (r!=NULL)
  {
   
   
    
   if (r->entry.degree==f->entry.degree)
   {
    del_r=r;
    f->entry.coefficient=f->entry.coefficient+r->entry.coefficient;
    

    front_r->next=r->next;
    
    r=r->next;
    front_r=r;
    delete del_r;
   }
   else
   {
   front_r=r;
   r=r->next;
   }
  }
   if (f->entry.coefficient==0)
    {
     front_f->next=f->next;
     delete f;
     f=front_f;
     r=front->next;
    }
  front_f=f;
  f=f->next;
 }
      
 
 
}

Error_code Polynomial::equals_quotient(Polynomial p,Polynomial q,Polynomial &y)

 clear();
 y.clear();
 Term p_term,q_term,k_term;
 Polynomial temp;
 p.retrieve(p_term);//bei
 q.retrieve(q_term);

 while (!p.empty())
 {
  
  if (p_term.degree>=q_term.degree)
  {
  k_term.degree=p_term.degree-q_term.degree;
  k_term.coefficient=p_term.coefficient/q_term.coefficient;
  if (k_term.degree!=0)
   append (k_term);
  temp.mult_term(q,k_term);
  p.equals_difference(p,temp);
  
  if (p.size()==0)
  {append(k_term);break;}
  else
  {
   y=p;
   break;
  }
   p.retrieve(p_term);
  }
  else
  {
   k_term.degree=p_term.degree-q_term.degree;
      k_term.coefficient=p_term.coefficient/q_term.coefficient;
   append(k_term);
   temp.mult_term(q,k_term);
   p.equals_difference(p,temp);
   //p.serve_and_retrieve(p_term);
   y=p;
   break;
   

  }
 }
 
 //y.print();
 
 
 
 return success;}


//template
int Polynomial::degree()const
{
 if (empty())return -1;
 Term lead;
 retrieve(lead);
 return lead.degree;
}

fun.h功能函数的实现,如输入命令啊,提示啊,等等

#include "Polynomial.h"

Polynomial p,q,r,y;
void introduction()
{
 cout<<"多项式计算器[版本:1.0.0426]"< cout<<"(C) 版权所有 2005-2007 Lthsoft Corp. 作者:usufu "<}
void instructions()
{
 cout<<"帮助"< cout<<"请先输入命令,然后按提示操作."<  <<"命令说明:"<  <<"?:输入多项式."<  <<"=:输出结果."<  <<"+:做加法运算."<  <<"-:做减法运算."<  <<"*:做乘法运算."<  <<"/:做除法运算."<  <<"q:退出程序."<  <}
char get_command()
{
 char command;
 bool waiting=true;
 cout<<"请输入命令,并按键:"< while (waiting)
 {
 cin>>command;
 while (cin.get()!='/n')
  continue;
 command=tolower(command);
 
 if (command=='?'||command=='+'||command=='-'||command=='*'||command=='/'||command=='q'||command=='=')waiting=false;
 else {
 cout<<"您输入的命令不正确,请输入一个正确的命令:"<  <<"?:输入多项式. "
  <<"=:输出结果."<  <<"+:做加法运算. "
  <<"-:做减法运算."<  <<"*:做乘法运算. "
  <<"/:做除法运算. "<<"q:退出程序."<

 }
 }
 return command;
}
bool do_command(char command,Stack &stored_polynomials)
{
 //Polynomial p,q,r,y;
 
 switch (command)
 {
 case '?':
  p.read();
  if (stored_polynomials.push(p)==overflow)
   cout<<"警告:系统内存空间不够,将丢失你刚才输入的数据."<  break;
 case '=':
  if (stored_polynomials.empty())
   cout<<"无数据"<  
  
  else
  {
   stored_polynomials.top(p);
   p.print();
   
   if (!y.isempty()){cout<<"余项是:"<

  }
  
  break;
 case '+':
  if (stored_polynomials.empty())
   cout<<"无数据,无法计算"<  else
  {
   stored_polynomials.top(p);
   stored_polynomials.pop();
   if (stored_polynomials.empty())
   {cout<<"只有一个多项式,无法计算."<   else
   {
    stored_polynomials.top(q);
    stored_polynomials.pop();
    r.equals_sum(q,p);
    if (stored_polynomials.push(r)==overflow)
     cout<<"警告:系统内存空间不够,将丢失结果."<   }
  }
  break;
 case '-':
  if (stored_polynomials.empty())
   cout<<"无数据,无法计算"<  else
  {
   stored_polynomials.top(p);
   stored_polynomials.pop();
   if (stored_polynomials.empty())
   {cout<<"只有一个多项式,无法计算."<   else
   {
    stored_polynomials.top(q);
    stored_polynomials.pop();
    r.equals_difference(q,p);
    if (stored_polynomials.push(r)==overflow)
     cout<<"警告:系统内存空间不够,将丢失结果."<   }
  }
  break;
 case '*':
  if (stored_polynomials.empty())
   cout<<"无数据,无法计算"<  else
  {
   stored_polynomials.top(p);
   stored_polynomials.pop();
   if (stored_polynomials.empty())
   {cout<<"只有一个多项式,无法计算."<   else
   {
    stored_polynomials.top(q);
    stored_polynomials.pop();
    r.equals_product(q,p);
    if (stored_polynomials.push(r)==overflow)
     cout<<"警告:系统内存空间不够,将丢失结果."<   }
  }
  break;
 case '/':
  if (stored_polynomials.empty())
   cout<<"无数据,无法计算"<  else
  {
   stored_polynomials.top(p);
   stored_polynomials.pop();
   if (stored_polynomials.empty())
   {cout<<"只有一个多项式,无法计算."<   else
   {
    stored_polynomials.top(q);
    stored_polynomials.pop();
    r.equals_quotient(q,p,y);
   // y.print();
    if (stored_polynomials.push(r)==overflow)
     cout<<"警告:系统内存空间不够,将丢失结果."<   }
  }
  break; 
 case 'q':
  cout<<"感谢使用Lthsoft软件."<  return false;

 }
 return true;
}

polynomial.cpp文件,主函数,很简单,只有几行

#include "fun.h"

void main()
{
 Stack stored_polynomials;
 introduction();
 instructions();
 while (do_command(get_command(),stored_polynomials));
}

你可能感兴趣的:(算法与数据结构,C/C++)