C++ HW5-1 for WallPower

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

#include <iostream>
using namespace std;

int p(int x, int n)
{
 int temp = 1;
 for (int i = 1; i <= n; i++)
  temp *= x;
 return temp;
}

int main()
{
 int input[5], x, total = 0;
 for (int i = 0; i < 5; i++)
 {
  cout << "請輸入方程式中 " << i << "次項的係數 ";
  cin >> input[i];
 }
 cout << "請輸入預計算的變數值 ";
 cin >> x;
 for (int i = 0; i < 5; i++)
  total += p(x, i) * input[i];
 cout << "f(" << x << ")=" << total << endl;
 system("pause");
 return 0;
}


留言

這個網誌中的熱門文章

UVA 11321 Java