Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- class RegressaoLinear
- {
- private:
- int n;
- double *x;
- double *y;
- double a, b;
- public:
- //RegressaoLinear(): x(NULL), y(NULL), n(0) {} //Se eu deixar essa linha, ocorre um erro monstro (why???)
- void SetX(double*);
- void SetY(double*);
- void SetN(int);
- double SomatorioX();
- double SomatorioY();
- double SomatorioXY();
- double SomatorioX_Y();
- double SomatorioX2();
- double Somatorio2X();
- double TheS();
- double GetA();
- double GetB();
- void printN()
- {
- cout << n;
- }
- void printX()
- {
- for (int i = 0; i < n; i++) {
- cout << x[i] << " ";
- }
- }
- };
- void RegressaoLinear::SetX(double *x)
- {
- *this -> x = *x;
- }
- void RegressaoLinear::SetY(double *y)
- {
- *this -> y = *y;
- }
- void RegressaoLinear::SetN(int n)
- {
- this -> n = n;
- }
- double RegressaoLinear::SomatorioX()
- {
- double somaX = 0;
- for (int i = 0; i < n; i++) {
- somaX += x[i];
- }
- return somaX;
- }
- double RegressaoLinear::SomatorioY()
- {
- double somaY = 0;
- for (int i = 0; i < n; i++) {
- somaY += y[i];
- }
- return somaY;
- }
- double RegressaoLinear::SomatorioX_Y()
- {
- return (SomatorioX() * SomatorioY());
- }
- double RegressaoLinear::SomatorioXY()
- {
- double somaXY = 0;
- double hold;
- for (int i = 0; i < n; i++) {
- hold = x[i]*y[i];
- somaXY += hold;
- }
- return somaXY;
- }
- double RegressaoLinear::Somatorio2X()
- {
- return (SomatorioX() * SomatorioX());
- }
- double RegressaoLinear::SomatorioX2()
- {
- double hold;
- double SomaX2;
- for (int i = 0; i < n; i++) {
- hold = x[i]*x[i];
- SomaX2 += hold;
- }
- return SomaX2;
- }
- double RegressaoLinear::GetA()
- {
- return (((n*SomatorioXY())-(SomatorioX_Y())))/((n*SomatorioX2())- (Somatorio2X()));
- }
- double RegressaoLinear::GetB()
- {
- return ((SomatorioY()-(GetA()*SomatorioX()))/n);
- }
- double RegressaoLinear::TheS()
- {
- a = GetA();
- b = GetB();
- double hold;
- double SomaS = 0;
- for (int i = 0; i < n; i++) {
- hold = ((y[i]-((a*x[i])+b)) * (y[i]-((a*x[i])+b)));
- SomaS += hold;
- }
- return SomaS;
- }
- void AlocaValores(double **, int&);
- void AlocaValores(double **V);
- void printValores(double *, int);
- int main()
- {
- RegressaoLinear Regradora; //Calculadora de regressão linear
- int n; //Quantidade de x ou y digitados
- double *x;
- double *y; //Vetores dos valores
- AlocaValores(&x,n);
- AlocaValores(&y);
- Regradora.SetX(x);
- Regradora.SetN(n); //Se eu pôr esta linha abaixo de SeY(y), n muda de valor (why???)
- Regradora.SetY(y);
- Regradora.printN();
- cout << endl;
- Regradora.printX();
- cout << endl;
- cout << "Somatorio de X: " << Regradora.SomatorioX();
- return 0;
- }
- void AlocaValores(double **V, int &n)
- {
- double aux; //Variável auxiliar
- int p = 0; //Controla o tamanho do vetor dinâmico
- cout << "Digite os valores de dX (2x Enter para acabar):" << endl;
- (*V) = NULL; //Zera o ponteiro
- while (getch() != 'r') { //Verdadeiro até apertar Enter
- cout << "---> ";
- cin >> aux; //Leitura do valor de x
- p++; //Incrementa o tamanho do vetor
- (*V) = (double*)realloc((*V), p*sizeof(double)); //Alocação dinâmica
- (*V)[p-1] = aux; //Coloca o valor lido no vetor
- }
- n = p; //Quantidade de valores
- }
- void AlocaValores(double **V)
- {
- double aux; //Variável auxiliar
- int p = 0; //Controla o tamanho do vetor dinâmico
- cout << "Digite os valores de dY (2x Enter para acabar):" << endl;
- (*V) = NULL; //Zera o ponteiro
- while (getch() != 'r') { //Verdadeiro até apertar Enter
- cout << "---> ";
- cin >> aux; //Leitura do valor de x
- p++; //Incrementa o tamanho do vetor
- (*V) = (double*)realloc((*V), p*sizeof(double)); //Alocação dinâmica
- (*V)[p-1] = aux; //Coloca o valor lido no vetor
- }
- }
- void printValores(double *V, int n)
- {
- cout << endl;
- for (int i = 0; i < n; i++) {
- cout << V[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement