用c语言画矩形

Python016

用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列

}

用lineto函数矩形

#include<graphics.h>

main()

{int gdriver=DETECT,gmode

initgraph(&gdriver,&gmode,"c:\\tc")

cleardevice()

moveto(160,120)

lineto(480,120)

lineto(160,360)

lineto(160120)

getch()

closegraph()

}

图形和图像函数包含在graphics.h里面

rectangle() 画矩形函数

功能: 函数rectangle() 用当前绘图色、线型及线宽,画一个给定左上角与右下角的矩形(正方形或长方形)。

用法: 此函数调用方式为void rectangle(int left,int top,int right,int bottom)

说明: 参数left,top是左上角点坐标,right,bottom是右下角点坐标。如果有一个以上角点不在当前图形视口内,且裁剪标志clip设置的是真(1),那么调用该函数后,只有在图形视口内的矩形部分才被画出。

这个函数对应的头文件为graphics.h

返回值: 无

例: 下面的程序画一些矩形实例:

#i nclude<graphics.h>

void main()

{

int driver,mode

driver=DETECT

mode=0

initgrpah(&driver,&mode,"")

rectangle(80,80,220,200)

rectangle(140,99,180,300)

rectangle(6,6,88,88)

rectangle(168,72,260,360)

getch()

restorecrtmode()

}

#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