Question

A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are...

A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing polynomials using linked list. Consider the polynomial 4x3 + 6x2 + 10x + 6. We can use a linked list to represent this polynomial as in Figure below: Every node corresponds to a term in the Polynomial. The node stores the coefficient and the exponent of the term. The node also has a pointer to point the next node. However, all terms are not necessarily present in a polynomial, e.g., in 12x11 + 9x8 + 4x3 + 5x + 4, the term for x7 is not present. To represent this polynomial, we need a linked list of 5 nodes. Consider addition of two polynomials 5x12 + 2x9 + 4x7 + 6x6 + x3 and 7x8 + 2x7 + 8x6 + 6x4 + 2x2 + 3x + 40. The resulting polynomial is 5x12 + 2x9 + 7x8 + 6x7 + 14x6 + 6x4 +x3 + 2x2 + 3x + 40. Now notice how the addition is carried out. Let us say that the result of addition will be stored in a third linked list. We start with the term which is of highest degree in any of the polynomials. If there is no item having same exponent, we simply append the term to the new list, and continue with the process. Wherever we find that the exponents match, we simply add the coefficients and then store the term in the new list. If one list gets exhausted earlier and the other list still contains some lower order terms, then simply append the remaining terms to the new list. Page 5 of 7 Now we are in a position to write an algorithm for adding two polynomials. Let phead1, phead2 and phead3 represent the pointers of the three lists under consideration. We want to add phead1 and phead 2, and store the result in phead3. This addition can be performed using the procedure below. p1 = phead1; p2 = phead2; p3 = phead3; While ( (p1 != NULL) or (p2 != NULL) ) { If ( p1 = NULL ) { While ( p2 != NULL) { p3->exponent = p2->exponent; p3->coefficient = p2->coefficient; p2 = p2->next; p3 = p3->next; } } Else-If ( p2 = NULL ) { While ( p1 != NULL) { p3->exponent = p1->exponent; p3->coefficient = p1->coefficient; p1 = p1->next; p3 = p3->next; } } Else { While ( p1->exponent > p2->exponent ) { p3->exponent = p1->exponent; p3->coefficient = p1->coefficient; p1 = p1->next; p3 = p3->next; } While ( p1->exponent < p2->exponent ) { p3->exponent = p2->exponent; p3->coefficient = p2->coefficient; p2 = p2->next; p3 = p3->next; } While ( p1->exponent = p2->exponent ) { p3->exponent = p1-> exponent; Page 6 of 7 p3->coefficient = p1->coefficient + p2->coefficient; p1 = p1->next ; p2 = p2->next ; p3 = p3->next; } } } You should write a program in which user can provide polynomials as input. Each polynomial should be represented as a linked list. Your program should be capable of adding two polynomials and storing the resultant polynomial in another linked list. Note that the number of polynomials in the system can be up to 10. The polynomials should be indexed as 0, 1, …, 9. The Interface Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 1 Type the polynomial: x^4+5x^2 A polynomial is created. You now have total 1 polynomials in system. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 1 Type the polynomial: 100x^19+43x^4+11x^0 A polynomial is created. You now have total 2 polynomials in system. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 3 Which polynomial you want to see (0-9)? 4 The input is invalid. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 3 Which polynomial you want to see (0-9)? 0 The polynomial is: x^4+5x^2 Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 2 Let you want to perform C=A+B. Then enter A B C: 0 1 2 The input is invalid. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 1 Type the polynomial: 2x^2+3x^1+5x^0 Page 7 of 7 A polynomial is created. You now have total 3 polynomials in system. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 2 Let you want to perform C=A+B. Then enter A B C: 0 1 2 The polynomials 0 and 1 are added, and the result is stored in polynomial 2. Enter the corresponding number of your desired action: 1 – create a new polynomial, 2 – adding two polynomials, and 3 – print a polynomial. 3 Which polynomial you want to see (0-9)? 2 The polynomial is: 100x^19+44x^4+5x^2+11x^0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This is a really long question and is framed pretty poorly but from the information, that I have gathered, I will provide you with a working c++ code(which is going to be very long). One thing to remember, the first part of the code itself is not easy, you are being given the polynomial in the form of a string so you have to parse the string in such a manner so as to extract the necessary information from the string, the necessary information being the coefficients of  the polynomials and their corresponding exponents. Now remember why we are doing this, you are given the polynomial in the form of a string, so the computer or program cannot just extract the information of the polynomial without doing anything on the given string, so we have to parse the string accordingly. So here goes the code. It is quite big and may contain some bugs (i have checked it though). However, if you find any bug just let me know in the comments and I will be happy to help you out anytime. Thank you.

