如何在C#计算数字学中使用字母和数字 [英] How do I use letters and numbers in C# calculation numerology

查看:65
本文介绍了如何在C#计算数字学中使用字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将字母转换成数字用于我的数字计算并使用这个代码的平和:



I know how to convert letters to numbers for my numerology calculation and use this peace of code:

//Adress Analyse 5
public static number getAdressAnalyseNumber5(string firstnameCU)
{
    // First, we reduce each unit (First, and Lastname) of the Daily Name to a single-digit or a (masternumber?).
    //Next we add each of the resulting digits (or Master numbers) together and reduce the total again to a single-digit.
    string[] input = { firstnameCU };
    return simplifyUnits(input);
}



对于地址计算,我还需要包含数字0到9.

我该怎么做?

我是初学者,所以如果你能给我代码,我可以研究它,而不是给我一个提示,并且必须找到自己。



灵感的问候,



Wilco



添加了代码来自评论


For the adress calculation I also need to include the numbers 0 to 9.
How can I do that?
I'm a beginner, so if you can give me the code, I can study on it, in stead of giving me a hint, and must find myself out please.

Inspired greetings,

Wilco

Added code from comment

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Mysterious_You_1
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Mysterious_You());
        }
    }  
}
    
// This is the Numerology_Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// nummerpaar
public class number
{
    private int _n; // niet-vereenvoudigd nummer
    private int _nSimple;
    
    public number(int n)
    {
        _n = n;
        _nSimple = numService.simplify(n);
    }
    
    public int simplifiedNumber
    {
        get
        {
            return _nSimple;
        }
    }
    
    public int n
    {
        get
        {
            return _n;
        }
    }
    
    public override string ToString()
    {
        return _n == _nSimple ? _n.ToString() : _n.ToString() + '/' + _nSimple.ToString();
    }
}

abstract class numService
{
    private static List vowels = new List { 'a', 'e', 'i', 'o', 'u' };
    
    
    This is the method as example in the numerology_service.cs
    
    //Adress Analyse 1
    public static number getAdressAnalyseNumber1(string firstnameCU)
    {
        //this method works for the calculation of the name of somebody, and I will implement the numbers of the adress to work
    
        // First, we reduce each unit (First, and Lastname) of the Daily Name to a single-digit or a (masternumber?).
        //Next we add each of the resulting digits (or Master numbers) together and reduce the total again to a single-digit.
        string[] input = { firstnameCU };
        return simplifyUnits(input);
    }


// This is the Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mysterious_You_1
{
    public partial class Mysterious_You : Form
    {
        public Mysterious_You()
        {
            InitializeComponent();

            // This is the part of the Form, this works for the name.

            // I want the outcome of the different calculations appear in the outcome
            // boxes in the form in the same way as Life_Pat_outcome.Text

            private void Get_custom_report_Click(object sender, EventArgs e)
            {
                Adress_outcome_1.Text = numService.getAdressAnalyseNumber1(Adress_1.Text).ToString();
            }





我的尝试:



见上文,我只是编程的初学者,但没有时间深入了解它。对我来说,让这个程序尽快运行并了解所有事情非常重要。



What I have tried:

See text above, I am just a beginner in programming, but don't have the time dive deep in it right know. It is important for me to get this programm running as soon as possible, and learn all doing.

推荐答案

编写新解决方案比尝试理解你更容易代码



这个类提供了一些用于计算的静态方法

It was easier to write a new solution than try to understand your code

This class provides some static methods that does the calculations
public class MyNumerology
{
    /// <summary>
    /// Converts letters to number according to the following rules:
    /// a, j, s	=	1
    /// b, k, t	=	2	
    /// c, l, u	=	3	
    /// d, m, v	=	4	
    /// e, n, w	=	5	
    /// f, o, x	=	6	
    /// g, p, y	=	7	
    /// h, q, z	=	8	
    /// i, r	=	9	
    /// </summary>
    /// <param name="input">A string that can contain both letters and digits.</param>
    /// <returns>An array representing a digit for each character</returns>
    public static int[] CharactersToDigits(string input)
    {
        string s = input.ToLower();

        int val = 0;
        List<int> digits = new List<int>();
        foreach (char c in input.ToLower())
        {
            if (char.IsDigit(c))
            {
                // 48 is the decimal ASCII value for the digit 0
                val = Convert.ToInt32(c) - 48;
            }
            else if (char.IsLetter(c))
            {
                // Normalize the character and do modulus calculation (%)
                // This will give the value between 1 and 9 for any small letter 
                val = (((int)c - 'a') % 9) + 1;     
            }
            else
            {
                // [UPDATE]
                // Ignore everything but letters and numbers
                continue;

                // Remove this line. Just there to show what was changed
                // throw new Exception(string.Format("Unsupported character: '{0}'.", c));
            }

            digits.Add(val);
        }
        return digits.ToArray();
    }

    /// <summary>
    /// Recursive function that calculates the sum of the individual digits
    /// in the input number as long as the input is greater than or equal to 10
    /// </summary>
    /// <example>145 -> 1 + 4 + 5 = 10 -> 1 + 0 -> 1</example>
    /// <param name="number"></param>
    /// <returns>The simplified sum</returns>
    public static int SimplifyNumber(int number)
    {
        // Exit condition
        if (number < 10)
            return number;

        int sum = 0;
        int tmp = number;
        while (tmp > 0)
        {
            sum += tmp % 10;    // Modulo 10
            tmp = tmp / 10;     // Integer division
        }
        return SimplifyNumber(sum);
    }

    [UPDATE]
    public static int DoItAllInOneGo(string input)
    {
        int[] numbers = CharactersToDigits(input);
        int sum = numbers.Sum();
        return SimplifyNumber(sum);
    }
}





这是如何使用它:



This is how to use it:

int[] numbers = MyNumerology.CharactersToDigits("1535threevillageroadwestonfl33326");
int sum = numbers.Sum();  // Requires using System.Linq;
int result = MyNumerology.SimplifyNumber(sum);





[更新]

这是OP正在使用的实际变量,所以我添加了一个匹配的示例。



//而不是



[UPDATE]
This is the actual variables the OP is using, so I added an example to match that.

// Instead of

Adress_outcome_4.Text = numService.getAdressAnalyseNumber4(Adress_4.Text).ToString();



you


you do

Adress_outcome_4.Text = DoItAllInOneGo(Adress_4.Text).ToString();


这篇关于如何在C#计算数字学中使用字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