crsandu

Untitled

Apr 14th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. #define precizie 0.001
  6. #define set_precizie 2
  7.  
  8. using namespace std;
  9.  
  10. bool is_numar(char x)
  11. {
  12. if (x >= '0' && x <= '9')
  13. {
  14. return true;
  15. }
  16. return false;
  17. }
  18.  
  19. void anulare_space(char x[])
  20. {
  21. while (x[0] == ' ')
  22. {
  23. strcpy(x, x+1);
  24. }
  25. }
  26.  
  27. int det_grad(char x[])
  28. {
  29. char *p = strchr(x, '^');
  30. if (p == NULL)
  31. {
  32. if (strchr(x, 'x') != NULL)
  33. {
  34. return 2;
  35. }
  36. else
  37. {
  38. return 1;
  39. }
  40. }
  41. else
  42. {
  43. return (p[1] - '0') + 1;
  44. }
  45. }
  46.  
  47. int det_semn(char x[])
  48. {
  49. int sem = 1;
  50.  
  51. if (strchr("+-", x[0]) != NULL)
  52. {
  53. if (x[0] == '-')
  54. {
  55. sem = -1;
  56. }
  57. strcpy(x, x+1);
  58. }
  59.  
  60. return sem;
  61. }
  62.  
  63. int det_numar(char x[])
  64. {
  65. int num = 0, i = 0;
  66. while (is_numar(x[i]) == true)
  67. {
  68. num *= 10;
  69. num += x[i] - '0';
  70.  
  71. i++;
  72. }
  73. if (x[i] == 'x' && num == 0)
  74. {
  75. num = 1;
  76. }
  77. return num;
  78. }
  79.  
  80. void citire(int v[])
  81. {
  82. ifstream fin("date.in");
  83. char sir[1000], *p;
  84. int num, grad, semn;
  85.  
  86. fin.getline(sir, 950);
  87. p = strtok(sir, " ");
  88.  
  89. while(p)
  90. {
  91. anulare_space(p);
  92.  
  93. semn = det_semn(p);
  94. grad = det_grad(p);
  95. num = det_numar(p);
  96.  
  97. if (grad > v[0])
  98. {
  99. v[0] = grad;
  100. }
  101.  
  102. v[grad] = semn * num;
  103.  
  104. p = strtok(NULL, " ");
  105. }
  106. fin.close();
  107. }
  108.  
  109. float putere(float a, int b) /* a la putere b*/
  110. {
  111. float prod = 1;
  112. for (int i=1; i <= b; i++)
  113. {
  114. prod *= a;
  115. }
  116. return prod;
  117. }
  118.  
  119. float abs(float x)
  120. {
  121. if (x < 0)
  122. {
  123. x *= -1;
  124. }
  125. return x;
  126. }
  127.  
  128. float suma(int v[], float x)
  129. {
  130. float sum = 0;
  131. for (int i=1; i<= v[0]-1; i++)
  132. {
  133. sum += v[i] * putere(x, i-1);
  134. }
  135. return sum;
  136. }
  137.  
  138. int initi_indice(int v[])
  139. {
  140. int indice = 0;
  141. float sum = v[1];
  142. float coef_dom = 0;
  143.  
  144. //cout << 0 << " -> ";
  145. //cout << coef_dom << " " << sum << endl;
  146.  
  147. while (abs(coef_dom) <= abs(sum))
  148. {
  149. indice -= set_precizie;
  150. coef_dom = v[v[0]] * putere(indice, v[0]-1);
  151. sum = suma(v, indice);
  152. }
  153. return indice;
  154. }
  155.  
  156. /*int float_to_int(float a)
  157. {
  158. a *= 10000;
  159. while (a%10 == 0)
  160. {
  161. a /= 10;
  162. }
  163. return a;
  164. }
  165. */
  166. /*float recalibrare(float a)
  167. {
  168. int aux_a, aux_prec;
  169. aux_a = float_to_int(a);
  170. aux_prec = float_to_int(precizie);
  171.  
  172. while (a!= 0)
  173. {
  174. a /= 10;
  175. nr_cif++;
  176. }
  177. }
  178. */
  179. void det_root(int v[])
  180. {
  181. ofstream fout("date.out");
  182.  
  183. float sum = v[1];
  184. float coef_dom = 0; // 0^x = 0, pt. orice x ap lui R*
  185. int nr_rad = 1;
  186. bool semn, interval;
  187.  
  188. float indice = initi_indice(v);
  189.  
  190. if (v[1] < 0)
  191. {
  192. semn = false;
  193. }
  194. else
  195. {
  196. semn = true;
  197. }
  198.  
  199. while ((abs(coef_dom) <= abs(sum)) || indice <= 0)
  200. {
  201. if (coef_dom + sum == 0)
  202. {
  203. fout << "radacina[" << nr_rad << "] este egala cu: " << indice << endl;
  204. nr_rad++;
  205. }
  206. else
  207. {
  208. if (coef_dom + sum < 0 && semn == true)
  209. {
  210. if (interval == false)
  211. {
  212. interval = true;
  213.  
  214. nr_rad++;
  215. fout << "radacina[" << nr_rad << "] apartine intervalului: (" << indice << " ; ";
  216. }
  217. else
  218. {
  219. interval = false;
  220. fout << indice << ") " << endl;
  221. }
  222. semn = false;
  223. }
  224. else
  225. {
  226. if(coef_dom + sum > 0 && semn == false)
  227. {
  228. if (interval == false)
  229. {
  230. interval = true;
  231.  
  232. nr_rad++;
  233. fout << "radacina[" << nr_rad << "] apartine intervalului: (" << indice << " ; ";
  234. }
  235. else
  236. {
  237. interval = false;
  238. fout << indice << ") " << endl;
  239. }
  240.  
  241. }
  242. semn = true;
  243. }
  244. }
  245.  
  246.  
  247. indice += precizie;
  248. //indice = recalib
  249.  
  250. cout << indice << " -> ";
  251.  
  252.  
  253. coef_dom = v[v[0]] * putere(indice, v[0]-1)*1.00;
  254. sum = suma(v, indice);
  255.  
  256. cout << coef_dom << " " << sum << " -> " << coef_dom + sum << endl;
  257. }
  258. }
  259.  
  260. int v[10000];
  261.  
  262. int main()
  263. {
  264. citire(v);
  265. det_root(v);
  266. return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment