Thread: help with overloaded operators

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    I haven't yet saw an IDE even try to compete with VC++.
    I admint I didn't try t oexplore all IDEs on unix X windows..., but I suspect even there, nothing compares VC++, espacially when it is with Visual Assist.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    You are absolutely right about IDE. I am using VC++ with Visual Assist too.

    What I gave you is just a command line tool which I use to compile small programs to check if they are compiled in other compilers (GNU C++ in that case).

    VC++ doesn't support template members in template classes which I miss much (

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Well VC++ is the tool we have - for good and for bad.
    My code should also be ocmpiled on various Unix platforms, so I need to conform to "some" standard.

    Hey, how did you become a member?

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    5

    still need help

    i knew how to use the + to add the two operators when i used the this pointer and also the polynomial that i am adding, but i was wondering if i could get some help on the code on how to add the polynomials, the areas where i have stated not for sure what to put here is what i am having trouble with because i am not for sure how to keep going after i add the two like exponent polynomials and what to do if i find the place the polynomial is suppose to go but there is nothing to add to it, so it adds it self to the linked list, thanks:


    {
    ptrType cur; //pointer used to traverse linked list
    bool found;

    cur = head; //initialize cur to beginning of linked list
    found = false;
    //Searches efficiently for searchval
    //Since the linked list is order, the search stops when if
    //finds an element in the linked list which is greater than
    //searchval
    while((cur != NULL) && (!found) && (cur ->exponent >= searchval))
    {
    if(cur ->exponent == searchval)
    {
    found = true;
    -----not for sure what to put here----
    }
    else
    cur =cur->next;
    }

    if(!found)
    ----not for sure what to put here----
    }

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    First about "member": I really do not know. I took the C++ test soon & in this test made 45 right answers (I tried to argue about 3 of the "wrong" answers, but I still didn't receive answer from the support of the site). Maybe this is the reason, or maybe the fact that I am too active in the forum...

    Now about polynoms:
    I am not sure what are you trying to achive, but if you try to add two polinoms, you should do this:
    Merge the two list into resulting one (the one which you would return) & where you have elements with equal exponents add the two values.
    Is this what you are trying to achieve?

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    5
    yes that is what i am tring to achieve....

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    It would help if you'd put the decalaration of all variables and types in your code.
    E.g. - what is searchVal and what is a linked-list item looks like?

  8. #23
    Unregistered
    Guest
    here's a sample polynomial:

    x^2 + 3x - 7

    You could put this in the following form:

    1x^2 + 3x^1 + (-7)x^0

    so that in general each term in the statement is not a + sign conatains an int (coeffiicient) followed by a char (base) followed by the ^ symbol followed by an int (exponent). This information can be encapsulated in a struct with 3 data members like so:

    struct Nomial
    int coeficient
    int base
    int exponent

    In this scenario the polynomial could then be a linked list with these structs as the data member and a pointer to struct the type itself. Your node for the linked list could look something like this:

    struct Node
    Nomial nomial
    Node * next

    Certainly, the member variables for Nomial could be more sophisticated if desired, but this should do for demonstration purposes.

    Now lets say you have two polynomials

    3x^1 + 8x^0 and 4x^1 + 2x^0

    To add them you can just add the nodes of one list to the other to get

    3x^1 + 8x^0 + 4x^1 + 2x^0

    If you were doing this by hand you would then reduce the polynomial by adding like terms( adding the coeffiicients of nomials with the same base and exponent) to get this:

    7x^1 + 10x^0

    To do this with a linked list you need to look at each node compared to every other node in the list. If the base and exponent of the node (nomial) are the same then add the exponents of the two nodes and store it in one of the two nodes and delete the other node. To do this create two temp nodes, say one is stationaryNode and one is travelingNode. To start the search assign the root (or head) node of the list to stationary Node and then assign stationaryNode->next to travelingNode. If travelingNode is not NULL then compare the base and exponent of travelingNode to base and exponent of stationaryNode. If the are the same add the coefficient of travelingNode to the coefficient of stationaryNode and delete travelingNode. Continue until travelingNode is NULL. Then shift stationaryNode to stationaryNode->next and repeat the process. Continue until stationaryNode is NULL. When you are done the polynomial should be in reduced form.


    I'll leave it up to you to convert the manual statements into code.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Lets say that base is the same: e.g. so called 'x'
    This is a simple class that would give you what you need

    class Polynom
    {
    ......
    public:
    ......
    double operator () (double x);
    ....
    const Polynom operator+(const Polynom& other) const;
    or if you prefer:
    Polynom operator+(const Polynom& other) const;
    };

    double Polynom:perator() (double x)
    {
    double dRet = 0;
    ListElement* pCurrEl = list;
    while (pCurrEl)//If this is a condition for end
    {
    dRet += pCurrEl->Data->Coeff*pow(x, pCurrEl->Data->Exp);
    pCurrEl = pCurrEl->Next;
    }

    return dRet;
    }

    ///Assume that this is a slow function
    ///Unless you have a reference counter of your
    ///List as more possibly a copy costructor will be called
    const Polynom operator+(const Polynom& other) const
    {
    //I assume that the lists are sorted!!!!!!
    Polynom polynom;
    ListElement* pCurrElThis = list;
    ListElement* pCurrElOther = other.list;

    while (pCurrElThis && pCurrElOther)//if this is
    //an end condition
    {
    if (pCurrElThis->Data->Exp == pCurrElOther->Data->Exp)
    {
    polynom.Add(pCurrElThis->Data->Exp,
    pCurrElThis->Data->Coeff + pCurrElOther->Data->Coeff);
    pCurrElThis = pCurrElThis->Next;
    pCurrElOther = pCurrElOther->Next;
    }
    else if (pCurrElThis->Data->Exp < pCurrElOther->Data->Exp)
    {
    polynom.Add(pCurrElThis->Data->Exp,
    pCurrElThis->Data->Coeff);
    pCurrElThis = pCurrElThis->Next;
    }
    else if (pCurrElThis->Data->Exp > pCurrElOther->Data->Exp)
    {
    polynom.Add(pCurrElOther->Data->Exp,
    pCurrElOther->Data->Coeff);
    pCurrElOther = pCurrElOther->Next;
    }
    }
    if (!pCurrElThis)
    {
    while (pCurrElOther)
    {
    polynom.Add(pCurrElOther->Data->Exp,
    pCurrElOther->Data->Coeff);
    pCurrElOther = pCurrElOther->Next;
    }
    }
    else if (!pCurrElOther)
    {
    while (pCurrElThis)
    {
    polynom.Add(pCurrElThis->Data->Exp,
    pCurrElThis->Data->Coeff);
    pCurrElThis = pCurrElThis->Next;
    }
    }


    assert(!pCurrElThis && !pCurrElOther);
    return polynom;
    }

    //Of course check the code again, I just scraped it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iostream overloaded operators
    By peckitt99 in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2007, 05:32 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  4. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM
  5. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 03:52 PM