html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

C#实现数组元素的交集和并集运算_.NET教程_编程技术

[ ] 已经帮助:人解决问题

编写一个类实现数组元素的交集和并集运算
要求:
      1、要有构造函数对数组初始化
      2、包含交集和并集两个单独的方法
      3、要有一个打印数组元素的方法
      4、编写一个测试类测试数组
      5、要求控制台中要分别输出两个原数组以及将交集和并集运算结果输出UstHTML5中文学习网 - HTML5先行者学习网

代码:UstHTML5中文学习网 - HTML5先行者学习网

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. namespace ConsoleApplication4 
  5.     class Program 
  6.     { 
  7.         static void Main(string[] args) 
  8.         { 
  9.             int[] A = { 65, 42, 11, 8, -4}; 
  10.             int[] B = { 2,90, 2, 11, 33, 8}; 
  11.             Console.WriteLine("合并前的元素集A:"); 
  12.             foreach (int x in A) 
  13.             { 
  14.                 Console.Write("{0}  ", x); 
  15.             } 
  16.             Console.WriteLine("/n合并前的元素集B:"); 
  17.             foreach (int x in B) 
  18.             { 
  19.                 Console.Write("{0}  ", x); 
  20.             } 
  21.             Set s = new Set(A, B); 
  22.             if (s.SetA != null && s.SetB != null
  23.             { 
  24.                 Console.WriteLine("/n合并为:"); 
  25.                 foreach (int x in s.GetUnion()) 
  26.                 { 
  27.                     Console.Write("{0}  ", x); 
  28.                 } 
  29.  
  30.                 Console.WriteLine("/n交集为:"); 
  31.                 foreach (int x in s.GetIntersection()) 
  32.                 { 
  33.                     Console.Write("{0}  ", x); 
  34.                 } 
  35.             } 
  36.             Console.ReadKey(); 
  37.         } 
  38.     } 
  39. class Set 
  40.     private int[] setA; 
  41.     private int[] setB; 
  42.  
  43.     public int[] SetA 
  44.     { 
  45.         get  
  46.         { 
  47.             return setA; 
  48.         } 
  49.         set 
  50.         { 
  51.             if (IsRepeated(value)) 
  52.             { 
  53.                 Console.WriteLine("/nA数组中有重复元素!"); 
  54.                // Application.Exit(); 
  55.  
  56.                 //Console.WriteLine(IsRepeated(value));  
  57.             } 
  58.             else 
  59.             { 
  60.                 setA = value; 
  61.             } 
  62.         } 
  63.     } 
  64.     public int[] SetB 
  65.     { 
  66.         get 
  67.         { 
  68.             return setB; 
  69.         } 
  70.         set 
  71.         { 
  72.             if (IsRepeated(value)) 
  73.             { 
  74.                 Console.WriteLine("/nB数组中有重复元素!"); 
  75.                // Application.Exit(); 
  76.  
  77.                 //Console.WriteLine(IsRepeated(value));   
  78.             } 
  79.             else 
  80.             { 
  81.                 setB = value; 
  82.             } 
  83.         } 
  84.     } 
  85.     //构造函数 
  86.     public Set(int[] setA, int[] setB) 
  87.     { 
  88.         SetA = setA; 
  89.         SetB = setB; 
  90.     } 
  91.     //判断数组中是否有元素重复,返回true表示有重复 
  92.     private bool IsRepeated(int[] array) 
  93.     { 
  94.         bool flag = false
  95.  
  96.         for (int i = 0; i < array.Length; i++) 
  97.         { 
  98.            for (int j = 0; j<array.Length; j++) 
  99.            { 
  100.                if ((array[i] == array[j]) && (i != j)) 
  101.                { 
  102.                    flag = true
  103.                    break
  104.                } 
  105.            } 
  106.         } 
  107.         return flag; 
  108.     } 
  109.     //判断element是否在setA中 
  110.     private bool IsInSetA(int element) 
  111.     { 
  112.         bool flag = false
  113.         foreach (int ele in setA) 
  114.         { 
  115.             if (ele == element) 
  116.             { 
  117.                 flag = true
  118.             } 
  119.         } 
  120.         return flag;  
  121.     } 
  122.     //求两个集合的交集 
  123.     public int[] GetIntersection() 
  124.     { 
  125.         //找出不同元素的个数 
  126.         int count = 0; 
  127.         int aLength = 0; 
  128.         int bLength = 0; 
  129.         
  130.         aLength = setA.Length; 
  131.         bLength = setB.Length; 
  132.  
  133.         // int len = setA.Length; 
  134.         for (int j = 0; j < bLength; j++) 
  135.         { 
  136.             if (IsInSetA(setB[j])) 
  137.              { 
  138.                  count++; 
  139.              } 
  140.          } 
  141.         int[] c = new int[count] ; 
  142.         int len =0 ; 
  143.         for (int i = 0; i < aLength; i++) 
  144.         { 
  145.             for (int j = 0; j < bLength; j++) 
  146.             { 
  147.                 if (setA[i] == setB[j]) 
  148.                 { 
  149.                     c[len++] = setB[j]; 
  150.                 } 
  151.             } 
  152.         } 
  153.         return c; 
  154.     } 
  155.     //求两个集合的并集 
  156.     public int[] GetUnion( ) 
  157.     { 
  158.              int aLength = 0; 
  159.              int bLength = 0 ; 
  160.         
  161.             aLength = setA.Length; 
  162.             bLength = setB.Length; 
  163.             //找出不同元素的个数 
  164.             int count = 0; 
  165.             // int len = setA.Length; 
  166.             for (int j = 0; j < bLength; j++) 
  167.             { 
  168.                 if (IsInSetA(setB[j])) 
  169.                 { 
  170.                     count++; 
  171.                 } 
  172.             } 
  173.             //建一个新的数组,存放合并后的元素 
  174.             int x = aLength + bLength - count; 
  175.             int len = aLength; 
  176.             int[] c = new int[x]; 
  177.  
  178.             //把A数组中的元素先存入数组c 
  179.             for (int i = 0; i < len; i++) 
  180.             { 
  181.                 c[i] = setA[i]; 
  182.             } 
  183.             //在数组B中找出数组A中没有的元素,并放入数组C中 
  184.             for (int i = 0; i < bLength; i++) 
  185.             { 
  186.                 int j; 
  187.                 for (j = 0; j < aLength; j++) 
  188.                 { 
  189.                     if (setA[j] == setB[i]) 
  190.                     { 
  191.                         break
  192.                     } 
  193.                 } 
  194.                 if (j == aLength) 
  195.                 { 
  196.                     c[len++] = setB[i]; 
  197.                 } 
  198.             }    
  199.         return c; 
  200.     } 
UstHTML5中文学习网 - HTML5先行者学习网
UstHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助