Code:-

#include <bits/stdc++.h> // imports all necessary headers

using namespace std;

class poly
{
public:
   int coefficient;
   int exponent;
   poly *next;
};

int mark[20];

int main()
{  
   poly* head[20];
   for(int i = 0; i < 20; i++)
   {
       head[i] = NULL;
   }
   int cur_idx = -1;
   while(1)
   {
       cout << "Enter the corresponding number of your desired action" << endl;
       int id;
       cout << "Press 1 to create a new polynomial" << endl;
       cout << "Press 2 to add to two polynomials" << endl;
       cout << "Press 3 to print a polynomial" << endl;
       cin >> id;
       if(id == 1)
       {
           cout << "Type the Polynomial" << endl;
           string str, temp_str = "";
           cin >> str;
           str += '+';
           bool now = true;
           poly* prev = NULL;
           poly* temp = NULL;
           cur_idx++;
           mark[cur_idx]++;
           cout << "A Polynomial is created. You now have total of " << cur_idx << " polynomials in system" << endl;
           for(int i = 0; i < (int)str.size(); i++)
           {
               if(str[i] == '+')
               {
                   string coeff = "", expo = "";
                   stringstream ss1, ss2;
                   int j = 0;
                   while(temp_str[j] != 'x')
                   {
                       coeff += temp_str[j];
                       j++;
                   }
                   while(j < (int)temp_str.size())
                   {
                       if(isdigit(temp_str[j]))
                           expo += temp_str[j];
                       j++;
                   }
                   int COEFF = 0, EXPO = 0;
                   ss1 << coeff;
                   ss2 << expo;
                   ss1 >> COEFF;
                   ss2 >> EXPO;
                   COEFF = max(1, COEFF);
                   temp_str = "";
                   if(now)
                   {
                       now = false;
                       head[cur_idx] = new poly();
                       head[cur_idx]->coefficient = COEFF;
                       head[cur_idx]->exponent = EXPO;
                       head[cur_idx]->next = NULL;
                       prev = head[cur_idx];
                   }
                   else
                   {
                       temp = new poly();
                       temp->coefficient = COEFF;
                       temp->exponent = EXPO;
                       temp->next = NULL;
                       prev->next = temp;
                       prev = temp;
                   }
               }
               else
               {
                   temp_str += str[i];
               }
           }
       }
       if(id == 2)
       {
           cout << "Let you want to perform C = A + B" << endl;
           cout << "Then enter A B C:";
           int A, B, C;
           cin >> A >> B >> C;
           if(!mark[A] or !mark[B] or !mark[C])
           {
               cout << "The input is invalid" << endl;
           }
           else
           {
               poly* p1 = head[A];
               poly* p2 = head[B];
               poly* p3 = head[C];
               while((p1 != NULL) or (p2 != NULL))
               {
                   if(p1 == NULL)
                   {
                       while(p2 != NULL)
                       {
                           p3->exponent = p2->exponent;
                           p3->coefficient = p2->coefficient;
                           p2 = p2->next;
                           p3 = p3->next;
                       }
                   }
                   else if(p2 == NULL)
                   {
                       while(p1 != NULL)
                       {
                           p3->exponent = p1->exponent;
                           p3->coefficient = p1->coefficient;
                           p1 = p1->next;
                           p3 = p3->next;
                       }
                   }
                   else
                   {
                       while(p1->exponent > p2->exponent)
                       {
                           p3->exponent = p1->exponent;
                           p3->coefficient = p1->coefficient;
                           p1 = p1->next;
                           p3 = p3->next;
                       }
                       while(p1->exponent < p2->exponent)
                       {
                           p3->exponent = p2->exponent;
                           p3->coefficient = p2->coefficient;
                           p2 = p2->next;
                           p3 = p3->next;
                       }
                       while(p1->exponent = p2->exponent)
                       {
                           p3->exponent = p1-> exponent;
                           p3->coefficient = p1->coefficient + p2->coefficient;
                           p1 = p1->next;
                           p2 = p2->next;
                           p3 = p3->next;
                       }
                   }
               }
           }
       }
       if(id == 3)
       {
           cout << "Which Polynomial you want to see ?" << endl;
           int no;
           cin >> no;
           if(no > cur_idx)
           {
               cout << "The input is invalid" << endl;
           }
           else
           {
               poly* temp = head[no];
               string disp_pol = "";
               while(temp != NULL)
               {
                   disp_pol += to_string(temp->coefficient);
                   disp_pol += "x^";
                   disp_pol += to_string(temp->exponent);
                   disp_pol += '+';
                   temp = temp->next;
               }
               for(int k = 0; k < disp_pol.size() - 1; k++)
                   cout << disp_pol[k];
               cout << endl;
           }
       }
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    Please answer this in python 3, thank you. For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a...

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • each A polynomial may be represented as a linked list where each node contains the coefficient...

    each A polynomial may be represented as a linked list where each node contains the coefficient and exponent of a term of the polynomial. The polynomial 4X-3X-5 would be represented as the linked list. as the linked list the polnomial eql -5 0 Write a program system that reads two polynomials, stores them as linked lists, adds them together, and prints the result as a polynomial. The result should be a third linked list. Hint: Travers both polynomials. If a...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...

  • The language is java if you coukd shiwnstep by step on how to get each section

    the language is java if you coukd shiwnstep by step on how to get each section ebapps/blackboard/execute/content/file?cmd-view&content jid _975287 1&course id:_693311 Lab 3 Directions (linked lists) Program #1 1. Show PolynomialADT interface 2. Create the PolyNodeClass with the following methods: default constructor, . overloaded constructor, copy constructor, setCoefficient, setExponent, setNext, getCoefficient, getExponent, getNext 3. Create the PolynomialDataStrucClass with the following methods: defaut constructor, overloaded constructor, copy constructor, isEmpty. setFirstNode. getfirstNode,addPolyNodeFirst (PolyNode is created and set to the end of polynomial),...

  • in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) =...

    in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the example, you would store p(x) as power of x. For (5,10),(9,7),(-1,1),(一10,0) Supply member functions to add, multiply, and print polynomials. Supply a com- structor that makes a polynomial from a single term. For example, the polynomia; can be constructed as Polynomial p(Term(-10, 0)); p.add (Polynomial(Term(-1, 1))); p.add (Polynomial(Term(9,...

  • Which of the following versions of the polynomial class implementation provides the most space efficient representation...

    Which of the following versions of the polynomial class implementation provides the most space efficient representation for polynomials with a large degree and very few terms, for example: 10x+ x200. Recall that the degree of the polynomial is the largest exponent of the term with a non-zero coefficient. SELECT THE SINGLE BEST ANSWER A static array-based implementation, where only the coefficients are stored in the array, and the exponent of each term is the index of the coefficient in the...

  • Let U be the subspace of P3 defined by U= {pEP3 : p(0)=0} 'character to indicate an exponent and x as the variable,...

    Let U be the subspace of P3 defined by U= {pEP3 : p(0)=0} 'character to indicate an exponent and x as the variable, eg. 5x^2-2x+1 Give a basis for U Give your answer as a comma-separated list of polynomials, using the B =0 Let U be the subspace of P3 defined by U= {pEP3 : p(0)=0} 'character to indicate an exponent and x as the variable, eg. 5x^2-2x+1 Give a basis for U Give your answer as a comma-separated list...

  • Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are...

    Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...

  • : 2: Let T : P1 → P2 be the linear map taking a polynomial p(t)...

    : 2: Let T : P1 → P2 be the linear map taking a polynomial p(t) to its antiderivative P(t) satisfying P(0) = 0 (e.g. T(5 + 2t) 5t + t2). Find two matrices A, B representing the corresponding linear map R2 + R3, the first with respect to the standard bases of P2 and P3, and the second with respect to the bases B = {1,1+t} B' = {1,1 +t, 1+t+t2}

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT