Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我如何编写一个程序来读取整数的平方数组并确定它是否是一个幻方?_C++_Magic Square - Fatal编程技术网

C++ 我如何编写一个程序来读取整数的平方数组并确定它是否是一个幻方?

C++ 我如何编写一个程序来读取整数的平方数组并确定它是否是一个幻方?,c++,magic-square,C++,Magic Square,这是类广场和主要功能 const int max_size = 9; class Square { public: void read(); //the square data is read bool is_magic(); // determin if the given square is a magic square private: int sum_row(int i); // returns the row su

这是类广场和主要功能

const int max_size = 9;
class Square {
   public:
      void read();     //the square data is read
      bool is_magic();     // determin if the given square is a magic square
   private:
      int sum_row(int i);     // returns the row sum of the ith row
      int sum_col(int i);      // returns the col sum of the ith row
      int sum_maindiag();   // returns the main the main diagonal sum
      int sum_other();     // returns the non-main diagonal sum
      int size;
      int grid[max_size][max_size];
};
void main()
{
      cout << "Magic Square Program" << endl << endl;
      cout << "Enter a square of integers:" << endl;
      Square s;
      s.read();
      if (s.is_magic()) cout << "That square is magic!" << endl;
      else cout << "That square is not magic." << endl;
}
const int max_size=9;
阶级广场{
公众:
void read();//读取方形数据
bool is_magic();//确定给定的正方形是否为幻方
私人:
int sum_row(int i);//返回第i行的行和
int sum_col(int i);//返回第i行的列和
int sum_maindiag();//返回主对角线和
int sum_other();//返回非主对角线和
整数大小;
整数网格[max_size][max_size];
};
void main()
{

cout因此基本上您必须编写和实现Square类。您详细介绍的这个类有两个公共方法,这意味着这些方法可以在任何地方调用。因此,您主要调用s.read()方法和s.is_magic()所以你声明了一个Square实例并调用它,然后用s.read()调用s中的read()方法,它是Square类的一个实例

square类中有一组私有函数可以帮助编写它。私有函数是只能在该类中调用的函数。因此,首先在square类中创建read方法。您应该使用sum_row()和sum_col()等辅助函数帮助编写读取函数。此外,私有类变量(如size)也可以在类内的函数之间使用


如果您有任何问题,请留下评论。但是如果您试图不亲自编写代码,这里没有人会为您编写代码。顺便说一句,我在这里交替使用了方法/函数,您可以根据需要查看它们之间的区别。

一个很好的软件开发方法分为四个阶段:需求、设计、编码,测试

  • 要求。你真正想做的是什么?在你的情况下,检查魔方
  • 设计。你想如何做到这一点?在编写代码之前规划你的软件。用通俗易懂的英语(或任何语言)写出来
  • 编码。现在你有了一个计划,写下你的代码
  • 测试。测试你的软件,确保它符合你的计划
  • 你可以在一次的小迭代中完成这项工作,关于如何完成这项工作,有很多不同的方法,但这是一种很好的方法来完成编写程序的任务


    在你的例子中,你已经到了第二阶段。所以,花点时间想想什么是幻方,想想如何去检查它。然后试着把你的算法写进代码中。

    要正确格式化你的代码,把所有东西缩进4个或更多的空格。或者,选择你的代码,然后按
    {}
    按钮(这将为您缩进)。学习使用预览和设置代码格式。另外请注意,我们将帮助您学习,但我们不会为您做家庭作业。到底是什么问题?输入/输出?检查是否有魔法的算法?为什么不查找什么是幻方?您提供的类提供了确定输入是否为一所需的所有信息。是否应该不需要超过10行这样做。我知道这是什么,我对如何继续进行感到困惑。我刚开始做C++,我不完全理解它。我想我明白你说的话,我很抱歉我只是想让别人做这件事,但我没有这个经验。