?? polyvalueonedim.cpp
字號:
//PolyValueOneDim.cpp 一維實(復)多項式求值示例
#include <iostream> //輸入輸出流頭文件
#include <polynomials.h> //多項式及連分式頭文件
using namespace std; //名字空間
//求多項式p(x)=2x^6-5x^5+3x^4+x^3-7x^2+7x-20
//在x=-0.9,0.9,-1.1,1.1,-1.3,1.3處的函數值
void main(void)
{
cout << "PolyValueOneDim()" << endl << endl;
//多項式系數與自變量都為實數
cout << "Coefficient and variable are real. Result is real: " << endl;
double dCoff[7] = {-20.0, 7.0, -7.0, 1.0, 3.0, -5.0, 2.0};
double x[6] = {0.9, -0.9, 1.1, -1.1, 1.3, -1.3};
valarray<double> vCoff(dCoff,7);
cout.setf(ios::fixed|ios::left); //輸出數據為定點法,靠左對齊
cout.precision(6); //精度6位
for(size_t st = 0; st < 6; st++) //輸出多項式值
{
double dValue = PolyValueOneDim(vCoff, 7, x[st]);
cout << "x(" << st << ") = " << x[st];
cout << "\t p(" << st << ") = " << dValue << endl;
}
cout << endl;
//多項式系數與自變量都為復數
cout << "Coefficient and variable are complex. Result is complex: " << endl;
complex<double> cdCoff[7] =
{ complex<double>(1.2, 3.4),
complex<double>(-3.2, -4.8),complex<double>(2.7, -6.4),
complex<double>(3.1, 5.9), complex<double>(3.5, 3.9),
complex<double>(12.1, -5.4), complex<double>(-2.5, 8.4),
};
complex<double> cx[6] =
{
complex<double>(0.9, 1.4), complex<double>(-0.5, 1.4),
complex<double>(-0.9, -1.8),complex<double>(-1.1, -1.4),
complex<double>(1.1, 1.9), complex<double>(-1.5, 1.9),
};
valarray<complex<double> > vcdCoff(cdCoff,7);
for(st = 0; st < 6; st++) //輸出多項式值
{
complex<double> cp = PolyValueOneDim(vcdCoff, 7, cx[st]);
cout << "cx(" << st << ") = " << cx[st];
cout << "\t cp(" << st << ") = " << cp << endl;
}
cout << endl;
//多項式系數為實數,自變量為復數
cout << "Coefficient is real. Variable is complex. Result is complex: " << endl;
double drCoff[7] = {1,2,3,4,5,6,7};
valarray<double> vdrCoff(drCoff,7);
for(st = 0; st < 6; st++) //輸出多項式值
{
complex<double> dp = PolyValueOneDim(vdrCoff, 7, cx[st]);
cout << "cx(" << st << ") = " << cx[st];
cout << "\t dp(" << st << ") = " << dp << endl;
}
cout << endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -