如何在C中定义矩形的框架

如何在C中定义矩形的框架,c,C,我有一个关于教授交给我们的任务的问题。我们得到的任务是这个网站上的L6: L6:画一个矩形 编写一个C程序,要求用户输入矩形的高度和宽度,然后绘制相应的矩形: Height of rectangle? 10 Width of rectangle? 20 #################### # # # # # # # # #

我有一个关于教授交给我们的任务的问题。我们得到的任务是这个网站上的L6:

L6:画一个矩形 编写一个C程序,要求用户输入矩形的高度和宽度,然后绘制相应的矩形:

Height of rectangle? 10
Width of rectangle? 20
####################
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
####################

Height of rectangle? 5
Width of rectangle? 30
##############################
#                            #
#                            #
#                            #
##############################

Height of rectangle? 10
Width of rectangle? 40
########################################
#                                      #
#                                      #
#                                      #
#                                      #
#                                      #
#                                      #
#                                      #
#                                      #
########################################
到目前为止,我已经编写了以下代码:

int main()
{
    int height;
    int width;
    int a;
    int b;

    printf("Height of rectangle? ");
    scanf_s("%d", &height);

    printf("Width of rectangle? ");
    scanf_s("%d", &width);

    for (a = 0;a != height;a++)
    {
        {
            for (b = 0; b != width; b++)
                if ((a == 0))
                {
                    printf("#");
                }
                else
                {
                    printf("");
                }
        }
    }

    _getch();
}
我知道在输出中,
a==0
将是第一行,
a==1
将是第二行,如此类推。但我无法想象如何定义宽度,例如第2行的开头或开头只有一个“#”

你能帮忙吗

所以我在一个朋友的帮助下找到了一个解决方案,算法几乎完成了。谢谢你的帮助

#include <stdio.h>
#include <conio.h>

