Question

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 decision on whether to use a doubly-linked list and/or a tail reference. Explain your decisions in the cover letter; why are your choices good for the polynomial project?

Basic polynomial functions: read a polynomial from the user, read a polynomial from a file, add two polynomials (output to screen and file), evaluate a polynomial (output to screen and file). Evaluate takes a value for x (the variable), then calculates the polynomial’s value.

Input / output. The program will work interactively (good for testing during development), and with files. All input (user and file) is guaranteed to be correct.

Input files. There will be two final input files, with the same format. The first input file is test data from you, and will be four add operations and four evaluate operations, in any order. This file is created from a test plan with eight cases. For each case, first, describe what you are testing (examples: adding with missing terms, adding with no common terms, evaluating with zero); second, give the specific data being tested; third, give the expected result. Follow the format of the sample test plan on Moodle.    The second input file is posted on Moodle. Your program should not depend on a fixed number of add and evaluate operations. I’ll do additional testing on submitted projects.

Each operation takes three lines in the file:

Add has the word “add” (line #1), then the two polynomials in normal form (one per line).

Evaluate has the word “evaluate” (line #1), followed by the polynomial (in normal form) to evaluate (line #2), followed by the x value (line #3).

Output log file. This file will record the operations you used for final testing (taken from the input file) and the results. It starts with a heading, which includes your name. Then, for each operation, it gives the operation (add or evaluate), the polynomial(s) used, the x value (for evaluate), and the result (new polynomial for the add operation, final value for evaluate). Put a blank line between each operation’s data. Be sure to check this file to validate your answers.

Methods in the polynomial class.

add two polynomials: some terms may be missing, polynomials may be different lengths

read polynomial from file: prompt user for filename; assumptions: the file exists and is readable, the polynomials are correctly formatted

read polynomial from user, assumption: perfect user

print polynomial – coefficients of 1 do not print, terms with a zero coefficient do not print

evaluate polynomial – x values will be integers

Polynomial syntax, normal form.

polynomials are ordered from high-order term to low-order term

coefficients of one are not displayed

x^2 not 1x^2

terms with zero coefficients are not stored or displayed

0x^3

superscripts – use carets (^)

white space – exactly one space between terms and plus signs (input and output), no spaces in terms

i.example: 25x^3 + 6x + 2

Further specifications.

main is in a separate driver class

x will be the only variable

no multiple terms of the same order (e.g., two x^2 terms)

no subtraction in the polynomial

Development. Use incremental development: first create a design for program structure, then create a stubbed form of the program. Continue by creating and testing one operation at a time. Comment out the stubs once the methods are implemented.

Collaboration. Since we have already gone over parts of this in class, do not do any additional joint work with classmates, and do not solicit solutions from anyone. Do not take code from online sources. Turning in work that is not your own will result in a grade of zero for the project.

Plan to allow time for getting my help as needed.

User testing extra credit. Have a classmate run your program and comment on the user interface, especially usability: was it easy to understand the input required? were messages clear? was the output clearly labeled? etc. Allow yourself time after the testing to improve your program. Discuss the testing session in your cover letter. This discussion should be detailed, not just a couple sentences saying it went fine and he or she liked it.

Swapped test plans extra credit. Exchange test plans with a classmate to further test your program. Hand in their test file and your output file.

Other extra credit. If you finish early and your program is well tested, you can see me to choose (more) extra credit.

Reminder: no extra credit on late assignments; don't start extra credit until the project is finished and tested.

Reminder: to be counted on time, projects must posted on Moodle before class. No drafts accepted.

Post on Moodle: source code (.java file(s)), your input data file, output from your input file, output from my input file, cover letter with additional discussions, any additional extra credit. Interactive work was for development, so no output for that is handed in. Name all files clearly. As usual, do not hand in any incomplete or incorrect programs (on the on-time date), and check the specifications carefully.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip.h>
using namespace std;

struct poly {
    int coeff;
    int pow_val;
    poly* next;
};

class add {
    poly *poly1, *poly2, *poly3;

public:
    add() { poly1 = poly2 = poly3 = NULL; }
    void addpoly();
    void display();
};

void add::addpoly()
{
    int i, p;
    poly *newl = NULL, *end = NULL;
    cout << "Enter highest power for x\n"; cin >> p;
    //Read first poly
    cout << "\nFirst Polynomial\n"; for (i = p; i >= 0; i--) {
        newl = new poly;
        newl->pow_val = p;
        cout << "Enter Co-efficient for degree" << i << ":: "; cin >> newl->coeff;
        newl->next = NULL;
        if (poly1 == NULL)
            poly1 = newl;
        else
            end->next = newl;
        end = newl;
    }

    //Read Second poly
    cout << "\n\nSecond Polynomial\n"; end = NULL; for (i = p; i >= 0; i--) {
        newl = new poly;
        newl->pow_val = p;
        cout << "Enter Co-efficient for degree" << i << ":: "; cin >> newl->coeff;
        newl->next = NULL;
        if (poly2 == NULL)
            poly2 = newl;
        else
            end->next = newl;
        end = newl;
    }

    //Addition Logic
    poly *p1 = poly1, *p2 = poly2;
    end = NULL;
    while (p1 != NULL && p2 != NULL) {
        if (p1->pow_val == p2->pow_val) {
            newl = new poly;
            newl->pow_val = p--;
            newl->coeff = p1->coeff + p2->coeff;
            newl->next = NULL;
            if (poly3 == NULL)
                poly3 = newl;
            else
                end->next = newl;
            end = newl;
        }
        p1 = p1->next;
        p2 = p2->next;
    }
}

void add::display()
{
    poly* t = poly3;
    cout << "\n\nAnswer after addition is : ";
    while (t != NULL) {
        cout.setf(ios::showpos);
        cout << t->coeff;
        cout.unsetf(ios::showpos);
        cout << "X" << t->pow_val;
        t = t->next;
    }
}
int main()
{
    add obj;
    obj.addpoly();
    obj.display();
}
Add a comment
Know the answer?
Add Answer to:
Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials 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
  • In Java implement a class called Polynomials: This class should store the polynomial using a linked...

    In Java implement a class called Polynomials: This class should store the polynomial using a linked list with nodes. (do not use existing list classes in Java). This class should store only terms with non-zero coefficients. This class should store the polynomial terms in decreasing order of their powers. This class should have a constructor with no parameters that creates a polynomial with no terms, i.e. the polynomial 0. This class should have another constructor that takes a polynomial as...

  • C++ PRPGRAM- double-linked lists Create the .h and .cpp files for an Element class that contains...

    C++ PRPGRAM- double-linked lists Create the .h and .cpp files for an Element class that contains the data and pointers for a double-link list for this application. Your linked list must consist of objects of this class. The class should contain at least the following elements (you may have more if you wish): The standard four constructors (default, parameterized, copy, move) The standard assignment operators (copy, move) A destructor Stream operators (<< and >>) The standard relational operators (<, <=,...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • I need help filling in the the code in the methods add, multiply, and evaluate package...

    I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial {       /**    * Reads a polynomial from an input stream (file or keyboard). The storage format    * of the polynomial is:    * <pre>    * <coeff> <degree>    * <coeff> <degree>    * ...    *...

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • in C++, please help. Not only do we need to create an ADT but create a...

    in C++, please help. Not only do we need to create an ADT but create a main file as well to implement everything. For this program you will be creating a stack ADT to allow the client program to pick up treasures for a Star Wars scavenger game to get everyone ready for the opening of Galaxy’s Edge in 2020. Each treasure should have a (a) name, (b) description, (c) category, (d) what it is used for, and (e)if this...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

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