2010-08-24 109 views
0

如何使用C#自定义函数从一种符号的比例转换为另一种符号的比例。将符号从一种位置转换为另一种符号

abstract string Convert(string value, string fromBase, string toBase); 

- 基本符号

fromBase符号的字符串表示规模 - 字符串表示数字

至基站的基地 - 字符串表示的数值基础,你必须变换

PS 串表示符号的碱基位置刻度可包括任何符号表示数字升序

例如

值=“GSAK”

fromBase = “A,S,G,K” – four(4) is the base scale of notation (If write arabic figures: 0,1,2,3) 
    toBase= “0,1,2,3,4,5,6,7,8,9” – ten(10) is the base scale of notation 
    return value = “147” 

回答

1

我首先翻译输入值添加到其中一种数字数据类型(即长),然后编码为目标格式。解析和编码的类的实现草案(未经测试,当然不是最优):

public class Formatter 
{ 
    List<char> symbols; 
    int base; 

    public Formatter(string format) 
    { 
    string[] splitted = format.Split(","); 
    symbols = splitted.Select(x => x[0]).ToList(); 
    base = symbols.Size; 
    } 

    public long Parse(string value) 
    { 
    long result = 0; 
    foreach(char c in value) 
    { 
     long n = symbols.IndexOf(c); 
     result = result*base+n; 
    } 
    return result; 
    } 

    public string Encode(long value) 
    { 
    StringBuilder sb = new StringBuilder(); 
    while(value>0) 
    { 
     long n = value % base; 
     value /= base; 
     sb.Insert(0, symbols[n]); 
    } 
    return sb.ToString(); 
    } 
} 
+0

你的类只能转换为10的标度比例。我需要转换为一些符号的比例。 – 2010-08-24 16:29:24

+0

嗨@Grienders,您需要为每个比例创建此类的实例。比例应该由提供的符号列表的长度决定。你为什么说它只有10级? – Grzenio 2010-08-24 19:08:44

+0

你的类只转换成十进制符号,不是吗? – 2010-08-24 19:49:09

相关问题