解释以下程序 [英] explain the following program

查看:69
本文介绍了解释以下程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream.h  >  
#include < conio.h >
#defined s 20
class matrix
{
int a [s] [s]时,X,Y;
public
void get();
void put();
矩阵运算符+(矩阵);
矩阵运算符 - (矩阵);
矩阵运算符 *(矩阵);
matrix transpose();
};
void matrix :: get()
{
cout<< 输入矩阵的顺序\ n;
cin>> x>> y;
cout<< 输入矩阵\ n;
for int i = 0 ; I< X;我++)>
for int j = 0 ; J< Y; ​​J ++)GT;
cin>> a [i] [j];
}
void matrix :: put()
{
cout<< ans是:\ n;
for int i = 0 ; I< X;我++)>
{
cout<< \ n \t;
for int j = 0 ; J< Y; ​​J ++)GT;
cout<< a [i] [j]<< ;
}
}
矩阵矩阵::运算符+(矩阵b)
{
矩阵r;
if ((x!= bx)||(y!= by))
{
cout<< \ n \t矩阵添加不可能,结果不正确\ n \ n;
r.x = 0 ;
r.y = 0 ;
}
else
{
r.x = x;
r.y = y;
}
for int i = 0 ; I< X;我++)>
for int j = 0 ; J< Y; ​​J ++)GT;
r.a [i] [j] = a [i] [j] + b.a [i] [j];
return r;
}
矩阵矩阵:: 运算符 - (矩阵b)
{
矩阵r;
if ((x!= bx)||(y!= by))
{
cout<< \ n \t矩阵减法是不可能的,结果不正确\ n \ n;
r.x = 0 ;
r.y = 0 ;
}
else
{
r.x = x;
r.y = y;
}
for int i = 0 ; I< X;我++)>
for int j = 0 ; J< Y; ​​J ++)GT;
r.a [i] [j] = a [i] [j] -b.a [i] [j];
return r;
}
矩阵矩阵:: 运算符 *(矩阵b)
{
矩阵r;
if ((x!= by)||(y!= bx))
{
cout<< \ n \t矩阵乘法不可能,结果不正确\ n \ n;
r.x = 0 ;
r.y = 0 ;
}
else
{
r.x = x;
r.y = b.y;
}
for int i = 0 ; I< S;我++)>
for int j = 0 ; J< S; J ++)GT;
r.a [i] [j] = 0 ;
for (i = 0 ; i< x; i ++)>
for (j = 0 ; j< b.y; j ++)>
for int k = 0 ;(K< Y)||(K>< BX); K ++)>
r.a [i] [j] + = a [i] [k] * b.a [k] [j];
return r;
}
矩阵矩阵:: transpose()
{
矩阵r;
for int i = 0 ; I< X;我++)>
for int j = 0 ; J< Y; ​​J ++)GT;
r.a [i] [j] = a [j] [i];
r.x = x;
r.y = y;
return r;
}
void main()
{
clrscr();
char op;
矩阵a,b,c;
int t = 1 ;
while (t)
{
cout<< \t选择选项\\\
\ n 1)矩阵加法\ n 2)矩阵减法\ n 3)矩阵乘法\ n 4)矩阵转置\ n 5 )退出\ n
;
op = getch();
switch (op)
{
case ' 1'
cout<< \ n \t矩阵添加\ n;
a.get();
b.get();
c = a + b;
c.put();
break ;
case ' 2'
cout<< \ n \t矩阵减法\ n ;
a.get();
b.get();
c = a-b;
c.put();
break ;
case ' 3'
cout<< \ n \t矩阵乘法\ n ;
a.get();
b.get();
c = a * b;
c.put();
break ;
case ' 4'
cout<< \ n \t matrix transpose \ n ;
a.get();
c = a.transpose();
c.put();
break ;
case ' 5'
cout<< \ n \t按任意键退出\ n< /跨度>;
t = 0 ;
break ;
默认
cout<< \ n \t输入有效选项\ n;
}
getch();
}
getch();
}

解决方案

看起来不是那么强大(有点丑陋)的程序,用于计算矩阵的各种操作。

#include<iostream.h>
#include<conio.h>
#define s 20
class matrix
{
int a[s][s],x,y;
public:
void get();
void put();
matrix operator+(matrix);
matrix operator-(matrix);
matrix operator*(matrix);
matrix transpose();
};
void matrix::get()
{
cout<<"enter the order of matrix \n";
cin>>x>>y;
cout<<"enter the matrix \n";
for(int i=0;i<x;i++)>
for(int j=0;j<y;j++)>
cin>>a[i][j];
}
void matrix::put()
{
cout<<"the ans is: \n";
for(int i=0;i<x;i++)>
{
cout<<"\n \t";
for(int j=0;j<y;j++)>
cout<<a[i][j]<<"   ";
}
}
matrix matrix::operator+(matrix b)
{
matrix r;
if((x!=b.x)||(y!=b.y))
{
cout<<"\n \t matrix addition is not possible the result is incorrect \n\n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=y;
}
for(int i=0;i<x;i++)>
for(int j=0;j<y;j++)>
r.a[i][j]=a[i][j]+b.a[i][j];
return r;
}
matrix matrix::operator-(matrix b)
{
matrix r;
if((x!=b.x)||(y!=b.y))
{
cout<<"\n \t matrix subtraction is not possible the result is incorrect \n \n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=y;
}
for(int i=0;i<x;i++)>
for(int j=0;j<y;j++)>
r.a[i][j]=a[i][j]-b.a[i][j];
return r;
}
matrix matrix::operator*(matrix b)
{
matrix r;
if((x!=b.y)||(y!=b.x))
{
cout<<"\n \t matrix multiplication is not possible the result is incorrect \n\n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=b.y;
}
for(int i=0;i<s;i++)>
for(int j=0;j<s;j++)>
r.a[i][j]=0;
for(i=0;i<x;i++)>
for(j=0;j<b.y;j++)>
for(int k=0;(k<y)||(k><b.x);k++)>
r.a[i][j]+=a[i][k]*b.a[k][j];
return r;
}
matrix matrix::transpose()
{
matrix r;
for(int i=0;i<x;i++)>
for(int j=0;j<y;j++)>
r.a[i][j]=a[j][i];
r.x=x;
r.y=y;
return r;
}
void main()
{
clrscr();
char op;
matrix a,b,c;
int t=1;
while(t)
{
cout<<"\t select option \n \n 1)matrix addition\n 2)matrix subtraction \n 3)matrix multiplication \n 4)matrix transpose \n 5)exit \n";
op=getch();
switch(op)
{
case '1':
cout<<"\n \t matrix addition \n";
a.get();
b.get();
c=a+b;
c.put();
break;
case '2':
cout<<"\n \t matrix subtraction \n";
a.get();
b.get();
c=a-b;
c.put();
break;
case'3':
cout<<"\n \t matrix multiplication \n";
a.get();
b.get();
c=a*b;
c.put();
break;
case'4':
cout<<"\n \t matrix transpose \n";
a.get();
c=a.transpose();
c.put();
break;
case'5':
cout<<"\n \t press any key to exit \n";
t=0;
break;
default:
cout<<"\n \t enter a valid option \n";
}
getch();
}
getch();
}

解决方案

Looks a not so robust (and bit ugly) program for computing various operations with matrices.


这篇关于解释以下程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