C++ HW5-1 for WallPower

有一個方程式如下,a[4]x^4+a[3]^3+a[2]x^2+a[1]x+a[0],請設計一個C++程式,
輸入一元四次方程式係數,以陣列儲存。再輸入變數值求結果。
參考輸出如下圖。

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int p(int x, int n)
  5. {
  6. int temp = 1;
  7. for (int i = 1; i <= n; i++)
  8. temp *= x;
  9. return temp;
  10. }
  11.  
  12. int main()
  13. {
  14. int input[5], x, total = 0;
  15. for (int i = 0; i < 5; i++)
  16. {
  17. cout << "請輸入方程式中 " << i << "次項的係數 ";
  18. cin >> input[i];
  19. }
  20. cout << "請輸入預計算的變數值 ";
  21. cin >> x;
  22. for (int i = 0; i < 5; i++)
  23. total += p(x, i) * input[i];
  24. cout << "f(" << x << ")=" << total << endl;
  25. system("pause");
  26. return 0;
  27. }
  28.  
  29.  

留言

這個網誌中的熱門文章

C# 井字遊戲