c#Alpha电话号码转换器 [英] c# Alpha Telephone Number Translator
问题描述
我有一个家庭作业,程序将接受与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.
我已经尝试了几种方法来获得工作,并留下他们注意到他们可能是我需要使用的机会,因为我尝试使用开关声明我现在只用字母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.
推荐答案
删除枚举
和
,而是对地图进行查找。我创建了一个.NET小提琴,演示如何按需要这里工作。
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>
是出于某些奇怪的原因,那么我想你可以使用此版本 - 其中我使用枚举
。这是更多的工作,更多的移动部分,你更难维护。
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#
类型枚举
默认从 int
继承,但您可以指定各种其他数字类型,如 byte
或 long
等...其中一个关键的手段是从一种类型的拳击/拆箱和铸造/转换的概念到另一个如果您有枚举
定义为枚举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?
这实际上会导致char ?
- 看看 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.
这个疯狂的复杂问题是固定的: p>
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 '\0';
}
}
这篇关于c#Alpha电话号码转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!