【课程设计|C++】C++程序设计基础:矩阵运算器
海轰Pro
共 21478字,需浏览 43分钟
·
2021-07-12 16:02
课程设计要求
设计一个程序实现两个矩阵的基本运算
两个矩阵相加 两个矩阵相减 两个矩阵相乘 求一个矩阵的转置矩阵
难度 🌟
运行界面
运行环境:Visual Studio Code
1.矩阵a、b初始化
// 矩阵a,b
vector<vector<int> > a;
vector<vector<int> > b;
// 输入矩阵
vector<vector<int> > input(string name)
{
vector<vector<int> > ans;
int m = 0;
int n = 0;
cout << "请输入矩阵" << name << "的行、列数(以空格间隔 例如:2 2):";
cin >> m >> n;
for (int i = 0; i < m; ++i)
{
cout << "请输入第" << (i + 1) << "行元素(各元素之间以空格隔开)" << endl;
vector<int> temp;
for (int j = 0; j < n; ++j)
{
int num;
cin >> num;
temp.push_back(num);
}
ans.push_back(temp);
}
return ans;
}
// 初始化 矩阵a b
void initial()
{
a.clear();
b.clear();
a = input("a");
b = input("b");
}
效果图如下
2.菜单显示
// 菜单显示
void menu()
{
cout << "**************************" << endl;
cout << " 菜单 " << endl;
cout << " 1. 矩阵加法 " << endl;
cout << " 2. 矩阵减法 " << endl;
cout << " 3. 矩阵乘法 " << endl;
cout << " 4. 矩阵转置 " << endl;
cout << " 5. 清空矩阵 " << endl;
cout << " 6. 退出程序 " << endl;
cout << "**************************" << endl;
cout << "请输入您的选择:【例如选择1.矩阵加法 请输入:1】" << endl;
}
效果图如下
3.矩阵加法
// add() : 矩阵加法
void add()
{
// 判断是否可以相加
if (a.size() == b.size() && a.back().size() == b.back().size())
{
vector<vector<int> > sum(a.size(), vector<int>(a[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < a[0].size(); ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
cout << "矩阵a,b 相加的结果如下:" << endl;
for (int i = 0; i < sum.size(); ++i)
{
for (int j = 0; j < sum[0].size(); ++j)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b 不可以进行相加!" << endl;
}
}
效果图如下
4.矩阵减法
// sub() 矩阵减法
void sub()
{
// 判断是否可以相减
if (a.size() == b.size() && a.back().size() == b.back().size())
{
vector<vector<int> > sum(a.size(), vector<int>(a[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < a[0].size(); ++j)
{
sum[i][j] = a[i][j] - b[i][j];
}
}
cout << "矩阵a,b 相减的结果如下:" << endl;
for (int i = 0; i < sum.size(); ++i)
{
for (int j = 0; j < sum[0].size(); ++j)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b 不可以进行相减!" << endl;
}
}
效果图如下
5.矩阵乘法
// mul() 矩阵乘法
void mul()
{
// 判断是否可以相乘 (m✖️n)* (n✖️k)
if (a[0].size() == b.size())
{
vector<vector<int> > res(a.size(), vector<int>(b[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < b[0].size(); ++j)
{
int sum = 0;
for (int k = 0; k < b.size(); ++k)
{
sum += (a[i][k] * b[k][j]);
}
res[i][j] = sum;
}
}
cout << "矩阵a,b 相乘的结果如下:" << endl;
for (int i = 0; i < res.size(); ++i)
{
for (int j = 0; j < res[0].size(); ++j)
{
cout << res[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b不可以进行相乘!" << endl;
}
}
效果图如下
6.矩阵转置
// 转置
void transpose()
{
cout << "转置矩阵 选择:1.a矩阵 2.b矩阵 " << endl;
cout << "请输入你的选择:";
int choice;
cin >> choice;
vector<vector<int> > temp;
switch (choice)
{
case 1:
temp = a;
break;
case 2:
temp = b;
break;
default:
cout << "输入错误!" << endl;
break;
}
vector<vector<int> > res(temp[0].size(), vector<int>(temp.size(), 0));
for (int i = 0; i < temp.size(); ++i)
{
for (int j = 0; j < temp[0].size(); ++j)
{
res[j][i] = temp[i][j];
}
}
cout << "矩阵转置的结果如下:" << endl;
for (int i = 0; i < res.size(); ++i)
{
for (int j = 0; j < res[0].size(); ++j)
{
cout << res[i][j] << " ";
}
cout << endl;
}
}
效果图如下
7.重置矩阵
// 清空矩阵a b
void clear()
{
a.clear();
b.clear();
cout << "清空矩阵成功!" << endl;
}
效果图如下
源码
/*
write by Haihong
2021 07 06
首发于:公众号【海轰Pro】
*/
#include <iostream>
#include <vector>
using namespace std;
// 矩阵a,b
vector<vector<int> > a;
vector<vector<int> > b;
// 输入矩阵
vector<vector<int> > input(string name)
{
vector<vector<int> > ans;
int m = 0;
int n = 0;
cout << "请输入矩阵" << name << "的行、列数(以空格间隔 例如:2 2):";
cin >> m >> n;
for (int i = 0; i < m; ++i)
{
cout << "请输入第" << (i + 1) << "行元素(各元素之间以空格隔开)" << endl;
vector<int> temp;
for (int j = 0; j < n; ++j)
{
int num;
cin >> num;
temp.push_back(num);
}
ans.push_back(temp);
}
return ans;
}
// 初始化 矩阵a b
void initial()
{
a.clear();
b.clear();
a = input("a");
b = input("b");
}
// add() : 矩阵加法
void add()
{
// 判断是否可以相加
if (a.size() == b.size() && a.back().size() == b.back().size())
{
vector<vector<int> > sum(a.size(), vector<int>(a[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < a[0].size(); ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
cout << "矩阵a,b 相加的结果如下:" << endl;
for (int i = 0; i < sum.size(); ++i)
{
for (int j = 0; j < sum[0].size(); ++j)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b 不可以进行相加!" << endl;
}
}
// sub() 矩阵减法
void sub()
{
// 判断是否可以相减
if (a.size() == b.size() && a.back().size() == b.back().size())
{
vector<vector<int> > sum(a.size(), vector<int>(a[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < a[0].size(); ++j)
{
sum[i][j] = a[i][j] - b[i][j];
}
}
cout << "矩阵a,b 相减的结果如下:" << endl;
for (int i = 0; i < sum.size(); ++i)
{
for (int j = 0; j < sum[0].size(); ++j)
{
cout << sum[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b 不可以进行相减!" << endl;
}
}
// mul() 矩阵乘法
void mul()
{
// 判断是否可以相乘 (m✖️n)* (n✖️k)
if (a[0].size() == b.size())
{
vector<vector<int> > res(a.size(), vector<int>(b[0].size(), 0));
for (int i = 0; i < a.size(); ++i)
{
for (int j = 0; j < b[0].size(); ++j)
{
int sum = 0;
for (int k = 0; k < b.size(); ++k)
{
sum += (a[i][k] * b[k][j]);
}
res[i][j] = sum;
}
}
cout << "矩阵a,b 相乘的结果如下:" << endl;
for (int i = 0; i < res.size(); ++i)
{
for (int j = 0; j < res[0].size(); ++j)
{
cout << res[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "矩阵a,b不可以进行相乘!" << endl;
}
}
// 转置
void transpose()
{
cout << "转置矩阵 选择:1.a矩阵 2.b矩阵 " << endl;
cout << "请输入你的选择:";
int choice;
cin >> choice;
vector<vector<int> > temp;
switch (choice)
{
case 1:
temp = a;
break;
case 2:
temp = b;
break;
default:
cout << "输入错误!" << endl;
break;
}
vector<vector<int> > res(temp[0].size(), vector<int>(temp.size(), 0));
for (int i = 0; i < temp.size(); ++i)
{
for (int j = 0; j < temp[0].size(); ++j)
{
res[j][i] = temp[i][j];
}
}
cout << "矩阵转置的结果如下:" << endl;
for (int i = 0; i < res.size(); ++i)
{
for (int j = 0; j < res[0].size(); ++j)
{
cout << res[i][j] << " ";
}
cout << endl;
}
}
// 清空矩阵a b
void clear()
{
a.clear();
b.clear();
cout << "清空矩阵成功!" << endl;
}
// 菜单显示
void menu()
{
cout << "**************************" << endl;
cout << " 菜单 " << endl;
cout << " 1. 矩阵加法 " << endl;
cout << " 2. 矩阵减法 " << endl;
cout << " 3. 矩阵乘法 " << endl;
cout << " 4. 矩阵转置 " << endl;
cout << " 5. 清空矩阵 " << endl;
cout << " 6. 退出程序 " << endl;
cout << "**************************" << endl;
cout << "请输入您的选择:【例如选择1.矩阵加法 请输入:1】" << endl;
}
// 主函数
int main()
{
int flag = 1;
int choice = 0; // 用户选择
cout << "欢迎使用本系统~" << endl;
// 用户输入矩阵a b
initial();
while (flag == 1)
{
menu();
cin >> choice;
switch (choice)
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
transpose();
break;
case 5:
clear();
cout << "请重新输入矩阵a b" << endl;
initial();
break;
case 6:
flag = 0;
break;
default:
cout << "非法输入!" << endl;
break;
}
}
cout << "感谢您的使用!" << endl;
return 0;
}
结语
希望对您有所帮助
如有错误欢迎小伙伴指正~
我是 海轰ଘ(੭ˊᵕˋ)੭
如果您觉得写得可以的话
请点个赞吧
谢谢支持❤️
评论