Polynomial long division: Difference between revisions

Line 521:
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <math.hiostream>
#include <iterator>
#include <vector>
 
using namespace std;
typedef vector<double> Poly;
 
// does: prints all members of vector
// input: c - ASCII char with the name of the vector
// dA - degreereference ofto polynomial (vector)
void Print(char cname, intconst d, double*Poly &A) {
// A - pointer to vector
cout << cname << "[(" << iA.size()-1 << "]) = " << A[i] << endl";
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() {
doublePoly *N,* D,* d,* q,* r; // vectors - N / D == q && N % D == r
intsize_t dN, dD, dd, dq, dr; // degrees of vectors
size_t i; // loop counter
int i; // iterators
 
// setting the degrees of vectors
cout << "Enter the degree of N: ";
cin >> dN;
cout << "Enter the degree of D: ";
cin >> dD;
dq = dN-dD;
dr = dN-dD;
 
if( dD < 01 || dN < 1 ) {
cerr << "Error: degree of D and N must be positive.\n";
return 1;
}
 
// allocation and initialization of vectors
N=new double [.resize(dN+1]);
cout << "Enter the coefficients of N:"<<endl;
for ( i = 0; i <= dN+1; i++ ) {
cout << "N[" << i << "]= " << endl;
cin >> N[i];
}
 
D=new double [.resize(dN+1]);
cout << "Enter the coefficients of D:"<<endl;
for ( i = 0; i <= dD+1; i++ ) {
cout << "D[" << i << "]= " << endl;
cin >> D[i];
}
 
d=new double [.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;
if( dN >= dD ) {
while(dN >= dD) {
// 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 d+1; i++ )
}
for( i = 0 ; i < dD + 1 ; i++ ) {
d[i+dN-dD] = D[i];
}
dd = dN;
 
Print( 'd', dd, d );
 
// calculating one element of q
q[dN-dD] = N[dN]/d[dd];
 
Print( 'q', dq, q );
 
// d equals d * q[dN-dD]
for( i = 0 ; i < dq + 1 ; i++ ) {
d[i] = d[i] * q[dN-dD];
}
 
Print( 'd', dd, d );
 
// N equals N - d
for( i = 0 ; i < dN + 1 ; i++ ) {
N[i] = N[i] - d[i];
}
dN--;
 
Print( 'N', dN, N );
cout << "-----------------------" << endl << endl;
 
}
 
}
 
// r equals N
for( i = 0 ; i <= dN + 1 ; i++ ) {
r[i] = N[i];
}
dr = dN;
 
cout << "=========================" << endl << endl;
cout << "-- Result --" << endl << endl;
 
Print( 'q', dq, q );
Print( 'r', dr, r );
}
 
}</lang>
// dealocation
delete [] N;
delete [] D;
delete [] d;
delete [] q;
delete [] r;
}</lang>
 
=={{header|C#|C sharp}}==