In: Computer Science
introduction
A univariate polynomial (i.e. a polynomial of one variable) is
defined as the sum of terms of the form axb
where a is a real coefficient, x is a variable, and b ≥ 0 is an
integer exponent. Therefore, the following
mathematical expressions are univariate polynomials.
4.5x^3 – 2.1x^2 + 6.2x – 9.7
-8.3x^4 + 7.6x
2.0x^2
0
The degree of a polynomial is defined as its highest-valued
exponent. Hence, the degrees of the four
polynomials above are 3, 4, 2 and 0, respectively. Note as well
that the terms of each polynomial are
sorted by decreasing exponent values.
The following mathematical expressions are not univariate
polynomials. The first has a negative exponent
and the second is expressed in terms of two variables, x and
y.
x^2 + x – x ^-2
9.4xy
For this assignment, each pair or trio of programmers is expected
to create, manipulate, and output a
collection of polynomials via a menu-driven user interface.
Task 5: Main Program
The class Program includes the static Main method which serves as the entry point of execution.
class Program
{
static void Main (string[] args)
{
Polynomial S; // Collection of polynomials
…
}
}
The main program provides the user interface to manipulates the collection S of polynomials, namely:
1) To create a polynomial and insert it into S (no index is needed),
2) To add two polynomials from S (retrieved by index) and to insert the resultant polynomial into S,
3) To multiply two polynomials from S (retrieved by index) and to insert the resultant polynomial into S,
4) To delete the polynomial from S at a given index, and
5) To evaluate the polynomial from S at a given index.
To aid the user, the collection of polynomials is output before each operation. The polynomials are numbered (indexed) starting at 0.
Indices should be checked before an add, multiply, delete or evaluate is performed.
Please use C# to answer this question!!!
class Polynomial: IComparable
{
double[] Coefficents;
public Polynomial(double[] coeffs)
{
Init(coeffs);
}
public void Init(double[] coeffs)
{
Coefficents = coeffs;
}
public int GetDegree()
{
return Coefficients.Length - 1;
}
public double evaluate(double x)
{
double y = 0;
double px = 1;
for (int i = 0; i < Coefficients.Length; i++)
{
y += px * Coefficients[i];
px *= x;
}
return y;
}
// Create a string representation
public override string ToString()
{
string result;
if (Coefficients.Length)
result = Double.ToString(Coefficients[0]);
for (int i = 1; i < Coefficients.Length; i++)
{
if (Coefficients[i] < 0)
result += " - ";
else
result += " + ";
result += Double.ToString(Math.Abs(Coefficients[i]));
result += "*x^";
result += Int.ToString(i);
}
return result;
}
// A[] represents coefficients of first polynomial
// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
static int[] add(int[] A, int[] B, int m, int n)
{
int size = max(m, n);
int[] sum = new int[size];
// Initialize the porduct polynomial
for (int i = 0; i < m; i++)
{
sum[i] = A[i];
}
// Take ever term of first polynomial
for (int i = 0; i < n; i++)
{
sum[i] += B[i];
}
return sum;
}
static int[] multiply(int []A, int []B,
int m, int n)
{
int[] prod = new int[m + n - 1];
// Initialize the porduct polynomial
for (int i = 0; i < m + n - 1; i++)
{
prod[i] = 0;
}
// Multiply two polynomials term by term
// Take ever term of first polynomial
for (int i = 0; i < m; i++)
{
// Multiply the current term of first polynomial
// with every term of second polynomial.
for (int j = 0; j < n; j++)
{
prod[i + j] += A[i] * B[j];
}
}
return prod;
}
}