예제 #1
0
        static void Main()
        {
            int[] a_arr = new int[] { 0, 5, 10, 32, 45 };

            IntegerSet a_set = new IntegerSet(a_arr);

            C.WriteLine("A is: " + a_set + "\n");

            int[] b_arr = new int[] { 0, 3, 10, 15, 25 };

            IntegerSet b_set = new IntegerSet(b_arr);

            C.WriteLine("B is: " + b_set + "\n");

            IntegerSet a_un_b = a_set.Union(b_set);

            C.WriteLine("Union of A and B is: " + a_un_b + "\n");

            IntegerSet a_in_b = a_set.Intersection(b_set);

            C.WriteLine("Intersection of A and B is: " + a_in_b + "\n");

            if (a_set.Equals(b_set))
            {
                C.WriteLine("Sets A and B are equal." + "\n");
            }
            else
            {
                C.WriteLine("Sets A and B are not equal." + "\n");
            }

            C.WriteLine("Copying a to b...");
            b_set = new IntegerSet(a_arr);
            if (a_set.Equals(b_set))
            {
                C.WriteLine("Sets A and B are equal." + "\n");
            }
            else
            {
                C.WriteLine("Sets A and B are not equal." + "\n");
            }

            C.WriteLine("Inserting 33 into set A...");
            a_set.InsertElement(33);
            C.WriteLine("Set a is now: " + a_set + "\n");

            C.WriteLine("Deleting 33 from set A...");
            a_set.DeleteElement(33);
            C.WriteLine("Set A is now: " + a_set + "\n");

            IntegerSet entry_set = new IntegerSet();

            entry_set.InputSet();

            C.WriteLine("\n" + "Set E is: " + entry_set + "\n");

            C.WriteLine("Emptying set e...");
            entry_set.EmptySet();
            C.WriteLine("Set E is: " + entry_set);
        }
예제 #2
0
        private IntegerSet StringToSet(String strSet)
        {
            IntegerSet returnSet = new IntegerSet();

            try {
                foreach (String s in strSet.Split(','))
                {
                    try
                    {
                        int    num;
                        String s2 = s;
                        num = Convert.ToInt32(s2);
                        returnSet.InsertElement(num);
                    }
                    catch (OverflowException)
                    {
                        status = "One value entered is not within converting range.";
                    }
                    catch (FormatException)
                    {
                        status = "One or more values entered is/are unrecognizable characters for converting.";
                    }
                    catch (Exception)
                    {
                        status = "Number is out of range of the Set.";
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                status = "You did not enter any values.";
            }

            return(returnSet);
        }
예제 #3
0
        static void Main(string[] args)
        {
            // initialize two sets
            Console.WriteLine("Input Set A");
            IntegerSet set1 = new IntegerSet();

            set1.InputSet();
            Console.WriteLine("\nInput Set B");
            IntegerSet set2 = new IntegerSet();

            set2.InputSet();

            IntegerSet union        = set1.Union(set2);
            IntegerSet intersection = set1.Intersection(set2);

            // prepare output
            Console.WriteLine("\nSet A contains elements:");
            Console.WriteLine(set1.ToString());
            Console.WriteLine("\nSet B contains elements:");
            Console.WriteLine(set2.ToString());
            Console.WriteLine(
                "\nUnion of Set A and Set B contains elements:");
            Console.WriteLine(union.ToString());
            Console.WriteLine(
                "\nIntersection of Set A and Set B contains elements:");
            Console.WriteLine(intersection.ToString());

            // test whether two sets are equal
            if (set1.IsEqualTo(set2))
            {
                Console.WriteLine("\nSet A is equal to set B");
            }
            else
            {
                Console.WriteLine("\nSet A is not equal to set B");
            }

            // test insert and delete
            Console.WriteLine("\nInserting 77 into set A...");
            set1.InsertElement(77);
            Console.WriteLine("\nSet A now contains elements:");
            Console.WriteLine(set1.ToString());

            Console.WriteLine("\nDeleting 77 from set A...");
            set1.DeleteElement(77);
            Console.WriteLine("\nSet A now contains elements:");
            Console.WriteLine(set1.ToString());

            // test constructor
            int[]      intArray = { 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 };
            IntegerSet set3     = new IntegerSet(intArray);

            Console.WriteLine("\nNew Set contains elements:");
            Console.WriteLine(set3.ToString());
            // end Main
        }
예제 #4
0
        static void Main(string[] args)
        {
            IntegerSet set1 = new IntegerSet(); //New up the IntegerSet for set1
            IntegerSet set2 = new IntegerSet(); //New up the IntegerSet for set2
            IntegerSet set3 = new IntegerSet(); //New up the IntegerSet for set3
            set1.InsertElement(1); //Set 1 of numbers to test
            set1.InsertElement(2);
            set1.InsertElement(3);
            set1.InsertElement(10);
            set1.InsertElement(5);
            set2.InsertElement(3); //Set 2 of number to test
            set2.InsertElement(4);
            set2.InsertElement(5);
            set2.InsertElement(10);
            set2.InsertElement(12);
            set3.InsertElement(1); //Set 3 of number to test
            set3.InsertElement(2);
            set3.InsertElement(3);
            set3.InsertElement(10);
            set3.InsertElement(5);
            set3.InsertElement(99);

            Console.WriteLine("Set 1: " + set1.ToString()); //Display the ToString method
            Console.WriteLine("Set 2: " + set2.ToString()); //Display the ToString method

            DisplayUnion(set1,set2); //Display Union method
            DisplayIntersection(set1, set2); //Display Intersection method
            DisplayInsertElement(set1); //Display InsertElement method
            DisplayDeleteElement(set2); //Display DeleteElement method
            DisplayEmptyIntegerSet(); //Display EmptyIntegerSet method

            if (!set1.IsEqualTo(set2)) //To test the IsEqualTo method
            {
                Console.WriteLine("Set 1 is NOT equal to Set 2");
            }
            if (set1.IsEqualTo(set3))
            {
                Console.WriteLine("Set 1 is equal to Set 3");
            }
        }
예제 #5
0
        private static IntegerSet InputSet()
        {
            IntegerSet temp = new IntegerSet();

            Console.Write("Enter number (-1 to end): ");
            int number = Convert.ToInt32(Console.ReadLine());

            while (number != -1)
            {
                temp.InsertElement(number);

                Console.Write("Enter number (-1 to end): ");
                number = Convert.ToInt32(Console.ReadLine());
            } // end while

            return(temp);
        } // end method InputSet
예제 #6
0
파일: ModelHW.cs 프로젝트: liraop/wapp
        private Boolean TextToIntset(string text, IntegerSet set)
        {
            // check if the input text is empty or null
            if (String.IsNullOrWhiteSpace(text))
            {
                set.Clear();
                return(true);
            }
            // if the text has characters, try to add in a set
            try
            {
                foreach (var x in text.Split(','))
                {
                    try
                    {
                        set.InsertElement(Int32.Parse(x));
                    }
                    catch (FormatException e)
                    {
                        set.Clear();
                        StatusOut = e.Message;
                        return(false);
                    }
                    catch (ArgumentNullException e)
                    {
                        set.Clear();
                        StatusOut = e.Message;
                        return(false);
                    }
                    catch (OverflowException e)
                    {
                        set.Clear();
                        StatusOut = e.Message;
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                StatusOut = e.Message;
                return(false);
            }

            return(true);
        }
예제 #7
0
        public void Convert() // this function will be called when the compute button is pressed.
        {
            In1Text = In1Text.TrimEnd(',', ' ');
            In2Text = In2Text.TrimEnd(',', ' ');
            string[] arr1 = In1Text.Split(',', ',');
            string[] arr2 = In2Text.Split(',', ','); //seperate words into arrays
            _s1.Clear();
            _s2.Clear();                             //make sure s1 and s2 are clear before use.
            ErrText  = "";                           //clear error message
            ErrColor = Brushes.Black;
            if (In1Text == "")
            {
                ErrText += "Set 1 is empty\n";
            }
            else
            {
                foreach (string i in arr1)
                {
                    try
                    {
                        //if (i == "") continue;
                        uint j = uint.Parse(i);
                        if (j > 100)
                        {
                            throw new Exception("Number Exceed max value 100!");
                        }
                        _s1.InsertElement(j);
                    }
                    catch (Exception e)
                    {
                        ErrColor = Brushes.Red;
                        ErrText += e.Message;
                        _s1.Clear();
                        UnionText = "";
                        InterText = "";
                        return;
                    }
                }
            }

            if (In2Text == "")
            {
                ErrText += "Set 2 is empty\n";
            }
            else
            {
                foreach (string i in arr2)
                {
                    try
                    {
                        //if (i == "") continue;
                        uint j = uint.Parse(i);
                        if (j > 100)
                        {
                            throw new Exception("Number Exceed max value 100!");
                        }
                        _s2.InsertElement(j);
                    }
                    catch (Exception e)
                    {
                        ErrColor = Brushes.Red;
                        ErrText += e.Message;
                        _s2.Clear();
                        UnionText = "";
                        InterText = "";
                        return;
                    }
                }
            }

            IntegerSet inter = _s1.Intersection(_s2);
            IntegerSet uni   = _s1.Union(_s2);

            UnionText = uni.ToString();
            InterText = inter.ToString();
            ErrText  += "Here is the result\n";
        }
예제 #8
0
        public void Calculate()
        {
            set1.Clear();
            set2.Clear();
            try
            {
                ErrorTextOutput     = "";
                TopBoxTextOutput    = "";
                BottomBoxTextOutput = "";

                string[] input1String = _topBoxTextInput.Split(',');
                string[] input2String = _bottomBoxTextInput.Split(',');
                int      size1        = input1String.Length;
                int      size2        = input2String.Length;
                int[]    input1       = new int[size1];
                int[]    input2       = new int[size2];

                for (int i = 0; i < size1; i++)
                {
                    input1[i] = Int32.Parse(input1String[i]);
                }

                for (int i = 0; i < size2; i++)
                {
                    input2[i] = Int32.Parse(input2String[i]);
                }

                foreach (int i in input1)
                {
                    try{
                        set1.InsertElement(i);
                        ErrorTextOutput = "";
                    }catch (Exception e) {
                        ErrorTextOutput     = e.Message;
                        TopBoxTextOutput    = "";
                        BottomBoxTextOutput = "";
                    }
                }

                foreach (int i in input2)
                {
                    try {
                        set2.InsertElement(i);
                        ErrorTextOutput = "";
                    }catch (Exception e) {
                        ErrorTextOutput     = e.Message;
                        TopBoxTextOutput    = "";
                        BottomBoxTextOutput = "";
                    }
                }
            }
            catch (Exception e) {
                ErrorTextOutput     = e.Message;
                TopBoxTextOutput    = "";
                BottomBoxTextOutput = "";
            }
            if (ErrorTextOutput == "")
            {
                TopBoxTextOutput    = "";
                BottomBoxTextOutput = "";
                IntegerSet intersection = set1.Intersection(set2);
                IntegerSet union        = set1.Union(set2);
                TopBoxTextOutput    = union.ToString();
                BottomBoxTextOutput = intersection.ToString();
            }
        }
예제 #9
0
        public void Combine()
        {
            _set1.Clear();
            _set2.Clear();
            ErrorText        = "";
            UnionText        = "---";
            IntersectionText = "---";
            errorMessage[0]  = "";
            errorMessage[1]  = "";

            try
            {
                string[] array1 = Set1Input.Split(',');
                string[] array2 = Set2Input.Split(',');

                int[] arrayUno = new int[array1.Length];
                int[] arrayDos = new int[array2.Length];

                for (int i = 0; i < arrayUno.Length; i++)
                {
                    arrayUno[i] = Int32.Parse(array1[i]);
                }
                for (int i = 0; i < arrayDos.Length; i++)
                {
                    arrayDos[i] = Int32.Parse(array2[i]);
                }

                foreach (uint i in arrayUno)
                {
                    try
                    {
                        if (i > 100)
                        {
                            throw new System.ArgumentException("value can't be greater than 100", "Set 1");
                            break;
                        }
                        //uint j = uint.Parse(i);
                        _set1.InsertElement(i);
                        ErrorText = "";
                    }
                    catch (Exception e)
                    {
                        errorMessage[0]  = e.Message;
                        UnionText        = "---";
                        IntersectionText = "---";
                    }
                }
                foreach (uint i in arrayDos)
                {
                    try
                    {
                        if (i > 100)
                        {
                            throw new System.ArgumentException("value can't be greater than 100", "Set 2");
                            break;
                        }
                        //uint j = uint.Parse(i);
                        _set2.InsertElement(i);
                        ErrorText = "";
                    }
                    catch (Exception e)
                    {
                        errorMessage[1]  = "\n" + e.Message;
                        UnionText        = "---";
                        IntersectionText = "---";
                    }
                }
            }
            catch (Exception e)
            {
                errorMessage[0]  = e.Message;
                UnionText        = "---";
                IntersectionText = "---";
            }

            if (errorMessage[0] == "" && errorMessage[1] == "")
            {
                IntersectionText = "---";
                UnionText        = "---";
                ErrorText        = "";
                IntegerSet intersectSet = _set1.Intersection(_set2);
                IntegerSet unionSet     = _set1.Union(_set2);
                UnionText        = unionSet.ToString();
                IntersectionText = intersectSet.ToString();
                ErrorText        = "SUCCESS!";
            }
            else
            {
                ErrorText = errorMessage[0] + "\n" + errorMessage[1];
            }
        }
예제 #10
0
 //To test InsertElement method
 static void DisplayInsertElement(IntegerSet set1)
 {
     Console.WriteLine("Before InsertElement in Set 2: " + set1.ToString());
     set1.InsertElement(99);
     Console.WriteLine("After InsertElement in Set 2: " + set1.ToString());
 }
예제 #11
0
    static void Main(string[] args)
    {
        try
          {
         // Test for the empty set
         IntegerSet empty = new IntegerSet();
         Console.WriteLine("Low Range: {0}", empty.LOW_RANGE);
         Console.WriteLine("High Range: {0}", empty.HIGH_RANGE);
         Console.WriteLine("Empty set: {0} \n", empty);

         // Test InsertElement method is set A
         IntegerSet setA = new IntegerSet();
         setA.InsertElement(0);
         setA.InsertElement(1);
         setA.InsertElement(50);
         setA.InsertElement(100);
         Console.WriteLine("Insert elements in Set A: {0}", setA);

         // Test InsertElement method is set B
         IntegerSet setB = new IntegerSet();
         setB.InsertElement(100);
         setB.InsertElement(99);
         setB.InsertElement(50);
         setB.InsertElement(0);
         Console.WriteLine("Insert elements in Set B: {0} \n", setB);

         // Test DeleteElement method in set A and B
         setA.DeleteElement(100);
         setA.DeleteElement(99); // element not in set
         setB.DeleteElement(0);
         Console.WriteLine("Delete 100 from Set A: {0}", setA);
         Console.WriteLine("Delete 0 from Set B: {0} \n", setB);

         // Test Union method for set A and B
         IntegerSet unionSet = setA.Union(setB);
         Console.WriteLine("Union of A and B: {0} \n", unionSet);

         // Test Intersection method for set A and B
         IntegerSet intersectSet = setA.Intersection(setB);
         Console.WriteLine("Intersection of A and B: {0} \n", intersectSet);

         // Test IsEqualTo method for set A, B, and C
         IntegerSet setC = new IntegerSet();
         setC.InsertElement(0);
         setC.InsertElement(1);
         setC.InsertElement(50);
         Console.WriteLine("Set A: {0}", setA);
         Console.WriteLine("Set B: {0}", setB);
         Console.WriteLine("Set C: {0}", setC);
         Console.WriteLine("Is Set A equal to Set B? {0}", setA.IsEqualTo(setB));
         Console.WriteLine("Is Set A equal to Set C? {0} \n", setA.IsEqualTo(setC));

         // Test isValid private method
         Console.WriteLine("Try to insert element 999");
         setA.InsertElement(999);
          }

          catch (IndexOutOfRangeException ex)
          {
         Console.WriteLine("*******************************************");
         Console.WriteLine(ex.Message);
         Console.WriteLine("*******************************************");
          }

          // Freeze console window
          Console.ReadLine();
    }