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. 


To obtain the weighted sum, take the digit to the left of the check digit and then every second 

digit leftwards.  Add each of these digits to itself.  Then, add each digit of each sum to form 

the weighted sum of the even positioned digits.  Add each of the remaining SIN digits (except 

the check digit) to form the sum of the odd positioned digits.  Add the two sums and subtract 

the next highest number ending in zero from their total.  If this number is the check digit, the 

whole number is a valid SIN; otherwise, the whole number is not a valid SIN. 

Specifications

Design a program that validates a Canadian Social Insurance Number (SIN).  Your program 

keeps accepting a whole number and determining if that whole number is a valid SIN.  Your 

program terminates when the user enters 0 as the whole number.  The output from your 

program looks something like: 

 SIN Validator

 =============

 SIN (0 to quit): 193456787

 This is a valid SIN.

 SIN (0 to quit): 193456788

 This is not a valid SIN.

 SIN (0 to quit): 0

 Have a Nice Day!


Code: 


Class CanadaSin: 

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

namespace Canada
{
    class CanadaSIN
    {
        public static void runProgram()
        {
            // Create list to store each DIGIT
            List<int> number = new List<int>();

            //Receive 9 DIGITS
            Console.WriteLine("\nEnter 9 digits number: ");
            do
            {
                int addNum = 1;

                try
                {
                    addNum = Int32.Parse(Console.ReadLine());
                }
                catch
                {
                    Console.WriteLine("Please don't enter null\n");
                    return;
                }
             
                if (0 < addNum && addNum <= 9)
                {
                    number.Add(addNum);
                }
                else
                {
                    Console.WriteLine("You exceed number from 1 - 9");
                }
            } while (number.Count <=8);
             
             
         

            //Show 9 DIGITS
            Console.Write("Your SIN: ");
            foreach (int obj in number)
            {
                Console.Write(obj);
            }
            Console.WriteLine("");


            // add first set of alternates to themselves
            int pair1 = number[1] + number[1];
            int pair2 = number[3] + number[3];
            int pair3 = number[5] + number[5];
            int pair4 = number[7] + number[7];

            int[] digit1 = pair1.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
            int[] digit2 = pair2.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
            int[] digit3 = pair3.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
            int[] digit4 = pair4.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();

            int[] storePair;
            storePair = digit1.Concat(digit2).Concat(digit3).Concat(digit4).ToArray();

            Console.WriteLine("");

            int totalOfDigit = storePair.Sum();

            int leftOverDigits = number[0] + number[2] + number[4] + number[6];

            int finalTotal = totalOfDigit + leftOverDigits;

            int temp = 1;
            int highestInteger = 10;
            while (temp * 10 <= finalTotal)
            {
                temp++;
            }
            highestInteger *= temp;

            if (highestInteger - finalTotal != number[8])
            {
                Console.WriteLine("This is not a valid SIN.");
            }
            else
            {
                Console.WriteLine("This is a valid SIN.");
            }
            Console.WriteLine("");
        }
    }
}

Class Program:

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

namespace Canada
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice = 0;
            do
            {
                CanadaSIN.runProgram();
                Console.WriteLine("Do you want to restart ?: Yes = 1 - No = 0");
                Console.Write("Your choice: ");
                choice = Int32.Parse(Console.ReadLine());
            } while (choice != 0);
        }
    }
}

Summary:

  • Array là cái jề ?
    • Là tập hợp phần tử cùng kiểu dữ liệu được lưu trữ tuần tự trong bộ nhớ
  • Ép kiểu là cái jề ?
    • Cho 1 cái "()" trước 1 biến mà mình muốn ép kiểu nó, rồi cho DATATYPE vào trong cái "()" đó
  • Operator là cái mô ?
    • Là mấy cái dấu Logial( &&, ||, ^, &, |), relation (==, !=, >, <, <=, >=), Arithmetic(+, -, *, /, %), Ternary or Conditional(exp1 ? exp2 : exp3) ......etc.

No comments:

Post a Comment