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();
        }    }     }

10.Form Events


Form Events

using System.Windows.Forms;
namespace samp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String str = "", str2 = "";
            if (textBox1.Text != "")
            {
                str += "name is" + textBox1.Text + "\n";
            }
            else
            {                str2 += "enter name\n";
            }
            if (textBox2.Text != "")
            {
                str += "age is" + textBox2.Text + "\n";
            }
            else
            {                str2 += "enter age\n";
            }
            if (textBox3.Text != "")
            {
                str += "Phno is" + textBox3.Text + "\n";
            }
            else
            {                str2 += "enter phno\n";
            }
            if (radioButton1.Checked)
                str += "gender is:" + radioButton1.Text + "\n";
            else if (radioButton2.Checked)
                str += "gender is:" + radioButton1.Text + "\n";
            else
                str2 += "enter gender\n";
           if (checkBox1.Checked)
                str += "Hobbies is:" + checkBox1.Text + "\n";
            else if (checkBox2.Checked)
                str += "Hobbies is:" + checkBox2.Text + "\n";
            else if (checkBox3.Checked)
                str += "Hobbies is:" + checkBox3.Text + "\n";
            else
                str2 += "enter Hobbies\n";
            if (str2 != "")
            {                MessageBox.Show(str2);
            }
            else
            {                MessageBox.Show(str);
            }
        }    }      }

8.Complex Number Addition using Binary Operator Overloading


Complex Number Addition using  Binary Operator Overloading

namespace operoverload
{
    public struct Complex
    {
        public int real;
        public int imaginary;
        public Complex(int real, int imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }
        // Declare which operator to overload (+), the types
        // that can be added (two Complex objects), and the
        // return type (Complex):
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }
        // Override the ToString method to display an complex number in the suitable format:
        public override string ToString()
        {
            return (String.Format("{0} + {1}i", real, imaginary));
        }
        class program
        {
            public static void Main()
            {
                int r1, r2, i1, i2;
                Console.WriteLine("Enter real value of First complex number:");
                r1=int.Parse(Console.ReadLine());
                Console.WriteLine("Enter imaginary value of First complex number:");
                i1 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter real value of Second complex number:");
                r2 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter imaginary value of Second complex number:");
                i2 = int.Parse(Console.ReadLine());
                Complex num1 = new Complex(r1,i1);
                Complex num2 = new Complex(r2,i2);
                // Add two Complex objects (num1 and num2) through the
                // overloaded plus operator:
                Complex sum = num1 + num2;

                // Print the numbers and the sum using the overriden ToString method:
                Console.WriteLine("First complex number:  {0}", num1);
                Console.WriteLine("Second complex number: {0}", num2);
                Console.WriteLine("The sum of the two numbers: {0}", sum);
                Console.ReadLine();
            }                     }          }          }