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 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>    * ...    *...

  • 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...

  • 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...

  • team, create a Java program that adds or removes entries from a linked list based on...

    team, create a Java program that adds or removes entries from a linked list based on user input. You can use the index of the entry as the key to identify the record for addition or deletion. The program can be as simple as you like but must add an entry to the list based on the user input and also must delete the proper item in the list based on the index entered by the user Display your list...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • Its a report . Implement and test a preliminary version of the polynomial class, using a...

    Its a report . Implement and test a preliminary version of the polynomial class, using a small array to store the coefficients of the polynomial in order of increasing exponents, following the design pattern of object-oriented approach. Suppose the range of the exponents is in [0, 30). • Use an array as a private member variable. • insert(), adding a new term on specific exponent. • remove(), deleting a term. • Have a add() method, deciding the parameters and the...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

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