多项式计算

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

以下是源代码:

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

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

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

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

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

  }

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

  
}
//链栈定义结束.


//链队列定义开始
template <class Queue_entry>
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<Queue_entry> *front,*rear;
};
template <class Queue_entry>
Queue<Queue_entry>::Queue()
{
 front=rear=NULL;
}
template <class Queue_entry>
bool Queue<Queue_entry>::empty()const
{
 if (front==NULL)
  return true;
 else return false;
}
template <class Queue_entry>
Error_code Queue<Queue_entry>::append(const Queue_entry &item)
{
 Node<Queue_entry> *new_rear=new Node<Queue_entry>(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 <class Queue_entry>
Error_code Queue<Queue_entry>::serve()
{
 if (front==NULL)return underflow;
 Node<Queue_entry> *old_front=front;
 front=old_front->next;
 if (front==NULL)rear=NULL;
 delete old_front;
 return success;
}
template <class Queue_entry>
Error_code Queue<Queue_entry>::retrieve(Queue_entry &item)const
{
 if (front==NULL)return fail;
 item=front->entry;
 return success;
}

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

  }

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

  }

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


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

}
template <class Queue_entry>
void Extened_queue<Queue_entry>::clear()
{
 while (serve()==success);

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

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

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

#include "link.h"
#include <iostream.h>
#include <cctype>
#include <stdio.h>

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 Queue_entry>
class Polynomial:private Extened_queue<Term>
{
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 <class Queue_entry>
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的指数,"
  <<"依次输入每一项,并且按指数递减输入."<<endl
  <<"输入系数0或指数0来完成一个多项式的输入"<<endl;
 do
 {
  cout<<"请输入系数:"<<flush;
  cin>>coefficient;
  if (coefficient!=0.0)
  {
   cout<<"请输入指数:"<<flush;
   cin>>exponent;
   if ((!first_term&&exponent>=last_exponent)||exponent<0)
   { 
    exponent=0;
    cout<<"错误的指数输入,将丢失最近输入的一项."
     <<endl;
   }
   else
   {
    Term new_term(exponent,coefficient);
    append(new_term);
    first_term=false;

   }
   last_exponent=exponent;
  }
  
 }while (coefficient!=0.0&&exponent!=0);
}
//template <class Queue_entry>
void Polynomial::print()const
{
 Node<Term> *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<<r;
  if (print_term.degree>1||print_term.degree<0)cout<<"X^"<<print_term.degree;
  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<<endl;
}
//template <class Queue_entry>//加法
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<Term> *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<Term> *f=front;
 Node<Term> *r;
 Node<Term> *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 <class Queue_entry>
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]"<<endl;
 cout<<"(C) 版权所有 2005-2007 Lthsoft Corp. 作者:usufu "<<endl<<endl;
}
void instructions()
{
 cout<<"帮助"<<endl;
 cout<<"请先输入命令,然后按提示操作."<<endl
  <<"命令说明:"<<endl
  <<"?:输入多项式."<<endl
  <<"=:输出结果."<<endl
  <<"+:做加法运算."<<endl
  <<"-:做减法运算."<<endl
  <<"*:做乘法运算."<<endl
  <<"/:做除法运算."<<endl
  <<"q:退出程序."<<endl
  <<endl<<"祝您使用愉快!"<<endl<<endl;
}
char get_command()
{
 char command;
 bool waiting=true;
 cout<<"请输入命令,并按<Eenter>键:"<<endl;
 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<<"您输入的命令不正确,请输入一个正确的命令:"<<endl
  <<"?:输入多项式. "
  <<"=:输出结果."<<endl
  <<"+:做加法运算. "
  <<"-:做减法运算."<<endl
  <<"*:做乘法运算. "
  <<"/:做除法运算. "<<"q:退出程序."<<endl<<endl;

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

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

 }
 return true;
}

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

#include "fun.h"

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值