0

I have to implement a program that computes the value of the polynomial given a value X, inverts the polynomial, the addition, the subtraction and the multiplication of two polynomials. The two polynomials can have different degrees and not all terms will be guaranteed to have non-null coefficients. I managed to do something, but I got stuck on implementing the addition, subtraction and multiplication operations.

main.cpp

#include <iostream>
#include <queue.h>
#include <queue.cpp>
#include <poli.h>
#include <poli.cpp>
#include <Stack.h>
#include <Stack.cpp>
using namespace std;

int main()
{

poli<term> p1(3);

poli<term> p2(4);

cout<<"Polinomul p1:"<<endl;
p1.read();
cout<<endl;
cout<<"Polinomul p2:"<<endl;
p2.read();
p1.sort();
p2.sort();

cout<<"Valoare in punctul x=3 a polinomului p1 este:"<<p1.val_pol(3)<<endl;
cout<<"Valoare in punctul x=3 a polinomului p2 este:"<<p2.val_pol(3)<<endl;

p1.display();
p1.invert();
cout<<endl;
p1.display();
cout<<endl;

p2.display();
p2.invert();
cout<<endl;
p2.display();

Stack<term> p3;

return 0;
}

poli.h

#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.cpp

#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;
    }

Stack.h

#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.cpp

#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();






}

queue.h

#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.cpp

#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<)

}*/

This is my code. I get no errors, but my val_pol() function does not work(it says p1.val_pol(3) is nan and p2.val_pol(3) is 0 and I also have a problem with implementing addition, subtraction and multiplication. What I did wrong with val_pol() and how should i implement those functions?

  • 1
    I recommend replacing your arrays with `std::vector`. With `std::vector`, you don't have to manage memory (such as allocation and deallocation). – Thomas Matthews Mar 29 '16 at 20:17
  • I need you to use a debugger on your code since I'm having issues with mine. Please post which statement is causing the issue (don't specify line number unless you number all the lines). – Thomas Matthews Mar 29 '16 at 20:20
  • Is the parameter to `poli` constructor the highest power or the number of terms? What happens with this polynomial: 2x^2 + 7, or 4x^4-27x^2? – Thomas Matthews Mar 29 '16 at 20:22
  • If you used `std::vector`, which grows dynamically, you would not need a parameter to your `poli` constructor. – Thomas Matthews Mar 29 '16 at 20:23
  • See also [Storing Templates Functions in CPP files](http://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file), about why you should not place templates in source files. – Thomas Matthews Mar 29 '16 at 20:27
  • The parameter power is the highest power. 2x^2+7 for power=3 is 0x^3+2x^2+0x^1+7x^0. I get no errors, the problem is somewhere in invert() or in val_pol() but I can't spot it. – Ruxandra N. Mar 29 '16 at 20:29

0 Answers0