stackoverflow1453

Programming&&Games&&Life

Archive for May, 2015

Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication17
{
    class Program
    {
        static void Main(string[] args)
        {
            //create the employee objects
            Employee[] arr = { new Employee("Tapir", "Topla", 3400), new Employee("Tapir", "Maltoz", 2100.43), new Employee("Patoz", "Veren", 31400.33), new Employee("Canisi", "Gelberi", 7400.33), new Employee("Tpouz", "Gelberi", 3500.33) };
            Console.WriteLine("******************************************");
            Console.WriteLine("Olusturulan objeler su sekilde: ");
            Console.WriteLine("******************************************");
            //print 
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }

            //Employees earning in the range $5,000.00-$50000,000.00 per month:
            var filter =
                from e in arr
                where e.AylikKazanc > 5000 && e.AylikKazanc < 50000
                select e;

            Console.WriteLine("******************************************");
            Console.WriteLine("5bin 50bin arasinda kazananan insanlar sunlar: ");
            Console.WriteLine("******************************************");
            //print
            foreach (var item in filter)
            {
                Console.WriteLine(item);
            }

            //Employees when sorted by name descending:
            var sortedName =
                from e in arr
                orderby e.Ad descending
                select e;

            Console.WriteLine("******************************************");
            Console.WriteLine("Isimlere gore azalan siralama yaptiriliyor: ");
            Console.WriteLine("******************************************");
            foreach (var item in sortedName)
            {
                Console.WriteLine(item);
            }

            //Employees when sorted by name ascending
            var sortedNameReverse =
                from e in arr
                orderby e.Ad ascending
                select e;

            Console.WriteLine("******************************************");
            Console.WriteLine("Isimlere gore artan siralama yaptiriliyor: ");
            Console.WriteLine("******************************************");
            foreach (var item in sortedNameReverse)
            {
                Console.WriteLine(item);
            }

            //First employee when sorted by name:
            var sortedNameReversev2 =
                from e in arr
                orderby e.Ad ascending
                select e;

            Console.WriteLine("******************************************");
            Console.WriteLine("Isme gore artan siralama yaptirildiginda ilk gelen obje: ");
            Console.WriteLine("******************************************");

            if (sortedNameReversev2.Any())
                Console.WriteLine(sortedNameReversev2.First());

            Console.WriteLine("******************************************");
            Console.WriteLine("Birbirinden farkli soyisimleri ni goster: ");
            Console.WriteLine("******************************************");

            var DistinctSoyisImler =
                from e in arr
                select e.Soyad;

            foreach (var item in DistinctSoyisImler.Distinct())
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("******************************************");
            Console.WriteLine("Birbirinden farkli isimleri ni goster: ");
            Console.WriteLine("******************************************");

            var DisctinctAdlar =
                from e in arr
                select e.Ad;

            foreach (var item in DisctinctAdlar.Distinct())
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("******************************************");
            Console.WriteLine("Yenibir class olusturur: ");
            Console.WriteLine("******************************************");

            var newAnonymousClass =
                from e in arr
                select new { e.Ad, aylikgaZam = e.AylikKazanc * 1.1 };

            foreach (var item in newAnonymousClass)
            {
                Console.WriteLine(item);
            }
        }
    }
}

Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            // implicitly typed local variable
            var aray = new[] { 32, 443, 3, 5, 5, 2, 64, 6, 5645, 3 };

            var sorted =
                    from value in aray
                    where value > 5 && value < 400
                    select value;
            //printing out sorted

            Console.WriteLine("Printing out sorted");
            foreach (var item_ in sorted)
            {
                Console.WriteLine(item_);
            }
        }
    }
}

Constructor Overloading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTime
{
    //fields
    private int hour;
    private int minute;
    private int seconds;

    //property
    public int Hour
    {
        get { return hour; }
        set { hour = value; }
    }

    //constructor #1
    public myTime()
    {
        setTime(0, 0, 0);
    }
    //constructor #2
    public myTime(int h)
    {
        setTime(h, 0, 0);
    }
    //constructor #3
    public myTime(int h, int n)
    {
        setTime(h, n, 0);
    }
    //constructor #4
    public myTime(int h, int n, int z)
    {
        setTime(h, n, z);
    }
    //constructor #5
    public myTime(myTime x)
    {
        setTime(x.hour, x.minute, x.seconds);
    }

    //setTime
    public void setTime(int Hour, int Minute, int Seconds)
    {
        hour = Hour;
        minute = Minute;
        seconds = Seconds;

    }

    //convert to Universal Time(24 hr format)
    public string ToUniversalTime()
    {
        return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, seconds);
    }

    public string ToStandartTime()
    {
        string ampm;

        if (hour > 12)
        {
            ampm = "PM";
            hour = hour % 12;
        }

        else
            ampm = "AM";

        return hour.ToString() + ":" + minute.ToString() + ":" + seconds.ToString() + " " + ampm;
    }
}


//****************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTimetest
{
    static void Main(string[] args)
    {
        Console.WriteLine();
        myTime a = new myTime();
        Console.WriteLine(a.Hour.ToString());
        a.Hour = 23;  //set property
        Console.WriteLine(a.Hour);
        a.setTime(13, 23, 55);

        myTime b, c, d, e;

        //different constructors called
        b = new myTime(2);
        c = new myTime(2, 5);
        d = new myTime(2, 5, 9);
        e = new myTime(d);

        Console.WriteLine(b.ToUniversalTime());
        Console.WriteLine(c.ToUniversalTime());
        Console.WriteLine(d.ToUniversalTime());
        Console.WriteLine(e.ToUniversalTime());

    }
}

