Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <math.h>
- using namespace std;
- /* Data Structure with step status */
- typedef struct s_dots
- {
- float a;
- float b;
- float c;
- struct s_dots *next;
- } t_dots;
- /* Create step with dots */
- t_dots *ft_create_dot(float a, float b, float c)
- {
- t_dots *dt;
- dt = (t_dots*)malloc(sizeof(t_dots));
- if (dt)
- {
- dt->a = a;
- dt->b = b;
- dt->c = c;
- dt->next = NULL;
- }
- return (dt);
- }
- /* Get value function of X */
- float f(float x)
- {
- float sol;
- sol = 1 / (2 * sin(x)) + 1 - pow(x - 2, 2.0 / 5.0);
- return (sol);
- }
- /* Show up more info */
- void ft_showmore(t_dots *dt)
- {
- int i;
- i = 0;
- while (dt)
- {
- cout << "Step(" << i << "): " << endl;
- cout << "\ta: " << dt->a << endl;
- cout << "\tb: " << dt->b << endl;
- cout << "\tc: " << dt->c << endl;
- dt = dt->next;
- i++;
- }
- }
- int main(void)
- {
- float a, b, c, ep;
- t_dots *dt;
- t_dots *st;
- int i;
- i = 0;
- /* Input Validation */
- try {
- cout << "a: ";
- cin >> a;
- if (cin.fail())
- throw "Data Type Error";
- cout << "b: ";
- cin >> b;
- if (cin.fail())
- throw "Data Type Error";
- cout << "eps: ";
- cin >> ep;
- if (cin.fail())
- throw "Data Type Error";
- }
- catch (char *err){
- cout << err << endl;
- system("pause");
- return (0);
- }
- dt = ft_create_dot(a, b, 0);
- st = dt;
- /* Cycle with step calculating */
- while (b - a > ep) {
- c = (a + b) / 2;
- dt->next = ft_create_dot(a, b, c);
- cout << "c" << i << ": " << c << endl;
- i++;
- if (f(b) * f(c) < 0)
- a = c;
- else
- b = c;
- dt = dt->next;
- }
- /* MOOOOAAAAR */
- cout << "Solution: " << (a + b) / 2 << endl;
- cout << "Wanna see more info? (Press ANY)" << endl;
- system("pause");
- ft_showmore(st);
- system("pause");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement