如何在C中居中放置长方体?
好的,现在我需要关于如何居中我的箱子的帮助,我尝试了很多方法来解决它,但没有找到正确的方法。我对这一点还是新手,抱歉,如果它让人困惑的话如何在C中居中放置长方体?,c,C,好的,现在我需要关于如何居中我的箱子的帮助,我尝试了很多方法来解决它,但没有找到正确的方法。我对这一点还是新手,抱歉,如果它让人困惑的话 void box() { int i, j, w=20, h=12; putchar(218); for(i=0;i<w-2;i++){ putchar(196); } putchar(191); printf("\n"); for(i=0;i<h-2;i++) {
void box()
{
int i, j, w=20, h=12;
putchar(218);
for(i=0;i<w-2;i++){
putchar(196);
}
putchar(191);
printf("\n");
for(i=0;i<h-2;i++)
{
putchar(179);
for(j=0;j<w-2;j++){
printf(" ");
}
putchar(179);
printf("\n");
}
putchar(192);
for(i = 0; i<w-2;i++)
{
putchar(196);
}
putchar(217);
}
int main()
{
gotoxy(30,0); printf("\nRobby Robot walks in a Cartesian plane.\n");
box();
gotoxy(30,5); printf("a. Orgigin\n");
gotoxy(30,6); printf("b. Location\n");
gotoxy(30,7); printf("c. Move\n");
gotoxy(30,8); printf("d. Exit");
}
void box()
{
int i,j,w=20,h=12;
普查尔(218);
对于(i=0;i#define SWIDTH(80)/*您可能需要更改此屏幕宽度值*/
空框()
{
int i,j,w=20,h=12;
普查尔(218);
对于(i=0;IIS C或C++),它们是两种不同的语言。标准版本都不使用“代码> GoTooYy())/Cube >。中心点,在一维上,是“代码>距离/2 < /代码>”。在计算中心点之后,可能需要添加偏移量。另一个公式:<代码>左TythPo位置=(距离/ 2)-(Box宽度/ 2)。
。在打印每行方框之前,您可能需要使用gotoxy
。顺便说一句,您拼错了“Orgigin”,我想应该是“origin”<代码> Goodoy 既不是标准C,也不是C++。如果我正确地记得它是Turbo的残余。如果你对一个更便携的解决方案感兴趣,你可能想看看<代码>诅咒< /C> >或代码> NcReSs< /Cord> LabalRay. IM使用DeVC++,这就是我如何使用空GoTyy(int x,int y){HANDLE hConsoleOutput;COORD COORD;COORD.X=X;COORD.Y=Y;hConsoleOutput=GetStdHandle(STD_OUTPUT_HANDLE);setConsoleorSorposition(hConsoleOutput,COORD);}你能解释一下#define SWIDTH是什么意思吗?它对我未来的编程活动非常有帮助。#define
是一个预处理器指令,它在程序中将
SWIDTH
定义为(80)
。在将程序送入编译器之前,预处理器只需将SWIDTH
一词替换为(80)
。
#define SWIDTH (80) /* you may need to change this screen-width value */
void box()
{
int i, j, w=20, h=12;
putchar(218);
for(i=0; i<(SWIDTH-w)/2; i++) putchar(' ');
for(i=0; i<w-2; i++)
{
putchar(196);
}
putchar(191);
printf("\n");
for(i=0; i<h-2; i++)
{
for(j=0; j<(SWIDTH-w)/2; j++) putchar(' ');
putchar(179);
for(j=0; j<w-2; j++)
{
printf(" ");
}
putchar(179);
printf("\n");
}
for(i=0; i<(SWIDTH-w)/2; i++) putchar(' ');
putchar(192);
for(i = 0; i<w-2; i++)
{
putchar(196);
}
putchar(217);
}
|<--------------------- SWIDTH ---------------------->|
| ------------------ |
| | | |
| <- BOXWIDTH (W) -> |
|<- (SWIDTH-W)/2 -> |
...