如何编写一个程序来读取整数方阵并确定它是否是幻方?
这是类广场,也是主要功能。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以基本上你必须编写并实现 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.
开发软件的一个好方法是分为 4 个阶段:需求、设计、编码、测试。
您可以一次性以小规模迭代的方式完成此操作,具体操作方法有很多变化,但这是完成编写程序任务的好方法。
就您而言,您已进入第 2 阶段。因此,请花点时间思考一下幻方是什么,并思考如何检查它。然后尝试将您的算法写入代码中。
A good way to go about software is in 4 phases: Requirements, Design, Coding, Testing.
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.