1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <initializer_list>
#include <cctype>
using namespace std;
//=====================================================================
template <typename T> class polynomial // Polynomial over field of type T
{
vector<T> coeff;
public:
// Various constructors
polynomial() { coeff = { T{} }; } // Construct as "zero"
polynomial( const vector<T> &V ) { coeff = V; } // Construct from a vector
polynomial( const initializer_list<T> &V ) { coeff = vector<T>( V.begin(), V.end() ); } // Construct from initialiser list
polynomial( const string str, char X = 'x' ); // Construct from string
T eval( T x );
};
//----------------------------
template <typename T> polynomial<T>::polynomial( const string str, char X ) // Construct from string
{
// First strip blanks
string s;
for ( char c : str ) if ( !isspace( c ) ) s += c;
// Break into units at every '+' or '-'; the limits will be [p,q)
int p = 0, q = p;
while ( q < s.size() )
{
for ( q = p + 1; q < s.size() && s[q] != '+' && s[q] != '-'; q++ );
string unit = s.substr( p, q - p ); // should be of form "cxn", meaning c times x^n
// Pick out coefficient and exponent
T c = 1;
int n = 0;
int pos = unit.find( X ); // position of char X
if ( pos == string::npos ) // X not found; pure number
{
stringstream( unit ) >> c;
}
else // identify coefficient (c) and exponent (n)
{
if ( pos != 0 ) // pos == 0 would mean default c = 1
{
string first = unit.substr( 0, pos );
if ( first == "+" ) c = 1; // just "+" means +1
else if ( first == "-" ) c = -1; // just "-" means -1
else stringstream( first ) >> c;
}
if ( pos == unit.size() - 1 )
{
n = 1;
}
else
{
stringstream( unit.substr( pos + 1 ) ) >> n;
}
}
if ( n + 1 > coeff.size() ) coeff.resize( n + 1 );
coeff[n] += c;
p = q;
}
}
//----------------------------
template <typename T> T polynomial<T>::eval( T x ) // Evaluate polynomial at x
{
T result = 0;
for ( int r = coeff.size() - 1; r >= 0; r-- ) result = coeff[r] + result * x;
return result;
}
//=====================================================================
int main()
{
vector<double> V = { 1.0, 0.0, -3.0, 2.0 };
polynomial<double> p( V ); // vector
polynomial<double> q( { 1.0, 0.0, -3.0, 2.0 } ); // initialiser list
polynomial<double> r( " 1 - 3x2 + 2x3" ); // formula in 'x'
polynomial<int> s( "2t3 - 2t2 + 1 - t2", 't' ); // formula in 't', coefficients out of order and repeats
cout << "x" << '\t' << "p(x)" << '\t' << "q(x)" << '\t' << "r(x)" << '\t' << "s(x)" << '\n';
for ( int i = 0; i <= 10; i++ )
{
double arg = i;
cout << arg << '\t'
<< p.eval( arg ) << '\t'
<< q.eval( arg ) << '\t'
<< r.eval( arg ) << '\t'
<< s.eval( i ) << '\n';
}
}
|