用c语言画矩形

Python012

用c语言画矩形,第1张

每行起始和结束字符均是你的第3个参数

矩形第1行和最后一行中间是第3个参数,其他行根据第4个参数决定是空格或者第3个参数

程序可以这样写:

...

for ( m=0m<am++ )

{

printf("%c",c)//第1列

if ( m==0 || m==a-1 ) //第1行和最后一行

for ( n=1n<b-1n++ ) printf("%c",c)

else //中间的行

for ( n=1n<b-1n++ ) if ( d==0 ) printf(" ")else printf("%c",c)//空心或否

printf("%c\n",c)//最后1列

}

或者可以写:

for ( m=0m<am++ )

{

printf("%c",c)//第1列

if ( m==0 || m==a-1 || d!=0) for ( n=1n<b-1n++ ) printf("%c",c)

else for ( n=1n<b-1n++ ) printf(" ")

printf("%c\n",c)//最后1列

}

#include <stdio.h>

int main(){

    int width, height, fill

    char chr

    scanf("%d %d %c %d", &height, &width, &chr, &fill)

    if(height > 10 || height < 3){

        perror("Height must between 3 and 10!")

        return 1

    }

    if(width > 10 || width < 5){

        perror("Width must between 5 and 10!")

        return 1

    }

    for(int i=0 i < height i++){

        if(i == 0 || i == height-1 || fill){

            for(int j=0 j < width j++){

                printf("%c", chr)

            }

        }else{

            printf("%c", chr)

            for(int j=0 j < width-2 j++){

                printf(" ")

            }

            printf("%c", chr)

        }

        printf("\n")

    }

    return 0