c++ - 将网格写入文件

标签 c++

我正在编写代码以读取一组数字,然后将它们显示在屏幕上的 4x4 网格中,然后程序将确定它是否是幻方。我的问题是如何让用户将数字输入到 readData 方法中显示的文件中,以便在调用该方法时显示用户网格。

此外,文件 data.txt 存储在哪里?

谢谢

#include<iostream>
#include<fstream>
using namespace std;
const int dimension = 4;   // dimension for the array
typedef int Sq[dimension] [dimension];  // declare vector for type names

void ReadData(Sq square ) // read data from file
{  ifstream inFile;
  char fileName[13] = "data.txt";
  inFile.open (fileName);  // open file for reading                
  for (int r = 0; r < dimension; r++)   // row loop
      for ( int c = 0; c < dimension; c++)  // column loop
            inFile >> square[r][c]; // read data into matrix
      inFile.close( ); // close the file                
}

void Display ( const Sq square ) // display matrix
{ cout << " Magic Square Program " << endl << endl;
 for (int r = 0; r < dimension; r++)
   { for ( int c = 0; c < dimension; c++)
        { cout.width(6);  //set output width to 6 characters
           cout << square[r][c] << "\t ";  // display numbers
            }
       cout << endl;
    }    
 }
bool magicSquare( Sq square)  // add rows, columns, and diagonals
{ int firstSum = 0, sum;
  bool magic = true;
  for (int r = 0; r < dimension; r++) // add 1st column for a comparison
      firstSum += square[r][1]; 
   for (int r = 1; r < dimension; r++) // row loop first when adding rows
      { sum = 0;
         for ( int c = 0; c < dimension; c++)
             sum += square[r][c];  // add row
         if ( sum != firstSum)  // check for magic failure
              return (false);  // not magic
       }

     for ( int c = 0; c < dimension; c++)   // column loop first when adding columns
       { sum = 0;
          for (int r = 0; r < dimension; r++)
              sum += square[r][c];   // add columns
              if ( sum != firstSum)  // check for magic failure
               return (false);  // not magic
        }
     sum = 0;
     for (int r = 0; r < dimension; r++)  
          sum += square[r][r];   // add front diagonal
        if ( sum != firstSum)  // check for magic failure
           return (false);  // not magic
      sum = 0;
      for (int r = 0; r < dimension; r++)  
          sum += square[r][dimension - r - 1];   // add back diagonal
         if ( sum != firstSum)  // check for magic failure
          return (false);  // not magic
      else
          return (true);
      }  // end magicSquare function

int main( )
{
  Sq square;
 ReadData( square);  // read data from file
 Display ( square);  // display matrix
 if ( magicSquare(square) )   // check for magic property
    cout << "\n This Square is Magic \n " << endl;
 else
    cout << "\n This Square is Not Magic \n " << endl;
 system("Pause");
 return(0);
 }

最佳答案

最简单的方法是让程序将文件名作为命令行参数。 Main 应该看起来像这样,其中 argc 是参数的数量,argv[] 是指向它们的 char 指针数组 (argv[0] 始终是可执行文件的名称)。参见 What does int argc, char *argv[] mean?

int main(int argc, char * argv[])

那么,你做

if (argc == 2)
{
   ReadData(square, argv[1]);
   ...
}
else
  ...

ReadData 看起来像这样:-

void ReadData(Sq &square, const std::string &filename) // read data from file
{
  ifstream inFile;
  inFile.open (filename);  // open file for reading   

注意!您需要将 square 作为引用参数 (&square),否则您的输入数据将被忽略。

关于c++ - 将网格写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660534/

相关文章:

c++ - 通过 std::tuple<...> 实现 map() 和 each() - 将索引作为模板参数传递给仿函数

c++ - std::is_same - 从 integral_constant 继承函数的用例

c++ - 给定输入生成真值表?

c++ - 两个大型稀疏 vector 上的按位运算符没有循环?

c++ - 使用 std::Optional 时的命名返回值优化

c++ - 如果标准 C++ 库函数为 char 值返回 int,是否需要强制转换?

c++ - OpenCV 错误 : Assertion failed in cvAdaptiveThreshold

c++ - opengl中灯光的区别

c++ - 将数组中的某些整数相加

c++ - 浮点分辨率似乎比它应该的更受限制