C++ 二维阵列幻方
我不能让它工作。我需要构造一个名为ThreeBy3的类,它管理一个3乘3的整数数组。类需要有方法来显示其数组,并确定数组是否符合幻方的条件。C++ 二维阵列幻方,c++,arrays,C++,Arrays,我不能让它工作。我需要构造一个名为ThreeBy3的类,它管理一个3乘3的整数数组。类需要有方法来显示其数组,并确定数组是否符合幻方的条件。 从下面的文件中读取3 x 3数组。对于每一次读取,创建一个ThreeBy3实例,然后显示数组,并在其后显示一行输出,说明数组是否为幻方。当实例不再需要时,不要忘记删除它 最后的动态指针有问题。多谢各位 这是我的代码: #include <iostream> #include <fstream> #include <iomani
从下面的文件中读取3 x 3数组。对于每一次读取,创建一个ThreeBy3实例,然后显示数组,并在其后显示一行输出,说明数组是否为幻方。当实例不再需要时,不要忘记删除它 最后的动态指针有问题。多谢各位 这是我的代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <istream>
using namespace std;
using std::cout;
using std::endl;
using std::istream;
{
class ThreeBy3
{
static const int SQ = 3;
static const int rows[][3][2];
static const int rowCount;
int square[SQ][SQ];
int sum;
public:
ThreeBy3(int ms[][3]);
bool isMagicSquare();
void display(std::ostream& os);
};
const int ThreeBy3::rows[][3][2] = {
{ { 0, 0 }, { 0, 1 }, { 0, 2 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 } },
{ { 0, 0 }, { 1, 0 }, { 2, 0 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 0, 2 }, { 1, 2 }, { 2, 2 } },
{ { 0, 0 }, { 1, 1 }, { 2, 2 } },
{ { 0, 2 }, { 1, 1 }, { 2, 0 } },
};
const int ThreeBy3::rowCount = (sizeof rows) / (sizeof rows[0]);
bool ThreeBy3::isMagicSquare()
{
for (int i = 0; i < 8; i++)
{
int x = 0;
for (int j = 0; j < 3; j++)
x += square[rows[i][j][0]][rows[i][j][1]];
if (x != sum)
return false;
}
return true;
}
void ThreeBy3::display(std::ostream& os)
{
const int W = 2;
os << std::setprecision(1) << std::fixed;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << square[i][j] << " ";
cout<< endl;
}
}
}
int main ()
{
static const int SQ = 3;
std::ifstream ifs;
std::string path = "path-to/msq2.txt";
int iv;
int ix, jx;
int ms[3][3] = { { 0 }, { 0 }, { 0 } };
int rcd = 0;
ThreeBy3 *msq;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
while (!ifs.eof())
{
bool readThreeBy3(std::istream& strm, int ms[][3]);
{
int ix, jx, iv;
for (ix = 0; ix < 3; ++ix)
{
for (jx = 0; jx < 3; ++jx)
{
strm >> iv;
if (strm.eof())
return false;
ms[ix][jx] = iv;
}
}
return true;
}
++rcd;
if (msq->isMagicSquare())
{
cout << " is a magic square" << endl;
}
else
{
cout << " is NOT a magic square" << endl;
}
}
eof:
ifs.close();
return 0;
}
我改变了几件事: 在C++中,你不能在另一个函数中定义一个局部函数,所以我把它移到全局范围。但是没有理由有一个自由函数——如果它是在内部数组上操作的成员函数,那么我们根本不需要main中的数组。所以我把它作为一个成员函数 另一件事是,您不需要指向ThreeBy3的指针,因为没有理由动态分配它——它的使用寿命永远不会超过分配给它的函数的使用寿命。所以我把它变成了一个局部变量 此外,您的循环不应该基于文件的结尾-许多其他事情可能会导致它完成读取数据。所以我把它改成了检查其他终端条件 我用名称空间std删除了
代码>虽然在课堂上很有用,但这不是好的编程实践
以下是我将如何更改它以使其运行:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <istream>
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::ifstream;
using std::string;
class ThreeBy3
{
static const int SQ = 3;
static const int sum = 15;
static const int rows[][3][2];
static const int rowCount;
int square[SQ][SQ];
public:
bool isMagicSquare();
void display(ostream& os);
bool read(istream& os);
};
const int ThreeBy3::rows[][3][2] = {
{ { 0, 0 }, { 0, 1 }, { 0, 2 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 } },
{ { 0, 0 }, { 1, 0 }, { 2, 0 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 0, 2 }, { 1, 2 }, { 2, 2 } },
{ { 0, 0 }, { 1, 1 }, { 2, 2 } },
{ { 0, 2 }, { 1, 1 }, { 2, 0 } },
};
const int ThreeBy3::rowCount = (sizeof rows) / (sizeof rows[0]);
bool ThreeBy3::isMagicSquare()
{
for (int i = 0; i < rowCount; i++)
{
int x = 0;
for (int j = 0; j < SQ; j++)
x += square[rows[i][j][0]][rows[i][j][1]];
if (x != sum)
return false;
}
return true;
}
void ThreeBy3::display(ostream& os)
{
os << std::setprecision(1) << std::fixed;
for (int i = 0; i < SQ; ++i)
{
os << "\n";
for (int j = 0; j < SQ; ++j)
os << square[i][j] << " ";
}
}
bool ThreeBy3::read(istream& is)
{
for (int ix = 0; ix < SQ; ++ix)
for (int jx = 0; jx < SQ; ++jx)
if (!(is >> square[ix][jx]))
return false;
return true;
}
int main ()
{
string path = "path-to/msq2.txt";
ifstream ifs;
ThreeBy3 msq;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
while (msq.read(ifs))
{
msq.display(cout);
if (msq.isMagicSquare())
{
cout << "is a magic square" << endl;
}
else
{
cout << "is NOT a magic square" << endl;
}
}
ifs.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::endl;
使用std::istream;
使用std::ostream;
使用std::ifstream;
使用std::string;
三比三班
{
静态常数int SQ=3;
静态常数int和=15;
静态常量int行[][3][2];
静态常量int行计数;
国际广场[SQ][SQ];
公众:
bool isMagicSquare();
无效显示(ostream&os);
bool-read(istream&os);
};
常量int ThreeBy3::行[][3][2]={
{ { 0, 0 }, { 0, 1 }, { 0, 2 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 } },
{ { 0, 0 }, { 1, 0 }, { 2, 0 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 0, 2 }, { 1, 2 }, { 2, 2 } },
{ { 0, 0 }, { 1, 1 }, { 2, 2 } },
{ { 0, 2 }, { 1, 1 }, { 2, 0 } },
};
const int ThreeBy3::rowCount=(sizeof rows)/(sizeof rows[0]);
boolThreeby3::isMagicSquare()
{
对于(int i=0;i 操作系统这不是有效的代码-它无法编译。例如,函数readThreeBy3在另一个函数中…另外,如果您在最后说动态指针有什么问题,这会很有帮助。我不知道如何让它编译是个问题。我不知道应该将该函数放在哪里。对于dy也是如此namic pointer*msg我不知道为什么它不能被识别,我应该如何声明它
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <istream>
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::ifstream;
using std::string;
class ThreeBy3
{
static const int SQ = 3;
static const int sum = 15;
static const int rows[][3][2];
static const int rowCount;
int square[SQ][SQ];
public:
bool isMagicSquare();
void display(ostream& os);
bool read(istream& os);
};
const int ThreeBy3::rows[][3][2] = {
{ { 0, 0 }, { 0, 1 }, { 0, 2 } },
{ { 1, 0 }, { 1, 1 }, { 1, 2 } },
{ { 2, 0 }, { 2, 1 }, { 2, 2 } },
{ { 0, 0 }, { 1, 0 }, { 2, 0 } },
{ { 0, 1 }, { 1, 1 }, { 2, 1 } },
{ { 0, 2 }, { 1, 2 }, { 2, 2 } },
{ { 0, 0 }, { 1, 1 }, { 2, 2 } },
{ { 0, 2 }, { 1, 1 }, { 2, 0 } },
};
const int ThreeBy3::rowCount = (sizeof rows) / (sizeof rows[0]);
bool ThreeBy3::isMagicSquare()
{
for (int i = 0; i < rowCount; i++)
{
int x = 0;
for (int j = 0; j < SQ; j++)
x += square[rows[i][j][0]][rows[i][j][1]];
if (x != sum)
return false;
}
return true;
}
void ThreeBy3::display(ostream& os)
{
os << std::setprecision(1) << std::fixed;
for (int i = 0; i < SQ; ++i)
{
os << "\n";
for (int j = 0; j < SQ; ++j)
os << square[i][j] << " ";
}
}
bool ThreeBy3::read(istream& is)
{
for (int ix = 0; ix < SQ; ++ix)
for (int jx = 0; jx < SQ; ++jx)
if (!(is >> square[ix][jx]))
return false;
return true;
}
int main ()
{
string path = "path-to/msq2.txt";
ifstream ifs;
ThreeBy3 msq;
ifs.open(path);
if (!ifs)
{
cout << "file not found:" << path << endl;
return -1;
}
while (msq.read(ifs))
{
msq.display(cout);
if (msq.isMagicSquare())
{
cout << "is a magic square" << endl;
}
else
{
cout << "is NOT a magic square" << endl;
}
}
ifs.close();
return 0;
}