#include"iostream" #include"iomanip" #include"fstream" #include"vector" using namespace std; typedef vector<vector<int>> Mat; Mat input(istream&in, Mat &a); Mat matAdd(Mat &a, Mat &b); void print (Mat &a); int main() { ifstream in("C:\\Users\\木俊\\Desktop\\Project1\\1.txt"); for (int r, c; in >> r>> c;) { Mat a(r,vector<int>(c)); Mat b = a; Mat cc = a; a=input(in, a); b = input(in, b); cc = matAdd(a, b); print(cc); } system("pause"); } void print(Mat &a) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[0].size(); j++) { cout<<setw(4)<< a[i][j]; } cout << endl; } } Mat matAdd(Mat &a, Mat &b) { Mat c = a; for (int i = 0; i < a.size(); i++) for (int j = 0; j < a[0].size(); j++) c[i][j]= a[i][j]+b[i][j]; return c; } Mat input(istream&in, Mat &a) { for (int i = 0; i < a.size(); i++) for (int j = 0; j < a[0].size(); j++) in >> a[i][j]; return a; }
|
|