找回密码
 注册
Simdroid-非首页
查看: 400|回复: 4

[2. C/C++/C#] 【求助】我是C++新手,帮我看看下面的程序哪儿有问题?

[复制链接]
发表于 2003-11-4 10:26:32 | 显示全部楼层 |阅读模式 来自 北京
#include <stdio.h>
  
const minsize=5;
struct element
{
  short row,col;
  double val;
  element *next;
};
class matrix
{
  short rows,cols;
  element * elems;
  double dummy;
  double & insertelem(element * elem,short row,short col);
public:
  matrix(short rows,short cols);
  ~matrix();
  short rows1(){return rows;}
  short cols1(){return cols;}
  double & operator () (short row,short col);
  friend matrix operator + (matrix &,matrix &);
  friend matrix operator * (matrix &,matrix &);
  void print();
};
matrix::matrix(short rows,short cols)
{
  matrix::rows =(rows<minsize?minsize:rows);
  matrix::cols =(cols<minsize?minsize:cols);
  elems=0;
  dummy=0.0;
}
matrix::~matrix ()
{
  element * elem=elems;
  element * tmp;
  while(elem!=0)
  {
    tmp=elem;
    elem=elem->next ;
    delete tmp;
  }
}
double & matrix::insertelem (element *elem,short row,short col)
{
  element * newelem=new element;
  if (newelem==0)
    return dummy;
  newelem->row =row;
  newelem->col =col;
  newelem->val =0.0;
  if(elem==elems && (elems==0 || row<elems->row || row==elems->row && col<elems->col ))
  {
    newelem->next =elems;
    elems=newelem;
  }
  else
  {
    newelem->next =elem->next;
    elem->next =newelem;
  }
  return newelem->val ;
}
double & matrix:perator () (short row,short col)
{
  if (row<1 || row>rows || col<1 || col>cols)
    return dummy;
  if (elems==0 || row<elems->row || row==elems->row && col<elems->col )
    return insertelem(elems,row,col);
  for (element * elem=elems;elem->next !=0;elem=elem->next )
  {
    if (row==elem->next->row )
    {
      if (col==elem->next ->col )
        return elem->next->val;
      else if (col<elem->next ->col )
        break;
    }
    else if (row<elem->next ->row )break;
  }
  return insertelem(elem,row,col);
}
matrix operator + (matrix & p,matrix & q)
{
  matrix m(p.rows ,q.cols );
  for (element * pe=p.elems ;pe!=0;pe=pe->next )
    m(pe->row  ,pe->col )=pe->val ;
  for (element * qe=q.elems ;qe!=0;qe=qe->next )
    m(qe->row ,qe->col )+=qe->val ;
  return m;
}
matrix operator * (matrix & p,matrix & q)
{
  matrix m(p.rows  ,q.cols );
  for (element * pe=p.elems ;pe!=0;pe=pe->next )
    for (element * qe=q.elems ;qe!=0;qe=qe->next )
      if (pe->col ==qe->row )
        m(pe->row ,qe->col )+=pe->val *qe->val ;
  return m;
}
void matrix::print ()
{
  element * elem =elems;
  for (short row =1;row<=rows;++row)
  {
    for (short col =1;col<=cols;++col)
      if(elem!=0 && elem->row==row && elem->col==col)
      {
        printf("%8.2f",elem->val);
        elem=elem->next;
      }
      else printf("%8.2f",0.0);
    printf("\n");
    }
  printf("\n");
}
  
int main()
{
  matrix ma1(5,5);
  matrix ma2(5,5);
  matrix ma3(5,5);
  ma1(2,2)=1.0;
  ma1(3,3)=2.0;
  ma1(4,4)=3.0;
  ma1(1,1)=4.0;
  ma1(1,5)=5.0;
  ma1(1,3)=6.0;
  ma2(2,2)=10.0;
  ma2(3,3)=20.0;
  ma2(3,5)=30.0;
  ma2(5,2)=40.0;
  ma1.print ();
//  getchar();
  ma2.print ();
//  printf("%8.2f\n",ma1(2,2));
  ma3=ma1+ma2;
//  getchar();
  ma3.print ();
  ma3=ma1*ma2;
//  getchar();
  ma3.print ();
  return 0;
}
  
运行时,错误见附件。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
发表于 2003-11-4 18:10:58 | 显示全部楼层 来自 陕西西安

回复: 【求助】我是C++新手,帮我看看下面的程序哪儿有问题?

Simdroid开发平台
这样可以吗
  
#include <stdio.h>  
  
const minsize=5;  
struct element  
{  
  short row,col;  
  double val;  
  element *next;  
};  
class matrix  
{  
  short rows,cols;  
  element * elems;  
  double dummy;  
  double & insertelem(element * elem,short row,short col);  
public:  
  matrix(short rows,short cols);  
  deletematrix();  
  short rows1(){return rows;}  
  short cols1(){return cols;}  
  double & operator () (short row,short col);  
  friend matrix operator + (matrix &,matrix &);  
  friend matrix operator * (matrix &,matrix &);  
  void print();  
};  
matrix::matrix(short rows,short cols)  
{  
  matrix::rows =(rows<minsize?minsize:rows);  
  matrix::cols =(cols<minsize?minsize:cols);  
  elems=0;  
  dummy=0.0;  
}  
matrix::deletematrix ()  
{  
  element * elem;  
  element * tmp;
  
  elem=elems;
  while(elem)  
  {  
  tmp=elem;  
    elem=elem->next ;
  delete tmp;  
  }  
  
}  
double & matrix::insertelem (element *elem,short row,short col)  
{  
  element * newelem=new element;  
  if (newelem==0)  
    return dummy;  
  newelem->row =row;  
  newelem->col =col;  
  newelem->val =0.0;  
  if(elem==elems && (elems==0 || row<elems->row || row==elems->row && col<elems->col ))  
  {  
    newelem->next =elems;  
    elems=newelem;  
  }  
  else  
  {  
    newelem->next =elem->next;  
    elem->next =newelem;  
  }  
  return newelem->val ;  
}  
double & matrix:perator () (short row,short col)  
{  
  if (row<1 || row>rows || col<1 || col>cols)  
    return dummy;  
  if (elems==0 || row<elems->row || row==elems->row && col<elems->col )  
    return insertelem(elems,row,col);  
  for (element * elem=elems;elem->next !=0;elem=elem->next )  
  {  
    if (row==elem->next->row )  
    {  
      if (col==elem->next ->col )  
        return elem->next->val;  
      else if (col<elem->next ->col )  
        break;  
    }  
    else if (row<elem->next ->row )break;  
  }  
  return insertelem(elem,row,col);  
}  
  
matrix operator + (matrix & p,matrix & q)  
{  
  matrix m(p.rows ,q.cols );  
  for (element * pe=p.elems ;pe!=0;pe=pe->next )  
    m(pe->row ,pe->col )=pe->val ;  
  for (element * qe=q.elems ;qe!=0;qe=qe->next )  
    m(qe->row ,qe->col )+=qe->val ;  
  return m;  
}  
  
matrix operator * (matrix & p,matrix & q)  
{  
  matrix m(p.rows ,q.cols );  
  for (element * pe=p.elems ;pe!=0;pe=pe->next )  
    for (element * qe=q.elems ;qe!=0;qe=qe->next )  
      if (pe->col ==qe->row )  
        m(pe->row ,qe->col )+=pe->val *qe->val ;  
  return m;  
}  
void matrix::print ()  
{  
  element * elem =elems;  
  for (short row =1;row<=rows;++row)  
  {  
    for (short col =1;col<=cols;++col)  
      if(elem!=0 && elem->row==row && elem->col==col)  
      {  
        printf("%8.2f",elem->val);  
        elem=elem->next;  
      }  
      else printf("%8.2f",0.0);  
    printf("\n");  
    }  
  printf("\n");  
}  
  
int main()  
{  
  matrix ma1(5,5);  
  matrix ma2(5,5);  
  matrix ma3(5,5);  
  ma1(2,2)=1.0;  
  ma1(3,3)=2.0;  
  ma1(4,4)=3.0;  
  ma1(1,1)=4.0;  
  ma1(1,5)=5.0;  
  ma1(1,3)=6.0;  
  ma2(2,2)=10.0;  
  ma2(3,3)=20.0;  
  ma2(3,5)=30.0;  
  ma2(5,2)=40.0;  
  ma1.print ();  
//  getchar();  
  ma2.print ();  
//  printf("%8.2f\n",ma1(2,2));  
  
  ma3=ma1+ma2;  
  
//  getchar();  
  ma3.print ();
  
  ma3=ma1*ma2;  
//  getchar();  
  ma3.print ();  
  
  ma1.deletematrix () ;
  ma2.deletematrix () ;
  ma3.deletematrix () ;
  
  return 0;  
}
 楼主| 发表于 2003-11-9 14:18:39 | 显示全部楼层 来自 北京

回复: 【求助】我是C++新手,帮我看看下面的程序哪儿有问题?

谢谢你!coolhand  
可是我不太明白,为什么用析构函数就不行,而改成一个别的成员函数就行了?
这样一来是不是求和与求积的函数里的对象的就没有析构了?
发表于 2003-11-26 17:18:25 | 显示全部楼层 来自 江苏南京

回复: 【求助】我是C++新手,帮我看看下面的程序哪儿有问题?

你应该重载拷贝构造函数,否则编译器会给你生成一个默认构造函数,
它不会复制所有的数据,而是仅仅复制指针
发表于 2006-5-4 22:17:38 | 显示全部楼层 来自 江苏南京

求助:求最短巡视路径的算法

如何用C实现10个点之间的最短巡视路径,要求每个点都经过且仅经过一次。用什么算法啊?
谢谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-11-1 19:28 , Processed in 0.036139 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表