c#实现二元关系性质的判定

 
    二元关系是离散数学的基本概念,是以有序对为元素的集合,定义在某一集合上的二元关系有自反性、反自反性、反对称性、传递性等性质。这些性质在计算机知识学习中非常重要,但是实现对某个二元关系的准确判定比较困难。利用c#编写了一个小工具来实现对其判断。
逻辑层代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 二元关系判定
{
         class ClassDuality
        {
                 private int age;

                 public int Age
                {
                        get { return age; }
                        set { age = value; }
                }
                 /// <summary>
                 /// 判断自反和反自反
                 /// </summary>
                 /// <param name="a"></param>
                 /// <returns></returns>
                 public int zifan( double[,] a, int n)
                {
                         int flag1 = 0,flag2=0;
                         for( int i=0;i<n;i++)
                                 for ( int j = 0; j < n; j++)
                                {
                                         if (i == j)
                                        {
                                                 if (a[i, j] == 1)
                                                {
                                                        flag1++;
                                                }
                                                 else if (a[i, j] == 0)
                                                {
                                                        flag2++;
                                                }
                                        }
                                }
                         if (flag1 == n)
                        {
                                 return 1; //若为1表示自反性
                        }
                         else     if (flag2 == n)
                        {
                                 return 0; //若为1表示反自反性
                        }
                         else    
                        {
                                 return -1; //若为-1表示既不自反也不反自反
    
                        }
    
                }
                 /// <summary>
                 /// 判断对称和反对陈
                 /// </summary>
                 /// <param name="a"></param>
                 /// <returns></returns>
                 public int duichen( double[,] a, int n)
                {
                         int flag = 0, flag1 = 0,flag2=0;
                         for( int i=0;i<n;i++)
                                 for ( int j = 0; j < n; j++)
                                {
                                         if (i != j&&a[i, j] != 0)
                                        {
                                                
                                                        flag = 1;

                                        }
                                         else if (i != j && a[i, j] != a[j, i])
                                        {
                                                flag1 = 1;

                                        }
                                         else if (a[i, j] == 1 && a[j, i] == 0)
                                        {
                                                flag2++;
                                        }
                                    
                                }
                         if (flag == 0)
                        {
                                 return 1; //表示既对称有反对称
                        }
                         else if (flag1 == 0)
                        {
                                 return 2; //表示对称
                        }
                         else if (flag2 == n)
                        {
                                 return 3; //表示反对陈
                        }
                         else
                        {
                                 return 0;
                        }
                }
                 /// <summary>
                 /// 判断传递
                 /// </summary>
                 /// <param name="a"></param>
                 /// <returns></returns>
                 public int chuandi( double[,] a, int n)
                {
                         bool t = true;
                         int i = 0,j=0,k=0;
                         while (i < n)
                        {
                                 while (j < n)
                                {
                                         if (a[i, j] == 1)
                                        {
                                                 while (k < n)
                                                {
                                                         if (a[j, k] == 1 && a[i, k] != 1)
                                                        {
                                                                t = false;     break;
                                                        }
                                                         else
                                                        {
                                                                k++;
                                                        }
                                                }


                                        }
                                        
                                                j++;
                                        
                                }
                                i++;
    
                        }
                         if (t == true)
                        {
                                 return 1; //传递
                        }
                         else
                        {
                                 return 0; //非传递
                        }
    
                }
        }
}
 
实现代码如下:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 二元关系判定
{
         public partial class Form1 : Form
        {
                 public Form1()
                {
                        InitializeComponent();
                }

                 private void button2_Click( object sender, EventArgs e)
                {
                        textBox1.Text = "";
                }
                 private int arr()
                {
                         string text1 = textBox1.Text;
                         string[] result = System.Text.RegularExpressions.Regex.Split(text1, "\r\n");
                         string[] ss = System.Text.RegularExpressions.Regex.Split(result[0], ",");
                         return result.Length;
    
                }
                 private double[,] read()
                {
                         string text1 = textBox1.Text;
                         double[,] resultArray;
                         string[] result = System.Text.RegularExpressions.Regex.Split(text1, "\r\n");
                         string[] ss = System.Text.RegularExpressions.Regex.Split(result[0], ",");
                        resultArray= new double[result.Length,ss.Length];
                         if (result.Length != ss.Length)
                        {
                                 return resultArray;
                        }
                        
                         int i = 0, j = 0;
                         foreach ( string str in result)
                        {
                                 string[] res = System.Text.RegularExpressions.Regex.Split(str, ",");
                                 foreach ( string re in res)
                                {
                                        resultArray[i, j] = Convert.ToDouble(re);
                                        j++;
                                }
                                i++;
                                j = 0;
                        }
                         return resultArray;
                }

                 private void button1_Click( object sender, EventArgs e)
                {
                         try    
                        {
                                 double[,] result = read();
                                 if (result.Length == 0)
                                {
                                        MessageBox.Show( "输入的矩阵错误!");
                                }
                                 foreach ( double i in result)
                                {
                                         if (i != 1 && i != 0)
                                        {
                                                MessageBox.Show( "输入数字有误!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                                                 return;
                                        }
                                }
                                ClassDuality cd = new ClassDuality();
                                 string res = "该矩阵有以下性质:\n";
                                 if (cd.zifan(result,arr()) == 1)
                                {
                                        res = res + "1、自反的\n";
                                        
                                }
                                 else if (cd.zifan(result,arr()) == 0)
                                {
                                        res = res + "1、反自反的\n";
                                }
                                 else if (cd.zifan(result,arr()) == -1)
                                {
                                        res = res + "1、既不是自反的也不是反自反的\n";
                                }
                                 if (cd.duichen(result,arr()) == 1)
                                {
                                        res = res + "2、既是对称又是反对陈的\n";
                                }
                                 else if (cd.duichen(result,arr()) == 2)
                                {
                                        res = res + "2、对称的\n";
                                }
                                 else if (cd.duichen(result,arr()) == 3)
                                {
                                        res = res + "2、反对称的\n";
                                }
                                 else if (cd.duichen(result,arr()) == 0)
                                {
                                        res = res + "2、既不是对称的又不是反对称的\n";
                                }
                                 if (cd.chuandi(result,arr()) == 1)
                                {
                                        res = res + "3、传递的\n";
                                }
                                 else if (cd.chuandi(result,arr()) == 0)
                                {
                                        res = res + "4、非传递的";
                                }
                                MessageBox.Show(res, "二元关系性质",MessageBoxButtons.OK,MessageBoxIcon.Information);

                        }
                         catch
                        {
                                MessageBox.Show( "输入错误字符!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        }

                }
        }
}
整个源程序在附件
 
 
 

你可能感兴趣的:(C#,职场,休闲,二元关系)