Question: [Data Structures] C++ Debug code for part A (errors in polynomial.cpp) and help w part B of exercise. /* * * * * * *
[Data Structures] C++ Debug code for part A (errors in polynomial.cpp)
and help w part B of exercise.
/* * * * * * * * * * * * * * * * * * * * main.cpp * * * * * * * * * * * * * * * * * * * * * * /
#include
int main()
{ polyll p1; p1.poly_add(0,5); p1.poly_add(1.5,4); p1.poly_add(1.7,2); p1.poly_add(1.8,1); p1.poly_add(1.9,0); cout
/* * * * * * * * * * * * * * * * * * * * polynomial.h * * * * * * * * * * * * * * * * * * * * * * /
#ifndef Header_h #define Header_h #include
class polyll { private: struct polynode { float coef; int exp; polynode* link; }*p; public: polyll(); ~polyll(); void poly_add(float c, int e); float coefficient(int power); int degree(); void display_poly(); //void poly_add(polyll& l1, polyll& l2); void changeCoefficient(float newCoefficient, int power); }; #endif /* Header_h */
/* * * * * * * * * * * * * * * * * * * * polynomial.cpp * * * * * * * * * * * * * * * * * * * * * * /
#include "polynomial.h" #include
polyll::polyll() { p = NULL; } void polyll::poly_add(float c, int e) { if(c==0) return; else { polynode* temp = p; if(temp == NULL) { temp = new polynode; p = temp; } else { while(temp->link != NULL) temp = temp->link; temp->link = new polynode; temp = temp->link; } temp->coef = c; temp->exp = e; temp->link = NULL; } } float polyll::coefficient(int power) { polynode* temp = p; while(temp->link != NULL) { if(temp->exp == power) { return temp->coef; break; } temp = temp->link; } } //this gives me problems.....
void polyll::changeCoefficient(, ) { polynode* temp = p; if(temp==NULL) { temp = new polynode; p = temp; } else { while(temp->link != NULL) { if(temp->exp == power) { temp->coef = newCoefficient; } temp = temp->link; } } } int polyll::degree() { polynode* temp = p; int degree = -100; while (temp->link != NULL) { if(temp->exp > degree) { degree= temp->exp; } temp = temp -> link; } return degree; } void polyll::display_poly() { polynode* temp=p; int f = 0; cout coeff > 0) cout exp !=0) cout coef exp; else cout link; f=1; } } polyll::~polyll() //define nodo, loop hasta q p llegue a NULL, mueve nodo, borra p, q es p. { polynode* q; while(p !=NULL) { q = p->link; delete p; p=q; } }
5. Consider a sparse implementation of the ADT polynomial that stores only the terms with nonzero coefficients. For example, you can represent the revised polynomial p in Exercise 9 of Chapter 1 with the linked chain shown in Figure 4-10. a. Complete the sparse implementation. b. Define a traverse operation for the ADT polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefficients explicitly. Define a class DoublyLinkedBag that implements the ADT bag by using a doubly linked chain, as shown in Figure 4-9. Use the class of nodes that Exercise 10 defines. 6. Use the classes for a set or a bag, as defined in this chapter or described in the previous projects, to create a spell checker. Consult the details given in Programming Problems 13 and 14 of Chapter 3. Specify and implement an ADT character string by using a linked chain of characters. Include typical opera- tions such as finding its length, appending one string to another, finding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. 8. FIGURE 4-10 A sparse polynomial degree headPtr coeff power next 5. Consider a sparse implementation of the ADT polynomial that stores only the terms with nonzero coefficients. For example, you can represent the revised polynomial p in Exercise 9 of Chapter 1 with the linked chain shown in Figure 4-10. a. Complete the sparse implementation. b. Define a traverse operation for the ADT polynomial that will allow you to add two sparse polynomials without having to consider terms with zero coefficients explicitly. Define a class DoublyLinkedBag that implements the ADT bag by using a doubly linked chain, as shown in Figure 4-9. Use the class of nodes that Exercise 10 defines. 6. Use the classes for a set or a bag, as defined in this chapter or described in the previous projects, to create a spell checker. Consult the details given in Programming Problems 13 and 14 of Chapter 3. Specify and implement an ADT character string by using a linked chain of characters. Include typical opera- tions such as finding its length, appending one string to another, finding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. 8. FIGURE 4-10 A sparse polynomial degree headPtr coeff power next
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
