如何编写一个程序来读取整数方阵并确定它是否是幻方?

发布于 2024-10-17 00:56:14 字数 890 浏览 0 评论 0原文

这是类广场,也是主要功能。

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

This is the class square and the main function.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-10-24 00:56:14

所以基本上你必须编写并实现 Square 类。您详细介绍的方法有两个公共方法,这意味着可以在任何地方调用这些方法。因此,在您的 main 中,您将调用 s.read() 方法和 s.is_magic() 来访问该类。因此,您声明 Square 的一个实例并将其命名为 s,然后使用 s.read() 调用 s 中的 read() 方法,该 s 是 square 类的实例。

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

如果您有任何疑问,请发表评论。但是,如果您试图摆脱自己编写代码的麻烦,那么这里没有人会为您编写代码。顺便说一下,我在这里互换使用了方法/函数,如果您愿意,您可以查看它们之间的区别。

So basically you have to write and implement the Square class. The one that you've detailed has two public methods which means that those methods can be called anywhere. Therefore in your main you're calling the s.read() method and s.is_magic() to access the class. So you declare an instance of Square and call it s and then you use s.read() to call the read() method within s which is a instance of the class square.

You have a bunch of private functions in the square class to help write it. Private functions are functions that can only be called within that class. So start by making the read method within the square class. You should use the helper functions like sum_row() and sum_col() to help write your read function. Also the private class vars like size are able to be used across functions within the class.

If you have any questions leave a comment. But if you're trying to get out of writing the code yourself no one here is going to write it for you. By the way I've used methods/functions interchangeably here, you can look up what the difference is if you want.

-残月青衣踏尘吟 2024-10-24 00:56:14

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

  1. 要求。你真正想做的是什么?对于您的情况,请检查 Magic Squares。
  2. 设计。你想怎么做?在编写代码之前规划您的软件。用简单的英语(或任何语言)写下来。
  3. 编码。现在您有了计划,就可以编写代码了。
  4. 测试。测试您的软件以确保它按照您的计划进行。

您可以一次性以小规模迭代的方式完成此操作,具体操作方法有很多变化,但这是完成编写程序任务的好方法。

就您而言,您已进入第 2 阶段。因此,请花点时间思考一下幻方是什么,并思考如何检查它。然后尝试将您的算法写入代码中。

A good way to go about software is in 4 phases: Requirements, Design, Coding, Testing.

  1. Requirements. What is it that you actually want to do? In your case, check for Magic Squares.
  2. Design. How do you want to do this? Plan your software before you write code. Write it out in plain English (or whatever language).
  3. Coding. Now that you have a plan, write your code.
  4. Testing. Test your software to make sure it does what you set out to do.

You can do this in small iterations, all at once, there's a lot of variations on how to go about this, but it's a good way to approach the task of writing a program.

In your case, you're up to phase 2. So take the time to think about what a Magic Square is, and think of how to go about checking for it. Then try to take your algorithm and write it into code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文