C++中如何重载输入输出流运算符使其可用于矩阵的输入输出?
代码如下,怎么改:classMatrix{public:Matrix();friendistream&operator>>(istream&,Matrix&);frien...
代码如下,怎么改:
class Matrix
{
public:
Matrix();
friend istream& operator >> (istream&,Matrix&);
friend ostream& operator << (ostream&,Matrix&);
friend Matrix operator + (Matrix &a,Matrix &b);
private:
int term[2][3];
};
Matrix::Matrix()
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
term[i][j]=0;
}
}
}
Matrix operator + (Matrix &a,Matrix &b)
{
int i,j;
Matrix c;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
c.term[i][j]=a.term[i][j]+b.term[i][j];
}
}
return c;
}
istream & operator >> (istream & input,Matrix & a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
input>>term[i][j];
}
}
return input;
}
ostream & operator << (istream& output,Matrix& a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
output<<term[i][j];
}
}
return output;
}
int main()
{
Matrix a,b,c;
cout<<"a-->Input:"<<endl;
cin>>a;
cout<<"a="<<a<<endl;
cout<<"b-->Input:"<<endl;
cin>>b;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
c = a + b;
cout<<"c=a+b="<<c<<endl;
} 展开
class Matrix
{
public:
Matrix();
friend istream& operator >> (istream&,Matrix&);
friend ostream& operator << (ostream&,Matrix&);
friend Matrix operator + (Matrix &a,Matrix &b);
private:
int term[2][3];
};
Matrix::Matrix()
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
term[i][j]=0;
}
}
}
Matrix operator + (Matrix &a,Matrix &b)
{
int i,j;
Matrix c;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
c.term[i][j]=a.term[i][j]+b.term[i][j];
}
}
return c;
}
istream & operator >> (istream & input,Matrix & a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
input>>term[i][j];
}
}
return input;
}
ostream & operator << (istream& output,Matrix& a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
output<<term[i][j];
}
}
return output;
}
int main()
{
Matrix a,b,c;
cout<<"a-->Input:"<<endl;
cin>>a;
cout<<"a="<<a<<endl;
cout<<"b-->Input:"<<endl;
cin>>b;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
c = a + b;
cout<<"c=a+b="<<c<<endl;
} 展开
2个回答
展开全部
可以将矩阵封装成一个类,然后重载该类的输入输出函数。
参考代码如下:
class matrix
{
public:
int ** p;
int x,y;
matrix(int m, int n)
{
x = m;
y = n;
p = new int*[x];
for(int i = 0; i < x; i ++)
p[i] = new int[y];
}
};//定义类。
istream & operaroy >> (istream & in, matrix & v)//重载输入>>。
{
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
in>>v.p[i][j];
return in;
}
ostream &operator<<(ostream & out, const matrix &v)//重载输出<<。
{
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
out<<v.p[i][j]<< ',';
out << endl;
}
return out;
}
展开全部
#include<iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend istream& operator >> (istream&,Matrix&);
friend ostream& operator << (ostream&,Matrix&);
friend Matrix operator + (Matrix &a,Matrix &b);
private:
int term[2][3];
};
Matrix::Matrix()
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
term[i][j]=0;
}
}
}
Matrix operator + (Matrix &a,Matrix &b)
{
int i,j;
Matrix c;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
c.term[i][j]=a.term[i][j]+b.term[i][j];
}
}
return c;
}
istream & operator >> (istream & input,Matrix & a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
input>>a.term[i][j];
}
}
return input;
}
ostream & operator << (ostream& output,Matrix & a)
{
int i,j;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
output<<a.term[i][j];
}
}
return output;
}
int main()
{
Matrix a,b,c;
cout<<"a-->Input:"<<endl;
cin>>a;
cout<<"a="<<a<<endl;
cout<<"b-->Input:"<<endl;
cin>>b;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
c = a + b;
cout<<"c=a+b="<<c<<endl;
}
更多追问追答
追问
用VC++6.0编译还是出错,提示为:fatal error C1001: INTERNAL COMPILER ERROR
追答
这个不是代码问题报错。
而是你工程类型和设置决定了,必须使用“预编译头文件“
如果其它设置没错,那么在你的代码最上边一行加上:
#include "stdafx.h"
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询