Polynomial long division: Difference between revisions
Content added Content deleted
Line 521: | Line 521: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp |
<lang cpp> |
||
#include < |
#include <iostream> |
||
#include <iterator> |
|||
#include <vector> |
|||
using namespace std; |
using namespace std; |
||
typedef vector<double> Poly; |
|||
// does: prints all members of vector |
// does: prints all members of vector |
||
// input: c - ASCII char with the name of the vector |
// input: c - ASCII char with the name of the vector |
||
// |
// A - reference to polynomial (vector) |
||
⚫ | |||
// A - pointer to vector |
|||
⚫ | |||
⚫ | |||
copy(A.begin(), A.end(), ostream_iterator<decltype(A[0])>(cout, " ")); |
|||
int i; |
|||
cout << "]\n"; |
|||
⚫ | |||
⚫ | |||
cout << "Degree of " << c << ": " << d << endl << endl; |
|||
} |
} |
||
int main() { |
int main() { |
||
Poly N, D, d, q, r; // vectors - N / D == q && N % D == r |
|||
size_t dN, dD, dd, dq, dr; // degrees of vectors |
|||
size_t i; // loop counter |
|||
int i; // iterators |
|||
// setting the degrees of vectors |
// setting the degrees of vectors |
||
cout << "Enter the degree of N:"; |
cout << "Enter the degree of N: "; |
||
cin >> dN; |
cin >> dN; |
||
cout << "Enter the degree of D:"; |
cout << "Enter the degree of D: "; |
||
cin >> dD; |
cin >> dD; |
||
dq = dN-dD; |
dq = dN-dD; |
||
dr = dN-dD; |
dr = dN-dD; |
||
⚫ | |||
cerr << "Error: degree of D and N must be positive.\n"; |
|||
return 1; |
|||
⚫ | |||
// allocation and initialization of vectors |
// allocation and initialization of vectors |
||
N |
N.resize(dN+1); |
||
cout << "Enter the coefficients of N:"<<endl; |
cout << "Enter the coefficients of N:"<<endl; |
||
for ( i = 0; i < dN |
for ( i = 0; i <= dN; i++ ) { |
||
cout << "N[" << i << "]= " |
cout << "N[" << i << "]= "; |
||
cin >> N[i]; |
cin >> N[i]; |
||
} |
} |
||
D |
D.resize(dN+1); |
||
cout << "Enter the coefficients of D:"<<endl; |
cout << "Enter the coefficients of D:"<<endl; |
||
for ( i = 0; i < dD |
for ( i = 0; i <= dD; i++ ) { |
||
cout << "D[" << i << "]= " |
cout << "D[" << i << "]= "; |
||
cin >> D[i]; |
cin >> D[i]; |
||
} |
} |
||
d |
d.resize(dN+1); |
||
q.resize(dq+1); |
|||
for( i = dD+1 ; i < dN+1; i++ ) { |
|||
r.resize(dr+1); |
|||
D[i] = 0; |
|||
⚫ | |||
q=new double [dq+1]; |
|||
for( i = 0 ; i < dq + 1 ; i++ ) { |
|||
q[i] = 0; |
|||
⚫ | |||
r=new double [dr+1]; |
|||
for( i = 0 ; i < dr + 1 ; i++ ) { |
|||
r[i] = 0; |
|||
} |
|||
⚫ | |||
cout << "Degree of D is less than zero. Error!"; |
|||
} |
|||
cout << "-- Procedure --" << endl << endl; |
cout << "-- Procedure --" << endl << endl; |
||
if( dN >= dD ) { |
if( dN >= dD ) { |
||
while(dN >= dD) { |
while(dN >= dD) { |
||
// d equals D shifted right |
// d equals D shifted right |
||
d.assign(d.size(), 0); |
|||
for( i = 0 ; i < dN + 1 ; i++ ) { |
|||
d[i] = 0; |
|||
⚫ | |||
} |
|||
for( i = 0 ; i < dD + 1 ; i++ ) { |
|||
d[i+dN-dD] = D[i]; |
d[i+dN-dD] = D[i]; |
||
} |
|||
dd = dN; |
dd = dN; |
||
Print( 'd' |
Print( 'd', d ); |
||
// calculating one element of q |
// calculating one element of q |
||
q[dN-dD] = N[dN]/d[dd]; |
q[dN-dD] = N[dN]/d[dd]; |
||
Print( 'q' |
Print( 'q', q ); |
||
// d equals d * q[dN-dD] |
// d equals d * q[dN-dD] |
||
for( i = 0 ; i < dq + 1 ; i++ ) |
for( i = 0 ; i < dq + 1 ; i++ ) |
||
d[i] = d[i] * q[dN-dD]; |
d[i] = d[i] * q[dN-dD]; |
||
} |
|||
Print( 'd' |
Print( 'd', d ); |
||
// N equals N - d |
// N equals N - d |
||
for( i = 0 ; i < dN + 1 ; i++ ) |
for( i = 0 ; i < dN + 1 ; i++ ) |
||
N[i] = N[i] - d[i]; |
N[i] = N[i] - d[i]; |
||
} |
|||
dN--; |
dN--; |
||
Print( 'N' |
Print( 'N', N ); |
||
cout << "-----------------------" << endl << endl; |
cout << "-----------------------" << endl << endl; |
||
} |
} |
||
} |
} |
||
// r equals N |
// r equals N |
||
for( i = 0 ; i < dN |
for( i = 0 ; i <= dN ; i++ ) |
||
r[i] = N[i]; |
r[i] = N[i]; |
||
} |
|||
dr = dN; |
|||
cout << "=========================" << endl << endl; |
cout << "=========================" << endl << endl; |
||
cout << "-- Result --" << endl << endl; |
cout << "-- Result --" << endl << endl; |
||
Print( 'q' |
Print( 'q', q ); |
||
Print( 'r' |
Print( 'r', r ); |
||
⚫ | |||
⚫ | |||
// dealocation |
|||
delete [] N; |
|||
delete [] D; |
|||
delete [] d; |
|||
delete [] q; |
|||
delete [] r; |
|||
⚫ | |||
=={{header|C#|C sharp}}== |
=={{header|C#|C sharp}}== |