Polynomial long division: Difference between revisions

Content added Content deleted
Line 521: Line 521:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<lang cpp>
#include <math.h>
#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
// d - degree of vector
// A - reference to polynomial (vector)
void Print(char name, const Poly &A) {
// A - pointer to vector
cout << name << "(" << A.size()-1 << ") = [ ";
void Print(char c, int d, double* A) {
copy(A.begin(), A.end(), ostream_iterator<decltype(A[0])>(cout, " "));
int i;
cout << "]\n";

for (i=0; i < d+1; i++)
cout << c << "[" << i << "]= " << A[i] << endl;
cout << "Degree of " << c << ": " << d << endl << endl;
}
}


int main() {
int main() {
double *N,*D,*d,*q,*r; // vectors - N / D = q N % D = r
Poly N, D, d, q, r; // vectors - N / D == q && N % D == r
int dN, dD, dd, dq, dr; // degrees of vectors
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;


if( dD < 1 || dN < 1 ) {
cerr << "Error: degree of D and N must be positive.\n";
return 1;
}


// allocation and initialization of vectors
// allocation and initialization of vectors
N=new double [dN+1];
N.resize(dN+1);
cout << "Enter the coefficients of N:"<<endl;
cout << "Enter the coefficients of N:"<<endl;
for ( i = 0; i < dN+1; i++ ) {
for ( i = 0; i <= dN; i++ ) {
cout << "N[" << i << "]= " << endl;
cout << "N[" << i << "]= ";
cin >> N[i];
cin >> N[i];
}
}


D=new double [dN+1];
D.resize(dN+1);
cout << "Enter the coefficients of D:"<<endl;
cout << "Enter the coefficients of D:"<<endl;
for ( i = 0; i < dD+1; i++ ) {
for ( i = 0; i <= dD; i++ ) {
cout << "D[" << i << "]= " << endl;
cout << "D[" << i << "]= ";
cin >> D[i];
cin >> D[i];
}
}


d=new double [dN+1];
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;
}

if( dD < 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 ; i++ )
}
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', dd, 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', dq, 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', dd, 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', dN, N );
Print( 'N', N );
cout << "-----------------------" << endl << endl;
cout << "-----------------------" << endl << endl;


}
}

}
}


// r equals N
// r equals N
for( i = 0 ; i < dN + 1 ; i++ ) {
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', dq, q );
Print( 'q', q );
Print( 'r', dr, r );
Print( 'r', r );
}


</lang>
// dealocation
delete [] N;
delete [] D;
delete [] d;
delete [] q;
delete [] r;
}</lang>


=={{header|C#|C sharp}}==
=={{header|C#|C sharp}}==