2017-04-03 78 views
-2

我正在嘗試創建一個sodoku程序,並且我需要找到一種方法來編輯我所有函數中的相同數組。我已經讀過,指針可以用於這個,但我不知道如何很好地使用指針。如何編輯原始數組遍歷所有函數C++

#include <iostream> 
#include <fstream> 
using namespace std; 

char upperCase(char letter) 
{ 
    int decVal = (int) letter; 
    if (decVal >= 97 && decVal <= 122) 
     decVal -= 32; 
    letter = (char) decVal; 
    return letter; 
} 

void readFile(int board[9][9]) 
{ 
    char fileName[256]; 
    int row = 0; 
    int col = 0; 
    cout << "Where is your board located? "; 
    cin >> fileName; 
    ifstream fin(fileName); 
    while (fin >> board[row][col]) 
    { 
     col++; 
     if (col == 9) 
     { 
     row++; 
     col = 0; 
     } 
    } 

} 

void displayOptions() 
{ 
    cout << "Options:" << endl; 
    cout << " ? Show these instructions" << endl; 
    cout << " D Display the board" << endl; 
    cout << " E Edit one square" << endl; 
    cout << " S Show the possible values for a square" << endl; 
    cout << " Q Save and quit" << endl; 
    cout << endl; 
} 

void displayBoard(int board[9][9]) 
{ 
    int row = 0; 
    int col = 0; 
    int rowNum = 1; 
    bool onSide = true; 
    cout << " A B C D E F G H I" << endl; 
    while (row <= 8) 
    { 
     if ((row == 3 || row == 6) && col == 0) 
     cout << " -----+-----+-----" << endl; 

     if (onSide) 
     { 
     cout << rowNum << " "; 
     onSide = false; 
     } 

     if (col == 3 || col == 6) 
     cout << "|"; 
     else 
     cout << " "; 

     if (board[row][col] == 0) 
     cout << " "; 
     else 
     cout << board[row][col]; 
     col++; 

     if (col == 9) 
     { 
     row++; 
     col = 0; 
     rowNum++; 
     onSide = true; 
     cout << endl; 
     } 
    } 
} 

void editSquare(int board[9][9]) 
{ 
    char cord[2]; 
    int row; 
    int col; 
    bool run = true; 
    bool fail = false; 
    while (run) 
    { 
     run = false; 
     fail = false; 
     cout << "What are the coordinates of the square: "; 
     cin >> cord; 

     if (cord[0] == 'A' || cord[0] == 'a') 
     row = 0; 
     else if (cord[0] == 'B' || cord[0] == 'b') 
     row = 1; 
     else if (cord[0] == 'C' || cord[0] == 'c') 
     row = 2; 
     else if (cord[0] == 'D' || cord[0] == 'd') 
     row = 3; 
     else if (cord[0] == 'E' || cord[0] == 'e') 
     row = 4; 
     else if (cord[0] == 'F' || cord[0] == 'f') 
     row = 5; 
     else if (cord[0] == 'G' || cord[0] == 'g') 
     row = 6; 
     else if (cord[0] == 'H' || cord[0] == 'h') 
     row = 7; 
     else if (cord[0] == 'I' || cord[0] == 'i') 
     row = 8; 
     else 
     { 
     cout << "ERROR: Square '"; 
     for (int i = 0; i < 2; i++) 
      cout << cord[i]; 
     cout << "' is invalid" << endl; 
     run = true; 
     fail = true; 
     } 

     if (!fail) 
     { 
     if (cord[1] == '1') 
      col = 0; 
     else if (cord[1] == '2') 
      col = 1; 
     else if (cord[1] == '3') 
      col = 2; 
     else if (cord[1] == '4') 
      col = 3; 
     else if (cord[1] == '5') 
      col = 4; 
     else if (cord[1] == '6') 
      col = 5; 
     else if (cord[1] == '7') 
      col = 6; 
     else if (cord[1] == '8') 
      col = 7; 
     else if (cord[1] == '9') 
      col = 8; 
     else 
     { 
      cout << "ERROR: Square '"; 
      for (int i = 0; i < 2; i++) 
       cout << cord[i]; 
      cout << "' is invalid" << endl; 
      run = true; 
     } 
     } 
    } 

    cord[0] = upperCase(cord[0]); 

    if (board[row][col] == 0) 
    { 
     int number; 
     cout << "What is the value at '"; 
     for (int i = 0; i < 2; i++) 
     cout << cord[i]; 
     cout << "': "; 
     cin >> number; 
     if (number >= 0 && number <= 9) 
     board[row][col] = number; 
     else 
     { 
     cout << "ERROR: Value '" << number << "' in square '"; 
     for (int i = 0; i < 2; i++) 
      cout << cord[i]; 
     cout << "' is invalid" << endl; 
     } 
    } 
    else 
    { 
     cout << "ERROR: Square '"; 
     for (int i = 0; i < 2; i++) 
     cout << cord[i]; 
     cout << "' is filled" << endl; 
    } 
} 

