printing a square with diagonals

前端 未结 4 1354
佛祖请我去吃肉
佛祖请我去吃肉 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:07

    Think like this. You only have to worry about the first half plus the middle. This is because the second half is the same as the first but reversed.

    Then you figure out a formula for how a specific row should look like, only by which row it is and total number of rows.

    The first row is easy. It's a +, 2n-1 - and another +.

    Then each row except the middle one will be two |, with a \ and a / and a total of 2n-3 spaces. If y is the row and the second row is indexed 0, then (2n-3)-2y spaces in the middle and y spaces outside the middle.

    Lastly, we have the middle row that is two |, n-1 spaces, X, n-1 spaces, |.

    Putting it all together:

    // First row
    putchar('+');
    for(int i=0; i<2*n-1; i++)
            putchar('-');
    putchar('+');
    putchar('\n');
    
    // Those between
    for(int y=0; y<n-1; y++) {
        putchar('|');
        for(int i=0; i<y; i++)
            putchar(' ');
        putchar('\\');
        for(int i=0; i<(2*n-3)-2*y; i++)
            putchar(' ');
        putchar('/');
        for(int i=0; i<y; i++)
            putchar(' ');
        putchar('|');
        putchar('\n');
    }
    
    // Middle row
    putchar('|');
    for(int i=0; i<n-1; i++)
        putchar(' ');
    putchar('X');
    for(int i=0; i<n-1; i++)
        putchar(' ');
    putchar('|');
    putchar('\n');
    

    This prints correctly to the middle row. In order to print the rest, just copy the "Those between" for loop, but change to for(int y=n-2; y>0; y--) and switch places on \ and /. Lastly, just copy the code for the first row.

    0 讨论(0)
  • 2021-01-23 08:12

    To elaborate a bit on @EugeneSh.'s idea: Think of it this way. You have two loops in which you're iterating over (2n+1) x (2n+1) characters in the output. Each of these has to be one of the following: +, -, |, \, /, X or it has to have a (a space character).

    You can't avoid printing all those (2n+1)^2 characters. So - don't try to. Just decide, for each of them, which one it needs to be. Form the appropriate conditions and check for them. You've already done it for 3 of the possible characters - just do the rest.

    The "trick" is that if none of the conditions for the non-space characters is satisfied - you print a space ().

    PS - You might want to consider the switch() { ... } statement for doing this, with default: used for printing the space.

    0 讨论(0)
  • 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 <stdio.h>
    
    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

    +---+
    |   |
    |   |
    |   |
    |   |
    
    0 讨论(0)
  • 2021-01-23 08:21

    Homework is an exploration.

    You are trying to print an object that will have a number of decisions. You need to code for each one. Simple code for this might try the approach of 'print all the characters in a square, deciding what each should be.' That's your approach so far. Next you need to write if statements and correctly answer do I print '+', '-', '|', '\', '/', 'X', or ' '?

    You need to think about each character in order. For example, your first test, if (i == 0 && (j == 0 || j == size * 2)), but you need to expand it to get the '+' on the last line as well.

    One base idea is to first get something working. I suggest you start with your code and add items as you go. Here is an outline with '+' working:

    for (int i = 0;i < ((size * 2) + 1);i++){
        for (int j = 0; j < ((size * 2) + 1);j++){
            if ((i == 0 || i == size * 2) && (j == 0 || j == size * 2)){
                printf("+");
            }
            // more else if for '-', etc.
            else {
                printf(" ");
            }
        }
    }
    

    Once you have it printing the '+' at the four corners of a square, add the logic for each other charcter, checking that you get a square with the '-', '_', etc., correct.

    One other point is that there are other ways to do the problem, and you should explore how some seem less error prone than others. For example, if you change your outline to:

    int last = size * 2
    for (int i = 0; i < last + 1; i++){
        for (int j = 0; j < last + 1; j++){
            if ((i == 0 || i == last) && (j == 0 || j == last) {
               ...
    

    you may find that eliminating repetition and parenthesis makes it less error prone. Notice or keep track of the errors you make.

    0 讨论(0)
提交回复
热议问题