我必须实现一个程序,该程序计算给定值X的多项式的值,反转多项式,加法,减法和两个多项式的乘法。两个多项式可以具有不同的度数 并非所有术语都保证具有非空系数。 我设法做了一些事情,但我一直坚持实施加法,减法和乘法运算。
的main.cpp
#ifndef POLI_H
#define POLI_H
#include <queue.h>
#include <Stack.h>
template<typename T>
class queue;
struct term{
int grad;
double coef;
};
template<typename T>
class poli
{
public:
queue<T> p;
struct term *ter;
int power;
public:
poli(int a);
~poli();
void invert();
void read();
double val_pol(int a);
void display();
void sort();
void set_power(int a);
int get_power();
};
#endif // POLI_H
poli.h
#include "poli.h"
#include <iostream>
#include <Stack.h>
#include <math.h>
#include <queue.h>
using namespace std;
template<typename T>
poli<T>::poli(int a)
{
power = a;
ter = new term[power+1];
int i;
for(i=0 ; i<=power ; i++)
ter[i].coef = 0;
}
template<typename T>
poli<T>::~poli(){}
template<typename T>
void poli<T>:: read(){
for(int i=power;i>=0;i--)
{
cout<<"Introduceti gradul ";
cin>>ter[i].grad;
while(ter[i].grad>power)
cin>>ter[i].grad;
cout<<"Introduceti coeficientul";
cin>>ter[i].coef;
p.enqueue(ter[i]);
if(ter[i].grad==power)
return;
}
}
template<typename T>
void poli<T>::sort(){
for(int i=0;i<power;i++)
for(int j=i;j<=power;j++)
if(ter[j].grad>ter[i].grad)
{
term aux=ter[i];
ter[i]=ter[j];
ter[j]=aux;
}
}
template<typename T>
void poli<T>:: display(){
for(int i=0;i<=power;i++)
if(ter[i].coef!=0)
{
if(ter[i].grad==0)
cout<<ter[i].coef;
else
if(ter[i].coef==1)
cout<<"x^"<<ter[i].grad;
else
cout<<ter[i].coef<<"*x^"<<ter[i].grad;
if(i<p.get_size())
cout<<"+";
}
}
template<typename T>
double poli<T>:: val_pol(int a){
Stack<double> s;
for(int i=0;i<power;i++)
if(ter[i].grad==0)
s.push(ter[i].coef);
else
s.push(s.peek()+ter[i].coef*pow(a,ter[i].grad));
return s.peek();
}
template<typename T>
void poli<T>::invert(){
int x=power;
if(p.get_size()<=1)
return;
else
for(int i=0;i<power/2+1;i++)
{ double aux=ter[i].coef;
ter[i].coef=ter[x].coef;
ter[x].coef=aux;
x--;
}
}
template<typename T>
void poli<T>:: set_power(int a){
power=a;
}
template<typename T>
int poli<T>:: get_power(){
return power;
}
poli.cpp
#ifndef STACK_H
#define STACK_H
#define NMAX 10
#include <poli.h>
#include <queue.h>
template<typename T>
class queue;
template<typename T>
class poli;
template<typename T>
class Stack {
private:
T stackArray[NMAX];
int topLevel;
public:
void push(T x);
int isEmpty();
T pop();
T peek();
T sort();
T searchNum (T num);
void display();
T sorting();
void searchOdd();
void searchEven();
Stack();
~Stack();
void add(queue<poli<T> >& p1, queue<poli<T> >& p2);
};
#endif // STACK_H
Stack.h
#include "Stack.h"
#include <iostream>
#include <poli.h>
#include <queue.h>
using namespace std;
template<typename T>
Stack<T>::Stack() {
topLevel = -1;
}
template<typename T>
Stack<T>::~Stack() {
}
template<typename T>
void Stack<T>::push(T x) {
if (topLevel >= NMAX - 1)
{
cout<<"The stack is full: we have already NMAX elements!\n";
return;
}
stackArray[++topLevel] = x;
}
template<typename T>
int Stack<T>::isEmpty() {
return (topLevel < 0);
}
template<typename T>
T Stack<T>::pop() {
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[--topLevel];
}
template<typename T>
T Stack<T>::peek() {
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel];
}
template<typename T>
void Stack<T>::display()
{
if(isEmpty()){
cout<<"the stack is empty"<<endl;
return;
}
for(int i=0;i<=topLevel;i++)
cout<<stackArray[i]<<" ";
cout<<endl;
}
template<typename T>
T Stack<T>::sorting()
{
if(isEmpty()){
cout<<"the stack is empty"<<endl;
return 0;
}
for(int i=0;i<topLevel;i++)
for(int j=i+1;j<=topLevel;j++)
if(stackArray[i]>stackArray[j])
swap(stackArray[i],stackArray[j]);
}
template<typename T>
T Stack<T>::searchNum (T num)
{
for(int i=0;i<=topLevel;i++)
if(stackArray[i]==num) return 1;
return 0;
}
template<typename T>
void Stack<T>:: add(queue<poli<T> >& p1, queue<poli<T> >& p2){
int n,ok=-1;;
if(p1.get_size()>=p2.get_size())
{for(int i=0;i<p1.get_size();i++)
pop(p1.dequeue());
ok=1;}
else
if(p1.get_size()<=p2.get_size())
{for(int i=0;i<p2.get_size();i++)
pop(p2.dequeue());
ok=0;}
if(ok==1)
for(int i=0;i<p2.get_size();i++)
for(int j=0;j<p1.get_size();j++)
if(p1.get_grad()==p2.get_grad())
stackArray[j]=stackArray[j]+p1.dequeue();
}
Stack.cpp
#ifndef QUEUE_H
#define QUEUE_H
#define NMAX 10
#include <Stack.h>
template <typename T>
class queue
{
public:
T queueArray[NMAX];
int head, tail,size;
public:
queue();
~queue();
void display();
T peek();
T dequeue();
void enqueue(T x);
bool isEmpty();
int get_size();
};
#endif // QUEUE_H
queue.h
#include "queue.h"
#include <iostream>
using namespace std;
template<typename T>
queue<T>::queue() {
head = tail = size = 0;
}
template<typename T>
int queue<T>::get_size(){
return size;
}
template<typename T>
bool queue<T>::isEmpty() {
return (size==0);
}
template<typename T>
void queue<T>::enqueue(T x) {
if (size >= NMAX) {
cout << "The queue is FULL" << endl;
return;
}
queueArray[tail] = x;
tail=(tail+1)%NMAX;
size++;
}
template<typename T>
T queue<T>::dequeue() {
if (isEmpty()) {
cout << "The queue is EMPTY" << endl;
return 0;
}
T x = queueArray[head];
head=(head+1)%NMAX;
size--;
return x;
}
template<typename T>
T queue<T>::peek() {
if (isEmpty()) {
cout << "The queue is EMPTY" << endl;
return 0;
}
return queueArray[head];
}
template<typename T>
void queue<T>:: display() {
if(head<tail)
for(int i=head;i<tail;i++)
cout<<queueArray[i]<<" ";
else{
for(int i=head;i<NMAX;i++)
cout<<queueArray[i]<<" ";
for(int i=0;i<tail;i++)
cout<<queueArray[i]<<" ";
}
}
template<typename T>
queue<T>::~queue(){}
/*template<typename T>
Stack<T> queue<T>:: sum(queue<poli> p){
Stack<term> sum[20];
int n;
if(p1.get_size()>p2.get_size())
n=p2.get_size();
else
if(p1.get_size()<p2.get_size())
n=p2.get_size();
else
if(p1.get_size()==p2.get_size())
for(int i=0;i<)
}*/
queue.cpp
ylim = axes.get_ylim()
new_ylim = (min(ylim[0],-1), max(ylim[1], 1) )
axes.set_ylim(new_ylim)
这是我的代码。我没有错误,但我的val_pol()函数不起作用(它说p1.val_pol(3)是nan而p2.val_pol(3)是0,我也有实现加法,减法和乘法的问题。我是什么val_pol()做错了,我该如何实现这些功能呢?