在学习了链栈和链队列以后,试着完成了多项式计算的问题,除法的计算有些问题,还望高人指点.
以下是源代码:
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
{
next=NULL;
}
template
Node
{
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
};
template
Stack
{
top_node=NULL;
}
template
bool Stack
{
if (top_node==NULL)
return true;
else return false;
}
template
Error_code Stack
{
Node
if(new_top==NULL)return overflow;
top_node =new_top;
return success;
}
template
Error_code Stack
{
Node
if (top_node==NULL)return underflow;
top_node=old_top->next;
delete old_top;
return success;
}
template
Error_code Stack
{
if (top_node==NULL)return fail;
item=top_node->entry;
return success;
}
template
Stack
{
while(pop()==success);
}
template
Stack
{
Node
if (original_node==NULL)top_node=NULL;
else{
top_node=new_copy=new Node
while (original_node->next!=NULL)
{
original_node=original_node->next;
new_copy->next=new Node
new_copy=new_copy->next;
}
}
}
template
void Stack
{
Node
if (original_node==NULL)new_top=NULL;
else
{
new_copy=new_top=new Node
while (original_node->next!=NULL)
{
original_node=original_node->next;
new_copy->next=new Node
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
};
template
Queue
{
front=rear=NULL;
}
template
bool Queue
{
if (front==NULL)
return true;
else return false;
}
template
Error_code Queue
{
Node
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
{
if (front==NULL)return underflow;
Node
front=old_front->next;
if (front==NULL)rear=NULL;
delete old_front;
return success;
}
template
Error_code Queue
{
if (front==NULL)return fail;
item=front->entry;
return success;
}
template
Queue
{
while (serve()==success);
}
template
Queue
{
Node
if (original_node== NULL) front=NULL;
else
{
front=new_copy=new Node
while (original_node->next!=NULL)
{
original_node=original_node->next;
new_copy->next=new Node
new_copy=new_copy->next;
}
}
rear=new_copy;
}
template
void Queue
{
Node
if (original_node==NULL)new_front=NULL;
else
{
new_copy=new Node
new_front=new_copy;
while (original_node->next!=NULL)
{
original_node=original_node->next;
new_copy->next=new Node
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
{
Node
int count=0;
while (window!=NULL)
{
window=window->next;
count++;
}
return count;
}
template
void Extened_queue
{
while (serve()==success);
}
template
Error_code Extened_queue
{
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的指数,"
<<"依次输入每一项,并且按指数递减输入."<
{
cout<<"请输入系数:"<
if (coefficient!=0.0)
{
cout<<"请输入指数:"<
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
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 (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
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
Node
Node
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; } } } polynomial.cpp文件,主函数,很简单,只有几行 #include "fun.h" void main()
void introduction()
{
cout<<"多项式计算器[版本:1.0.0426]"<
void instructions()
{
cout<<"帮助"<
char get_command()
{
char command;
bool waiting=true;
cout<<"请输入命令,并按
{
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
{
//Polynomial p,q,r,y;
switch (command)
{
case '?':
p.read();
if (stored_polynomials.push(p)==overflow)
cout<<"警告:系统内存空间不够,将丢失你刚才输入的数据."<
case '=':
if (stored_polynomials.empty())
cout<<"无数据"<
else
{
stored_polynomials.top(p);
p.print();
if (!y.isempty()){cout<<"余项是:"<
break;
case '+':
if (stored_polynomials.empty())
cout<<"无数据,无法计算"<
{
stored_polynomials.top(p);
stored_polynomials.pop();
if (stored_polynomials.empty())
{cout<<"只有一个多项式,无法计算."<
{
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<<"无数据,无法计算"<
{
stored_polynomials.top(p);
stored_polynomials.pop();
if (stored_polynomials.empty())
{cout<<"只有一个多项式,无法计算."<
{
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<<"无数据,无法计算"<
{
stored_polynomials.top(p);
stored_polynomials.pop();
if (stored_polynomials.empty())
{cout<<"只有一个多项式,无法计算."<
{
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<<"无数据,无法计算"<
{
stored_polynomials.top(p);
stored_polynomials.pop();
if (stored_polynomials.empty())
{cout<<"只有一个多项式,无法计算."<
{
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 true;
}
{
Stack
introduction();
instructions();
while (do_command(get_command(),stored_polynomials));
}