函数定义问题

lihorse_1043 2008-09-13 07:23:55
#include <iostream>
#include <fstream>
using namespace std;
class SMatrix {
public:
SMatrix(char *filename);
~SMatrix();
void printSMatrix()const;
void printList()const;
void insert(int x, int y, int v);
friend SMatrix add(SMatrix x, SMatrix y);//我想做两个矩阵相加的函数,不知道如何定义,请教一下各位。如果能给出代码更好了,呵呵,谢了。
private:
struct Node {
int row, col, value;
Node *next;
};
int size;
Node *listPtr;
};
...全文
78 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihorse_1043 2008-09-13
  • 打赏
  • 举报
回复
学习中,谢了。
wangdeqie 2008-09-13
  • 打赏
  • 举报
回复

//这是偶以前写过的,功能应该类似,楼主可以参考下^_^
//---xcept.h---
class NoMem
{
public:
NoMem(){}
};
void my_new_handler()
{
throw NoMem();
}
class OutOfBounds
{
public:
OutOfBounds(){}
};
class BadInitializers
{
public:
BadInitializers(){}
};
class SizeMismatch
{
public:
SizeMismatch(){}
};

//---main.cpp---
#include<iostream>
#include"xcept.h"
using namespace std;
template<class T>
class SparseMatrix;
template<class T>
class Term
{
friend SparseMatrix<T>;
private:
int row,col;
T value;
};
template<class T>
class SparseMatrix
{

friend ostream& operator<<(ostream& out,const SparseMatrix<T>& x)
{
out<<"rows= "<<x.rows<<"columns= "<<x.cols<<endl;
out<<"nozero terms= "<<x.terms<<endl;
for(int i=0;i<x.terms;i++)
out<<"a("<<x.a[i].row<<','<<x.a[i].col<<")="<<x.a[i].value<<endl;
return out;
}

friend istream& operator>>(istream& in,SparseMatrix<T>& x)
{
cout<<"Enter number of rows,columns, and terms "<<endl;
in>>x.rows>>x.cols>>x.terms;
if(x.terms>x.MaxTerms)
throw NoMem();
for(int i=0;i<x.terms;i++)
{
cout<<"Enter row, colum, and value of term "<<(i+1)<<endl;
in>>x.a[i].row>>x.a[i].col>>x.a[i].value;
}
return in;
}

public:
SparseMatrix(int maxTerms=10);
~SparseMatrix(){delete [] a;}
void Transpose(SparseMatrix<T>& b)const;
void Add(const SparseMatrix<T>& b,SparseMatrix<T>& c)const;
void Output(ostream& out)const;
void Input(istream& in);
private:
void Append(const Term<T>& t);
int rows,cols;
int terms;
Term<T>* a;
int MaxTerms;
};
template<class T>
SparseMatrix<T>::SparseMatrix(int maxTerms)
{
if(maxTerms<1)
throw BadInitializers();
MaxTerms=maxTerms;
a=new Term<T>[MaxTerms];
terms=rows=cols=0;
}
template<class T>
void SparseMatrix<T>::Append(const Term<T>& t)
{
if(terms>=MaxTerms)
throw NoMem();
a[terms]=t;
terms++;
}
template<class T>
void SparseMatrix<T>::Add(const SparseMatrix<T>& b,SparseMatrix<T>& c)const
{
if(rows!=b.rows||cols!=b.cols)
throw SizeMismatch();
c.rows=rows;
c.cols=cols;
c.terms=0;
int ct=0,cb=0;
while(ct<terms && cb<b.terms)
{
int indt=a[ct].row*cols+a[ct].col;
int indb=b.a[cb].row*cols+b.a[cb].col;
if(indt<indb)
{
c.Append(a[ct]);
ct++;
}
else
{
if(indt==indb)
{
if(a[ct].value+b.a[cb].value)
{
Term<T> t;
t.row=a[ct].row;
t.col=a[ct].col;
t.value=a[ct].value+b.a[cb].value;
c.Append(t);
}
ct++;
cb++;
}
else
{
c.Append(b.a[cb]);
cb++;
}
}
}
for(;ct<terms;ct++)
c.Append(a[ct]);
for(;cb<b.terms;cb++)
c.Append(b.a[cb]);
}
template<class T>
void SparseMatrix<T>::Output(ostream& out)const
{
out<<"rows= "<<rows<<" "<<"columns= "<<cols<<endl;
out<<"nozero terms= "<<terms<<endl;
for(int i=0;i<terms;i++)
out<<"a("<<a[i].row<<','<<a[i].col<<")="<<a[i].value<<endl;
}
template<class T>
ostream& operator<<(ostream& out,const SparseMatrix<T>& x)
{
x.Output(out);
return out;
}

template<class T>
void SparseMatrix<T>::Input(istream& in)
{
cout<<"Enter number of rows,columns, and terms: "<<endl;
in>>rows>>cols>>terms;
if(terms>MaxTerms)
throw NoMem();
for(int i=0;i<terms;i++)
{
cout<<"Enter row, colum, and value of term: "<<(i+1)<<endl;
in>>a[i].row>>a[i].col>>a[i].value;
}
}
template<class T>
istream& operator>>(istream& in,SparseMatrix<T>& x)
{
x.Input(in);
return in;
}

void main()
{

SparseMatrix<int> a(4);
SparseMatrix<int> b(4);
SparseMatrix<int> c(4);
cin>>a;
cout<<a<<endl;
cin>>b;
cout<<b<<endl;
a.Add(b,c);
cout<<c<<endl;

}
baihacker 2008-09-13
  • 打赏
  • 举报
回复
楼主可以去参考blitz(有点难)

如果只是与简单的矩阵应用 看了CwMtx以后,你的水平会有所长进的。
wangdeqie 2008-09-13
  • 打赏
  • 举报
回复
1.写在类里面用一元(另一个是隐含的this对象)
2.写在类外面用二元
wangdeqie 2008-09-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;
class SMatrix
{
public:
SMatrix(char *filename);
~SMatrix();
void printSMatrix()const;
void printList()const;
void insert(int x, int y, int v);
SMatrix add(const SMatrix &y);//可以这么写
private:
struct Node
{
int row, col, value;
Node *next;
};
int size;
Node *listPtr;
};

void main()
{

}
  • 打赏
  • 举报
回复
friend SMatrix add(const SMatrix& x,const SMatrix& y);//
这样定义就好

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