简体   繁体   English

使用垂体,扩展的ASCII字符绘制用于用户定义尺寸的矩形

[英]drawing a rectangle for user-defined dimensions using for lops, using extended ASCII characters

I did some part of it but can't seem to get the whole rectangle. 我做了一部分,但似乎无法获得整个矩形。 Could someone point out what I'm doing wrong? 有人可以指出我做错了吗?

Here is my code: 这是我的代码:

printf("Enter the length and width of the rectangle : ");
scanf("%d%d",&length,&width);


printf("\n%c", 218);
for(i=1;i<=length;i++)
{
        printf("%c",196);

}

printf("%c",191);


 for(j=1;j<=width;j++)
 {
     printf("\n");
     printf("%c",179);
 }
    printf("\n");
    printf("%c", 192);

     for(i=1;i<=length;i++)
{
        printf("%c",196);

}

printf("%c", 217);

return 0;

My output 我的输出

You are only printing the first column and not the second one. 您仅打印第一列,而不打印第二列。 An approach would be to loop over the whole rectangle and check if you are at an edge or not. 一种方法是在整个矩形上循环并检查您是否在边缘。

#include <stdio.h>

int main()
{
    int length, width, i, j;

    printf("Enter the length and width of the rectangle : ");
    scanf("%d%d",&length, &width);

    for(i=0;i<width;i++)
    {
        for(j=0;j<length;j++)
        {
            if( j==0 || j==length-1 || i==0 || i==width-1 )
            {
                printf("%c", '*');
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

In your middle loop, you only print a single vertical bar before going to the next line. 在您的中间循环中,您只需要打印一个垂直条,然后再转到下一行。

You need to print the bar once, then add another loop to print the spaces, then print one more bar: 您需要打印一次条形,然后添加另一个循环以打印空格,然后再打印一个条形:

  for(j=1;j<=width;j++)
  {
     printf("\n");
     printf("%c",179);
     for (i=1;i<=length;i++) {
       printf(" ");
     }
     printf("%c",179);
  }
#include <stdio.h>
#include <stdlib.h>

int main()
{
int length, width,i,j;
   printf("Enter the length and width of the rectangle : ");
   scanf("%d%d",&length,&width);


printf("\n%c", 218);
for(i=1;i<=length;i++)
{
    printf("%c",196);

 }

   printf("%c",191);


    for(j=1;j<=width;j++)
 {
 printf("\n");
 printf("%c",179);
 for (i=1;i<=length;i++) {
   printf(" ");
 }
  printf("%c",179);
  }
    printf("\n");
    printf("%c", 192);

  for(i=1;i<=length;i++)
   { 
    printf("%c",196);

    }

 printf("%c", 217);

  return 0;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM