请使用C++实现矩阵的各种运算
1、矩阵创建
2、矩阵相加
3、矩阵相减
4、矩阵相乘
5、数字乘矩阵
6、矩阵上叠加
7、矩阵左右叠加
8、矩阵转置
9、矩阵旋转
10、矩阵求逆
11、矩阵输出
using namespace std;
int line; int column;
int line2; int column2;
int matrix2[100][100];
int matrix[100][100];
int result[100][100];
void Creat(int x) {
int line; int column;//行与列
cout << "请输入行与列";
cout << "行:";
cin >> line;
cout << "列:";
cin >> column;
int k = 0;
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
k++;
cout << k << " ";
}
cout << endl;
}
}
void add(int x) {//矩阵相加
cout << "请输入第一个矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
int matrix[100][100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin>>matrix[i][j];
}
cout << endl;
}
cout << "请输入第二个矩阵的行与列";
cout << "行:";
cin >> line2;
cout << "列:";
cin >> column2;
int matrix2[100][100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix2[i][j] = 0;
}
}
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << "请输入数字:"<<i+1<<"行"<<j+1<<"列";
cin >> matrix2[i][j];
}
cout << endl;
}
if (line == line2 && column == column2) {
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout<< matrix[i][j] + matrix2[i][j];
}
cout << endl;
}
}
else
{
cout << "无法进行运算,因为两个矩阵的行与列不相同";
}
}
void subtract(int x) {
cout << "请输入第一个矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
cout << "请输入第二个矩阵的行与列";
cout << "行:";
cin >> line2;
cout << "列:";
cin >> column2;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix2[i][j] = 0;
}
}
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix2[i][j];
}
cout << endl;
}
if (line == line2 && column == column2) {
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << matrix[i][j] - matrix2[i][j];
}
cout << endl;
}
}
else
{
cout << "无法进行运算,因为两个矩阵的行与列不相同";
}
}
void multiply(int x) {
cout << "请输入第一个矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
result[i][j]=0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
cout << "请输入第二个矩阵的行与列";
cout << "行:";
cin >> line2;
cout << "列:";
cin >> column2;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix2[i][j] = 0;
}
}
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix2[i][j];
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
for (int k = 0; k < line; k++) {
result[i][j]+= matrix[i][k]* matrix2[k][j];
}
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
cout << result[i][j]<<" ";
}cout << endl;
}
}
void scalar_multiply(int x) {
cout << "请输入矩阵的行与列";
cout << "行数(100):";
cin >> line;
cout << "列数(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
cout << "请输入数字";
int num;
cin >> num;
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << matrix[i][j] * num<<" ";
}cout << endl;
}
}
void superpositiontop(int x) {
cout << "请输入第一个矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
cout << "请输入第二个矩阵的行与列";
cout << "行:";
cin >> line2;
cout << "列:";
cin >> column2;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix2[i][j] = 0;
}
}
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix2[i][j];
}
}
if (line != line2) {
cout << "无法叠加";
}
else {
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
cout << matrix[i][j] << " ";
}cout << endl;
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
cout << matrix2[i][j] << " ";
}cout << endl;
}
}
}
void superpositionlef(int x) {
cout << "请输入第一个矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
cout << "请输入第二个矩阵的行与列";
cout << "行:";
cin >> line2;
cout << "列:";
cin >> column2;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix2[i][j] = 0;
}
}
for (int i = 0; i < line2; i++) {
for (int j = 0; j < column2; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix2[i][j];
}
}
if (column != column2) {
cout << "无法叠加";
}
else {
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
cout << matrix[i][j] << " ";
}cout << endl;
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column2; j++) {
cout << matrix2[i][j] << " ";
}cout << endl;
}
}
}
void transposition(int x) {
cout << "请输入矩阵的行与列";
cout << "行(100):";
cin >> line;
cout << "列(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << matrix[j][i] << " ";
}cout << endl;
}
}
void rotation(int x) {
cout << "请输入矩阵的行与列";
cout << "行数(100):";
cin >> line;
cout << "列数(100):";
cin >> column;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
cin >> matrix[i][j];
}
cout << endl;
}
for (int i = 0; i < line;i++ ) {
int k = line - 1;
for (int j = 0; j < column; j++) {
result[i][j] = matrix[k][i];
k--;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << result[i][j] << " ";
}cout << endl;
}
}
void change(int x){}
int main()
{
int x;
cout << "1.创建矩阵" << endl << "2.矩阵相加" << endl << "3.矩阵相减" << endl << "4.矩阵相乘" << endl << "5.数字乘矩阵" << endl
<< "6.矩阵上叠加" << endl << "7.矩阵左右叠加" << endl << "8.矩阵转置" << endl << "9.矩阵旋转" << endl << "10.矩阵求逆" << endl << "11.矩阵输出";
if (x == 1)
{
Creat(1);
}if (x == 2) {
add(2);
}if (x == 3)
{
subtract(3);
}if (x == 4)
{
multiply(4);
}if (x == 5)
{
scalar_multiply(5);
}if (x == 6) {
superpositiontop(6);
}if (x == 7) {
superpositionlef(7);
}if (x == 8) {
transposition(8);
}if (x == 9) {
rotation(9);
}if (x == 10) {
change(10);
}
if (x == 11) {
}
}
标签:cout,int,矩阵,column,++,100,line
From: https://www.cnblogs.com/yiyulhb/p/17595293.html