Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. /* Data Structure with step status */
  8.  
  9. typedef struct s_dots
  10. {
  11. float a;
  12. float b;
  13. float c;
  14. struct s_dots *next;
  15. } t_dots;
  16.  
  17. /* Create step with dots */
  18.  
  19. t_dots *ft_create_dot(float a, float b, float c)
  20. {
  21. t_dots *dt;
  22.  
  23. dt = (t_dots*)malloc(sizeof(t_dots));
  24. if (dt)
  25. {
  26. dt->a = a;
  27. dt->b = b;
  28. dt->c = c;
  29. dt->next = NULL;
  30. }
  31. return (dt);
  32. }
  33.  
  34. /* Get value function of X */
  35.  
  36. float f(float x)
  37. {
  38. float sol;
  39.  
  40. sol = 1 / (2 * sin(x)) + 1 - pow(x - 2, 2.0 / 5.0);
  41. return (sol);
  42. }
  43.  
  44. /* Show up more info */
  45.  
  46. void ft_showmore(t_dots *dt)
  47. {
  48. int i;
  49.  
  50. i = 0;
  51. while (dt)
  52. {
  53. cout << "Step(" << i << "): " << endl;
  54. cout << "\ta: " << dt->a << endl;
  55. cout << "\tb: " << dt->b << endl;
  56. cout << "\tc: " << dt->c << endl;
  57. dt = dt->next;
  58. i++;
  59. }
  60. }
  61.  
  62. int main(void)
  63. {
  64. float a, b, c, ep;
  65. t_dots *dt;
  66. t_dots *st;
  67. int i;
  68.  
  69. i = 0;
  70. /* Input Validation */
  71. try {
  72. cout << "a: ";
  73. cin >> a;
  74. if (cin.fail())
  75. throw "Data Type Error";
  76. cout << "b: ";
  77. cin >> b;
  78. if (cin.fail())
  79. throw "Data Type Error";
  80. cout << "eps: ";
  81. cin >> ep;
  82. if (cin.fail())
  83. throw "Data Type Error";
  84. }
  85. catch (char *err){
  86. cout << err << endl;
  87. system("pause");
  88. return (0);
  89. }
  90. dt = ft_create_dot(a, b, 0);
  91. st = dt;
  92. /* Cycle with step calculating */
  93. while (b - a > ep) {
  94. c = (a + b) / 2;
  95. dt->next = ft_create_dot(a, b, c);
  96. cout << "c" << i << ": " << c << endl;
  97. i++;
  98. if (f(b) * f(c) < 0)
  99. a = c;
  100. else
  101. b = c;
  102. dt = dt->next;
  103. }
  104. /* MOOOOAAAAR */
  105. cout << "Solution: " << (a + b) / 2 << endl;
  106. cout << "Wanna see more info? (Press ANY)" << endl;
  107. system("pause");
  108. ft_showmore(st);
  109. system("pause");
  110. return (0);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement