#include<iostream>
#include<iomanip>
using namespace std;
#include "p2.h"
void OperatorMenu()
{
system("title Matrix calculator");
system("color 1b");
cout<<" Operator Menu"<<endl<<endl;
cout<<" +. Add two Matrix"<<endl;
cout<<" -. One Matrix Minus another"<<endl;
cout<<" *. One Matrix Multiply another"<<endl;
cout<<" &. One number Multiply an Matrix"<<endl;
cout<<" e. Exit the program!"<<endl<<endl;
}
int main()
{
OperatorMenu();
int r, c, c1;
float num;
Matrix A, B, C;
char s;
while(1)
{cout<<"Input the operator:";
cin>>s;
switch(s)
{case '+':
cout<<"Input the row and column of the Matrix A:";
cin>>r>>c;
A.RowChange(r);
A.ColumnChange(c);
B.RowChange(r);
B.ColumnChange(c);
C.RowChange(r);
C.ColumnChange(c);
cout<<"Input the data of Matrix A:"<<endl;
cin>>A;
cout<<"Input the data of Matrix B:"<<endl;
cin>>B;
cout<<"A:"<<endl<<A;
cout<<"B:"<<endl<<B;
C=A+B;
cout <<"A+B:"<<endl<<C;
break;
case '-':
cout<<"Input the row and column of the Matrix A:";
cin>>r>>c;
A.RowChange(r);
A.ColumnChange(c);
B.RowChange(r);
B.ColumnChange(c);
C.RowChange(r);
C.ColumnChange(c);
cout<<"Input the data of Matrix A:"<<endl;
cin>>A;
cout<<"Input the data of Matrix B:"<<endl;
cin>>B;
cout<<"A:"<<endl<<A;
cout<<"B:"<<endl<<B;
C=A-B;
cout <<"A-B:"<<endl<<C;
break;
case '*':
cout<<"Input the row and column of the Matrix A and the column of Matrix B:";
cin>>r>>c>>c1;
A.RowChange(r);
A.ColumnChange(c);
B.RowChange(c);
B.ColumnChange(c1);
C.RowChange(r);
C.ColumnChange(c1);
cout<<"Input the data of Matrix A:"<<endl;
cin>>A;
cout<<"Input the data of Matrix B:"<<endl;
cin>>B;
cout<<"A:"<<endl<<A;
cout<<"B:"<<endl<<B;
C=A*B;
cout <<"A*B:"<<endl<<C;
break;
case '&':
cout<<"Input the row and column of the Matrix A:";
cin>>r>>c;
A.RowChange(r);
A.ColumnChange(c);
C.RowChange(r);
C.ColumnChange(c);
cout<<"Input the data of Matrix A:"<<endl;
cin>>A;
cout<<"Input the num:";
cin>>num;
C=num*A;
cout<<"A:"<<endl<<A<<endl<<num<<"*A:"<<endl<<C;
break;
case 'e':
cout<<" 谢谢使用!";
exit(0);
break;
default: cout<<"Unrecognize operator, Input again!"<<endl;
}
}
return 0;
}
Matrix::Matrix(int r, int c)
{row=r;
column=c;
}
void Matrix::RowChange(int r)
{row=r;
}
void Matrix::ColumnChange(int c)
{column=c;
}
istream& operator>>(istream& input, Matrix &m1)/*由于我们的类中定义了CArray类型的成员,
而CArray<A, A&>类的operator=是private类型(它继承自CObject::operator=,且被定义为private类型)。
并给出了解决之道:如果我们的类/结构体中有CArray(或CList等其他的派生自CObject类)的成员变量,
我们最好添加上一个public类型的operator=运算赋重载函数;*/
{for(int i=0; i<m1.row; i++)
{for(int j=0; j<m1.column; j++)
{input>>m1.Array[i][j];
}
}
return input;
}
ostream& operator<<(ostream& output, Matrix &m1)
{for(int i=0; i<m1.row; i++)
{for(int j=0; j<m1.column; j++)
{output<<setw(5)<<m1.Array[i][j]<<' ';
}
output<<endl;
}
return output;
}
Matrix operator*(float n, Matrix m1)
{for(int i=0; i<m1.row; i++)
{for(int j=0; j<m1.column; j++)
{m1.Array[i][j]*=n;
}
}
return m1;
}
Matrix Matrix::operator+(Matrix m1)
{for(int i=0; i<m1.row; i++)
{for(int j=0; j<m1.column; j++)
{m1.Array[i][j]+=Array[i][j];
}
}
return m1;
}
Matrix Matrix::operator-(Matrix m1)
{for(int i=0; i<m1.row; i++)
{for(int j=0; j<m1.column; j++)
{m1.Array[i][j]=Array[i][j]-m1.Array[i][j];
}
}
return m1;
}
Matrix Matrix::operator*(Matrix m1)
{Matrix m2(row, m1.column);
for(int i=0; i<row; i++)
for(int j=0; j<m1.column; j++)
{m2.Array[i][j]=0;
for(int k=0; k<column; k++)
{m2.Array[i][j]+=Array[i][k]*m1.Array[k][j];
}
}
return m2;
}
/*void OperatorMenu()
{system("title Matrix calculator");
system("color 1b");
cout<<" Operator Menu"<<endl<<endl;
cout<<" +. Add two Matrix"<<endl;
cout<<" -. One Matrix Minus another"<<endl;
cout<<" *. One Matrix Multiply another"<<endl;
cout<<" &. One number Multiply an Matrix"<<endl;
cout<<" e. Exit the program!"<<endl<<endl;
}*/
hht.zip_HHT c++_HHT程序_hht
版权申诉
110 浏览量
2022-09-22
21:23:27
上传
评论
收藏 1KB ZIP 举报
JaniceLu
- 粉丝: 85
- 资源: 1万+
最新资源
- CLShanYanSDKDataList.sqlite
- android应用商店
- btstack协议栈实战篇-HFP HF - Hands-Free
- MSFRosslerAll (2).for
- vs2022安装包在线安装包
- 项目实战:基于ASP.NET技术的网上图书订阅管理系统的设计与开发(程序源代码+毕业文档)
- Microsoft C++ 生成工具
- 基于SpringMVC+Bootstrap在线图书销售系统(PC端+server端源码+数据库SQL+演示讲解+毕业论文)
- 74LS190实现10以内、100以内十进制加/减计数器-由multisim软件电路仿真设计
- 毕业项目:网上图书订阅管理系统的设计与开发(ASP.NET技术+程序源代码+毕业文档)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