The realization of the root by the binomial method in the chain list

Source: Internet
Author: User

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace Std;
Class poly{
Public
Double C;
int e;
Poly*next;
};
Poly*input ();
Double f (poly*head,double x);
Double root (poly*head,double A, double b);
int main () {
Poly*head;
Double A, B;
Head = input ();
Cin >> a >> b;
cout << setiosflags (ios::fixed) << setprecision (3);
cout << Root (head, a, b) << Endl;
return 0;
}
Poly*input () {
/* Enter the items of the polynomial, return the head pointer */
Poly*p = new Poly;
Poly*head = p, *p1 = p;
while (Cin >> p->c >> p->e) {
P1 = p;
char ch = getchar ();
p = new Poly;
p1->next=p;
if (ch = = ' \ n ')/* encounters a space jump input */
Break
}
Delete p;
P1->next = NULL;
return head;
}
Double f (Poly*head, double x) {
/* Any number x to get the value of the expression */
Double sum = 0.0;
while (head) {
Sum + = Head->c*pow (x, head->e);
Head = head->next;
}
return sum;
}
Double root (Poly*head, double A, double b) {
/* Find root by dichotomy * *
Double X;
while (b-a>0.001) {
x = (A + b)/2;
if (ABS (f (head,x)) < 0.001)
return x;
else if (f (head,a) *f (head,x) < 0)
b = x;
Else
A = x;
}
return A;
}

The realization of the root by the binomial method in the chain list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.