0

很抱歉打扰你,但我需要帮助。

我有一个任务来制作一个带有类的程序并计算线性回归,但是我的链表类只接收一个值,我不知道为什么。代码编译但它不计算线性回归。

这是我的代码:

#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<istream>
#include<string>


float a,b,r;
float X[20],Y[20],n = 10, z;
int intAux = 0, intI;
//double dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
float dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
//double dblSXX,dblSYY,dblSXY;
float dblSXX,dblSYY,dblSXY;

using namespace std;

// Clase Nodo
//p.base=
class Nodo {
float fltEstProxySize;
float fltDevelHours;
float fltEst;
Nodo* next;

 public:
  Nodo() {};
  void SetVariables(float data)
  {
      fltEstProxySize = data;
      fltDevelHours = data;
      fltEst = data;
  }

  void SetData(float EPS, float DH, float ES)
  {  
      fltEstProxySize = EPS;
      fltDevelHours = DH;
      fltEst = ES;
  };

  void SetNext(Nodo* aNext) 
  { 
      next = aNext;
  };

  float Data() 
  {  
      return(fltEstProxySize, fltDevelHours, fltEst);
  };

  Nodo* Next() 
  { 
      return next; 
  };
  };


 //p.
 //... Clase de la Lista ...
 class Lista {
Nodo *head;

 public:
  Lista() { head = NULL; };
  void Print();
  void Append(float fltEstProxySize, float fltDevelHours, float fltEst);
  void Delete(float fltEstProxySize, float fltDevelHours, float fltEst);
 };


/**
 * ... Imprime el contenido de la lista
 */
 //i.