int main()
{
    int height;
    int width;



    printf("Height of rectangle? ");
    scanf_s("%d", &height);

    printf("Width of rectangle? ");
    scanf_s("%d", &width);


    for (int a = 1;a <= height;a++)
    { 
            for (int b = 1; b <= width; b++)
            {


                if ((a == 1) || (a == (width) || (b == 1) || (b == (height))))
                {
                    printf("#");
                }
                else
                    printf(" ");

        }
            printf("\n");
    }



    _getch();
}
#包括
#包括
int main()
{
内部高度;
整数宽度;
printf(“矩形的高度?”);
扫描单位(“%d”和高度);
printf(“矩形的宽度?”);
扫描单位(“%d”和宽度);
对于(int a=1;a而言,在空格前后打印“#”是一种特殊情况,可以在循环外处理。
处理此问题的一种方法类似于以下伪代码:

  • 在线循环
    • 打印一个“#”
    • 在从1到N-2的列上循环(而不是从0到N-1) 在第一行和最后一行打印“#” ' 在所有其他行上
  • 打印一个“#”

    它归结为两件事:

  • 如何获得模式

  • 如何编写可用的代码来生成这些模式

  • 你可以试试这些来得到答案

    对于图案,您将看到第一行和最后一行始终相同。打印w字符

    其他行的特殊之处是什么?只打印两个字符,其余是空格

    • 一行有多少个字符?
      w

    • 如果其中两个是
      *
      。有多少个空格?
      w-2

    • 有多少行?
      h

    • 这里的主要内容是什么?打印字符
    因此,请编写一个方法,将字符
    c
    n次打印,并重复使用它来打印
    '
    *

    代码
    void打印机(整数计数,字符c){
    
    对于代码段中的(int i=0;i您的算法看起来基本接近。(尽管存在一些语法问题,可能会导致意外的结果),您有两个开放的花括号紧跟在后面。第二个括号可能应该紧跟在
    for
    语句之后:

       // { ///move this...
            for (b = 0; b != width; b++)
            {// ...here
                if ((a == 0))
                {
                    printf("#");
                }
                else
                {
                    printf("");
                }
    
       /*->*/}  // aligned with its mate above
    
    在算法部分,您只需要在每个条件语句中包含端点。以下是如何实现的可编译示例:

    int main()
    {
        int height;
        int width;
        int a;
        int b;
        char q = 'a';
    
        while (q != 'q')
        {
    
            printf("Height of rectangle? ");
            scanf(" %d", &height);
    
            printf("Width of rectangle? ");
            scanf(" %d", &width);
    
    
            for (a = 0;a < height;a++)
            {
                    for (b = 0; b < width; b++)
                    {
    
                        if((a==0)||(a==(height-1))) // top and bottom lines
                        {
                            printf("#");
                        }
                        else //all other lines
                        {
                             if((b==0) || (b==(width-1))) printf("#");
                             else printf(" ");
                        }               
                    }
                    printf("\n"); // end of each line
    
            }
            printf("\n\nEnter q to quit, or any other key to Continue:\n");
            scanf(" %c", &q);
        }
        return 0; // should always include a return value for a non-void function, such as this one
    }
    
    intmain()
    {
    内部高度;
    整数宽度;
    INTA;
    int b;
    char q='a';
    而(q!=“q”)
    {
    printf(“矩形的高度?”);
    扫描频率(“%d”和高度);
    printf(“矩形的宽度?”);
    扫描频率(“%d”和宽度);
    对于(a=0;a<高度;a++)
    {
    对于(b=0;b

    语法问题在于大括号。
    {for()…}
    内部循环的位置不正确。应该是
    for(){…}

    我将提供一个表面上可能更复杂的解决方案,但它具有很大的灵活性:

    #include <assert.h>
    #include <stdio.h>
    
    static void draw_line(int width, char end, char middle)
    {
        assert(width > 0);
        int w = 0;
        putchar(end);
        for (w = 1; w < width - 1; w++)
            putchar(middle);
        if (w < width)
            putchar(end);
        putchar('\n');
    }
    
    static void draw_rectangle(int height, int width, char border, char fill)
    {
        assert(height > 0);
        for (int h = 0; h < height; h++)
            draw_line(width, border, (h == 0 || h == height - 1) ? border : fill);
    }
    
    static void test_drawing(int height, int width)
    {
        printf("%dx%d:\n", height, width);
        draw_rectangle(height, width, '#', ' ');
        putchar('\n');
    }
    
    int main(void)
    {
        // Testing small sizes, including degenerate cases
        for (int height = 1; height < 6; height++)
        {
            for (int width = 1; width < 6; width++)
                test_drawing(height, width);
        }
    
        // Testing examples from question
        test_drawing(10, 20);
        test_drawing( 5, 30);
        test_drawing(10, 40);
        return 0;
    }
    

    我想你会发现许多建议的解决方案都不能处理退化情况(宽度或高度或两者都小于3)。

    行的开头应该是
    b==0
    ,为什么不测试一下呢?对于宽度w和高度h:(1)打印第一行,即w
    (2)打印中间的h-2行,即(a)一个
    ,(b)w-2空格,(c)一个
    (3)打印最后一行,即w
    。完成。
    用于(…){{
    有两个语句块是不寻常的。从技术上讲,这并不是错误的,但没有经验丰富的C程序员会这样写。他们可能会将内部
    {
    }
    放在
    循环的内部
    主体周围(尽管目前在技术上也没有必要)。在修复逻辑时(与化妆品相反),您需要考虑如果用户输入的高度为2或1,或者实际上为0或负数,或者如果用户键入的是“大象”而不是数字,会发生什么情况。宽度也是如此。我是否需要嵌套第二个if-else条件?因为我尝试了不嵌套,并在第一行中获得了所有输出。如果需要嵌套,您能解释一下吗为什么?哦,是的,你是对的,语法是错误的,但即使使用错误的语法编译的程序,顺便说一句,我使用visual studio 2K15不确定我的上一条评论(已删除)是否正确捕获了错误。我现在正在查看它…我必须在哪里添加\n,因为如果我在第一个if-else条件下编写它,如printf(“\n”);在第一行或?@LukasWegner-我计算出来的每一个“#”之后都会有一个中断。我使用的是
    宽度
    高度
    ,有时我应该使用相反的值。显示的循环应该可以让您达到目的。替换代码中的该部分并测试它。注意“\n”在内部循环之外。看到这个网站上仍然有喜欢解决问题的人,这让人耳目一新
    #include <assert.h>
    #include <stdio.h>
    
    static void draw_line(int width, char end, char middle)
    {
        assert(width > 0);
        int w = 0;
        putchar(end);
        for (w = 1; w < width - 1; w++)
            putchar(middle);
        if (w < width)
            putchar(end);
        putchar('\n');
    }
    
    static void draw_rectangle(int height, int width, char border, char fill)
    {
        assert(height > 0);
        for (int h = 0; h < height; h++)
            draw_line(width, border, (h == 0 || h == height - 1) ? border : fill);
    }
    
    static void test_drawing(int height, int width)
    {
        printf("%dx%d:\n", height, width);
        draw_rectangle(height, width, '#', ' ');
        putchar('\n');
    }
    
    int main(void)
    {
        // Testing small sizes, including degenerate cases
        for (int height = 1; height < 6; height++)
        {
            for (int width = 1; width < 6; width++)
                test_drawing(height, width);
        }
    
        // Testing examples from question
        test_drawing(10, 20);
        test_drawing( 5, 30);
        test_drawing(10, 40);
        return 0;
    }
    
    1x1:
    #
    
    1x2:
    ##
    
    1x3:
    ###
    
    1x4:
    ####
    
    1x5:
    #####
    
    2x1:
    #
    #
    
    2x2:
    ##
    ##
    
    2x3:
    ###
    ###
    
    2x4:
    ####
    ####
    
    2x5:
    #####
    #####
    
    3x1:
    #
    #
    #
    
    3x2:
    ##
    ##
    ##
    
    3x3:
    ###
    #.#
    ###
    
    3x4:
    ####
    #..#
    ####
    
    3x5:
    #####
    #...#
    #####
    
    4x1:
    #
    #
    #
    #
    
    4x2:
    ##
    ##
    ##
    ##
    
    4x3:
    ###
    #.#
    #.#
    ###
    
    4x4:
    ####
    #..#
    #..#
    ####
    
    4x5:
    #####
    #...#
    #...#
    #####
    
    5x1:
    #
    #
    #
    #
    #
    
    5x2:
    ##
    ##
    ##
    ##
    ##
    
    5x3:
    ###
    #.#
    #.#
    #.#
    ###
    
    5x4:
    ####
    #..#
    #..#
    #..#
    ####
    
    5x5:
    #####
    #...#
    #...#
    #...#
    #####
    
    10x20:
    ####################
    #..................#
    #..................#
    #..................#
    #..................#
    #..................#
    #..................#
    #..................#
    #..................#
    ####################
    
    5x30:
    ##############################
    #............................#
    #............................#
    #............................#
    ##############################
    
    10x40:
    ########################################
    #......................................#
    #......................................#
    #......................................#
    #......................................#
    #......................................#
    #......................................#
    #......................................#
    #......................................#
    ########################################