Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with my C++ program, KEEP THIS CODE VERY SIMILAR TO THE ONE BELOW, it is just not working properly. This program require

Please help me with my C++ program, KEEP THIS CODE VERY SIMILAR TO THE ONE BELOW, it is just not working properly. This program require input and output file. The output should print out the results in the prompt below. Please also show the input and out put file result

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 4X33X23 would be represented as a linked list given below. Write a COMPLETE program that reads two polynomials in any order of exponent from an input file, stores them as sorted (in the descending order of the exponents) linked lists, adds them together, and prints the result as a polynomial to an output file. The result should be a third linked list. Hint: Travers both polynomials. If a particular exponent value is present in either of the two polynomials being summed, then it should be present in the answer. If it is present in both polynomials, then its coefficient is the sum of the corresponding coefficient in both polynomials. (If this sum is zero, the term should be deleted). NOTE: Must write a very general program using Object oriented concept. Do not use any library functionsAll methods used in the program MUST be written in C++ and included in the program. KEYWORDS: Classes Pointers Submit a. Documented program b. Input file. < Must contain the sample equations given below> c. Output file. d. All items stapled. Test run for the following inputs (Do not change the order of exponent in the input file) A) Equation 1 : 18X^57X^3+6X^2+6 Equation2: 8X^27X^4+6X^3+9X^28X+9 Result : 26X^57X^4X^3+15X^28X+15 B) Equation 1 : 8X^37X^5+6X^2+6 Equation2 : 8X^57X^2+6X^4+9X^38 Result: X^5+6X^4+17X^3X^22 C) Equation I : 8X^36+5X^5+6X^2+8X^87X^7+6X Equation2:9X^56X^2+6X^2+5X^418X^3-9 Result: 8X^87X^7+14X^5+5X^4+26X^3+6X15

//#include "polynomial.h" #include #include using namespace std;

// Node class for a term in a polynomial class Node { public: int coeff; // Coefficient of the term int exp; // Exponent of the term Node* next; // Pointer to the next term

// Constructor Node(int c, int e) { coeff = c; exp = e; next = NULL; } };

// Polynomial class class Polynomial { public: Node* head; // Pointer to the first term of the polynomial

// Constructor Polynomial() { head = NULL; }

// Method to add a term to the polynomial void addTerm(int c, int e) { Node* newNode = new Node(c, e); if (head == NULL || e > head->exp) { newNode->next = head; head = newNode; } else { Node* curr = head; while (curr->next != NULL && e < curr->next->exp) { curr = curr->next; } newNode->next = curr->next; curr->next = newNode; } }

// Method to add two polynomials and return the result as a new polynomial Polynomial add(Polynomial p) { Polynomial result; Node* a = head; Node* b = p.head; while (a != NULL && b != NULL) { if (a->exp > b->exp) { result.addTerm(a->coeff, a->exp); a = a->next; } else if (b->exp > a->exp) { result.addTerm(b->coeff, b->exp); b = b->next; } else { // a->exp == b->exp int sum = a->coeff + b->coeff; if (sum != 0) { result.addTerm(sum, a->exp); } a = a->next; b = b->next; } } // Add remaining terms from a or b while (a != NULL) { result.addTerm(a->coeff, a->exp); a = a->next; } while (b != NULL) { result.addTerm(b->coeff, b->exp); b = b->next; } return result; }

// Method to print the polynomial to a file void printToFile(string filename) { ofstream outfile(filename); if (!outfile.is_open()) { cout << "Error: could not open output file." << endl; return; } Node* curr = head; while (curr != NULL) { outfile << curr->coeff << "X^" << curr->exp; if (curr->next != NULL) { outfile << " + "; } curr = curr->next; } outfile.close(); } };

// Method to read a polynomial from a file Polynomial readPolynomialFromFile(string filename) { Polynomial p; ifstream infile(filename); if (!infile.is_open()) { cout << "Error: could not open input file." << endl; return p; } int c, e; while (infile >> c >> e) { p.addTerm(c, e); } infile.close(); return p; }

int main() { // Open input and output files std::ifstream infile("input.txt"); std::ofstream outfile("output.txt");

// Read in the two polynomials Polynomial poly1, poly2; infile >> poly1 >> poly2;

// Add the polynomials together Polynomial result = poly1 + poly2;

// Output the result outfile << result;

// Close input and output files infile.close(); outfile.close();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago