现在的位置: 首页 > 综合 > 正文

学习软件设计——C#练习(9)

2018年01月09日 ⁄ 综合 ⁄ 共 2295字 ⁄ 字号 评论关闭

 编写一个类实现数组元素的交集和并集运算
要求:

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

 

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int[] A = { 65, 42, 11, 8, -4};
            int[] B = { 2,90, 2, 11, 33, 8};
            Console.WriteLine("合并前的元素集A:");
            foreach (int x in A)
            {
                Console.Write("{0}  ", x);
            }

            Console.WriteLine("\n合并前的元素集B:");
            foreach (int x in B)
            {
                Console.Write("{0}  ", x);
            }

            Set s = new Set(A, B);
            if (s.SetA != null && s.SetB != null)
            {
                Console.WriteLine("\n合并为:");
                foreach (int x in s.GetUnion())
                {
                    Console.Write("{0}  ", x);
                }

                Console.WriteLine("\n交集为:");
                foreach (int x in s.GetIntersection())
                {
                    Console.Write("{0}  ", x);
                }
            }

           
            Console.ReadKey();
        }
    }
}
class Set
{
    private int[] setA;
    private int[] setB;

    public int[] SetA
    {
        get 
        {
            return setA;
        }
        set
        {
            if (IsRepeated(value))
            {
                Console.WriteLine("\nA数组中有重复元素!");
               // Application.Exit();

                //Console.WriteLine(IsRepeated(value)); 
            }
            else
            {
                setA = value;
            }
        }
    }
    public int[] SetB
    {
        get
        {
            return setB;
        }
        set
        {
            if (IsRepeated(value))
            {
                Console.WriteLine("\nB数组中有重复元素!");
               // Application.Exit();

                //Console.WriteLine(IsRepeated(value));  
            }
            else
            {
                setB = value;
            }
        }
    }
    //构造函数
    public Set(int[] setA, int[] setB)
    {
        SetA = setA;
        SetB = setB;
    }
    //判断数组中是否有元素重复,返回true表示有重复
    private bool IsRepeated(int[] array)
    {
        bool flag = false;

        for (int i = 0; i < array.Length; i++)
        {
           for (int j = 0; j<array.Length; j++)
           {
               if ((array[i] == array[j]) && (i != j))
               {
                   flag = true;
                   break;
               }
           }
        }
        return flag;
    }
    //判断element是否在setA中
    private bool IsInSetA(int element)
    {
        bool flag = false;
        foreach (int ele in setA)
        {
            if (ele == element)
            {
                flag = true;
            }
        }
        return flag; 
    }
    //求两个集合的交集
    public int[] GetIntersection()
    {
        //找出不同元素的个数
        int count = 0;
        int aLength = 0;
        int bLength = 0;
       
        aLength = setA.Length;
        bLength = setB.Length;

        // int len = setA.Length;
        for (int j = 0; j < bLength; j++)
        {
            if (IsInSetA(setB[j]))
             {
                 count++;
             }
         }
        int[] c = new int[count] ;
        int len =0 ;
        for (int i = 0; i < aLength; i++)
        {
            for (int j = 0; j < bLength; j++)
            {
                if (setA[i] == setB[j])
                {
                    c[len++] = setB[j];
                }
            }
        }
        return c;
    }
    //求两个集合的并集
    public int[] GetUnion( )
    {
             int aLength = 0;
             int bLength = 0 ;
       
            aLength = setA.Length;
            bLength = setB.Length;
      
            //找出不同元素的个数
            int count = 0;
            // int len = setA.Length;
            for (int j = 0; j < bLength; j++)
            {
                if (IsInSetA(setB[j]))
                {
                    count++;
                }
            }
            //建一个新的数组,存放合并后的元素
            int x = aLength + bLength - count;
            int len = aLength;
            int[] c = new int[x];

            //把A数组中的元素先存入数组c
            for (int i = 0; i < len; i++)
            {
                c[i] = setA[i];
            }
            //在数组B中找出数组A中没有的元素,并放入数组C中
            for (int i = 0; i < bLength; i++)
            {
                int j;
                for (j = 0; j < aLength; j++)
                {
                    if (setA[j] == setB[i])
                    {
                        break;
                    }
                }
                if (j == aLength)
                {
                    c[len++] = setB[i];
                }
            }
        
        return c;
    }

}

抱歉!评论已关闭.