Esempio n. 1
0
        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);
        }
Esempio n. 2
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";
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
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];
            }
        }
Esempio n. 5
0
 // to clear the sets and status message
 private void ClearSets()
 {
     StatusOut = "";
     setA.Clear();
     setB.Clear();
 }