instructions

advertisement
Assignment number 2
Dead line 25.04.2010(23:59)
Polynomial Calculator
Write a program that enables the user to manipulate two polynomials A and B.
The maximal rank of these polynomials is 9.
A third polynomial C is also used.
All the polynomials are defined as one-dimensional arrays of integers.
Each element of the arrays contains a coefficient and its index represents the
degree of the element.
For example, the next polynomial A(x) =19x^6+7 x^3 -5 x^2 -4 is represented
as :
index
value
0
-4
1
2
-5
3
7
4
5
6
19
7
8
9
Your program prints a menu, reads one out of the five options (Q,I,E,M,S ,D
only) from the user and displays the result of the suitable operation.
The program runs until the user chooses the option Q (Exit).
The menu is:
******** Main
Q: Quit
menu ********
I: Input polynomial
E: Evaluate polynomial
M: Mult of polynomials A x B
S: Sum of polynomials A + B
D: Derive polynomial
****************************
Please,enter your choice ->
1
o
I . Input polynomial
First you enter the name of the polynomial, as one character (A or B only) and
then you must enter the polynomial degree and its coefficient.
You can enter the degree and its’ coefficient in any order. Entering the same
degree and its coefficient more than once will erase their previous values.
Degree -1 indicates end of input. The new polynomial must then be displayed
in a correct form:




Elements with coefficients 0 should not be displayed (do not write 0x).
Coefficients of 1 should not be displayed (write x, not 1x).
x^1 should be written just x.
Before the highest degree element you should not print the symbol plus
(+), if it’s coefficient value is positive.
Example 1:
Enter the polynomial name ->A
Enter the degree of A ->2
Enter the coefficient of this (2) degree ->-5
Enter the degree A ->3
Enter the coefficient of this (3) degree ->7
Enter the degree of A ->0
Enter the coefficient of this (0) degree ->-4
Enter the degree of A ->-1
A(x) =7 x^3 -5 x^2 -4
Example 2:
2
Enter the polynomial name ->B
Enter the degree of B ->7
Enter the coefficient of this (7) degree ->105
Enter the degree of B ->1
Enter the coefficient of this (1) degree ->-27
Enter the degree of B ->0
Enter the coefficient of this (0) degree ->1
Enter the degree of B ->-1
B(x) =105 x^7 -27 x +1
Entering a value of degree different from the polynomial rank ( 0-9) will lead to
the next message:
Error : degree is out of range
and then the program will request the correct polynomial degree.
Example3:
Enter the polynomial name ->B
Enter the degree of B ->17
Error : degree is out of range
Enter the B polynomial's degree ->
E . Evaluate polynomial
First, you enter the name of the polynomial, as one character (A or B only) .
In case the polynomial isn’t initialized the following massage will appear:
Polynomial A is empty
(assuming the evaluated polynomial name is A)
3
and the program will pass to the main menu.
Example1:
Enter the polynomial name ->A
Enter the x value ->3
A(3) = 140
Example2:
We assume that B(x) =15
Enter the polynomial name ->B
Enter the x value ->7
B(7)=15
o
S. Sum of polynomials A + B
In this option the program computes the sum of the two polynomials A and
B , the result is stored in C.
The C polynomial value is displayed.
Example: (we assume that B(x) = 2x^5+x^2-3x+4)
C(x) = A(x)+B(x)=2x^5 +7 x^3 -4x^2-3x
In case the polynomial A or B or both are not initialized the
following message will appear:
Polynomial B is empty
(assuming the empty polynomial name is B)
4
Then the program will pass to the main menu.
o
M. Mult of polynomials A x B
Here the program computes the product of the two polynomials A, B and
stores it as C and displays its value.
Example1:
C(x) =A(x) x B(x)= 14x^8 - 10x^7 -x^5-26x^4 + 43x^3- 24x^2 +12x -16
Example2:
We assume that A(x)=x^9+1 and B(x)=x^9-1
C(x) =A(x) x B(x)= x^18-1
In case the polynomial A or B or both aren’t initialized the following
massage will appear:
Polynomial A is empty
(assuming the empty polynomial name is A)
and the program will pass to the main menu.
o
D. Derive polynomial
In this option the program computes the derivative of a polynomial (A or B)
and stores it in C.
In case the polynomial A or B is not initialized the
following message will appear:
Polynomial
B is empty (assuming the empty polynomial name is B)
Then the program will pass to the main menu.
5
Example1:
Enter the polynomial name ->A
C(x) = A'(x) = 21x^2 – 10x
Example2:
We assume that B(x) =9
Enter the polynomial name ->B
C(x) = B'(x) = 0
o
Q. Quit
Making this choice will cause in exiting the program.
Important notes:






The program must compile without errors or warnings in Visual Studio
2005, when using the project settings in prog102.vcproj.
The output of your programs MUST be exactly as in the example
program
Don't forget to check that the input data are valid and not out of the
defined range.
Don't forget to write clear comments, a nice presentation of the
program, correctly indented, with meaningful variable names.
Also, do not use anything you didn't learn in class. This includes
functions, pointers, libraries etc...
The program must not cause a runtime error at any point of its
execution.
All questions regarding the assignment should be sent
to Alexander Shkolnik (shkol@cs.bgu.ac.il) .
6
Download