C++复习笔记之从0到1(012)

循环,矩阵

#include<iostream>
using namespace std;
/*
    功能:两个mXn矩阵相加 
*/
int main() {
    
    cout << "input" << endl;
    
    int row = 0;
    int col = 0;
    cin >> row >> col;
    
    int rectangle[row][col];
    int rectangle2[row][col];
    int rectangle3[row][col];
    
    char ch;
    if ((ch=getchar())=='\n') {
        char c;
        while((c=getchar())!='\n')
        {
            if(c>='0'&&c<='9')
            {
                ungetc(c,stdin);
                for(int m = 0;m < row;m++) {
                    for(int n = 0; n < col;n++) {
                        cin >> rectangle[m][n];
                    }    
                }
                for(int a = 0;a < row;a++) {
                    for(int b = 0; b < col;b++) {
                        cin >> rectangle2[a][b];
                    }    
                }              
            }
        }
    }
    
    for(int x = 0;x < row;x++) {
        for(int y = 0;y < col;y++) {
            rectangle3[x][y] = rectangle[x][y] + rectangle2[x][y];
        }
    }
    
    cout << "output" << endl;
    int sum = 0;
    for(int i = 0;i< row;i++) {
        for(int j = 0;j < col;j++) {
            //sum += rectangle3[i][j];
            cout << rectangle3[i][j] << " ";
            if (j / (col - 1) == 1) cout << endl;
        }
    }
    return 0;
}

测试结果:

 

posted @ 2019-08-12 18:28 rui1105 阅读(...) 评论(...) 编辑 收藏