Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7. double determine1(double,double);
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13. double x1,x2,C;
  14. bool answer=false;
  15. cout <<"輸入區間端點1:\n";
  16. cin >> x1;
  17. cout <<"輸入區間端點2:\n";
  18. cin >> x2;
  19.  
  20. C=determine1(x1,x2);
  21.  
  22. if(C>0)
  23. {
  24. cout <<"此方程式在此區間無解\n";
  25. }
  26. else if (C<0)
  27. {
  28. answer = true;
  29. cout <<"此方程式在此區間有解\n";
  30. }
  31.  
  32. if(answer=true)
  33. {
  34. double temp;
  35. int i;
  36. for(i=0;i<=1000;i++)
  37. {
  38. temp=x1;
  39. x1=(x1+x2)/2;
  40.  
  41. C=determine1(x1,x2);
  42.  
  43. if(C>0)
  44. {
  45. x2=x1;
  46. x1=temp;
  47. }
  48.  
  49. }
  50.  
  51. cout << "解的近似值為:"<< x1 <<endl;
  52. }
  53. system("PAUSE");
  54. return 0;
  55. }
  56.  
  57. double determine1(double x1,double x2)
  58. {
  59. double A,B;
  60. A=pow(x1,3)+x1-1;
  61. B=pow(x2,3)+x2-1;
  62. return A*B;
  63. }
Add Comment
Please, Sign In to add comment