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

时间:2016-08-13 03:09:36

标签: c++ function void

c ++上的新内容如果我有任何错误的条款,请不要嘲笑我。我正在尝试编写一个程序,将绘制询问用户的单位,然后使用函数绘制房子。我遇到的问题出在// loop will break when v = 0 var v = 10 while (v) { if (v) do something... v--; } 函数中。到目前为止,这是我的进展。

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 函数的答案。谢谢你的时间!

3 个答案:

答案 0 :(得分:0)

应该相当容易调试,它打印出错误数量的空间用于房屋的“屋顶”。 问题应该包含在while ( r != height)块中 尝试使用else {cout << " ";}

弄乱else if(c%2==1){cout << " ";}

答案 1 :(得分:0)

这是一个解决方案。但是,只有宽度为奇数时,圆锥才会对称。

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

}

答案 2 :(得分:0)

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