polynomial

Discussion in 'C++' started by CuTe_Engineer, Mar 18, 2008.

  1. hiii,

    i wrote this program that can be used to process polynomial with
    coefficient that ae real numbers ..
    my Question is if i want to design a program to process poly. with
    coeff. that are complex number , How can i do it?

    this is my prog.

    emplate <class elemType>
    class arrayListType
    {
    public:
    const arrayListType<elemType>&
    operator=(const arrayListType<elemType>&);
    //Overloads the assignment operator

    bool isEmpty();
    //Function to determine whether the list is empty
    //Postcondition: Returns true if the list is empty;
    // otherwise, returns false.
    bool isFull();
    //Function to determine whether the list is full
    //Postcondition: Returns true if the list is full;
    // otherwise, returns false.
    int listSize();
    //Function to determine the number of elements in the list
    //Postcondition: Returns the value of length.
    int maxListSize();
    //Function to determine the size of the list
    //Postcondition: Returns the value of maxSize.
    void print() const;
    //Function to output the elements of the list
    //Postcondition: Elements of the list are output on the
    // standard output device.
    bool isItemAtEqual(int location, const elemType& item);
    //Function to determine whether the item is the same
    //as the item in the list at the position specified by
    //Postcondition: Returns true if the list[location]
    // is the same as the item; otherwise,
    // returns false.
    void insertAt(int location, const elemType& insertItem);
    //Function to insert an item in the list at the
    //position specified by location. The item to be inserted
    //is passed as a parameter to the function.
    //Postcondition: Starting at location, the elements
    // of the list are shifted down,
    // list[location] = insertItem;, and
    // length++;
    // If the list is full or location is out of
    // range, an appropriate message is displayed.
    void insertEnd(const elemType& insertItem);
    //Function to insert an item at the end of the list
    //The parameter insertItem specifies the item to be
    //inserted.
    //Postcondition: list[length] = insertItem; and length++;
    // If the list is full, an appropriate
    // message is displayed.
    void removeAt(int location);
    //Function to remove the item from the list at the
    //position specified by location
    //Postcondition: The list element at list[location] is
    // removed and length is decremented by 1.
    // If location is out of range, an appropriate message
    // is displayed.
    void retrieveAt(int location, elemType& retItem);
    //Function to retrieve the element from the list at the
    //position specified by location
    //Postcondition: retItem = list[location]
    // If location is out of range, an appropriate
    // message is displayed.
    void replaceAt(int location, const elemType& repItem);
    //Function to replace the elements in the list at the
    //position specified by location. The item to be replaced
    //is specified by the parameter repItem.
    //Postcondition: list[location] = repItem
    // If location is out of range, an appropriate
    // message is displayed.
    void clearList();
    //Function to remove all the elements from the list
    //After this operation, the size of the list is zero.
    //Postcondition: length = 0;
    int seqSearch(const elemType& item);
    //Function to search the list for a given item.
    //Postcondition: If the item is found, returns the location
    // in the array where the item is found;
    // otherwise, returns -1.
    void insert(const elemType& insertItem);
    void remove(const elemType& removeItem);
    arrayListType(int size = 100);
    arrayListType(const arrayListType<elemType>& otherList);
    ~arrayListType();

    protected:
    elemType *list; //array to hold the list elements
    int length; //to store the length of the list
    int maxSize; //to store the maximum size of the list
    };

    *********************************************************************************
    #ifndef H_polynomial
    #define H_polynomial

    #include <iostream>
    #include "arrayListType.h"

    using namespace std;

    class polynomialType: public arrayListType<double>
    {
    friend ostream& operator<<(ostream&, const polynomialType&);

    friend istream& operator>>(istream&, polynomialType&);

    public:
    polynomialType operator+(const polynomialType&);
    polynomialType operator-(const polynomialType&);

    polynomialType operator*(const polynomialType&);

    double operator() (double x);

    polynomialType(int size = 100);
    //constructor

    int min(int x, int y);
    int max(int x, int y);

    };
    #include <iostream>
    #include <cmath>
    #include "polynomialType.h"

    using namespace std;

    polynomialType::polynomialType(int size)
    : arrayListType<double>(size)
    {
    length = size;
    for(int i = 0; i < size; i++)
    list = 0;
    }

    double polynomialType::eek:perator() (double x)
    {
    double value = 0.0;

    for(int i = 0; i < length; i++)
    {
    if(list != 0.0)
    value = value + list * pow(x,i);
    }

    return value;

    }

    polynomialType polynomialType::eek:perator+
    (const polynomialType& right)
    {
    int size = max(length, right.length);
    int i;

    polynomialType temp(size);

    for(i = 0; i < min(length, right.length); i++)
    temp.list = list + right.list;

    if(size == length)
    for(i = min(length, right.length); i < length; i++)
    temp.list = list;
    else
    for(i = min(length, right.length); i < right.length; i++)
    temp.list = right.list;

    return temp;

    }

    polynomialType polynomialType::eek:perator-
    (const polynomialType& right)
    {
    int size = max(length, right.length);
    int i;

    polynomialType temp(size);

    for(i = 0; i < min(length, right.length); i++)
    temp.list = list - right.list;

    if(size == length)
    for(i = min(length, right.length); i < length; i++)
    temp.list = list;
    else
    for(i = min(length, right.length); i < right.length; i++)
    temp.list = -right.list;

    return temp;

    }

    polynomialType polynomialType::eek:perator*
    (const polynomialType& right)
    {
    polynomialType temp = right;

    cout<<"See Programming Exercise 10"<<endl;

    return temp;
    }

    int polynomialType::min(int x, int y)
    {
    if(x <= y)
    return x;
    else
    return y;
    }

    int polynomialType::max(int x, int y)
    {
    if(x >= y)
    return x;
    else
    return y;
    }

    ostream& operator<<(ostream& os, const polynomialType& p)
    {
    int i;
    int indexFirstNonzeroCoeff = 0;

    for(i = 0; i < p.length; i++)
    if(p.list != 0.0)
    {
    indexFirstNonzeroCoeff = i;
    break;
    }

    if(indexFirstNonzeroCoeff < p.length)
    {
    if(indexFirstNonzeroCoeff == 0)
    os<<p.list[indexFirstNonzeroCoeff]<<" ";
    else
    os<<p.list[indexFirstNonzeroCoeff]<<"x^"
    <<indexFirstNonzeroCoeff<<" ";

    for(i = indexFirstNonzeroCoeff + 1; i < p.length; i++)
    {
    if(p.list != 0.0)
    if(p.list >= 0.0)
    os<<"+ "<<p.list[i]
    <<"x^"<<i<<" ";
    else
    os<<"- "<<-p.list[i]
    <<"x^"<<i<<" ";
    }
    }
    else
    os<<"0";

    return os;
    }

    istream& operator>>(istream& is, polynomialType& p)
    {
    cout<<"The degree of this polynomial is: "
    <<p.length - 1<<endl;
    for(int i = 0; i < p.length; i++)
    {
    cout<<"Enter the coefficient of x^"<<i<<": ";
    is>>p.list[i];
    }

    return is;
    }
    ***************************************************************************

    driver
    //Test program: Polynomial Operations

    #include <iostream>

    #include "polynomialType.h"

    using namespace std;

    int main()
    {
    polynomialType p(8); //Line 1
    polynomialType q(4); //Line 2
    polynomialType t; //Line 3

    cin>>p; //Line 4
    cout<<endl<<"Line 5: p(x): "<<p
    <<endl; //Line 5

    cout<<"Line 6: p(5): "<<p(5)
    <<endl<<endl; //Line 6

    cin>>q; //Line 7
    cout<<endl<<"Line 8: q(x): "<<q
    <<endl<<endl; //Line 8

    t = p + q; //Line 9

    cout<<"Line 10: p(x) + q(x): "
    <<t<<endl; //Line 10

    cout<<"Line 11: p(x) - q(x): "
    <<p - q<<endl; //Line 11

    return 0;
    }[/i][/i][/i]
     
    CuTe_Engineer, Mar 18, 2008
    #1
    1. Advertisements

  2. CuTe_Engineer

    Paavo Helde Guest

    You can make the class polynomialType a class template like the
    arrayListType already is, and then instantiate it with std::complex. It
    might appear that some member functions have to be specialized for
    std::complex to work properly, but not necessarily, complex and real
    numbers should appear quite similar in polynomials.

    Some other suggestions below.
    This looks a lot like std::list or std::vector. Why reinvent the wheel?
    The data should be private, especially in the case of an implementation
    class.
    Questionable derivation. Do you really think that a polynomial "is a"
    array? To me it seems like an implementation detail, i.e. polynomial is
    implemented in terms of an array. Then the array should be a member, not
    a base class, or at least private inheritance should be used.
    What's wrong with std::min() and std::max()?


    You are messing with internals of arrayListType. The ctor of
    arrayListType should have done this initialization already, so you have
    either duplicate code here or a design error.


    Oh, I see why you are using derivation. Instead of multiple calls of
    retrieveAt() and replaceAt() you can now use bracket notation here and
    mess directly with other class' internals. One more reason to throw
    arrayListType away and replace it with std::vector.
    [i][i][i][/i][/i][/i]
     
    Paavo Helde, Mar 18, 2008
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.