c# Alpha 电话号码翻译器

c# Alpha Telephone Number Translator(c# Alpha 电话号码翻译器)

本文介绍了c# Alpha 电话号码翻译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业,程序将接受格式类似于 555-GET-FOOD 的任何电话号码.任务是将字母映射到数字,并将数字转换为等效的数字.
例如:A、B、C = 2;D、E、F = 3;等等……

I have a homework assignment where the program will accept any phone number in the format similar to 555-GET-FOOD. The task is to map the alphabetic letters to numbers and translate the number to its numeric equivalent.
For example: A, B, C = 2; D, E, F = 3; etc...

此时我们还没有介绍类或创建地图,因此这些不是可行的解决方案.本章确实涵盖了枚举,所以我正在努力使用枚举类型来解决.我设置了一种方法来验证数据(确保正确的字符数和连字符在正确的位置),并且确实可以正常工作.我设置了另一种方法来删除连字符,并且还使用了 ToUpper() 方法,这也可以正常工作,因此在这两种方法已经完成后,我使用数字设置了 foreach 循环.

We have not covered Classes or creating maps at this point so these would not be viable solutions. This chapter does cover the enum so I am working to solve using an Enumerated type. I have a method set up to validate the data (ensure the correct # of characters and the hyphens are in the correct place) and this does work correctly. I have another method set up to remove the hyphens and also uses the ToUpper() method and this also works correctly, so the foreach loop I have set up us using the number after these two methods have already finished.

我还设置了一种在转换发生后运行以重新添加连字符的方法,这也有效.

I have also set up a method to run after the conversion takes place to add the hyphens back in and this also works.

我已经尝试了几种方法来获得工作,并将它们留在注释掉它们可能是我需要使用的机会,因为我尝试使用 switch 语句,我现在只使用字母 A 设置,并计划完成剩余的信件,如果我能够让它工作的话.我认为我的问题是 foreach 循环使用的是 char 类型,而开关使用的是 int.在 foreach 循环中尝试代码时似乎是同样的问题,但我不知道如何解决,所以任何建议都值得赞赏.

I have tried several ways to get is to work and have left them in commented out on the chance they might be what I need to use, for my attempt with the switch statement I only set up with the letter A for now, and plan to finish the remaining letters if I am able to get this to work. I think the one my issues is the foreach loop is using a char type and the switch is using an int. Seems to be the same issue when trying the code inside the foreach loop but I am not sure how to fix so any suggestions are appreciated.

public enum AlphaNumber
{ 
    A=2, B=2, C=2, D=3, E=3, F=3, G=4, H=4, I=4, J=5, K=5, L=5, 
    M=6, N=6, O=6, P=7, Q=7, R=7, S=8, T=8, U=8, V=9, W=9, X=9, Y=9, Z=9
}
 private void UpdatePhone(AlphaNumber phone)
    {
        switch (phone)
        {
            case AlphaNumber.A:
           //return AlphaNumber.(int[])Enum.GetValues(typeof(AlphaNumber));
           //return (Enum.GetValues(typeof(AlphaNumber)));
           //   (int)ValueType;
                Enum.GetValues(typeof(AlphaNumber));
                break;                   
        }
private void translateButton_Click(object sender, EventArgs e)
    {
        numberLabel.Text = "";//Clear the numberLabel
        //Get a trimmed copy of the user's input.  
        string input = numberTextBox.Text.Trim();

        if (IsValidFormat(input))
        {
            Unformat(ref input);

            foreach (char ch in input)
            {
                if (char.IsLetter(ch))//                    {
                    ch = (char)Enums.AlphaNumber.Active;
                    //ch = (char)Enum.GetValues(typeof(AlphaNumber));
                    //ch = Enum.TryParse(AlphaNumber);
                   // ch = AlphaNumber.(int[])Enum.GetValues(typeof(AlphaNumber));
                    //UpdatePhone(ch);
                   MessageBox.Show("Character is char");                    }
            }

            TelephoneFormat(ref input);    
            numberLabel.Text = input;

我愿意接受任何建议并感谢提供的任何帮助.我需要帮助的地方是在 foreach 循环中,我正在查看每个值,如果它是一个字母,我想从枚举值中获取值并将 char 替换为数字值.

I am open to any suggestions and appreciate any help provided. Where I need help is inside the foreach loop, I am looking at each value and if it is a letter I want to get the value from the enum values and replace the char with the number value.

推荐答案

删除 enumswitch,改为查找地图.我创建了一个 .NET Fiddle 来演示此操作是否按要求进行here.

Drop the enum and the switch, and instead do a lookup against a map. I have created a .NET Fiddle that demonstrates this working as desired here.

但如果使用 Dictionary<char, char> 出于某种奇怪的原因是不可能的,那么我想你可以使用 这个版本——我在其中使用了enum.只是需要做更多的工作,而且活动部件越多,维护起来就越困难.

But if using a Dictionary<char, char> is for some odd reason out of question, then I suppose you could use this version -- in which I use the enum. It is just a lot more work and the more moving parts you have the harder it is to maintain.

C# 中,类型 enum 默认继承自 int,但您可以指定各种其他数字类型——例如 bytelong 等...其中一个关键要点是装箱/拆箱和从一种类型转换/转换为另一种类型的概念.如果您有一个 enum 定义为 enum Foo { Bar = 63 } 和 您尝试将其cast转换为char您希望得到什么?

In C# the type enum inherits from int by default, but you can specify various other numeric types -- like byte or long, etc... One of the key takeaways is the concept of boxing/unboxing and casting/converting from one type to another. If you have an enum defined as enum Foo { Bar = 63 } and you try to cast it to a char what would you expect to get?

这实际上会产生字符 ? -- 看看 ASCII 表 并找到 63 的 DEC 并查看它映射到 Char 列的内容.

This actually would result in the char ? -- take a look at the ASCII table and the find the DEC for 63 and look at what it maps to for the Char column.

这个疯狂的复杂混乱解决了这个问题:

The issue is fixed with this crazy complicated mess:

    public enum AlphaNumber
    { 
        A=2, B=2, C=2, D=3, E=3, F=3, G=4, H=4, I=4, J=5, K=5, L=5, 
        M=6, N=6, O=6, P=7, Q=7, R=7, S=8, T=8, U=8, V=9, W=9, X=9, Y=9, Z=9
    }

    public static class PhoneNumber
    {
        public static char ParseInput(char input)
        {
            if (input == '-' || char.IsDigit(input))
            {
                return input;
            }

            if (char.IsLetter(input))
            {
                var num = (AlphaNumber)(Enum.Parse(typeof(AlphaNumber), (char.IsLower(input) ? char.ToUpperInvariant(input) : input).ToString()));
                return ((int)num).ToString()[0];
            }

            return '';
        }
    }

这篇关于c# Alpha 电话号码翻译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:c# Alpha 电话号码翻译器

基础教程推荐