當前位置:首頁 » 編程語言 » c語言畫方

c語言畫方

發布時間: 2022-03-09 02:33:18

㈠ 如何用c語言用虛線畫一個長方形然後內部有一個由"*"組成的三角形 附圖

//這樣就可以了,直接復制粘貼就行,貼到編譯器上就能看到對的格式
#include <stdio.h>
int main()
{
printf("+-----------+\n");
printf("| * |\n");
printf("| ** |\n");
printf("| *** |\n");
printf("| **** |\n");
printf("| ***** |\n");
printf("| *** |\n");
printf("| * |\n");
printf("+-----------+\n");
return 0;

}

㈡ 用c語言來畫1個正方體,一個簡單的程序。。

#include "stdio.h"
#include "graphics.h"

main()
{
int gb=VGA,gm=VGAHI,errorcode;
initgraph(&gb,&gm,"");
bar3d(100,100,200,200,100,1);
getch();

}
樓上的已經給出詳細的了,我就給一個簡單點的吧。

㈢ 用符號「□」「■」C語言畫出正方形和正方形對角線

// Eclipse C++ 和 Code::Block 調試通過

// 提供控制台簡易菜單 以及正方形 和 長方形 輸出。

// 長方形 對角線 輸出不穩定 (幾何方程離散化需要更復雜的演算法,故為深入)長寬較大時可以用

// 邊長大約32 窗口無法顯示完整

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
usingnamespacestd;

//funcdeclaration-----------------------------------------
voidset_prefix_str(char*&str,intlen,charch);
voidprintln_str(constchar*str);
voidprint_pix(constchar*str);
intprint_rectangle(floatrectX,floatrectY);

//gloabl--------------------------------------------------
constchar*PIX0="□";
constchar*PIX1="■";
intLEN=8;
char*PREFIX=NULL;
//mainmenu-----------------------------------------------
intmain()
{
floatrectX;
floatrectY;
intoption=-1;
set_prefix_str(PREFIX,LEN,'');
while(1){
system("cls");
println_str("");
println_str("");
println_str("PrintRectangle");
println_str("");
println_str("");
println_str("[1]Print正方形");
println_str("[2]Print長方形(不穩定)");
println_str("[3]setting");
println_str("");
println_str("[0]Exit");
println_str("");
option=getch();
println_str("");
if(option<'0'oroption>'3'){
continue;
}
if('0'==option){
break;
}
if('3'==option){
print_pix("設置左側縮進:");
cin>>LEN;
set_prefix_str(PREFIX,LEN,'');
continue;
}
if('1'==option){
print_pix("正方形邊長:");
cin>>rectY;rectX=rectY;
println_str("");
}

if('2'==option){
print_pix("矩形長:");
cin>>rectX;
println_str("");
print_pix("矩形高:");
cin>>rectY;
println_str("");
}
if(rectX<0orrectX>64){
println_str("X參數范圍錯誤");
system("pause");
continue;
}
if(rectY<0orrectY>64){
println_str("Y參數范圍錯誤");
system("pause");
continue;
}
system("cls");
println_str("");
print_rectangle(rectX,rectY);
println_str("");
system("pause");
continue;
}
return0;
}

//tools---------------------------------------------------
voidprintln_str(constchar*str){
cout<<str<<endl<<PREFIX;//oruseprintftoprint
}
voidprint_str(constchar*str){
cout<<str<<PREFIX;//oruseprintftoprint
}

voidprint_pix(constchar*str){
cout<<str;//oruseprintftoprint
}
voidset_prefix_str(char*&str,intlen,charch){
if(str){
free(str);
}
//usenewormalloc
str=(char*)malloc(sizeof(char)*(len+1));
//newchar[](len+1)
//delete[](str)

for(inti=0;i<len;++i){
str[i]=ch;
}
str[len]='';
}
intprint_rectangle(floatrectX,floatrectY){
intmaxX=(int)(rectX);
intmaxY=(int)(rectY);
//對角線方程y=kx+b
//正方形則k=+/-1
floatk1=maxY/(float)(maxX);
floatk2=-maxY/(float)(maxX);
intcount=0;
for(inty=0;y<=maxY;++y){
for(intx=0;x<=maxX;++x){
//橫邊方程
if(0==yormaxY==y){
print_pix(PIX1);
++count;
continue;
}
//縱邊方程
if(0==xormaxX==x){
print_pix(PIX1);
++count;
continue;
}
//對角線方程y=kx+b
if((int)(k1*x)==yor(int)(k2*x)+maxY==y){
print_pix(PIX1);
++count;
continue;
}
print_pix(PIX0);
++count;
continue;
}
println_str("");
}
returncount;
}

㈣ 如何用c語言畫一個矩形

rectangle() 畫矩形函數

功能: 函數rectangle() 用當前繪圖色、線型及線寬,畫一個給定左上角與右下角的矩形(正方形或長方形)。

用法: 此函數調用方式為void rectangle(int left,int top,int right,int bottom);