bool writeFile(int board[9][9]) 
{ 
    int row = 0; 
    int col = 0; 
    char fileName[256]; 
    cout << "What file would you like to write your board to: "; 
    cin >> fileName; 
    ofstream fout(fileName); 
    while (row <= 8) 
    { 
     fout << board[row][col] << " "; 
     col++; 
     if (col == 9) 
     { 
     row++; 
     col = 0; 
     fout << endl; 
     } 
    } 
    cout << "Board written successfully" << endl; 
    return false; 
} 

bool getCommand(int board[9][9]) 
{ 
    bool run = true; 
    char input; 
    cout << endl << "> "; 
    cin >> input; 

    input = upperCase(input); 

    if (input == '?') 
     displayOptions(); 
    else if (input == 'D') 
     displayBoard(board); 
    else if (input == 'E') 
     editSquare(board); 
    else if (input == 'S'); 
    else if (input == 'Q') 
    { 
     run = writeFile(board); 
    } 
    else 
     cout << "ERROR: Invalid command" << endl; 

    return run; 
} 

int main() 
{ 
    bool run = true; 
    int board[9][9]; 
    readFile(board); 
    displayOptions(); 
    displayBoard(board); 
    while (run) 
     run = getCommand(board); 
    return 0; 
} 
+0

您的代碼已經全部在同一塊板上運行的功能。請澄清你在問什麼。如果您的代碼不符合您的預期,請發佈您的預期結果,並解釋這與您觀察到的有何不同。 –

回答

-2

你不應該通過統一的維數組以這種方式:

bool writeFile(int board[9][9]); 

你一次初始化數組:

int board[9][9]; 

然後,你把它傳遞給方法,指定在2D結構的情況下,每行只有多長,也就是列數。這種方法的簽名是這樣的:

bool writeFile(int board[][9]); 

在WriteFile的,板是你通過在同一個陣列,而不是值的副本,你可能不習慣。 如果你使用c風格的數組,你是否喜歡使用指針,因爲所有的數組變量其實都是一個指向第一個數組元素的指針。例如:

//given 
int array[9]; 
//array can be passed into a method signed "void DoSomethingWithArray(int* array); 
//or a method signed "void DoSomethingWithArray(int array[]); 

並且在這種方法中所做的所有更改都會影響數組,原始聲明變量。

在通過二維數組「board」的情況下,參數板會衰減(以編程方式)指向int數組的指針,也就是指向構造的第一行的指針是指向構造的第一個元素(int)的指針。這個事實不應該公然重要,因爲你可以用下標來加入下標,以熟悉的方式訪問任何元素。但是知道這一點很重要,因爲一旦你知道他們到達那裏的方式是通過地址,而不是通過價值,你不會想知道如何操縱相同的數組。另外

一注,可能對一些觀點闡明:

array[1]; 
//is the same as 
*(array + 1); 
//take the address of the first array element, move index*sizeof(elementT) addresses forward, 
//and then dereference. [] is just a syntactical abstraction of this behavior. 
+0

其實我剛剛應用了你的建議,似乎解決了我的問題。謝謝您的幫助。 –

+0

您的問題可能在於您認爲您正在改變價值的地方。我將在main中聲明一個示例板,手動編輯一些值,並將其傳遞給您的寫入函數。如果它打印修改後的值,那麼你知道你的問題發生在你編輯數組的地方,而不是你的寫作功能。 – schulmaster

+0

@DanielCollins沒問題! – schulmaster