Sunday, June 14, 2015

Day 6: Generics & Iterator

Exercise 01:

Code:

Class Ex01

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

namespace FPT_Day06
{
    class Ex01
    {
        public void compareTemp ()
        {
            List<int> storeNum = new List<int>();
            storeNum.Add(10);
            storeNum.Add(20);
            storeNum.Add(25);
            storeNum.Add(30);
            storeNum.Add(35);

            foreach(int number in storeNum)
            {
                if(number >= 25)
                {
                    Console.WriteLine(number);
                }
            }
            Console.ReadLine();
        }

        public static int GreaterCount (List<int> list, double min)
        {
            int count = 0;
            foreach(int number in list)
            {
                if(number >= min)
                {
                    count++;
                }
            }
            return count;
        }
    }

}

Class Program

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

namespace FPT_Day06
{
    class Program
    {

        static void Main(string[] args)
        {
            //Demo_Delegate obj = new Demo_Delegate();
            //obj.Show();

            Ex01 obj = new Ex01();
            Console.WriteLine("Temperature that exceed or equal 25: ");
            obj.compareTemp();

            List<int> number = new List<int>();
            number.Add(30);
            number.Add(10);
            number.Add(25);
            number.Add(40);
            number.Add(22);

            Console.WriteLine("Number of elements in list that greater or equal 20: " + Ex01.GreaterCount(number, 20));
            Console.ReadLine();

            //double[] num = new double[6] { 10.0, 20.2, 33.4, 44.5, 12.6, 16.7 };
            //Console.WriteLine("Number of elements in enumberale that greater or equal 20: " + Ex02.GreaterCount(num, 20)); 
            //Console.ReadLine();

        }
    }
}

Result


Exercise 02:

Code:

Class Ex02

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

namespace FPT_Day06
{
    class Ex02
    {
        public static int GreaterCount (IEnumerable<double> eble, double min)
        {
            int count = 0;
            foreach (double num in eble)
            {
                if(num.CompareTo(min) >= 0)
                {
                     count++;
                }
            }
            return count;
        }
    }
}

Class Program

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

namespace FPT_Day06
{
    class Program
    {

        static void Main(string[] args)
        {
            //Demo_Delegate obj = new Demo_Delegate();
            //obj.Show();

            //Ex01 obj = new Ex01();
            //Console.WriteLine("Temperature that exceed or equal 25: ");
            //obj.compareTemp();

            //List<int> number = new List<int>();
            //number.Add(30);
            //number.Add(10);
            //number.Add(25);
            //number.Add(40);
            //number.Add(22);

            //Console.WriteLine("Number of elements in list that greater or equal 20: " + Ex01.GreaterCount(number, 20));
            //Console.ReadLine();

            double[] num = new double[6] { 10.0, 20.2, 33.4, 44.5, 12.6, 16.7 };
            Console.WriteLine("Number of elements in enumberale that greater or equal 20: " + Ex02.GreaterCount(num, 20));
            Console.ReadLine();

        }
    }
}

Result


Summary

  • Generic
    • Các generic trong C# là các cấu trúc mà cho phép bạn dùng lại các tính năng đã được định nghĩa cho các kiểu dữ liêu khác nhau trong C#.
    • Generic đảm bảo chuẩn kiểu dữ liệu lúc biên dịch.
    • Một generic được khai báo với danh sách tham số kiểu được bao trong  2 dấu < >.
  • Iterator
    • Một iterator không phải là một thành phần dữ liệu nhưng nó là một cách của việc truy suất đến các phần tử.
    • Iterator có thể được thực hiện bằng phương thức GetEnumerator() mà trả về một tham chiếu tới interface IEnumerator.

Tuesday, June 2, 2015

Day 2: C# Array

Đề bài: 

SIN Validator

Background

A Canadian SIN has nine digits.  The right-most digit is a check digit that enables validation.  

For the  whole number to be a valid SIN, a weighted sum of the other digits plus this check 

digit must be divisible by 10.