返回顶部

收藏

MyXls源代码单文件版,适合项目开发

更多

MyXls源代码单文件版

生成任意阶幻方

using System;
using System.IO;
using org.in2bits.MyXls;

public class MagicSquare
{
    int n;
    int[,] array;
    public MagicSquare(int a)
    {
        n = a;
        array = new int[a, a];
    }
    public int this[int a, int b]
    {
        get
        {
            return array[a, b];
        }
        set
        {
            array[a, b] = value;
        }
    }
    public void printhf(Cells cells)
    {
        if (n > 0)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                    cells.Add(j+1,i+1,array[i, j]);
            }
        }
        else
            return;
    }
    public void IsCorrect()
    {
        int sum_x, sum_y, sum_diag1, sum_diag2;
        sum_diag1 = 0;
        sum_diag2 = 0;
        int sum_corect=n*(1+n*n)/2;
        for(int i=0;i<n;i++)
        {
            sum_x = 0;
            sum_y = 0;
            for (int j = 0; j < n; j++)
            {
                sum_x += this.array[i, j];
                sum_y += this.array[j, i];
            }
            if (sum_x != sum_corect || sum_y != sum_corect)
            {
                Console.WriteLine("幻方错误:行列和错误");
                break;
            }
            sum_diag1 += this.array[i, i];
            sum_diag2 += this.array[i, n-1-i];
        }
        if (sum_diag1 != sum_corect || sum_diag2 != sum_corect)
            Console.WriteLine("幻方错误:对角线和错误");
        else
            Console.WriteLine("经验证,该幻方正确");
    }
}

public class ele
{
    public int number;
    public int x;
    public int y;
    public ele()
    {
        number = 0;
        x = 0;
        y = 0;
    }
    public ele(int n, int array_x, int array_y)
    {
        number = n;
        x = array_x;
        y = array_y;
    }
}

