初学者c++绘制房屋程序
beginner c++ draw house program
c++新手,所以如果我弄错了任何术语,请不要嘲笑我。我试图写一个程序,将绘制要求用户单位,然后使用函数绘制房子。我遇到的问题是在我的drawcone
函数中,这是我到目前为止的进展。
#include<iostream>
using namespace std;
void drawcone(int height);
void drawHorizontalLine(int numXs);
void draw2VerticalLines(int numSpaces, int numRows);
void drawOneRow(int numSpaces);
void getDimensions(int& width, int& height);
void drawbox(int width, int height);
int main(){
int width;
int height;
getDimensions(height, width);
drawcone(height);
drawHorizontalLine(width);
draw2VerticalLines(width - 2, height - 2);
drawHorizontalLine(width);
return 0;
}
void drawbox(int width, int height){
drawHorizontalLine(width);
draw2VerticalLines(width - 2, height - 2);
drawHorizontalLine(width);
}
void drawcone(int height){
int base = height * 2;
int r = 0;
while ( r != height){
int c = 0;
while (c != base){
if(c==height-r || c==height+r)
cout << "X";
else
cout << " ";
c++;
}
cout << endl;
r++;
}
}
void drawHorizontalLine(int numXs)
{
int count;
for (count = 0; count < numXs; count++){
cout << "X";
}
cout << endl;
}
void draw2VerticalLines(int numSpaces, int numRows)
{
int rowCount;
for (rowCount = 0; rowCount < numRows; rowCount++){
drawOneRow(numSpaces);
}
}
void drawOneRow(int numSpaces)
{
int spaceCount;
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++){
cout << " ";
}
cout << "X" << endl;
}
void getDimensions(int& width, int& height){
cout << "Enter the width of the house" << endl;
cin >> width;
cout << "Enter the height of the house" << endl;
cin >> height;
}
正确的示例输出如下所示
X
X X
X X
XXXXX
X X
X X
XXXXX
我当前的输出如下所示
X
X X
X X
X X
XXXX
X X
X X
XXXX
我想让圆锥体稍微小一点,这样它就会与盒子成比例。我也更喜欢一个不涉及修改drawbox
函数的答案。感谢您的宝贵时间!
这里有一个解决方案。然而,只有当宽度为奇数时,圆锥体才会对称。
#include<iostream>
using namespace std;
void drawcone(int height);
void drawHorizontalLine(int numXs);
void draw2VerticalLines(int numSpaces, int numRows);
void drawOneRow(int numSpaces);
void getDimensions(int& width, int& height);
void drawbox(int width, int height);
int main() {
int width;
int height;
getDimensions(width, height);
drawcone(width);
drawHorizontalLine(width);
draw2VerticalLines(width - 2, height - 2);
drawHorizontalLine(width);
return 0;
}
void drawbox(int width, int height) {
drawHorizontalLine(width);
draw2VerticalLines(width - 2, height - 2);
drawHorizontalLine(width);
}
void drawcone(int width) {
for (int i=0; i<(width/2 + width%2); i++) {
for (int j=width/2-i; j>0; j--) {
cout << " ";
}
drawOneRow(i*2-1);
}
}
void drawHorizontalLine(int numXs)
{
int count;
for (count = 0; count < numXs; count++) {
cout << "X";
}
cout << endl;
}
void draw2VerticalLines(int numSpaces, int numRows)
{
int rowCount;
for (rowCount = 0; rowCount < numRows; rowCount++) {
drawOneRow(numSpaces);
}
}
void drawOneRow(int numSpaces)
{
int spaceCount;
cout << "X";
if (numSpaces > 0) {
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++) {
cout << " ";
}
cout << "X";
}
cout << endl;
}
void getDimensions(int& width, int& height) {
cout << "Enter the width of the house" << endl;
cin >> width;
cout << "Enter the height of the house" << endl;
cin >> height;
}
应该很容易调试,它打印出了错误的房屋'屋顶'的空间数。问题应包含在while ( r != height)
块中试着用else if(c%2==1){cout << " ";}
打乱else {cout << " ";}
#include<iostream>
using namespace std;
void roof(int height);
void hLine(int num);
void vLine(int spaces, int rows);
void row(int spaces);
void dimensions(int& width, int& height);
void box(int width, int height);
int main()
{
int width;
int height;
dimensions(height, width);
roof(height);
hLine(width);
vLine(width - 2, height - 2);
hLine(width);
return 0;
}
void box(int width, int height)
{
hLine(width);
vLine(width - 2, height - 2);
hLine(width);
}
void roof(int height)
{
int base = height * 2;
int r = 0;
while ( r != height)
{
int c = 0;
while (c != base)
{
if(c==height-r || c==height+r)
cout << "*";
else
cout << " ";
c++;
}
cout << endl;
r++;
}
}
void hLine(int num)
{
int count;
for (count = 0; count < num*2+1; count++)
{
cout << "-";
}
cout << endl;
}
void vLine(int spaces, int rows)
{
int numrows;
for (numrows = 0; numrows < rows; numrows++)
{
row(spaces);
}
}
void row(int spaces)
{
int numspaces;
cout << "{";
for (numspaces = 0; numspaces < spaces*2+3; numspaces++)
{
cout << "$";
}
cout << "}" << endl;
}
void dimensions(int& width, int& height)
{
cout<<"Enter the width of the house"<<endl;
cin>>width;
cout<<"Enter the height of the house"<<endl;
cin>>height;
}
相关文章:
- GLFW程序不绘制三角形
- 在Qpaintevent处理程序外面绘制
- Gdiplus::P mathGradientBrush in a MFC 应用程序不绘制
- 使用 Python 的 Matplotlib 绘制C++程序中生成的数据
- 在MFC C++应用程序中在屏幕上绘制点的最快方法
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- OpenGL绘制程序在每次击键时都会更改
- Windows 8 现代 UI 应用程序和 DirectX.如何绘制文本
- 程序未使用QPainter绘制线条
- 在第三方应用程序的窗口顶部绘制
- 绘制移动图标,它一直是最顶端的鼠标光标和工作的全屏应用程序
- 如何使用C++程序中的gnuplot绘制图形
- 如何在C程序中直接更改视频内存映射来绘制像素(无库函数)
- SDL:程序运行正常;仅绘制两行像素
- Qt程序在Windows中的折线绘制上崩溃
- 如何使用OpenGL在Windows上的同一应用程序中的两个单独的3D窗口中进行绘制
- 与测试平台应用程序相比,调试绘制Box2D非常大
- 为什么我的OpenGL 3.3应用程序没有绘制任何内容
- 绘制从类返回的文本会导致程序停止而不会出现错误
- 在直接绘制全屏应用程序的顶部显示另一个应用程序