English 中文(简体)
如何编写一个程序来读取整数的平方数组,并确定它是否为幻方?
原标题:How do I write a program that reads a square array of integers and determines whether or not it is a Magic square on not?

这是阶级广场的主要功能。

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;
}
问题回答

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

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

如果你有任何问题,请留下评论。但是,如果你试图摆脱自己编写代码的困境,这里没有人会为你编写代码。顺便说一句,我在这里可互换地使用了方法/函数,如果你愿意的话,你可以查找其中的区别。

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

  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.

你可以在小的迭代中同时完成这项工作,关于如何完成这项任务有很多变化,但这是一个很好的方法来完成编写程序的任务。

在你的情况下,你进入了第二阶段。因此,花点时间思考什么是魔术方块,并思考如何检查它。然后尝试使用您的算法并将其写入代码。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签