[数据结构]Magic-Square

//main.cpp

#include<iostream>
#include<string>
#include"square.h"
using namespace std;
void main() {
	cout << "***********************************Magic-Square************************************" << endl;
	cout << "Menu:" << endl;
	cout << "1.Judge a Magic-Square." << endl;
	cout << "2.Generate a standard Magic-Square." << endl;
	cout << "Please enter the number to enter the programme." << endl;
	square example;
	int key;
	lable:cin >> key;
	switch (key)
	{
	case 1:
		example.JudgeProgramme();
		break;
	case 2:
		example.GenerateProgramme();
		break;
	default:
		cout << "Please enter int number 1 or 2." << endl;
		goto lable;
		break;
	}
}

//square.h

#pragma once
const int maxrow = 30, maxcol = 30;
class square
{
public:
	void JudgeProgramme();
	void GenerateProgramme();
	square() {
		for (int i = 0; i < maxrow; i++)
			for (int j = 0; j < maxcol; j++)
				chessBoard[i][j] = 0;
		sum = 0;
		size = 0;
	}
private:
	void Initialize();
	void Judge();
	void Gengrate();
	void Print();
	int chessBoard[maxrow][maxcol];
	int sum;
	int size;
};

//square.cpp

#include<iostream>
#include<string>
#include"square.h"
using namespace std;
//***********************************************************************************
void square::Initialize() {
	cout << "Please enter the size of the square.(such as 1,2,3···30)" << endl;
lable:cin >> size;
	if (size > 30) {
		cout << "Please enter the size must under 30." << endl;
		goto lable;
	}
	else cout << "Please enter the number of each bottom.(Input according to the line.)" << endl;
	for (int i = 0; i < size; i++)
		for (int j = 0; j < size; j++)
			cin >> chessBoard[i][j];
}
//************************************************************************************
void square::Judge() {
	int first_sum = 0;
	for (int i = 0; i < size; i++)
		first_sum += chessBoard[0][i];
	for (int i = 0; i < size; i++) {				//行相等
		for (int j = 0; j < size; j++)
			sum += chessBoard[i][j];
		if (first_sum != sum) {
			cout << "It's not a magic square." << endl;
			return;
		}
		else sum = 0;
		for (int j = 0; j < size; j++)		//列相等
			sum += chessBoard[j][i];
		if (first_sum != sum) {
			cout << "It's not a magic square." << endl;
			return;
		}
		else sum = 0;
		for (int j = 0; j < size; j++)		//主对角线相等
			sum += chessBoard[j][j];
		if (first_sum != sum) {
			cout << "It's not a magic square." << endl;
			return;
		}
		else sum = 0;
		for (int j = 0; j < size; j++)		//次对角线相等
			sum += chessBoard[j][size-1-j];
		if (first_sum != sum) {
			cout << "It's not a magic square." << endl;
			return;
		}
		else sum = 0;
	}
	cout << "It's a magic square." << endl;
}
//******************************************************************************
void square::JudgeProgramme() {
	Initialize();
	Judge();
}
//******************************************************************************
void square::Gengrate() {
	cout << "Please enter the size of the square.(Require use odd number)" << endl;
lable:cin >> size;
	if (size%2==0) {
		cout << "Please enter an odd number." << endl;
		goto lable;
	}
	int row = 0, col = size / 2;
	int last_row = row, last_col = col;
	for (int i = 1; i <= size*size; i++)
	{
		bool condition = false;
		chessBoard[row][col] = i;
		last_row = row, last_col = col;
		row--;
		col++;
		while (!condition) {			//修改下一方格位置
			if (row == -1) {
				row = size - 1;
				continue;
			}
			if (col == size){
				col = 0;
				continue;
			}
			if (chessBoard[row][col] != 0){
				row = last_row + 1;
				col = last_col;
				continue;
			}
			condition = true;
		}
	}
}
//***************************************************************************
void square::Print()
{
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++)
			cout << chessBoard[i][j]<<'\t';
		cout << endl;
	}
}
//****************************************************************************
void square::GenerateProgramme() {
	Gengrate();
	Print();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值