首页 >
> 详细

CPSC 2150 Instructor Gladys Monagan Assignment #9 1

Langara College

CPSC 2150

Assignment #9: Univariate Polynomials

Assignment due with Brightspace at 10:00pm on Wednesday November 25

Read chapter 6.1, 6.2, 6.3, 6.4, 6.5, 6.6 of the textbook

Purpose

• The purpose of this assignment is to implement a C++ class called Polynomial

to represent a univariate polynomial with integer coefficients.

• To integrate code into an existing program, understanding other people’s

code.

• To use yet another interface for a program: not just line arguments or

redirection of code but using a menu style interface that can be run “in

batch”.

• To use functors and lambda functions in C++17.

Background

A univariate polynomial is a polynomial in one variable. We will use x as the

variable. A univariate polynomial has the form

a1 x

d1 + a2 x

d2 + … + an x

dn

where n >= 0, a1, a2, … an are integers and d1, d2, … dn are distinct nonnegative

integers.

For example, the polynomial

2 x4

+ 7 x2

- 3x5

+ 6

has n = 4 terms. The degree of this polynomial is 5 which is the highest power.

Also, the coefficient of the term of degree 4 is 2, the coefficient of x3

is 0.

Implementation

Implement an Abstract Data Type Univariate Univariate that provides the

following functionality (as per Univariate.h provided)

• creates a univariate polynomial, the zero polynomial

• creates a univariate polynomial given a coefficient and a degree

• takes a term and adds it to the polynomial (places the term in the right

place in the polynomial)

• destroys a polynomial

• determines if the polynomial is the zero polynomial

• prints the polynomial

• reads the polynomial from an input stream and builds a proper

polynomial

CPSC 2150 Instructor Gladys Monagan Assignment #9 2

• evaluates the polynomial for given value of x

• gives the degree of the polynomial (the degree of a polynomial is the

exponent of the term with the highest degree); as a special case, give -1

as the degree of the zero polynomial

• returns the coefficient of a term in the polynomial given a degree

(given an exponent)

• returns the number of terms in the polynomial

• returns a new polynomial which is the sum of 2 polynomials

About your implementation:

• use a binary search tree to store the terms by degree using the

function passed to determine if it is in descending or ascending order

• a tree node should have a pointer to a term (which stores the coefficient

of the term and the degree of the term) and a pointer to each ‘child

node’.

• overload the + operator for addition: the sum is a new polynomial

• overload << for printing and >> for reading

• provide a copy constructor, destructor and overload the assignment

operator for the polynomial

• free up heap memory that is no longer needed

• you should not have any terms (nor nodes for that matter) that have zero

as the term coefficient (thus you need to decide on a representation for

the zero polynomial which is the constant 0)

• you should only store once (in a node) a term of degree k (i.e. you

should not have two nodes for two terms both of degree k)

• you should be able to delete any node from the tree when needed (you

will probably need this functionality when summing up polynomials)

• pass the boolean function that compares two degrees (to know how to

insert into the binary search tree) as an argument to the polynomial

• when printing the polynomial, print the terms in descending order

based on degree

• use the Term class provided including the overloaded operator << for

output (found in the files Term.h and Term.cpp)

• use the Univariate.h provided for you (including the given definition of

Node) and expand it i.e. add code as needed and document

Example

CPSC 2150 Instructor Gladys Monagan Assignment #9 3

5 refers to the number of terms and the terms corresponding to the values

given above are

Note that after the terms are read into an instance of the Univariate class which

internally is represented with a Binary Search Tree, the polynomial will have

only 4 tree nodes (not 5). So, unlike Assignment #5, do not assume that the

input terms do not have the same degree. You program should handle the

case shown above where on input the term 3 x4

is given and then the term -x

4

.

Both have the same degree so they need to be “simplified” internally to 2 x4

as they are entered into the binary search tree.

Unlike Assignment #5, each term read is entered properly into the binary

search tree.

The degree of the above polynomial is 5.

Calling the function ‘coefficient’ of the polynomial with the argument 4 returns

2 because the coefficient of the term that has degree 4 (namely of 2 x4

) is 2

Calling the function ‘evaluate with x=1’ for the polynomial returns 12.

You need to enter the zero polynomial “somehow” so, when reading a term, if

the coefficient is zero, regardless of the degree, take that to be the zero

polynomial.

Application File

An application file is provided for you to use: it is in the file called

polyMath.cpp (pun intended).

It is a simple command driven (menu style) program that tests your Univariate

class.

We have provided a series of ‘commands’ and data in a file called input.txt as

a starting point. And, we have provided a file output.txt which gives the

results of calling polyMath with the line argument -batch

polyMath -batch < input.txt > output.txt

Optional / Bonus

If you feel energetic, differentiate the polynomial with respect to x. Return a

new polynomial when differentiating.

