如何在我的所有函数C ++中编辑原始数组

时间:2017-04-03 03:57:42

标签: c++

我正在尝试创建一个sodoku程序,我需要找到一种方法来编辑所有函数中的相同数组。我已经读过指针可以用于此但我真的不知道如何使用指针。

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

1 个答案:

答案 0 :(得分:-2)

您不应该以这种方式传递统一维数组:

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

你初始化你的数组ONCE:

int board[9][9];

然后,将它传递给一个方法,在2D构造的情况下,指定每行的长度,也就是列数。这样的方法签名看起来像这样:

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

在writeFile中,board是您传入的相同数组,而不是您可能习惯的值的副本。 如果你使用c样式数组,你是否喜欢使用指针,因为所有数组变量实际上都是IS指向第一个数组元素的指针。例如:

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

并且在这种方法中所做的所有更改都会影响数组,即原始声明变量。

在传递2D数组的情况下,在你的情况下,“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.