printing a square with diagonals

前端 未结 4 1497
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 07:44

I want to print this if the user typed in for example 2:

+---+ 
|\\ /|
| X |
|/ \\|
+---+

It\'s a square with a size of 2n+1 for each side of i

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 08:20

    Here is a slightly modified version of your program that prints spaces where you don't specify anything else.

    You can use it as a starting point.

    #include 
    
    int main(void)
    {
        int size = 2;
        int i, j;
        char ch;
    
        for (int i = 0;i < ((size * 2) + 1);i++){
            for (int j = 0; j < ((size * 2) + 1);j++){
                ch = ' ';
                if (i == 0 && (j == 0 || j == size * 2)){
                    ch = '+';
                }
                else if (i == 0 && j != i){
                    ch = '-';
                }
    
                if (i > 0 && (j == 0 || j == size * 2)){
                    ch = '|';
                }
                printf("%c", ch);
            }
            printf("\n");
        }
    
        return 0;
    }
    

    This prints

    +---+
    |   |
    |   |
    |   |
    |   |
    

提交回复
热议问题