public class test
{
    public static void Main()
    {
        XlsDocument xls = new XlsDocument();
        Worksheet PrimaryNumber = xls.Workbook.Worksheets.Add("MagicSquare");//状态栏标题名称
        Cells cells = PrimaryNumber.Cells;
        int n = 100;
        //string buff = Console.ReadLine();
        ele[] my_element = new ele[n * n];
        MagicSquare myMagicSquare = new MagicSquare(n);
        int array_x, array_y;
        if (n % 2 == 1)
        {
            array_x = n;
            array_y = (n + 1) / 2;
            for (int i = 1; i <= n * n; i++)
            {
                if (i == 1)
                    my_element[i - 1] = new ele(i, array_x, array_y);
                else
                {
                    if (i % n == 1)
                    {
                        array_x -= 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                    else
                    {
                        array_x += 1;
                        array_y += 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                }
            }
            for (int i = 0; i < n * n; i++)
            {
                while (my_element[i].x > n)
                    my_element[i].x -= n;
                while (my_element[i].y > n)
                    my_element[i].y -= n;
            }
            for (int i = 0; i < n * n; i++)
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            myMagicSquare.printhf(cells);
            myMagicSquare.IsCorrect();
        }
        else if (n % 4 == 2)
        {
            array_x = n / 2;
            array_y = ( n / 2 + 1) / 2;
            for (int i = 1; i <= (n / 2) * (n / 2); i++)
            {
                if (i == 1)
                    my_element[i - 1] = new ele(i, array_x, array_y);
                else
                {
                    if (i % (n/2) == 1)
                    {
                        array_x -= 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                    else
                    {
                        array_x += 1;
                        array_y += 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                }
            }
            for (int i = 0; i < (n / 2) * (n / 2); i++)
            {
                while (my_element[i].x > (n / 2))
                    my_element[i].x -= (n / 2);
                while (my_element[i].y > (n / 2))
                    my_element[i].y -= (n / 2);
            }
            for (int i = 0; i < (n / 2) * (n / 2); i++)
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            for (int i = 0; i < (n / 2); i++)
            {
                for (int j = 0; j < (n / 2); j++)
                {
                    myMagicSquare[i + n / 2, j + n / 2] = myMagicSquare[i, j] + n * n / 4;
                    myMagicSquare[i, j + n / 2] = myMagicSquare[i, j] + n * n * 2 / 4;
                    myMagicSquare[i + n / 2, j] = myMagicSquare[i, j] + n * n * 3 / 4;
                }
            }
            myMagicSquare.printhf(cells);
            int m = (n - 2) / 4;
            int tmp = 0;
            for (int i = 0; i < n / 2; i++)
            {
                if (i == (n / 2 + 1) / 2 - 1)
                {
                    for (int j = 0; j < m; j++)
                    {
                        tmp = myMagicSquare[i, 1 + j];
                        myMagicSquare[i, 1 + j] = myMagicSquare[i + n / 2, 1 + j];
                        myMagicSquare[i + n / 2, 1 + j] = tmp;
                    }
                }
                else
                {
                    for (int j = 0; j < m; j++)
                    {
                        tmp = myMagicSquare[i, j];
                        myMagicSquare[i, j] = myMagicSquare[i + n / 2, j];
                        myMagicSquare[i + n / 2, j] = tmp;
                    }
                }
            }
            for (int j = 1; j <= (m - 1); j++)
            {
                for (int i = 0; i < n / 2; i++)
                {
                    tmp = myMagicSquare[i, n - j];
                    myMagicSquare[i, n - j] = myMagicSquare[i + n / 2, n - j];
                    myMagicSquare[i + n / 2, n - j] = tmp;
                }
            }
            myMagicSquare.printhf(cells);
            myMagicSquare.IsCorrect();
        }
        else
        {
            int temp1 = 0;
            int temp2 = 0;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                    my_element[(i - 1) * n + j-1] = new ele((i - 1) * n + j, i, j);
            }
            for (int i = 0; i < n * n; i++)
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            if (n > 4)
            {
                for (int i = 0; i < (n / 4); i++)
                {
                    for (int j = 0; j < (n / 4); j++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            if ((4 * i + k ) < n / 2)
                            {
                                temp1 = myMagicSquare[4 * i + k, 4 * j + k];
                                myMagicSquare[4 * i + k, 4 * j + k] = myMagicSquare[n - 1 - 4 * i - k, n - 1 - 4 * j - k];
                                myMagicSquare[n - 1 - 4 * i - k, n - 1 - 4 * j - k] = temp1;
                                temp2 = myMagicSquare[4 * i + k, 4 * (j + 1) - k - 1];
                                myMagicSquare[4 * i + k, 4 * (j + 1) - k - 1] = myMagicSquare[n - 1 - 4 * i - k, n - 4 * (j + 1) + k];
                                myMagicSquare[n - 1 - 4 * i - k, n - 4 * (j + 1) + k] = temp2; 
                            }
                        }
                    }
                }
            }
            else
            {
                temp1 = myMagicSquare[0, 0];
                myMagicSquare[0, 0] = myMagicSquare[3, 3];
                myMagicSquare[3, 3] = temp1;
                temp1 = myMagicSquare[1, 1];
                myMagicSquare[1, 1] = myMagicSquare[2, 2];
                myMagicSquare[2, 2] = temp1;
                temp1 = myMagicSquare[0, 3];
                myMagicSquare[0, 3] = myMagicSquare[3, 0];
                myMagicSquare[3, 0] = temp1;
                temp1 = myMagicSquare[1, 2];
                myMagicSquare[1, 2] = myMagicSquare[2, 1];
                myMagicSquare[2, 1] = temp1;
            }
            myMagicSquare.printhf(cells);
            myMagicSquare.IsCorrect();
        }
        string xlsname = @"C:\MagicSquare";
        string xlsfile;
        int num = 2;
        if (File.Exists(xlsfile = xlsname + ".xls"))
            while (File.Exists(xlsfile = xlsname + num++ + ".xls")) ;
        xls.FileName = xlsfile;
        xls.Save();
    }
}

自动生成质数表并存入Excel文件

using System;
using System.IO;
using org.in2bits.MyXls;

namespace PrimeNumberTable
{
    class Program
    {
        static void Main(string[] args)
        {
            XlsDocument xls = new XlsDocument();
            Worksheet PrimaryNumber = xls.Workbook.Worksheets.Add("PrimaryNumber");//状态栏标题名称
            Cells cells = PrimaryNumber.Cells;
            int int1 = 10000;
            int sign = 0;
            int sign2 = 0;
            int d = 0;
            bool blnPrime = false;
            if (int1 > 0)
                sign = 1;
            else
                sign = -1;
            sign2 = sign * 2;
            int j = 1;
            //这个我应没记错,她是给出的是For i = int1 To sign Step -sign
            for (int i = int1; i >= sign2; i += -sign)
            {
                d = 2;
                blnPrime = true;
                while (d < Math.Abs(i) & blnPrime)
                {
                    if (i % d == 0)
                    {
                        blnPrime = false;
                        break;
                    }
                    d += 1;
                }
                if (blnPrime)
                    cells.Add(j++, 1, i);
            }
            if (blnPrime)
            {
                string xlsname = @"C:\PrimaryNumberTable";
                string xlsfile;
                int num = 2;
                if (File.Exists(xlsfile = xlsname + ".xls"))
                    while (File.Exists(xlsfile = xlsname + num++ + ".xls")) ;
                xls.FileName = xlsfile;
                xls.Save();
            }
        }
    }
}

http://www.oschina.net/code/snippet_156249_4855

//1.创建一个Excel文档:

XlsDocument xls = new XlsDocument();

//2.创建一个WorkSheet:

Worksheet ws = xls.Workbook.Worksheets.Add("WorkSheet1");

//3.指定列格式:

ColumnInfo colInfo = new ColumnInfo(xls, ws);
colInfo.ColumnIndexStart = 0;
colInfo.ColumnIndexEnd = 17;
colInfo.Width = 15 * 256;
ws.AddColumnInfo(colInfo);

//列格式必须每次都要重新定义,一个列格式不能重复使用。

//4.指定单元格样式:

XF xf = xls.NewXF();
xf.HorizontalAlignment = HorizontalAlignments.Centered;
xf.VerticalAlignment = VerticalAlignments.Centered;
xf.Pattern = 1;
xf.PatternColor = Colors.Default30;
xf.UseBorder = true;
xf.TopLineStyle = 1;
xf.TopLineColor = Colors.Black;
xf.BottomLineStyle = 1;
xf.BottomLineColor = Colors.Black;
xf.LeftLineStyle = 1;
xf.LeftLineColor = Colors.Black;
xf.RightLineStyle = 1;
xf.RightLineColor = Colors.Black;
xf.Font.Bold = true;
xf.Font.Height = 11 * 20;
xf.Font.ColorIndex = 1;

//5.给单元格赋值:

ws.Cells.Add(2, 3, "金额(万元)", xf);

//6.合并单元格:
ws.Cells.Merge(1, 2, 2, 2);
//或者
ws.AddMergeArea(new MergeArea(1, 2, 1, 1));

//7.MyXls合并单元格有个bug,就是合并后只是第一个单元格有样式,其余的样式丢失。所以写了个函数来合并:

MergeRegion(ref ws, xf, "机构", 1, 1, 2, 1);

public void MergeRegion(ref Worksheet ws, XF xf, string title, int startRow, int startCol, int endRow, int endCol)
{
      for (int i = startCol; i <= endCol; i++)
       {
            for (int j = startRow; j <= endRow; j++)
             {
                 ws.Cells.Add(j, i, title, xf);
             }
       }
       ws.Cells.Merge(startRow, endRow, startCol, endCol);
}

//虽然效率不怎么样,但是对于出Excel报表,还OK。

//8.指定单元格格式:

cell.Format = StandardFormats.Decimal_1;

//具体更多请参考源代码的StandardFormats类。

//9.保存或者发送Excel:

xls.Send();
//或者
xls.Save();

MyXls 导入EXECL到listview

private void button1_Click(object sender, EventArgs e)
       {
           System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
           if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               textBoxReadFile.Text = ofd.FileName;
               button1_Click(null, null);
           }
       }