說明: 參數left,top是左上角點坐標,right,bottom是右下角點坐標。如果有一個以上角點不在當前圖形視口內,且裁剪標志clip設置的是真(1),那麼調用該函數後,只有在圖形視口內的矩形部分才被畫出。

這個函數對應的頭文件為graphics.h

返回值: 無

例: 下面的程序畫一些矩形實例:
#include<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();
}

㈤ C語言如何畫X求完整代碼

#include<stdio.h>
void fun(int n){
int i,j;
for (i=0;i<=n/2;i++){
for (j=0;j<i;j++)printf(" ");
printf("*");for (j=1;j<n-i*2;j++)printf(" ");printf("\b*\n");
}
for (i=n/2-1;i>=0;i--){
for (j=0;j<i;j++)printf(" ");
printf("*");for (j=1;j<n-i*2;j++)printf(" ");printf("\b*\n");
}
}

int main()
{
int i,n,x;
scanf("%d",&n);
for (i=0;i<n;i++) {scanf("%d",&x);fun(x);printf("\n"); }
return 0;
}

㈥ C語言如何畫圖

framebuffer(幀緩沖)。
幀的最低數量為24(人肉眼可見)(低於24則感覺到畫面不流暢)。
顯卡與幀的關系:由cpu調節其數據傳輸速率來輸出其三基色的配比。
三基色:RGB(紅綠藍)。

在沒有桌面和圖形文件的系統界面,可以通過C語言的編程來實現在黑色背景上畫圖!

用下面的代碼,在需要的地方(有注釋)適當修改,就能畫出自己喜歡的圖形!

PS:同樣要編譯運行後才能出效果。

  • #include <stdio.h>

  • #include <sys/mman.h>

  • #include <fcntl.h>

  • #include <linux/fb.h>

  • #include <stdlib.h>

  • #define RGB888(r,g,b) ((r & 0xff) <<16 | (g & 0xff) << 8 | (b & 0xff))

  • #define RGB565(r,g,b) ((r & 0x1f) <<11 | (g & 0x3f) << 5 | (b & 0x1f))

  • int main()

  • {

  • int fd = open("/dev/fb0", O_RDWR);

  • if(fd < 0){

  • perror("open err. ");

  • exit(EXIT_FAILURE);

printf("xres: %d ", info.xres);

printf("yres: %d ", info.yres);

printf("bits_per_pixel: %d ", info.bits_per_pixel);

size_t len = info.xres*info.yres*info.bits_per_pixel >> 3;

unsigned long* addr = NULL;

addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);

if(addr == (void*)-1){

perror("mmap err. ");

㈦ C語言編寫一個程序輸出一個正方形

思路:輸出正方形即輸出正方形的外圍就行,外圍有個特點就是行列下標必有0或者是正方形的大小減一,輸入一個n表示正方形大小,輸出一個由*組成的正方形。

參考代碼:

#include<stdio.h>
intmain()
{
inti,j,n;
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==0||i==n-1||j==0||j==n-1)
printf("*");
else
printf("");
}
printf(" ");
}
return0;
}
/*
輸出:
5
*****
**
**
**
*****
*/

㈧ C語言怎麼畫矩形

用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();
}

㈨ 用C語言怎樣畫正方形

根據你的編譯器的繪圖函數.

有的有繪 rect 函數, (參數,4個角點坐標. 或一個角點坐標和長寬值).
那你用循環語句依次畫長寬相等的矩型.

如果只有畫直線函數.
那你用循環語句依次調 MoveTo, LineTo 按 角點坐標 畫直線.

㈩ c語言怎麼用符號畫空心長方形

#include<stdio.h>

#defineSIGN'*'

intmain(intargc,char*argv[])
{
intlength,width;
inti,j;

printf(":");
scanf("%d%d",&length,&width);

printf("Length:%d ",length);
printf("Width:%d ",width);

for(i=1;i<=width;i++)
{
if(i==1||i==width)
{
for(j=0;j<length;j++)
printf("%c",SIGN);
printf(" ");
}
else
{
printf("%c",SIGN);
for(j=0;j<length-2;j++)
printf("");
if(length>1)
printf("%c",SIGN);
printf(" ");
}
}

return0;
}

熱點內容
linux查看selinux狀態 發布:2025-07-20 13:36:35 瀏覽:913
福利資源解壓密碼 發布:2025-07-20 13:34:10 瀏覽:268
我的世界怎麼在手機搞伺服器 發布:2025-07-20 13:26:07 瀏覽:905
android系統鍵盤 發布:2025-07-20 13:19:38 瀏覽:500
程序化交易演算法 發布:2025-07-20 13:09:58 瀏覽:973
已關閉伺服器是什麼意思 發布:2025-07-20 13:06:49 瀏覽:709
三星s5密碼多少錢 發布:2025-07-20 12:55:14 瀏覽:961
密碼忘記了怎麼找回 發布:2025-07-20 12:46:18 瀏覽:534
華為的密碼鎖為什麼開不了 發布:2025-07-20 12:45:35 瀏覽:133
app登錄密碼在哪裡 發布:2025-07-20 12:22:31 瀏覽:693