2017-10-28 17 views
1

我想繪製圖上的幾個點,但我不確定如何去做。基本上我的目標是使用二維數組來繪製品脫。使用2維數組來繪製圖形

這裏是圖形應該是什麼樣子:

 1 2 3 4 5 6 7 8 9 10 
    1        
    2       C 
    3  F      
    4  F M F     
    5  F      
    6        
    7     X   
    8        
    9     B B B  
    10      C 

這裏是點到圖形(輸入文件):

Rows 10 
Cols 10 

M 4,3 
C 2,9 
X 7,7 
B 9,6 
B 9,7 
B 9,8 
C 10,8 
F 3,3 
F 5,3 
F 4,2 
F 4,4 

我已經這樣如果遇到使用if語句嘗試輸入文件中的字母會自動將其記錄到文件中。你可以給我一些關於如何做到這一點的想法。我不是要你們寫所有的代碼。謝謝。

+1

你究竟想要做什麼? – DimChtz

+1

具體問題是什麼?目前還不清楚問題是什麼。 – Ron

+0

對不起,如果我不清楚。給出的要點是一個.txt文件。行和列顯示網格的大小。而像「M」這樣的字母就是需要在該網格上繪製的圖形。 「M」的點是4,3(x,y)。所以它需要顯示在圖中所示的圖中。所以我的問題是,用給定的點,繪製一個網格,繪製所示的所有點。我會怎麼做? – Bane

回答

-1

你可以整理出文件數據的解析自己(或者是更好的做單獨售票)。至於打印出來的數據:

#include <iostream> 
#include <vector> 

class Graph 
{ 

private: 

    std::vector<char> _graph; 
    size_t _rows; 
    size_t _cols; 
    char _ch; 

public: 

    Graph(size_t rows, size_t cols, char ch = '.') : _rows(rows), _cols(cols), _ch(ch) { _graph.assign((_rows + 1) * (_cols + 1), _ch); } 

    bool PutChar(char c, size_t row, size_t col) 
    { 
     if (row > _rows || col > _cols || row == 0 || col == 0) 
      return false; 

     _graph[col + row * _rows] = c; 
     return true; 
    } 

    char GetChar(size_t row, size_t col) 
    { 
     if (row > _rows || col > _cols || row == 0 || col == 0) 
      return _ch; 

     return _graph[col + row * _rows]; 
    } 

    void PrintGraph() 
    { 
     printf("%3c", 0x20); 

     for (size_t col = 1; col <= _cols; ++col) 
      printf("%3zu", col); 

     printf("\n"); 

     for (size_t row = 1; row <= _rows; ++row) 
     { 
      printf("%3zu", row); 

      for (size_t col = 1; col <= _cols; ++col) 
      { 
       printf("%3c", GetChar(row, col)); 
      } 

      printf("\n"); 
     } 
    } 
}; 

int main() 
{ 

    Graph graph(10, 10); 

    graph.PutChar('M', 4, 3); 
    graph.PutChar('C', 2, 9); 
    graph.PutChar('X', 7, 7); 
    graph.PutChar('B', 9, 6); 
    graph.PutChar('B', 9, 7); 
    graph.PutChar('B', 9, 8); 
    graph.PutChar('C', 10, 8); 
    graph.PutChar('F', 3, 3); 
    graph.PutChar('F', 5, 3); 
    graph.PutChar('F', 4, 2); 
    graph.PutChar('F', 4, 4); 

    graph.PrintGraph(); 

    return 0; 
} 

打印:

enter image description here

我感到奇怪你的任務是,X是Y和Y是X和兩個開始1.這使得它尷尬,不知道爲什麼它需要這樣。

編輯:剛纔意識到上面並沒有完全按照要求使用二維數組,但模擬它。

2

基本上我的目標是繪製使用二維數組的點。

  • 你需要做的第一件事情就是從文本文件
  • 提取數據對於數據的每一行,將它們分爲:symbolxy

    e.g: 'M', 4, 3 corresponds to (symbol, x, y) 
    
  • 根據提取的數據更新您的二維陣列

  • matrix[x][y] = symbol;

你當然也可以抵消xy 1,因爲你的數組索引0開始