   private void button2_Click(object sender, EventArgs e)
       {

           string fileName = textBoxReadFile.Text;
           if (!File.Exists(fileName))
           {
               MessageBox.Show(string.Format("{0} not found!", fileName));
               return;
           }

    XlsDocument xls = new XlsDocument(@fileName);//打开EXCLE

           //获得Excel中的指定一个工作页

           Worksheet sheet = xls.Workbook.Worksheets[0];
           //读取数据 循环每sheet工作页的第5行开始

           for (int i = 5; i < sheet.Rows.Count; i++)
           {

                      //读取第一行第一列
                     zid = Convert.ToString(sheet.Rows[ushort.Parse(i.ToString())].GetCell(1).Value); 

                          ListViewItem lv1 = new ListViewItem(zid);
   //    第一行第5列
                          j = Convert.ToString(sheet.Rows[ushort.Parse(i.ToString())].GetCell(5).Value);
                     lv1.SubItems.Add(j);

                     listview1.Items.Add(lv1);

           }
       }
   }

封装MyXls ,支持读写单元格,读写到datatable &nbsp;~&nbsp;5KB&nbsp;&nbsp;&nbsp;&nbsp;下载(7)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using org.in2bits.MyXls;

namespace ProjBaseCS.OfficeOP
{
    /// <summary>
    /// 对EXCEL文档进行操作
    /// </summary>
    public class ExcelLib
    {
        private Workbook tempWb;
        public ExcelLib(string path)
        {
            ExcelPath = path;
            XlsDoc = new XlsDocument(path);
            XlsDoc.FileName = System.IO.Path.GetFileName(path);
            tempWb = XlsDoc.Workbook;
            CurrentSheet = 0;
        }
        public XlsDocument XlsDoc
        {
            get;
            private set;
        }
        /// <summary>
        /// 当前在操作第几个sheet,默认为0,使用的时候可以切换到自己需要的sheet上。
        /// </summary>
        public int CurrentSheet
        {
            get;
            set;
        }
        /// <summary>
        /// 文档路径
        /// </summary>
        public string ExcelPath
        {
            get;
            private set;
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="overwrite"></param>
        public void Save(bool overwrite= true)
        {
            this.SaveAs(ExcelPath, overwrite);
        }
        /// <summary>
        /// 存到别的位置
        /// </summary>
        /// <param name="path"></param>
        /// <param name="overwrite"></param>
        public void SaveAs(string path,bool overwrite= true)
        {
            string tempFileName = XlsDoc.FileName;
            XlsDoc.FileName=System.IO.Path.GetFileName(path);
            XlsDoc.Save(System.IO.Path.GetDirectoryName(path),overwrite);
            XlsDoc.FileName = tempFileName;
        }

        /// <summary>
        /// 获得单元格中的内容,注意索引从1开始
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public object GetCell(ushort row, ushort col)
        {
            Worksheet ws = tempWb.Worksheets[CurrentSheet];
            return ws.Rows[row].CellAtCol(col).Value;
        }

        public void WriteCell(ushort row, ushort col, object value)
        {
            Worksheet ws = tempWb.Worksheets[CurrentSheet];
            ws.Rows[row].CellAtCol(col).Value = value;
        }
        /// <summary>
        /// 将制定范围内的单元格内容返回,如果范围内的单元格是空,则该单元格返回null
        /// </summary>
        /// <param name="rowStart">行坐标的开始,以1开始</param>
        /// <param name="rowEnd">行坐标的结束,以1开始</param>
        /// <param name="colStart">列坐标的开始,以1开始</param>
        /// <param name="colEnd">列坐标的结束,以1开始</param>
        /// <returns></returns>
        public DataTable GetCells(ushort rowStart, ushort rowEnd, ushort colStart, ushort colEnd)
        {
            Worksheet ws = tempWb.Worksheets[CurrentSheet];

            DataTable dt = new DataTable("ExcelContent");
            for (int i = colStart; i <= colEnd; i++)
            {
                dt.Columns.Add("Column" + i.ToString());
            }
            DataRow dr;
            int colCount=colEnd-colStart+1;
            for (ushort i = rowStart; i <= rowEnd; i++)
            {
                dr = dt.NewRow();
                for (ushort j = 0; j < colCount; j++)
                {
                    if (ws.Rows[i].CellExists((ushort)(j + colStart)))
                        dr[j] = ws.Rows[i].GetCell((ushort)(j + colStart)).Value;
                    else
                        dr[j]=null;
                }
                dt.Rows.Add(dr);

            }

            return dt;
        }
        /// <summary>
        /// 将数据表中的内容写入到excel表格中
        /// </summary>
        /// <param name="dtValue">将要写入excel的数据表</param>
        /// <param name="row">写入位置的起始行坐标,从1开始</param>
        /// <param name="col">写入位置的起始列坐标,从1开始</param>
        public void WriteCells(DataTable dtValue,ushort row,ushort col)
        {
            Worksheet ws = tempWb.Worksheets[CurrentSheet];

            for (int i = 0; i < dtValue.Rows.Count; i++)
            {
                if (!ws.Rows.RowExists((ushort)(i + row)))
                    ws.Rows.AddRow((ushort)(i + row));
                for (int j = 0; j < dtValue.Columns.Count; j++)
                {                    
                    if (ws.Rows[(ushort)(i + row)].CellExists((ushort)(j + col)))
                        ws.Rows[(ushort)(i + row)].GetCell((ushort)(j + col)).Value = dtValue.Rows[i][j];
                    else
                        ws.Cells.Add((ushort)(i + row), (ushort)(j + col), dtValue.Rows[i][j]);
                }
            }
        }

    }
}

MyXls 导入EXECL到listview

private void button1_Click(object sender, EventArgs e)
       {
           System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
           if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               textBoxReadFile.Text = ofd.FileName;
               button1_Click(null, null);
           }
       }

   private void button2_Click(object sender, EventArgs e)
       {
           string fileName = textBoxReadFile.Text;
           if (!File.Exists(fileName))
           {
               MessageBox.Show(string.Format("{0} not found!", fileName));
               return;
           }
          XlsDocument xls = new XlsDocument(@fileName);//打开EXCLE
           //获得Excel中的指定一个工作页
           Worksheet sheet = xls.Workbook.Worksheets[0];
           //读取数据 循环每sheet工作页的第5行开始

           for (int i = 5; i < sheet.Rows.Count; i++)
           {
                      //读取第一行第一列
                     zid = Convert.ToString(sheet.Rows[ushort.Parse(i.ToString())].GetCell(1).Value); 

                          ListViewItem lv1 = new ListViewItem(zid);
   //    第一行第5列
                          j = Convert.ToString(sheet.Rows[ushort.Parse(i.ToString())].GetCell(5).Value);
                     lv1.SubItems.Add(j);
                     listview1.Items.Add(lv1);
           }
       }
   }

