Anonymous user
Polynomial long division: Difference between revisions
→{{header|C++}} abbreviate and idiomatize code
Line 521:
=={{header|C++}}==
<lang cpp
#include <
#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
//
▲void Print(char c, int d, double* A) {
copy(A.begin(), A.end(), ostream_iterator<decltype(A[0])>(cout, " "));
cout << "]\n";
for (i=0; i < d+1; i++)▼
▲ cout << c << "[" << i << "]= " << A[i] << endl;
}
int main() {
size_t i; // loop counter
// 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;
cerr << "Error: degree of D and N must be positive.\n";
return 1;
}▼
// allocation and initialization of vectors
N
cout << "Enter the coefficients of N:"<<endl;
for ( i = 0; i <= dN
cout << "N[" << i << "]= "
cin >> N[i];
}
D
cout << "Enter the coefficients of D:"<<endl;
for ( i = 0; i <= dD
cout << "D[" << i << "]= "
cin >> D[i];
}
d
q.resize(dq+1);
r.resize(dr+1);
▲ }
}▼
▲ if( dD < 0) {
cout << "-- Procedure --" << endl << endl;
if( dN >= dD ) {
while(dN >= dD) {
// d equals D shifted right
d.assign(d.size(), 0);
d[i+dN-dD] = D[i];
dd = dN;
Print( 'd'
// calculating one element of q
q[dN-dD] = N[dN]/d[dd];
Print( 'q'
// d equals d * q[dN-dD]
for( i = 0 ; i < dq + 1 ; i++ )
d[i] = d[i] * q[dN-dD];
Print( 'd'
// N equals N - d
for( i = 0 ; i < dN + 1 ; i++ )
N[i] = N[i] - d[i];
dN--;
Print( 'N'
cout << "-----------------------" << endl << endl;
}
}
// r equals N
for( i = 0 ; i <= dN
r[i] = N[i];
cout << "=========================" << endl << endl;
cout << "-- Result --" << endl << endl;
Print( 'q'
Print( 'r'
▲}</lang>
=={{header|C#|C sharp}}==
|