CPSC 2150 Instructor Gladys Monagan Assignment #9 4

To submit as Assignment #9 as a single compressed zip file (with two folders if you

do the bonus)

i. an input file to the executable polyMath that tests your class Univariate

(note that the program polyMath must read from std::cin. The input file

is a test plan: we have started it for you.

a. list the input polynomials in pairs

b. test various operations

c. list as the ‘mention’ command why you chose the values

ii. an output file that gives the results of your input file running the

program with the option ‘-batch’

iii. the source code

a. Term.h and Term.cpp as provided (you may expand them)

b. Univariate.h add the missing code and other functions/variables.

Please do not inline the code in the file Univariate.h

c. Univariate.cpp add the missing code and other

functions/variables

d. input.txt add commands

e. the following files provided for you

i. polyMath.cpp

ii. Makefile to compile and link polyMath.cpp in C++17

iii. Commands.cpp

iv. Commands.h

iv. If you implement differentiation submit this bonus part in a different,

separate folder with an input file with differentiation commands and zip

both folders into one single zip file.

v. A file called README.txt where

a. you indicate that you implemented the bonus part

b. you can list there what is not implemented in the assignment in

general (if you did not have time for certain parts)

c. you can tell us how you would design your program differently if

you had more time.

Some test cases which must be included in your input file

a = x7 + 3x3 + 6 – 8x + 4x4

b = 2x7 + 4x4 – 9x

a = x5 + x8 + x7 + x3 + x

4

b = 0

CPSC 2150 Instructor Gladys Monagan Assignment #9 5

a = -2x5 + 2x7

b = 2x9 + 2x7 + 2x5 + 2x3 + 2x4

a = 2x9+ 2x7 + 2x5 + 2x3 + 2x4

b = -2x5 + 2x7

Notes

Implement yourself the insertion of the nodes and deletion and the copying and

destroying of the BST and so on. Do not find a “library” that does it for you or use the

STL maps or sets.

To remind you what the course outline states

Plagiarism and Cheating will not be tolerated

• The assignments and labs that you submit must be your own. Code given in

class and textbook material may be included in an assignment or lab but you

must cite these sources.

• Unless otherwise stated, even if you cite the source but submit code that is not

your own as part of your assignment or lab, you will get zero and be reported

for cheating. Ask your instructor if in doubt.

• Students are expected to take measures to protect their work. Do not show

your code to anyone. Do not post your code anywhere. If you do and someone

copies your code and submits it, you will get zero and be reported for

cheating.

• If you pay someone to write your program or a part thereof, it is plagiarism.

• If a student cheats or plagiarizes in an assignment, this will be discussed in the

following code review and the student will get a zero in that code review.

• Do not post any material associated with this course. It is copyrighted

material.

So, yes, you may use the code that I put up on Brightspace. You will need to delete

nodes and though you don’t have to use what I provided, you could

Please do not include in your code non-standardized C++17

e.g

#pragma once

#include “pch.h”

And include the header file of the library functions that you use

e.g.

#include

#include

#include

#include

联系我们

- QQ：99515681
- 邮箱：99515681@qq.com
- 工作时间：8:00-23:00
- 微信：codinghelp2

- Cs2461-10实验程序代做、代写java，C/C++，Python编程设 2021-03-02
- 代写program程序语言、代做python，C++课程程序、代写java编 2021-03-02
- Programming课程代做、代写c++程序语言、Algorithms编程 2021-03-02
- 代写csc1-Ua程序、代做java编程设计、Java实验编程代做 代做留学 2021-03-02
- 代做program编程语言、代写python程序、代做python设计编程 2021-03-02
- 代写data编程设计、代做python语言程序、Python课程编程代写 代 2021-03-02
- Cse 13S程序实验代做、代写c++编程、C/C++程序语言调试 代写留学 2021-03-02
- Mat136h5编程代做、C/C++程序调试、Python，Java编程设计 2021-03-01
- 代写ee425x实验编程、代做python，C++，Java程序设计 帮做c 2021-03-01
- Cscc11程序课程代做、代写python程序设计、Python编程调试 代 2021-03-01
- 代写program编程、Python语言程序调试、Python编程设计代写 2021-03-01
- 代做r语言编程|代做database|代做留学生p... 2021-03-01
- Data Structures代写、代做r编程课程、代做r程序实验 帮做ha 2021-03-01
- 代做data留学生编程、C++，Python语言代写、Java程序代做 代写 2021-03-01
- 代写aps 105编程实验、C/C++程序语言代做 代写r语言程序|代写py 2021-03-01
- Fre6831 Computational Finance 2021-02-28
- Sta141b Assignment 5 Interactive Visu... 2021-02-28
- Eecs2011a-F20 2021-02-28
- Comp-251 Final Asssessment 2021-02-28
- 代写cs1027课程程序、代做java编程语言、代写java留学生编程帮做h 2021-02-28