幻方实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MagicSquare
{
    int n;
    int[,] array;
    public MagicSquare(int a)
    {
        n = a;
        array = new int[a, a];
    }
    public int this[int a, int b]
    {
        get
        {
            return array[a, b];
        }
        set
        {
            array[a, b] = value;
        }
    }
    public void printhf()
    {
        if (n > 0)
        {
            Console.WriteLine("{0}阶幻方构造如下:", n);
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    Console.Write("  {0}  ", array[i-1, j-1]);
                }
                Console.WriteLine();
            }
        }
        else
            return;
    }
    public void IsCorrect()
    {
        int sum_x, sum_y, sum_diag1, sum_diag2;
        sum_diag1 = 0;
        sum_diag2 = 0;
        int sum_corect=n*(1+n*n)/2;
        for(int i=0;i<n;i++)
        {
            sum_x = 0;
            sum_y = 0;
            for (int j = 0; j < n; j++)
            {
                sum_x += this.array[i, j];
                sum_y += this.array[j, i];
            }
            if (sum_x != sum_corect || sum_y != sum_corect)
            {
                Console.WriteLine("幻方错误:行列和错误");
                break;
            }
            sum_diag1 += this.array[i, i];
            sum_diag2 += this.array[i, n-1-i];
        }
        if (sum_diag1 != sum_corect || sum_diag2 != sum_corect)
        {
            Console.WriteLine("幻方错误:对角线和错误");
        }
        else
        {
            Console.WriteLine("经验证,该幻方正确");
        }
    }
}

