#include <iostream>
#include <fstream>
#include <cstring>
#define precizie 0.001
#define set_precizie 2
using namespace std;
bool is_numar(char x)
{
if (x >= '0' && x <= '9')
{
return true;
}
return false;
}
void anulare_space(char x[])
{
while (x[0] == ' ')
{
strcpy(x, x+1);
}
}
int det_grad(char x[])
{
char *p = strchr(x, '^');
if (p == NULL)
{
if (strchr(x, 'x') != NULL)
{
return 2;
}
else
{
return 1;
}
}
else
{
return (p[1] - '0') + 1;
}
}
int det_semn(char x[])
{
int sem = 1;
if (strchr("+-", x[0]) != NULL)
{
if (x[0] == '-')
{
sem = -1;
}
strcpy(x, x+1);
}
return sem;
}
int det_numar(char x[])
{
int num = 0, i = 0;
while (is_numar(x[i]) == true)
{
num *= 10;
num += x[i] - '0';
i++;
}
if (x[i] == 'x' && num == 0)
{
num = 1;
}
return num;
}
void citire(int v[])
{
ifstream fin("date.in");
char sir[1000], *p;
int num, grad, semn;
fin.getline(sir, 950);
p = strtok(sir, " ");
while(p)
{
anulare_space(p);
semn = det_semn(p);
grad = det_grad(p);
num = det_numar(p);
if (grad > v[0])
{
v[0] = grad;
}
v[grad] = semn * num;
p = strtok(NULL, " ");
}
fin.close();
}
float putere(float a, int b) /* a la putere b*/
{
float prod = 1;
for (int i=1; i <= b; i++)
{
prod *= a;
}
return prod;
}
float abs(float x)
{
if (x < 0)
{
x *= -1;
}
return x;
}
float suma(int v[], float x)
{
float sum = 0;
for (int i=1; i<= v[0]-1; i++)
{
sum += v[i] * putere(x, i-1);
}
return sum;
}
int initi_indice(int v[])
{
int indice = 0;
float sum = v[1];
float coef_dom = 0;
//cout << 0 << " -> ";
//cout << coef_dom << " " << sum << endl;
while (abs(coef_dom) <= abs(sum))
{
indice -= set_precizie;
coef_dom = v[v[0]] * putere(indice, v[0]-1);
sum = suma(v, indice);
}
return indice;
}
/*int float_to_int(float a)
{
a *= 10000;
while (a%10 == 0)
{
a /= 10;
}
return a;
}
*/
/*float recalibrare(float a)
{
int aux_a, aux_prec;
aux_a = float_to_int(a);
aux_prec = float_to_int(precizie);
while (a!= 0)
{
a /= 10;
nr_cif++;
}
}
*/
void det_root(int v[])
{
ofstream fout("date.out");
float sum = v[1];
float coef_dom = 0; // 0^x = 0, pt. orice x ap lui R*
int nr_rad = 1;
bool semn, interval;
float indice = initi_indice(v);
if (v[1] < 0)
{
semn = false;
}
else
{
semn = true;
}
while ((abs(coef_dom) <= abs(sum)) || indice <= 0)
{
if (coef_dom + sum == 0)
{
fout << "radacina[" << nr_rad << "] este egala cu: " << indice << endl;
nr_rad++;
}
else
{
if (coef_dom + sum < 0 && semn == true)
{
if (interval == false)
{
interval = true;
nr_rad++;
fout << "radacina[" << nr_rad << "] apartine intervalului: (" << indice << " ; ";
}
else
{
interval = false;
fout << indice << ") " << endl;
}
semn = false;
}
else
{
if(coef_dom + sum > 0 && semn == false)
{
if (interval == false)
{
interval = true;
nr_rad++;
fout << "radacina[" << nr_rad << "] apartine intervalului: (" << indice << " ; ";
}
else
{
interval = false;
fout << indice << ") " << endl;
}
}
semn = true;
}
}
indice += precizie;
//indice = recalib
cout << indice << " -> ";
coef_dom = v[v[0]] * putere(indice, v[0]-1)*1.00;
sum = suma(v, indice);
cout << coef_dom << " " << sum << " -> " << coef_dom + sum << endl;
}
}
int v[10000];
int main()
{
citire(v);
det_root(v);
return 0;
}
Comments