解决方案 »

  1.   

    本人多年前用BC3.1写的代码,共5个文件,供参考,有什么不明白,可以发短信:
    // xxfc.hpp
    #include <iostream.h>#ifndef __XXFC_HPP
    #define __XXFC_HPPclass DatArray // 动态数组类
    {  int count;public:  double *sj;
      DatArray()
      {
        sj = 0;
      }
      DatArray( int n )
      {
        Init( n );
      }
      void Init( int );
      ~DatArray()
      {
        Delete();
      }
      void Delete()
      {
        if( sj )
          delete sj;
      }
    };class FcArray         // 方程数组类
    {  DatArray *dat;
      double *jg;
      int count;public:  // 构造函数. 参数: 方程数组的元数
      FcArray( int );
      // 方程计算函数
      int Sum();
      void Print( ostream& );
      void Delete()
      {
        delete[] dat;
        delete[] jg;
      }
      // 赋值. 参数: 行, 列, 数据
      void Let( int x, int y, double s )
      {
        dat[x].sj[y] = s;
      }
      // 赋值: 参数: 行, 行数据数组
      void Let( int, double* );
      // 返回方程解. 参数: 某个元下标
      double Value( int x )
      {
        return jg[x];
      }
      ~FcArray()
      {
        Delete();
      }};#endif
    // xxfc.cpp#include <iomanip.h>
    #include "xxfc.hpp"void DatArray::Init( int n )
    {
      count = n;
      sj = new double[n];
      for( int i = 0; i < n; i ++ )
          sj[i] = 0.0;
    }void FcArray::Print( ostream &f )
    {
      int i, j;
      f << "方程式:\n" << setiosflags( ios::fixed );
      for( i = 0; i < count; i ++ )
      {
        for( j = 0; j < count; j ++ )
        {
          if( j )
    f << " + ";
          f << dat[i].sj[j] << 'X' << j;
        }
        f << " = " << dat[i].sj[j] << endl;
      }
      f << "方程解:" << endl;
      for( i = 0; i < count; i ++ )
        f << 'X' << i << " = " << jg[i] << endl;
    }void FcArray::Let( int x, double *s )
    {
      for( int i = 0; i <= count; i ++ )
        Let( x, i, s[i] );
    }FcArray::FcArray( int n )
    {
      count = n;
      dat = new DatArray[n];
      jg = new double[n];
      for( int i = 0; i < count; i ++ )
        dat[i].Init( n + 1 );
    }int FcArray::Sum()
    {
      int i, j, q, p;
      double a;
      DatArray x0( count );
      for(q = 0; q < count - 1; q ++)
      {
        p = q;
        while( dat[q].sj[q] == 0.0 ){
          p ++;
          if( p == count )
    return -1;
          for( j = 0; j < count; j ++ )
    x0.sj[j] = dat[q].sj[j];
          for( i = q; i < count - 1; i ++ )
    for( j = 0; j < count; j ++ )
      dat[i].sj[j] = dat[i + 1].sj[j];
          for( j = 0; j < count; j ++ )
    dat[count - 1].sj[j] = x0.sj[j];
        }
        for( i = q + 1; i < count; i ++ )
        {
          a = -( dat[i].sj[q] / dat[q].sj[q] );
          for( j = q; j < count; j ++ )
    dat[i].sj[j] += a * dat[q].sj[j];
          dat[i].sj[count] += a * dat[q].sj[count];
        }
      }
      for( j = 0; j < count; j ++ )
        x0.sj[j] = 0.0;
      jg[count - 1] = dat[count - 1].sj[count] / dat[count - 1].sj[count - 1];
      for( i = count - 2; i >= 0; i -- )
      {
        for( j = count - 1; j > i; j -- )
          x0.sj[i] += jg[j] * dat[i].sj[j];
        jg[i] = ( dat[i].sj[count] - x0.sj[i] ) / dat[i].sj[i];
      }
      return 0;
    }
      

  2.   

    // dyhg.hpp#ifndef __DYHG_HPP
    #define __DYHG_HPP#include "xxfc.hpp"class Dyhg
    {  DatArray *dat;
      double *jg;  int wcount;
      int hcount;
      double SumPf( int, int );public:  // 构造函数. 参数: 数据行数, 数据列数(自变量1, 自变量2, ..., 因变量)
      Dyhg( int, int );
      // 计算
      void Sum();
      void Print( ostream& );
      void Delete()
      {
        delete[] dat;
        delete[] jg;
      }
      // 赋值. 参数: 行, 列, 数据
      void Let( int x, int y, double s )
      {
        dat[x].sj[y] = s;
      }
      // 赋值: 参数: 行, 行数据数组(自变量1, 自变量2, ..., 因变量)
      void Let( int, double* );
      // 返回计算结果. 参数: 自变量系数下标(0 为常数项, 其余为自变量系数)
      double Value( int x )
      {
        return jg[x];
      }
      ~Dyhg()
      {
        Delete();
      }};#endif// dyhg.cpp#include "dyhg.hpp"
    #include <iomanip.h>Dyhg::Dyhg( int m, int n )
    {
      hcount = m;
      wcount = n;
      dat = new DatArray[m];
      jg = new double[n];
      for( int i = 0; i < hcount; i ++ )
        dat[i].Init( n );
    }void Dyhg::Let( int x, double *s )
    {
      for( int i = 0; i < wcount; i ++ )
        Let( x, i, s[i] );
    }double Dyhg::SumPf( int x, int y )
    {
      double a = 0.0;
      for( int i = 0; i < hcount; i ++ )
        a += ( dat[i].sj[x] * dat[i].sj[y] );
      return a;
    }void Dyhg::Sum()
    {
      int i, j, n = wcount - 1;
      double a, b;
      FcArray fc( wcount );
      fc.Let( 0, 0, hcount );
      for( i = 0; i < n; i ++ )
      {
        a = b = 0.0;
        for( j = 0; j < hcount; j ++ )
        {
          a += dat[j].sj[i];
          b += ( dat[j].sj[i] * dat[j].sj[i] );
        }
        fc.Let( 0, i + 1, a );
        fc.Let( i + 1, 0, a );
        fc.Let( i + 1, i + 1, b );
        for( j = i + 1; j < n; j ++ )
        {
          a = SumPf( i, j );
          fc.Let( i + 1, j + 1, a );
          fc.Let( j + 1, i + 1, a );
        }
      }
      a = 0.0;
      for( i = 0; i < hcount; i ++ )
        a += dat[i].sj[n];
      fc.Let( 0, wcount, a );
      for( i = 0; i < n; i ++ )
      {
        a = 0.0;
        for( j = 0; j < hcount; j ++ )
          a += ( dat[j].sj[i] * dat[j].sj[n] );
        fc.Let( i + 1, wcount, a );
      }
    //  fc.Print(cout);
      fc.Sum();
      for( i = 0; i < wcount; i ++ )
        jg[i] = fc.Value( i );
    }void Dyhg::Print( ostream &f )
    {
      f << "回归方程式:\n";
      f << " Y = " << jg[0];
      for( int i = 1; i < wcount; i ++ )
        f << " + " << jg[i] << 'X' << i;
      f << endl;
    }#include "dyhg.hpp"double ss[15][5] = {
      { 316, 1536, 874, 981, 3894 },
      { 385, 1771, 777, 1386, 4628 },
      { 299, 1565, 678, 1672, 4569 },
      { 326, 1970, 785, 1864, 5340 },
      { 441, 1890, 785, 2143, 5449 },
      { 460, 2050, 709, 2176, 5599 },
      { 470, 1873, 673, 1769, 5010 },
      { 504, 1955, 793, 2207, 5694 },
      { 348, 2016, 968, 2251, 5792 },
      { 400, 2199, 944, 2390, 6126 },
      { 496, 1328, 749, 2287, 5025 },
      { 497, 1920, 952, 2388, 5924 },
      { 533, 1400, 1452, 2093, 5657 },
      { 506, 1612, 1587, 2083, 6019 },
      { 458, 1613, 1485, 2390, 6141 },
    };void main()
    {
      Dyhg fc( 15, 5 );
      for( int i = 0; i < 15; i ++ )
        fc.Let( i, ss[i] );
      fc.Sum();
      fc.Print( cout );
    }