public class ele
{
    public int number;
    public int x;
    public int y;
    public ele()
    {
        number = 0;
        x = 0;
        y = 0;
    }
    public ele(int n, int array_x, int array_y)
    {
        number = n;
        x = array_x;
        y = array_y;
    }
}

public class test
{
    public static void Main()
    {
        int n = 0;
        string buff;
        buff = Console.ReadLine();
        n = Convert.ToInt32(buff);
        if (n == 2)
        {
            Console.WriteLine("2阶幻方不存在!");
            return;
        }
        ele[] my_element = new ele[n * n];
        MagicSquare myMagicSquare = new MagicSquare(n);
        int array_x, array_y;
        if (n % 2 == 1)
        {
            array_x = n;
            array_y = (n + 1) / 2;
            for (int i = 1; i <= n * n; i++)
            {
                if (i == 1)
                {
                    my_element[i - 1] = new ele(i, array_x, array_y);
                }
                else
                {
                    if (i % n == 1)
                    {
                        array_x -= 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                    else
                    {
                        array_x += 1;
                        array_y += 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                }
            }
            for (int i = 0; i < n * n; i++)
            {
                while (my_element[i].x > n)
                {
                    my_element[i].x -= n;
                }
                while (my_element[i].y > n)
                {
                    my_element[i].y -= n;
                }
            }
            for (int i = 0; i < n * n; i++)
            {
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            }
            myMagicSquare.printhf();
            myMagicSquare.IsCorrect();
        }
        else if (n % 4 == 2)
        {
            array_x = n / 2;
            array_y = ( n / 2 + 1) / 2;
            for (int i = 1; i <= (n / 2) * (n / 2); i++)
            {
                if (i == 1)
                {
                    my_element[i - 1] = new ele(i, array_x, array_y);
                }
                else
                {
                    if (i % (n/2) == 1)
                    {
                        array_x -= 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                    else
                    {
                        array_x += 1;
                        array_y += 1;
                        my_element[i - 1] = new ele(i, array_x, array_y);
                    }
                }
            }
            for (int i = 0; i < (n / 2) * (n / 2); i++)
            {
                while (my_element[i].x > (n / 2))
                {
                    my_element[i].x -= (n / 2);
                }
                while (my_element[i].y > (n / 2))
                {
                    my_element[i].y -= (n / 2);
                }
            }
            for (int i = 0; i < (n / 2) * (n / 2); i++)
            {
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            }
            for (int i = 0; i < (n / 2); i++)
            {
                for (int j = 0; j < (n / 2); j++)
                {
                    myMagicSquare[i + n / 2, j + n / 2] = myMagicSquare[i, j] + n * n / 4;
                    myMagicSquare[i, j + n / 2] = myMagicSquare[i, j] + n * n * 2 / 4;
                    myMagicSquare[i + n / 2, j] = myMagicSquare[i, j] + n * n * 3 / 4;
                }
            }
            myMagicSquare.printhf();
            int m = (n - 2) / 4;
            int tmp = 0;
            for (int i = 0; i < n / 2; i++)
            {
                if (i == (n / 2 + 1) / 2 - 1)
                {
                    for (int j = 0; j < m; j++)
                    {
                        tmp = myMagicSquare[i, 1 + j];
                        myMagicSquare[i, 1 + j] = myMagicSquare[i + n / 2, 1 + j];
                        myMagicSquare[i + n / 2, 1 + j] = tmp;
                    }
                }
                else
                {
                    for (int j = 0; j < m; j++)
                    {
                        tmp = myMagicSquare[i, j];
                        myMagicSquare[i, j] = myMagicSquare[i + n / 2, j];
                        myMagicSquare[i + n / 2, j] = tmp;
                    }
                }
            }
            for (int j = 1; j <= (m - 1); j++)
            {
                for (int i = 0; i < n / 2; i++)
                {
                    tmp = myMagicSquare[i, n - j];
                    myMagicSquare[i, n - j] = myMagicSquare[i + n / 2, n - j];
                    myMagicSquare[i + n / 2, n - j] = tmp;
                }
            }
            myMagicSquare.printhf();
            myMagicSquare.IsCorrect();
        }
        else
        {
            int temp1 = 0;
            int temp2 = 0;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    my_element[(i - 1) * n + j-1] = new ele((i - 1) * n + j, i, j);
                }
            }
            for (int i = 0; i < n * n; i++)
            {
                myMagicSquare[my_element[i].x - 1, my_element[i].y - 1] = my_element[i].number;
            }
            if (n > 4)
            {
                for (int i = 0; i < (n / 4); i++)
                {
                    for (int j = 0; j < (n / 4); j++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            if ((4 * i + k ) < n / 2)
                            {
                                temp1 = myMagicSquare[4 * i + k, 4 * j + k];
                                myMagicSquare[4 * i + k, 4 * j + k] = myMagicSquare[n - 1 - 4 * i - k, n - 1 - 4 * j - k];
                                myMagicSquare[n - 1 - 4 * i - k, n - 1 - 4 * j - k] = temp1;
                                temp2 = myMagicSquare[4 * i + k, 4 * (j + 1) - k - 1];
                                myMagicSquare[4 * i + k, 4 * (j + 1) - k - 1] = myMagicSquare[n - 1 - 4 * i - k, n - 4 * (j + 1) + k];
                                myMagicSquare[n - 1 - 4 * i - k, n - 4 * (j + 1) + k] = temp2; 
                            }
                        }
                    }
                }
            }
            else
            {
                temp1 = myMagicSquare[0, 0];
                myMagicSquare[0, 0] = myMagicSquare[3, 3];
                myMagicSquare[3, 3] = temp1;
                temp1 = myMagicSquare[1, 1];
                myMagicSquare[1, 1] = myMagicSquare[2, 2];
                myMagicSquare[2, 2] = temp1;
                temp1 = myMagicSquare[0, 3];
                myMagicSquare[0, 3] = myMagicSquare[3, 0];
                myMagicSquare[3, 0] = temp1;
                temp1 = myMagicSquare[1, 2];
                myMagicSquare[1, 2] = myMagicSquare[2, 1];
                myMagicSquare[2, 1] = temp1;
            }
            myMagicSquare.printhf();
            myMagicSquare.IsCorrect();
        }
    }
}

标签:c#

收藏

0人收藏

支持

0

反对

0

发表评论