implementing luhn algorithm using c#

后端 未结 9 464
醉梦人生
醉梦人生 2020-12-03 02:19

I am using following code to implement Luhn algorithm for credit card check in c# language but could not get the output to generate the check sum its showing validity: kindl

9条回答
  •  独厮守ぢ
    2020-12-03 02:43

    Here are some extension methods that compute a Luhn checkdigit, validate a number with a checkdigit, and add a checkdigit to a number. Tested in .NET 4.5.

    There are extension methods for strings, ints, int64s and IList.

    I got some ideas for this from rosettacode.org

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    
    public static class CheckDigitExtension
    {
        static readonly int[] Results = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };
    
        #region extension methods for IList
    
        /// 
        /// For a list of digits, compute the ending checkdigit 
        /// 
        /// The list of digits for which to compute the check digit
        /// the check digit
        public static int CheckDigit(this IList digits)
        {
            var i = 0;
            var lengthMod = digits.Count%2;
            return (digits.Sum(d => i++ % 2 == lengthMod ? d : Results[d]) * 9) % 10;
        }
    
        /// 
        /// Return a list of digits including the checkdigit
        /// 
        /// The original list of digits
        /// the new list of digits including checkdigit
        public static IList AppendCheckDigit(this IList digits)
        {
            var result = digits;
            result.Add(digits.CheckDigit());
            return result;
        }
    
        /// 
        /// Returns true when a list of digits has a valid checkdigit
        /// 
        /// The list of digits to check
        /// true/false depending on valid checkdigit
        public static bool HasValidCheckDigit(this IList digits)
        {
            return digits.Last() == CheckDigit(digits.Take(digits.Count - 1).ToList());
        }
    
        #endregion extension methods for IList
    
        #region extension methods for strings
    
        /// 
        /// Internal conversion function to convert string into a list of ints
        /// 
        /// the original string
        /// the list of ints
        private static IList ToDigitList(this string digits)
        {
            return digits.Select(d => d - 48).ToList();
        }
    
        /// 
        /// For a string of digits, compute the ending checkdigit 
        /// 
        /// The string of digits for which to compute the check digit
        /// the check digit
        public static string CheckDigit(this string digits)
        {
            return digits.ToDigitList().CheckDigit().ToString(CultureInfo.InvariantCulture);
        }
    
        /// 
        /// Return a string of digits including the checkdigit
        /// 
        /// The original string of digits
        /// the new string of digits including checkdigit
        public static string AppendCheckDigit(this string digits)
        {
            return digits + digits.CheckDigit(); 
        }
    
        /// 
        /// Returns true when a string of digits has a valid checkdigit
        /// 
        /// The string of digits to check
        /// true/false depending on valid checkdigit
        public static bool HasValidCheckDigit(this string digits)
        {
            return digits.ToDigitList().HasValidCheckDigit();
        }
    
        #endregion extension methods for strings
    
        #region extension methods for integers
    
        /// 
        /// Internal conversion function to convert int into a list of ints, one for each digit
        /// 
        /// the original int
        /// the list of ints
        private static IList ToDigitList(this int digits)
        {
            return digits.ToString(CultureInfo.InvariantCulture).Select(d => d - 48).ToList();
        }
    
        /// 
        /// For an integer, compute the ending checkdigit 
        /// 
        /// The integer for which to compute the check digit
        /// the check digit
        public static int CheckDigit(this int digits)
        {
            return digits.ToDigitList().CheckDigit();
        }
    
        /// 
        /// Return an integer including the checkdigit
        /// 
        /// The original integer
        /// the new integer including checkdigit
        public static int AppendCheckDigit(this int digits)
        {
            return digits * 10 + digits.CheckDigit();
        }
    
        /// 
        /// Returns true when an integer has a valid checkdigit
        /// 
        /// The integer to check
        /// true/false depending on valid checkdigit
        public static bool HasValidCheckDigit(this int digits)
        {
            return digits.ToDigitList().HasValidCheckDigit();
        }
    
        #endregion extension methods for integers
    
        #region extension methods for int64s
    
        /// 
        /// Internal conversion function to convert int into a list of ints, one for each digit
        /// 
        /// the original int
        /// the list of ints
        private static IList ToDigitList(this Int64 digits)
        {
            return digits.ToString(CultureInfo.InvariantCulture).Select(d => d - 48).ToList();
        }
    
        /// 
        /// For an integer, compute the ending checkdigit 
        /// 
        /// The integer for which to compute the check digit
        /// the check digit
        public static int CheckDigit(this Int64 digits)
        {
            return digits.ToDigitList().CheckDigit();
        }
    
        /// 
        /// Return an integer including the checkdigit
        /// 
        /// The original integer
        /// the new integer including checkdigit
        public static Int64 AppendCheckDigit(this Int64 digits)
        {
            return digits * 10 + digits.CheckDigit();
        }
    
        /// 
        /// Returns true when an integer has a valid checkdigit
        /// 
        /// The integer to check
        /// true/false depending on valid checkdigit
        public static bool HasValidCheckDigit(this Int64 digits)
        {
            return digits.ToDigitList().HasValidCheckDigit();
        }
    
        #endregion extension methods for int64s
    }
    

    Here are some XUnit test cases that show how the extension methods work.

    public class CheckDigitExtensionShould
    {
        [Fact]
        public void ComputeCheckDigits()
        {
            Assert.Equal(0, (new List { 0 }).CheckDigit());
            Assert.Equal(8, (new List { 1 }).CheckDigit());
            Assert.Equal(6, (new List { 2 }).CheckDigit());
    
            Assert.Equal(0, (new List { 3, 6, 1, 5, 5 }).CheckDigit());
            Assert.Equal(0, 36155.CheckDigit());
            Assert.Equal(8, (new List { 3, 6, 1, 5, 6 }).CheckDigit());
            Assert.Equal(8, 36156.CheckDigit());
            Assert.Equal(6, 36157.CheckDigit());
            Assert.Equal("6", "36157".CheckDigit());
            Assert.Equal("3", "7992739871".CheckDigit());
        }
    
        [Fact]
        public void ValidateCheckDigits()
        {
            Assert.True((new List { 3, 6, 1, 5, 6, 8 }).HasValidCheckDigit());
            Assert.True(361568.HasValidCheckDigit());
            Assert.True("361568".HasValidCheckDigit());
            Assert.True("79927398713".HasValidCheckDigit());
        }
    
        [Fact]
        public void AppendCheckDigits()
        {
            Console.WriteLine("36156".CheckDigit());
            Console.WriteLine("36156".AppendCheckDigit());
            Assert.Equal("361568", "36156".AppendCheckDigit());
            Assert.Equal("79927398713", "7992739871".AppendCheckDigit());
        }
    }
    

提交回复
热议问题