2012-12-03 175 views
1

我有我的課:c + +繪製矩形位置

class Rectangle : public TwoDim 
{ 
public: 
    void fun() {}; 
    void printShape(); 
    Rectangle(int x1, int y1, int height1, int width1) 
    { 
     x = x1; 
     y = y1; 
     height = height1; 
     width = width1; 

    } 

}; 

而且功能打印:

void Rectangle::printShape() 
{ 
    { 

     cout << "+"; 
     for (int i = 0; i < width - 2; i++) 
     { 
      cout << "-"; 
     } 
     cout << "+\n"; 

     for (int i = 0; i < height - 2; i++) 
     { 
      cout << "|"; 
      for (int j = 0; j < +width - 2; j++) 
      { 
       cout << " "; 
      } 
      cout << "|\n"; 
     } 

     cout << "+"; 
     for (int i = 0; i < width - 2; i++) 
     { 
      cout << "-"; 
     } 
     cout << "+\n"; 
    } 

} 

如何更改我的功能,這樣我會畫從點開始的矩形(x, y)

非常感謝

+0

點(x,y)相對於什麼?你的意思是你想添加一些''\ n「'和'」「'字符來改變輸出嗎?或者,如果您想在終端或窗口上找到特定位置,則需要使用特定於操作系統的庫,而不僅僅是核心C++。 – aschepler

+0

聽起來像功課。你有沒有嘗試過?得到了一些我們可以看不到的代碼? – dgnorton

+0

@dgnorton我已經嘗試過,這是一項功課,但我太喜歡這樣做:( – Gan

回答

1

我會用實際印刷之前輸出ystd::endl s,然後有效地打印每一行之前輸出x" "開始。

+1

您不需要清空輸出'y'次。 – aschepler

+0

@aschepler除非通過分析證明它不可行,否則我寧願將它們串聯在一起。「 –

+0

@Luchian:爲什麼?(我應該認爲你會使用''' \ n'',字符而不是字符串) –