void Lista::Print() {

// ... Apuntador temporal ...
Nodo *tmp = head;

// ... No hay Nodos ...
if ( tmp == NULL ) {
    cout << "EMPTY" << endl;
    return;
}

// ... Existe un Nodo in the Lista ...
if ( tmp->Next() == NULL ) {
    cout << tmp->Data();
    cout << " --> ";
    cout << "NULL" << endl;
}
else {
    // ... Recorre la lista y la imprime ...
    do {
        cout << tmp->Data();
        cout << " --> ";
        tmp = tmp->Next();
    }
    while ( tmp != NULL );

    cout << "NULL" << endl;
}
  }

  //i.
  // ... Agrega un nodo a la lista ...
  void Lista::Append(float fltEstProxySize, float fltDevelHours, float fltEst){

// ... Aqui crea a Nodo nuevo ...
Nodo* newNodo = new Nodo();

newNodo->SetData(fltEstProxySize, fltDevelHours, fltEst);
newNodo->SetNext(NULL);

// ... Crea un apuntador temporal ...
Nodo *tmp = head;

if ( tmp != NULL ) {
    // ... El Nodo esta en la Lista ...
    // ... Recorre la Lista ...
    while ( tmp->Next() != NULL ) {
        tmp = tmp->Next();
    }
    // ... El ultimo Nodo de la lista ...
    tmp->SetNext(newNodo);
}
else {
    // ... Nuevo Nodo de la lista ...
    head = newNodo;
}
   }



   /**
    * ... Borra un Nodo de la Lista ...
    */
  //i.
  void Lista::Delete(float fltEstProxySize, float fltDevelHours, float fltEst){

// ... Crea un Nodo temporal ...
Nodo *tmp = head;

// ... No hay Nodos ...
if ( tmp == NULL )
    return;

// ... ultimo Nodo de la Lista ...
if ( tmp->Next() == NULL ) {
    delete tmp;
    head = NULL;
}
else {
    // ... Recorre los nodos ...
    Nodo *prev;
    do {
        //if ( tmp->Data() == data ) break;
        if(tmp->Data() == fltEstProxySize && tmp->Data() == fltDevelHours) break;
        prev = tmp;
        tmp = tmp->Next();
    } while ( tmp != NULL );

    // ... Ajusta los Nodos ...
    prev->SetNext(tmp->Next());

    // ... Borra el Nodo actual ...
    delete tmp;
}
   }



   //i.
   class RegresionLineal
   {
Nodo *head;
int i;
//double dblSumX=0,dblSumY=0,dblSumX2=0,dblSumY2=0,dblSumXY=0;
//double dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
//double dblSXX,dblSYY,dblSXY;

   public:
RegresionLineal(){head = NULL;};
void Calculo();

  };

  void RegresionLineal::Calculo()
  {
for (intI=0;intI < 20; intI++)
{   
    //if (X[intI] != 0)
    //{
        dblSumX += X[intI];
        dblSumX2 += (X[intI] * X[intI]);
    //}

    //if(Y[intI] !=0)
    //{
        dblSumY += Y[intI];
        dblSumY2 += (Y[intI] * Y[intI]);
        dblSumXY += (X[intI] * Y[intI]);
    //}
}

dblSXX = dblSumX2 - dblSumX * dblSumX / n;
dblSYY = dblSumY2 - dblSumY * dblSumY / n;
dblSXY = dblSumXY - dblSumX * dblSumY / n;

// ... pendiente infinita ...
if (abs(dblSXX) == 0)
    //return 0;
    z=0;


// ... calcula la pendiente ...
b = dblSXY / dblSXX;
a = dblSumY / n - (b) * dblSumX / n;

// ... calcula el coeficiente de regresion ...
if (abs(dblSYY) == 0)
    r = 1;
else
    r = dblSXY / sqrt(dblSXX * dblSYY);


     }


     // ... ... ... ... ... ... ... ... ... ... ... ... ...
    int main()
    {
   // ... Variables auxiliares ...
       float fltMedia2 = 0;
       float fltDevStd1 = 0;
   float fltDevStd2 = 0;
   float fltSuma1 = 0;
   float fltSuma2 = 0;
   float fltCuenta = 0;
   int intF = 0;
   float fltAux1 = 0, fltAux4 = 0, fltMedia1, fltAux3;
float fltAux2 = 0;


Lista lista;
RegresionLineal RL;

// ... inicializa arreglo ...
for (intI = 0; intI < 20; intI++)
{
    X[intI] = 0;
    Y[intI] = 0;
}

// ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 

cout << "\nPlease introduce the values of table 1: (Note: to finish just type -1 and hit enter)\n";

while(!cin.eof())
{   //d.
    cout << "\nPlease introduce X: ";
    cin >> fltAux1;
    cout << "\nPlease introduce Y: ";
    cin >> fltAux2;
    cout << "\nPlease introduce the estimate: ";
    cin >> fltAux3;
    //d.
    //d.

    if ((fltAux1 == -1) || (fltAux2 == -1))
    {
        break;
    }
    else
    {
        lista.Append(fltAux1, fltAux2, fltAux3);
        X[intI] = fltAux1;
        Y[intI] = fltAux2;
        fltCuenta = fltCuenta + 1;
        intI++;
        cout << "fltAux1: " << fltAux1 << endl;
        cout << "fltAux2: " << fltAux2 << endl;
    }
}

lista.Print();
// ... Calcula la regresión lineal ...
RL.Calculo();
cout << "\na: " << a << "\n" << endl;

cout << "Suma X: " << dblSumX << endl;
cout << "Suma Y: " << dblSumY << endl;
cout << "Suma X2: " << dblSumX2 << endl;
cout << "Suma Y2: " << dblSumY2 << endl;
cout << "Suma X*Y:" << dblSumXY << endl;


return 0;
   }
4

1 回答 1

1

尽管代码中还有其他几个问题/可能的改进,但您现在面临的问题是因为您在函数循环intI之前没有重置为 0 。whilemain

以下是更正后的代码摘录:

// ... inicializa arreglo ...
for (intI = 0; intI < 20; intI++)
{
    X[intI] = 0;
    Y[intI] = 0;
}

// ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 

cout << "\nPlease introduce the values of table 1: (Note: to finish just type -1 and hit enter)\n";

intI = 0;          //Corrected here

while(!cin.eof())
于 2012-09-20T04:55:03.860 回答