Property

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTime
{
    //fields
    private int hour;
    private int minute;
    private int seconds;    
    
    //property
    public int Hour
    {
        get { return hour; }
        set { hour = value; }
    }
    

    //constructor
    public myTime()
    {
        hour = 5;
        minute = 5;
        seconds = 5;
    }

    //setTime
    public void setTime(int Hour, int Minute, int Seconds)
    {
        hour = Hour;
        minute = Minute;
        seconds = Seconds;

    }

    //convert to Universal Time(24 hr format)
    public string ToUniversalTime()
    {
        return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, seconds);
    }

    public string ToStandartTime()
    {
        string ampm;

        if (hour > 12)
        {
            ampm = "PM";
            hour = hour % 12;
        }

        else
            ampm = "AM";

        return hour.ToString() + ":" + minute.ToString() + ":" + seconds.ToString() + " " + ampm;
    }
}

//*************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTimetest
{
    static void Main(string[] args)
    {
        Console.WriteLine();
        myTime a=new myTime();
        Console.WriteLine(a.Hour.ToString());
        a.Hour = 23;  //set property
        Console.WriteLine(a.Hour);        
        a.setTime(13, 23, 55);
        Console.WriteLine(a.ToUniversalTime());
        Console.WriteLine(a.ToStandartTime());        
        
    }

}

Classes and Objects

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTime
{
    //fields
    private int hour;
    private int minute;
    private int seconds;

    //constructor
    public myTime()
    {
        hour = 5;
        minute = 5;
        seconds = 5;
    }

    //setTime
    public void setTime(int Hour, int Minute, int Seconds)
    {
        hour = Hour;
        minute = Minute;
        seconds = Seconds;

    }

    //convert to Universal Time(24 hr format)
    public string ToUniversalTime()
    {
        return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, seconds);
    }

    public string ToStandartTime()
    {
        string ampm;

        if (hour > 12)
        {
            ampm = "PM";
            hour = hour % 12;
        }

        else
            ampm = "AM";

        return hour.ToString() + ":" + minute.ToString() + ":" + seconds.ToString() + " " + ampm;
    }
}


//******************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class myTimetest
{
    static void Main(string[] args)
    {
        Console.WriteLine();
        myTime a=new myTime();

        a.setTime(13, 23, 55);
        Console.WriteLine(a.ToUniversalTime());
        Console.WriteLine(a.ToStandartTime());        
        
    }

}

Double Subscripted Arrays

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare a 2*2 double subscripted array
            int[,] c = new int[2, 2];

            c[0, 0] = 1;
            c[0, 1] = 2;
            c[1, 0] = 3;
            c[1, 1] = 4;

            //declare 5*2 array
            int[,] k = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };

            //declare 10 by 30 array
            double[,] m = new double[10, 30];

            Console.WriteLine("c nin satır sayısı : " + c.GetLength(0));
            Console.WriteLine("c nin sutun sayısı : " + c.GetLength(1));

            displayArray(c);

            Console.WriteLine(" \n\nk nin satir sayisi : " + k.GetLength(0));
            Console.WriteLine("k nin sutun sayisi : " + k.GetLength(1));

            displayArray(k);

            Console.WriteLine(" \n\nm nin satir sayisi : " + m.GetLength(0));
            Console.WriteLine("m nin sutun sayisi : " + m.GetLength(1));

            displayArray(m);

            sum(k);
        }
        //display an integer array
        public static void displayArray(int[,] z)
        {
            for (int i = 0; i < z.GetLength(0); i++) //get rows
            {
                Console.WriteLine();
                for (int j = 0; j < z.GetLength(1); j++) //get column number
                {
                    Console.Write(" " + z[i, j]);
                }
            }
        }

        //display a double array
        public static void displayArray(double[,] z)
        {
            for (int i = 0; i < z.GetLength(0); i++) //get rows
            {
                Console.WriteLine();
                for (int j = 0; j < z.GetLength(1); j++) //get column number
                {
                    Console.Write(" " + z[i, j]);
                }
            }
        }

        //sum of the elements of the array
        public static void sum(int[,] z)
        {
            int sum = 0;

            //get row #
            for (int i = 0; i < z.GetLength(0); i++)
            {   
                //get column #
                for (int j = 0; j < z.GetLength(1); j++)
                {
                    sum += z[i, j];
                }
            }

            Console.WriteLine("\n\nSum of the elements of array is: " + sum);
        }
    }
}

Binary Search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BinarySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myarray = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 };

            Array.Sort(myarray);

            foreach (var item in myarray)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Enter key to be searched for: ");

            if (SearchBinary(myarray, Convert.ToInt32(Console.ReadLine())) == 1)
            {
                Console.WriteLine("Program will terminate now.");
            }
            else
                Console.WriteLine("Key is not found in array...");
        }

        public static int SearchBinary(int[] x, int value)
        {
            int found = 0;
            int passes = 0;
            int lowerboundary = 0;
            int upperboundary = x.Length - 1;

            while (found != 1)
            {
                passes += 1;                 

                if (value == x[(upperboundary + lowerboundary+1) / 2])
                {
                    Console.WriteLine("Value found at index " + (upperboundary + lowerboundary+1) / 2);
                    found = 1;
                    return 1;
                }     

                else if (Math.Abs(upperboundary - lowerboundary) <= 1)
                {
                    found = 0;
                    return 0;
                }

                else if (value < x[(upperboundary + lowerboundary+1) / 2])
                {
                    upperboundary = (upperboundary + lowerboundary+1) / 2 - 1;
                    Console.WriteLine("Searched key is in lower half and pass# is: " + passes);
                    found = 0;
                }               

                else
                {
                    lowerboundary = (upperboundary + lowerboundary+1) / 2 + 1;
                    Console.WriteLine("Searched key is in upper half and pass# is " + passes);
                    found = 0;
                }
            }
          
            return 0;
        }
    }
}