Re: [問題] 請問C++的多項式運算

看板Programming作者 (烏木)時間18年前 (2007/09/23 19:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
※ 引述《granzi (烏木)》之銘言: : ※ 引述《bens1 (藍星)》之銘言: : :  題目 多項式ꄠ x^6-6x^5+15x^4-20x^3+15x^2-6x+1 : :  x=0.99到1.01 每次間隔0.001 : : 不知道在C中如何寫出多項式函式 : :  還有x=0.99到1.01中 : :  應該用哪個迴圈 : :  我是C程式新手 : :  請各位高手解答 : :  謝謝 : #include <math.h> : #include <stdio.h> : double polyn(double x) { : double funValue = 0.0; : funValue = pow(x, 6.0) - 6.0*pow(x, 5.0) + 15.0*pow(x, 4.0) - : 20.0*pow(x, 3.0) + 15.0*pow(x, 2.0) - 6.0*x + 1.0; : return funValue; : } : int main(int argc, char **argv) { : double sum = 0.0; : for (double iter = 0.99; iter <= 1.01; iter += 0.001) { : sum += polyn(iter); : } : printf("%g\n", sum); : return 0; : } : 然後用gcc -lm -std=gnu99 -o polyn polyn.c 編譯 : 如果你用的是Borland C++ 一類的IDE,請查閱一下BCB有沒有支援C99標準 對不起,我沒注意到你要的是C++ 把include 改成 #include <cmath> #include <iostream> using namespace std; 然後main的部份改成 int main(int argc, char **argv) { double sum = 0.0; double iter=0.99; while(iter <= 1.01) { sum += polyn(iter); iter += 0.001; } cout << sum << endl; return 0; } 然後用g++ -o polyn -lm polyn.cxx編譯,g++支援C++98到什麼程度我不太清楚, 你可以去http://gcc.gnu.org/ 去看一下最新的gcc資訊 C++98能不能像C99一樣可以直接迴圈宣告我不清楚 當然,這似乎不是個好習慣 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.68.240 ※ 編輯: granzi 來自: 220.135.68.240 (09/23 19:43) ※ 編輯: granzi 來自: 220.135.68.240 (09/23 19:46) ※ 編輯: granzi 來自: 220.135.68.240 (09/23 19:51)
文章代碼(AID): #16zaxYoI (Programming)
文章代碼(AID): #16zaxYoI (Programming)