当前位置: 首页 > 工具软件 > Square Menu > 使用案例 >

[数据结构]Magic-Square

能修谨
2023-12-01
//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();
}

 类似资料: