銆愯绋嬭璁★綔C++銆慍++绋嬪簭璁捐鍩虹锛氱煩闃佃繍绠楀櫒
Posted 娴疯桨Pro
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愯绋嬭璁★綔C++銆慍++绋嬪簭璁捐鍩虹锛氱煩闃佃繍绠楀櫒相关的知识,希望对你有一定的参考价值。
璇剧▼璁捐瑕佹眰
璁捐涓€涓▼搴忓疄鐜颁袱涓煩闃电殑鍩烘湰杩愮畻
-
涓や釜鐭╅樀鐩稿姞 -
涓や釜鐭╅樀鐩稿噺 -
涓や釜鐭╅樀鐩镐箻 -
姹備竴涓煩闃电殑杞疆鐭╅樀
闅惧害 馃専
杩愯鐣岄潰
杩愯鐜锛歏isual Studio Code
1.鐭╅樀a銆乥鍒濆鍖?/strong>
// 鐭╅樀a锛宐
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 << "鐨勮銆佸垪鏁帮紙浠ョ┖鏍奸棿闅?nbsp;渚嬪锛? 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;
}
// 鍒濆鍖?nbsp;鐭╅樀a b
void initial()
{
a.clear();
b.clear();
a = input("a");
b = input("b");
}
鏁堟灉鍥惧涓?/p>
2.鑿滃崟鏄剧ず
// 鑿滃崟鏄剧ず
void menu()
{
cout << "**************************" << endl;
cout << " 鑿滃崟 " << endl;
cout << " 1. 鐭╅樀鍔犳硶 " << endl;
cout << " 2. 鐭╅樀鍑忔硶 " << endl;
cout << " 3. 鐭╅樀涔樻硶 " << endl;
cout << " 4. 鐭╅樀杞疆 " << endl;
cout << " 5. 娓呯┖鐭╅樀 " << endl;
cout << " 6. 閫€鍑虹▼搴?nbsp; " << endl;
cout << "**************************" << endl;
cout << "璇疯緭鍏ユ偍鐨勯€夋嫨锛氥€愪緥濡傞€夋嫨1.鐭╅樀鍔犳硶 璇疯緭鍏ワ細1銆? << endl;
}
鏁堟灉鍥惧涓?/p>
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锛宐 鐩稿姞鐨勭粨鏋滃涓嬶細" << 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锛宐 涓嶅彲浠ヨ繘琛岀浉鍔狅紒" << endl;
}
}
鏁堟灉鍥惧涓?/p>
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锛宐 鐩稿噺鐨勭粨鏋滃涓嬶細" << 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锛宐 涓嶅彲浠ヨ繘琛岀浉鍑忥紒" << endl;
}
}
鏁堟灉鍥惧涓?/p>
5.鐭╅樀涔樻硶
// mul() 鐭╅樀涔樻硶
void mul()
{
// 鍒ゆ柇鏄惁鍙互鐩镐箻 锛坢鉁栵笍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锛宐 鐩镐箻鐨勭粨鏋滃涓嬶細" << 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锛宐涓嶅彲浠ヨ繘琛岀浉涔橈紒" << endl;
}
}
鏁堟灉鍥惧涓?/p>
6.鐭╅樀杞疆
// 杞疆
void transpose()
{
cout << "杞疆鐭╅樀 閫夋嫨锛?.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;
}
}
鏁堟灉鍥惧涓?/p>
7.閲嶇疆鐭╅樀
// 娓呯┖鐭╅樀a b
void clear()
{
a.clear();
b.clear();
cout << "娓呯┖鐭╅樀鎴愬姛锛? << endl;
}
鏁堟灉鍥惧涓?/p>
婧愮爜
/*
write by Haihong
2021 07 06
棣栧彂浜庯細鍏紬鍙枫€愭捣杞癙ro銆?br>*/
#include <iostream>
#include <vector>
using namespace std;
// 鐭╅樀a锛宐
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 << "鐨勮銆佸垪鏁帮紙浠ョ┖鏍奸棿闅?nbsp;渚嬪锛? 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;
}
// 鍒濆鍖?nbsp;鐭╅樀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锛宐 鐩稿姞鐨勭粨鏋滃涓嬶細" << 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锛宐 涓嶅彲浠ヨ繘琛岀浉鍔狅紒" << 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锛宐 鐩稿噺鐨勭粨鏋滃涓嬶細" << 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锛宐 涓嶅彲浠ヨ繘琛岀浉鍑忥紒" << endl;
}
}
// mul() 鐭╅樀涔樻硶
void mul()
{
// 鍒ゆ柇鏄惁鍙互鐩镐箻 锛坢鉁栵笍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锛宐 鐩镐箻鐨勭粨鏋滃涓嬶細" << 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锛宐涓嶅彲浠ヨ繘琛岀浉涔橈紒" << endl;
}
}
// 杞疆
void transpose()
{
cout << "杞疆鐭╅樀 閫夋嫨锛?.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. 閫€鍑虹▼搴?nbsp; " << endl;
cout << "**************************" << endl;
cout << "璇疯緭鍏ユ偍鐨勯€夋嫨锛氥€愪緥濡傞€夋嫨1.鐭╅樀鍔犳硶 璇疯緭鍏ワ細1銆? << endl;
}
// 涓诲嚱鏁?/span>
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 << "璇烽噸鏂拌緭鍏ョ煩闃礱 b" << endl;
initial();
break;
case 6:
flag = 0;
break;
default:
cout << "闈炴硶杈撳叆锛? << endl;
break;
}
}
cout << "鎰熻阿鎮ㄧ殑浣跨敤锛? << endl;
return 0;
}
缁撹
甯屾湜瀵规偍鏈夋墍甯姪
濡傛湁閿欒娆㈣繋灏忎紮浼存寚姝o綖
鎴戞槸 娴疯桨喱?喋娽禃藡)喋?/strong>
濡傛灉鎮ㄨ寰楀啓寰楀彲浠ョ殑璇?/p>
璇风偣涓禐鍚?/strong>
璋㈣阿鏀寔鉂わ笍
以上是关于銆愯绋嬭璁★綔C++銆慍++绋嬪簭璁捐鍩虹锛氱煩闃佃繍绠楀櫒的主要内容,如果未能解决你的问题,请参考以下文章