473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

linked lists polynomial help

1 New Member
I am having trouble writing my c++ code for a polynomial
I need help with istream and operator*

Here is my code:
[

Expand|Select|Wrap|Line Numbers
  1. #include "polynomial.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. polynomial::polynomial()
  7. {
  8.     head=NULL;
  9. }
  10.  
  11. polynomial::polynomial(const polynomial& p)
  12. {
  13.     {
  14.     node<term>* otherNode = p.head;
  15.  
  16.     while (otherNode != NULL){
  17.         orderedInsert(otherNode->data.coef, otherNode->data.expn);
  18.         otherNode = otherNode->next;
  19.     }
  20. }
  21.  
  22. polynomial::~polynomial()
  23. {
  24.     node<term> *nextNode = head, *deadNode = NULL;
  25.  
  26.     while(nextNode != head){
  27.         deadNode = nextNode;
  28.         nextNode = nextNode->next;
  29.         delete &deadNode;
  30.     }
  31. }
  32.  
  33. polynomial& polynomial::operator=( const polynomial& p )
  34. {
  35.     return *this;
  36. }
  37.  
  38. double polynomial::evaluate ( double x )
  39. {
  40.     double sum=0;
  41.     term* currentTerm=head;
  42.     while(currentTerm !=NULL)
  43.     {
  44.         sum += currentTerm->coef * pow(x, currentTerm->expn);
  45.         currentTerm=currentTerm->next;
  46.     }
  47.     return sum;
  48. }
  49. int polynomial::degree() const
  50. {
  51.     return head->expn;
  52. }
  53.  
  54. void polynomial::orderedInsert(double n, int e) {
  55.     {
  56.         if(head == NULL){
  57.             head = new node<term>(term(n, e), NULL);
  58.         }else{
  59.  
  60.             node<term>* p1 = head;
  61.             node<term>* p2 = p1;
  62.             while ( p1 != NULL && n > p1->data.expn )
  63.             {
  64.                 p2 = p1;
  65.                 p1 = p1->next;
  66.             }
  67.             if ( p1 != NULL )
  68.             {
  69.                 if ( p1->data.expn == e){
  70.                     p1->data.coef += n;
  71.                 }else if ( p1 == head)
  72.                 {
  73.                     head = new node<term>(term(n, e), NULL);
  74.                 }
  75.                 else
  76.                 {
  77.                     p2->next = new node<term>(term(n, e), NULL);
  78.                 }
  79.             }else{
  80.                 p2->next = new node<term>(term(n, e), p2);
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. polynomial polynomial::operator+ ( const polynomial& p) const
  87. {
  88.     polynomial result;
  89.     node<term>* reader = p.head;
  90.     while (reader != NULL){
  91.         result.orderedInsert(reader->data.coef, reader->data.expn);
  92.         reader = reader->next;
  93.     }
  94.     reader = head;
  95.     while (reader != NULL){
  96.         result.orderedInsert(reader->data.coef, reader->data.expn);
  97.         reader = reader->next;
  98.     }
  99.     return result;
  100. }
  101.  
  102.   polynomial polynomial::operator- ( const polynomial& p ) const
  103. {
  104.     polynomial result;
  105.     node<term> *reader = p.head;
  106.     while (reader != NULL){
  107.         result.orderedInsert(-(reader->data.coef), reader->data.expn);
  108.         reader = reader->next;
  109.     }
  110.     reader = head;
  111.     while (reader != NULL){
  112.         result.orderedInsert(reader->data.coef, reader->data.expn);
  113.         reader = reader->next;
  114.     }
  115.     return result;
  116. }
  117.  
  118.  
  119. polynomial polynomial::operator* ( const polynomial& p ) const
  120. {
  121.     polynomial result;
  122.     return result;
  123. }
  124.  
  125. ostream& operator<< ( ostream& os, const polynomial& )
  126. {
  127.     node<term> *printer = poly.head;
  128.     while(printer != NULL){
  129.         os << printer->toString(os);
  130.         if(printer->nextPoint() != NULL){
  131.             os << " + ";
  132.         }
  133.     }
  134.     return os;
  135. }
  136.  
  137.  
  138. istream& operator>> ( istream& is, polynomial& p )
  139. {
  140.     return is;
  141. }
]


Thanks so much
May 8 '11 #1
1 1830
weaknessforcats
9,208 Recognized Expert Moderator Expert
Is there some reason why you are not using std::list instead of writing your own linked list code?
May 9 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
2
by: Skywise | last post by:
I am fairly new to linked lists. I am trying to write a class using linked lists. It seems to work fine, but I need to know if I have any resource leaks in it because I plan on using this class...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
6
by: paudirac | last post by:
Hi, I need to maintain N linked lists where N is determined at runtime. The linked lists are defined as follows, struct linked_list { int data; struct linked_list next; }
17
by: Foodbank | last post by:
Hi, I have to write a program that will use linked lists to print the number of unique words, total words, and the most frequent word from a text file. I've gotten a decent amount of it done...
5
by: John Young | last post by:
Hi, I have a couple of questions that I hope someone can help me with. I'm pretty new to c# and have been able to use C# Express pretty well (it's a great IDE). My problem is twofold.... 1. I...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
23
by: Just Another Victim of the Ambient Morality | last post by:
I'm looking for a linked list implementation. Something iterable with constant time insertion anywhere in the list. I was wondering if deque() is the class to use or if there's something else. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.