gpt4 book ai didi

c++ - 将网格写入文件

转载 作者:行者123 更新时间:2023-11-28 03:20:15 24 4
gpt4 key购买 nike

我正在编写代码以读取一组数字,然后将它们显示在屏幕上的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com