2012-06-30 49 views
7

Tôi chắc rằng điều này đã được thực hiện hàng trăm lần, nhưng tôi hy vọng có một cách thực sự đơn giản để thực hiện việc này. Tôi muốn thay đổi từ thành int.Chuyển từ (chuỗi) thành Int

Giống như ví dụ sau

One = 1 
Two = 2 
Three = 3 

Vì vậy, về cơ bản nếu tôi có chuỗi "One" nó được chuyển đổi sang 1, ngay cả nếu tôi có thể lấy lại một chuỗi "1" Tôi chỉ có thể chuyển đổi đó.

+3

bao nhiêu số bạn cần phải hỗ trợ? 10 trở lên? – gdoron

+0

Về cơ bản, tất cả các con số đều giống như nếu tôi có một trăm nghìn sáu trăm –

+0

http://www.daniweb.com/software-development/csharp/threads/209656/convert-words-into-numbers – JDPeckham

Trả lời

19

Did này cho vui ... có lẽ nhiều trường hợp cạnh đó sẽ thất bại ...

private static Dictionary<string,long> numberTable= 
    new Dictionary<string,long> 
     {{"zero",0},{"one",1},{"two",2},{"three",3},{"four",4}, 
     {"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9}, 
     {"ten",10},{"eleven",11},{"twelve",12},{"thirteen",13}, 
     {"fourteen",14},{"fifteen",15},{"sixteen",16}, 
     {"seventeen",17},{"eighteen",18},{"nineteen",19},{"twenty",20}, 
     {"thirty",30},{"forty",40},{"fifty",50},{"sixty",60}, 
     {"seventy",70},{"eighty",80},{"ninety",90},{"hundred",100}, 
     {"thousand",1000},{"million",1000000},{"billion",1000000000}, 
     {"trillion",1000000000000},{"quadrillion",1000000000000000}, 
     {"quintillion",1000000000000000000}}; 
public static long ToLong(string numberString) 
{ 
    var numbers = Regex.Matches(numberString, @"\w+").Cast<Match>() 
     .Select(m => m.Value.ToLowerInvariant()) 
     .Where(v => numberTable.ContainsKey(v)) 
     .Select(v => numberTable[v]); 
    long acc = 0,total = 0L; 
    foreach(var n in numbers) 
    { 
     if(n >= 1000) 
     { 
      total += (acc * n); 
      acc = 0; 
     } 
     else if(n >= 100){ 
      acc *= n; 
     } 
     else acc += n;   
    } 
    return (total + acc) * (numberString.StartsWith("minus", 
      StringComparison.InvariantCultureIgnoreCase) ? -1 : 1); 
} 
+0

Tại sao bạn có đôi 'tiếp tục '? Bạn chỉ có thể sử dụng 'else'. (và 1, tất nhiên.) – Ryan

+2

Có, tôi đồng ý ... tốt hơn với các phát biểu khác (và cũng làm giảm kích thước mã và thỏa mãn sự ám ảnh OCD của tôi mà không có thanh cuộn trong câu trả lời của tôi !!!) – spender

+0

Lưu ý rằng 'nhanh brown fox' và 'zero' đều trả về' 0'. –

11

Đây là phương pháp thực hiện điều đó. Nếu bạn cần một phạm vi rộng hơn, nó dễ dàng mở rộng; chỉ cần sử dụng một số long, một số ulong hoặc thậm chí là BigInt và thêm các mục khác vào từ điển modifiers.

static int ParseEnglish(string number) { 
    string[] words = number.ToLower().Split(new char[] {' ', '-', ','}, StringSplitOptions.RemoveEmptyEntries); 
    string[] ones = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; 
    string[] teens = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; 
    string[] tens = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; 
    Dictionary<string, int> modifiers = new Dictionary<string, int>() { 
     {"billion", 1000000000}, 
     {"million", 1000000}, 
     {"thousand", 1000}, 
     {"hundred", 100} 
    }; 

    if(number == "eleventy billion") 
     return int.MaxValue; // 110,000,000,000 is out of range for an int! 

    int result = 0; 
    int currentResult = 0; 
    int lastModifier = 1; 

    foreach(string word in words) { 
     if(modifiers.ContainsKey(word)) { 
      lastModifier *= modifiers[word]; 
     } else { 
      int n; 

      if(lastModifier > 1) { 
       result += currentResult * lastModifier; 
       lastModifier = 1; 
       currentResult = 0; 
      } 

      if((n = Array.IndexOf(ones, word) + 1) > 0) { 
       currentResult += n; 
      } else if((n = Array.IndexOf(teens, word) + 1) > 0) { 
       currentResult += n + 10; 
      } else if((n = Array.IndexOf(tens, word) + 1) > 0) { 
       currentResult += n * 10; 
      } else if(word != "and") { 
       throw new ApplicationException("Unrecognized word: " + word); 
      } 
     } 
    } 

    return result + currentResult * lastModifier; 
} 
Các vấn đề liên quan