初学者c++绘制房屋程序



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;
}

最新更新