Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chương trình C++ tính đạo hàm của đa thức


Cho một chuỗi chứa hạng tử đa thức, nhiệm vụ là đánh giá đạo hàm của đa thức đó.

Đa thức là gì?

Đa thức đến từ hai từ: "Poly" nghĩa là "nhiều" và "nomial" nghĩa là "hạng tử", bao gồm nhiều hạng tử. Biểu thức đa thức là biểu thức chứa biến, hệ số và số mũ, chỉ liên quan đến các phép toán như cộng, nhân và trừ của biến.

Ví dụ đa thức

x² + x + 1

Đạo hàm của đa thức p(x) = mxⁿ sẽ là:

m * n * x^(n-1)

Ví dụ

Input: str = "2x^3 +1x^1 + 3x^2"    val = 2 Output: 37 Explanation: 6x^2 + 1x^0 + 6x^1    Thay x = 2    6*4 + 1 + 6*2 = 24 + 1 + 12 = 37 Input: str = "1x^3"    val = 2 Output: 12 Explanation: 1 * 3 *x^2    Thay x = 2    3 * 4 = 12

Phương pháp giải quyết:

  • Nhập chuỗi và giá trị x.
  • Duyệt chuỗi và kiểm tra chữ số và biến.
  • Tiếp tục cộng và duyệt chuỗi cho đến khi gặp '+'.
  • Áp dụng công thức m * n * x^(n-1).
  • Trả về kết quả.

Thuật toán

Start Step 1-> In function long long term(string polyterm, long long val)    Declare and initialize coeffStr = ""    Declare i    Loop For i = 0 and polyterm[i] != 'x' and i++       Call coeffStr.push_back(polyterm[i])       Set coeff = atol(coeffStr.c_str())       Declare and initialize powStr = ""       Loop For i = i + 2 and i != polyterm.size() and i++ powStr.push_back(polyterm[i])          Set power = atol(powStr.c_str());    Return coeff * power * pow(val, power - 1) Step 2-> In function long long value(string& str, int val)    Set ans = 0    Call istringstream is(str)    Declare string polyterm    Loop While is >> polyterm       If polyterm == "+" then,          Continue       Else          Set ans = (ans + term(polyterm, val))    Return ans Step 3-> In function int main()    Declare and initialize str = "2x^3 + 1x^1 + 3x^2"    Declare and initialize val = 2    Print the value received by value(str, val) Stop

Ví dụ mã nguồn

#include using namespace std; long long term(string polyterm, long long val) {    //tìm hệ số    string coeffStr = "";    int i;    for (i = 0; polyterm[i] != 'x'; i++)       coeffStr.push_back(polyterm[i]);    long long coeff = atol(coeffStr.c_str());    // lấy giá trị lũy thừa    string powStr = "";    for (i = i + 2; i != polyterm.size(); i++)       powStr.push_back(polyterm[i]);    long long power = atol(powStr.c_str());    // Với ax^n, trả về a(n-1)x^(n-1)    return coeff * power * pow(val, power - 1); } long long value(string& str, int val) {    long long ans = 0;    // dùng istringstream để lấy input theo token    istringstream is(str);    string polyterm;    while (is >> polyterm) {       // kiểm tra nếu token bằng '+' thì       // tiếp tục với chuỗi       if (polyterm == "+")          continue;          // Ngược lại tìm đạo hàm của hạng tử          // cụ thể đó       else          ans = (ans + term(polyterm, val));    }    return ans; } // main function int main() {    string str = "2x^3 + 1x^1 + 3x^2";    int val = 2;    cout << value(str, val);    return 0; }

Kết quả

37