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

嵌套循环、条件判断、中文字符、char

#include<iostream>
using namespace std;

/*
    功能:根据参数,画出矩形
    思路:
        1 获得控制台参数
        2 根据参数画出矩形,例如,7 7 @ 0 分别是:高 宽 符号 空心或者实心(0或者1) 
        画出7X7矩阵,空心的或者非空心的 
*/
int main() { 
    
    int height;
    int wide;
    char ch; // 不支持中文字符??? 
    int b;
    
    cin >> height >> wide >> ch >> b;
    //cout << "height: " << height << " wide: " << wide << " ch: " << ch << " bool: "<< b << endl;
    
    if(b == 0) { // 画空心矩形 
        for(int i = 0;i < height;i++) {
            if(i == 0 || i == (height - 1)) {
                for(int j = 0;j < wide;j++) {
                    cout << ch;    
                } 
                cout << endl;
            } else {
                //for(int k = 0;k < (wide - 2);k++) {
                    cout << ch;
                    for(int l = 0;l < (wide - 2);l++) { // l需要初始为0 
                        cout << " ";
                    }
                    cout << ch << endl;
                //}
                //cout << endl;
            }
        } 
    } else if(b == 1) { // 画实心矩形 
        for(int x = 0;x < height;x++){
            for(int y = 0;y < wide;y++){
                cout << ch;
            }
            cout << endl;
        }
    } else {
        cout << "error!!!" << endl;
    }
     
}

测试结果:

猜你喜欢

转载自www.cnblogs.com/mrray1105/p/11255426.html
今日推荐