#include <iostream>
using namespace std;
class magic
{
public:
void getdata();
void setfirstmagic(int a,int b);
void generatemagic();
void printmagic();
private:
int m[4][4];
int step;
int first;
int sum;
};
void magic::getdata()
{
m[0][0]=first;
int c=1;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
m[i][j]=first+(c-1)*step;
c++;
}
}
void magic::setfirstmagic(int a,int b)
{
first=a;
step=b;
}
void magic::generatemagic()
{
sum=m[0][0]+m[3][3];
for(int i=0,j=0;i<4,j<4;i++,j++)
m[i][j]=sum-m[i][j];
for(int i=0,j=3;i<4,j>=0;i++,j--)
m[i][j]=sum-m[i][j];
}
void magic::printmagic()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cout<<" "<<m[i][j];
cout<<endl;
}
}
int main()
{
magic m1;
int a,b;
cout<<"输入第一个数据"<<endl;
cout<<a;
cout<<"输入间隔"<<endl;
cout<<b;
m1.setfirstmagic(a,b);
m1.getdata();
cout<<"初始魔方"<<endl;
m1.printmagic();
m1.generatemagic();
cout<<"最终魔方"<<endl;
m1.printmagic();
system("pause");
return 0;
}
using namespace std;
class magic
{
public:
void getdata();
void setfirstmagic(int a,int b);
void generatemagic();
void printmagic();
private:
int m[4][4];
int step;
int first;
int sum;
};
void magic::getdata()
{
m[0][0]=first;
int c=1;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
m[i][j]=first+(c-1)*step;
c++;
}
}
void magic::setfirstmagic(int a,int b)
{
first=a;
step=b;
}
void magic::generatemagic()
{
sum=m[0][0]+m[3][3];
for(int i=0,j=0;i<4,j<4;i++,j++)
m[i][j]=sum-m[i][j];
for(int i=0,j=3;i<4,j>=0;i++,j--)
m[i][j]=sum-m[i][j];
}
void magic::printmagic()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cout<<" "<<m[i][j];
cout<<endl;
}
}
int main()
{
magic m1;
int a,b;
cout<<"输入第一个数据"<<endl;
cout<<a;
cout<<"输入间隔"<<endl;
cout<<b;
m1.setfirstmagic(a,b);
m1.getdata();
cout<<"初始魔方"<<endl;
m1.printmagic();
m1.generatemagic();
cout<<"最终魔方"<<endl;
m1.printmagic();
system("pause");
return 0;
}