如何用c++写一个金字塔
时间: 2024-05-13 08:13:31 浏览: 12
我们可以使用循环语句来实现打印金字塔,代码如下:
```
#include <stdio.h>
int main()
{
int rows, i, j, space;
printf("请输入金字塔的行数:");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
```
上述代码中,我们首先输入金字塔的行数,然后利用两个循环语句来打印金字塔的每一行。第一个循环用于打印空格,第二个循环用于打印星号。其中,第一个循环中的 space 变量表示每行需要打印的空格数,第二个循环中的 j 变量表示每行需要打印的星号数。
相关问题
帮我用c++写一个金字塔打印Bo Kang的算法
好的,以下是用C语言打印金字塔形式的“Bo Kang”:
```
#include <stdio.h>
int main() {
int i, j, k, n = 5; // n为金字塔的层数
char *str = "Bo Kang";
for (i = 1; i <= n; i++) {
for (j = 0; j < n - i; j++)
printf(" "); // 输出左侧空格
k = 0;
while (k != 2 * i - 1) {
printf("%c", str[k % 7]); // 输出姓名
k++;
}
printf("\n"); // 输出换行
}
return 0;
}
```
输出结果:
```
B
ooo
KKKKK
aaaaaaa
nnnnnnnnn
```
如何用C++写出一个窗口实现金字塔旋转
要在C++中实现一个窗口,可以使用图形库例如OpenGL和GLUT。下面是一个简单的代码示例,可以帮助你实现一个旋转的金字塔窗口:
```c++
#include <GL/glut.h>
GLfloat vertices[4][3] = { {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0} };
GLfloat colors[4][3] = { {0.0,0.0,1.0}, {0.0,1.0,0.0}, {1.0,0.0,0.0}, {1.0,1.0,0.0} };
GLubyte indices[4] = { 0,1,2,3 };
GLfloat theta = 0.0;
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1.5, 1.5, 1.5, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glRotatef(theta, 0.0, 0.0, 1.0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glutSwapBuffers();
}
void spinDisplay(void) {
theta += 1.0;
if (theta > 360.0)
theta -= 360.0;
glutPostRedisplay();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Rotating Pyramid");
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
return 0;
}
```
在这个代码中,我们定义了一个包含4个顶点和4个颜色的金字塔。我们还定义了一个旋转角度theta,然后使用glRotatef函数来旋转金字塔。我们还使用gluLookAt函数设置了视角。最后,我们使用glVertexPointer和glColorPointer函数来指定顶点和颜色数据,然后使用glDrawElements函数来绘制金字塔。
在主函数中,我们使用glut库来创建窗口和显示图形。我们还使用glutIdleFunc函数来设置旋转金字塔的动画效果。当程序空闲时,spinDisplay函数会被调用,它会更新旋转角度theta,并使用glutPostRedisplay函数来重新绘制金字塔。
你可以根据自己的需要修改这个代码来实现你自己的旋转金字塔窗口。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)