Sunday, 9 September 2012

9.Integer & String Sorting using Delegate & Exceptions


Integer & String Sorting using Delegate & Exceptions

namespace deligation
{
    delegate void except(int n);
    public class sort
    {
        static void intsor(int k)
        {
            try
            {
                int i, j, temp;
                int[] a = new int[5];
                Console.WriteLine("\nEnter" + k + "Integers");
                for (i = 0; i < k; i++)
                {
                    a[i] = int.Parse(Console.ReadLine());
                }
                for (i = 0; i < k; i++)
                {
                    for (j = i + 1; j < k; j++)
                    {
                        if (a[i] > a[j])
                        {
                            temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;
                        }           }                }
                Console.WriteLine("\nAfter Sorting Integer\n");
                for (i = 0; i < k; i++)
                {
                    Console.WriteLine(a[i]);
                }
            }
            catch (FormatException e1)
            {
                Console.WriteLine(e1.Message);
            }
                Console.ReadLine();
           }
        static void strsor(int q)
        {
            try
            {
                string temp;
                int o, p;
                string[] a = new string[5];
                Console.WriteLine("\nEnter" + q + "Strings");
                for (o = 1; o <= q; o++)
                {
                    a[o] = Console.ReadLine();                }
                for (o = 1; o <= q; o++)
                {
                    for (p = o + 1; p <= q; p++)
                    {
                           if (string.Compare(a[o], a[p]) > 0)
                        {
                            temp = a[o];
                            a[o] = a[p];
                            a[p] = temp;
                        }          }      }
                Console.WriteLine("\nAfter Sorting A String\n");
                for (o = 1; o <= q; o++)
                {
                    Console.WriteLine(a[o]);
                }
            }
            catch (IndexOutOfRangeException e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.ReadLine();
        }
       static void Main(string[] args)
        {             try
            {                int i;
            Console.WriteLine("**************Sorting concept using Delegates******************\n\n");
                Console.Write("\nEnter n:");
                i = Int32.Parse(Console.ReadLine());
                Console.WriteLine("1.Integer Sorting 2.String Sorting ");
                Console.Write("Enter option:");
                int op = Int32.Parse(Console.ReadLine());
           switch (op)
                {
                    case 1:
                        except g = new except(intsor);
                        g(i);
                        break;
                    case 2:
                        except g1 = new except(strsor);
                        g1(i);
                        break;
                    default:
                        Console.WriteLine("Default case");
                        break;
                      }
            }
            catch (FormatException e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.ReadLine();
        }    }     }

No comments:

Post a Comment