Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class RegressaoLinear
  8. {
  9. private:
  10. int n;
  11. double *x;
  12. double *y;
  13. double a, b;
  14.  
  15. public:
  16. //RegressaoLinear(): x(NULL), y(NULL), n(0) {} //Se eu deixar essa linha, ocorre um erro monstro (why???)
  17.  
  18. void SetX(double*);
  19. void SetY(double*);
  20. void SetN(int);
  21.  
  22. double SomatorioX();
  23. double SomatorioY();
  24. double SomatorioXY();
  25. double SomatorioX_Y();
  26. double SomatorioX2();
  27. double Somatorio2X();
  28. double TheS();
  29.  
  30. double GetA();
  31. double GetB();
  32.  
  33. void printN()
  34. {
  35. cout << n;
  36. }
  37.  
  38. void printX()
  39. {
  40. for (int i = 0; i < n; i++) {
  41. cout << x[i] << " ";
  42. }
  43. }
  44. };
  45.  
  46. void RegressaoLinear::SetX(double *x)
  47. {
  48. *this -> x = *x;
  49. }
  50.  
  51. void RegressaoLinear::SetY(double *y)
  52. {
  53. *this -> y = *y;
  54. }
  55.  
  56. void RegressaoLinear::SetN(int n)
  57. {
  58. this -> n = n;
  59. }
  60.  
  61. double RegressaoLinear::SomatorioX()
  62. {
  63. double somaX = 0;
  64.  
  65. for (int i = 0; i < n; i++) {
  66. somaX += x[i];
  67. }
  68.  
  69. return somaX;
  70. }
  71.  
  72. double RegressaoLinear::SomatorioY()
  73. {
  74. double somaY = 0;
  75.  
  76. for (int i = 0; i < n; i++) {
  77. somaY += y[i];
  78. }
  79.  
  80. return somaY;
  81. }
  82.  
  83. double RegressaoLinear::SomatorioX_Y()
  84. {
  85. return (SomatorioX() * SomatorioY());
  86. }
  87.  
  88. double RegressaoLinear::SomatorioXY()
  89. {
  90. double somaXY = 0;
  91. double hold;
  92.  
  93. for (int i = 0; i < n; i++) {
  94. hold = x[i]*y[i];
  95. somaXY += hold;
  96. }
  97.  
  98. return somaXY;
  99. }
  100.  
  101. double RegressaoLinear::Somatorio2X()
  102. {
  103. return (SomatorioX() * SomatorioX());
  104. }
  105.  
  106. double RegressaoLinear::SomatorioX2()
  107. {
  108. double hold;
  109. double SomaX2;
  110.  
  111. for (int i = 0; i < n; i++) {
  112. hold = x[i]*x[i];
  113. SomaX2 += hold;
  114. }
  115.  
  116. return SomaX2;
  117. }
  118.  
  119. double RegressaoLinear::GetA()
  120. {
  121. return (((n*SomatorioXY())-(SomatorioX_Y())))/((n*SomatorioX2())- (Somatorio2X()));
  122. }
  123.  
  124. double RegressaoLinear::GetB()
  125. {
  126. return ((SomatorioY()-(GetA()*SomatorioX()))/n);
  127. }
  128.  
  129. double RegressaoLinear::TheS()
  130. {
  131. a = GetA();
  132. b = GetB();
  133.  
  134. double hold;
  135. double SomaS = 0;
  136.  
  137. for (int i = 0; i < n; i++) {
  138. hold = ((y[i]-((a*x[i])+b)) * (y[i]-((a*x[i])+b)));
  139. SomaS += hold;
  140. }
  141.  
  142. return SomaS;
  143. }
  144.  
  145. void AlocaValores(double **, int&);
  146. void AlocaValores(double **V);
  147. void printValores(double *, int);
  148.  
  149. int main()
  150. {
  151. RegressaoLinear Regradora; //Calculadora de regressão linear
  152. int n; //Quantidade de x ou y digitados
  153. double *x;
  154. double *y; //Vetores dos valores
  155.  
  156. AlocaValores(&x,n);
  157. AlocaValores(&y);
  158.  
  159. Regradora.SetX(x);
  160. Regradora.SetN(n); //Se eu pôr esta linha abaixo de SeY(y), n muda de valor (why???)
  161. Regradora.SetY(y);
  162.  
  163. Regradora.printN();
  164. cout << endl;
  165. Regradora.printX();
  166.  
  167. cout << endl;
  168. cout << "Somatorio de X: " << Regradora.SomatorioX();
  169.  
  170. return 0;
  171. }
  172.  
  173. void AlocaValores(double **V, int &n)
  174. {
  175. double aux; //Variável auxiliar
  176. int p = 0; //Controla o tamanho do vetor dinâmico
  177.  
  178. cout << "Digite os valores de dX (2x Enter para acabar):" << endl;
  179.  
  180. (*V) = NULL; //Zera o ponteiro
  181. while (getch() != 'r') { //Verdadeiro até apertar Enter
  182.  
  183. cout << "---> ";
  184. cin >> aux; //Leitura do valor de x
  185.  
  186. p++; //Incrementa o tamanho do vetor
  187. (*V) = (double*)realloc((*V), p*sizeof(double)); //Alocação dinâmica
  188.  
  189. (*V)[p-1] = aux; //Coloca o valor lido no vetor
  190. }
  191. n = p; //Quantidade de valores
  192. }
  193.  
  194. void AlocaValores(double **V)
  195. {
  196. double aux; //Variável auxiliar
  197. int p = 0; //Controla o tamanho do vetor dinâmico
  198.  
  199. cout << "Digite os valores de dY (2x Enter para acabar):" << endl;
  200.  
  201. (*V) = NULL; //Zera o ponteiro
  202. while (getch() != 'r') { //Verdadeiro até apertar Enter
  203.  
  204. cout << "---> ";
  205. cin >> aux; //Leitura do valor de x
  206.  
  207. p++; //Incrementa o tamanho do vetor
  208. (*V) = (double*)realloc((*V), p*sizeof(double)); //Alocação dinâmica
  209.  
  210. (*V)[p-1] = aux; //Coloca o valor lido no vetor
  211. }
  212. }
  213.  
  214. void printValores(double *V, int n)
  215. {
  216. cout << endl;
  217. for (int i = 0; i < n; i++) {
  218. cout << V[i] << " ";
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement