中文简体数字转换为阿拉伯数字(大家帮看下有没有反例,并提出改进意见)
这是界面!,以下为代码!大家帮忙找下反例,能指出程序的不足就更好了,小弟多多努力!
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Text.RegularExpressions; 10 11 namespace ChinesNumToint 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void btnChange_Click(object sender, EventArgs e) 21 { 22 //零,一,二,三,四,五,六,七,八,九,十,百,千,万 23 //一万一千一百一十一 24 //一万零一 25 //二零零二 26 //五百六十万 27 // 28 string CNum= txtCNum.Text; 29 if (IsCNUM(CNum)) 30 { 31 txtANum.Text = "中文数字有误"; 32 } 33 else if(CNum.Length>8) 34 { 35 txtANum.Text = "数太大了,哥萎了!"; 36 } 37 else 38 { 39 txtANum.Text = ToNumAll(txtCNum.Text).ToString(); 40 } 41 42 } 43 /// <summary> 44 /// 转换9999及以下的数字 45 /// </summary> 46 /// <param name="str"></param> 47 /// <returns></returns> 48 private double ToNum1(string str) 49 { 50 char[] cNums=new char[7]; 51 cNums[1]='千'; 52 cNums[3] = '百'; 53 cNums[5] = '十'; 54 if ((!str.Contains('千'))&&(!str.Contains('百'))&&(!str.Contains('十'))) 55 { 56 if (str.Length==1) 57 { 58 cNums[0]='零'; 59 cNums[2] = '零'; 60 cNums[4] = '零'; 61 cNums[6] = str[0]; 62 } 63 else if (str.Length==2) 64 { 65 cNums[0] = '零'; 66 cNums[2] = '零'; 67 cNums[4] = str[0]; 68 cNums[6] = str[1]; 69 } 70 else if(str.Length==3) 71 { 72 cNums[0] = '零'; 73 cNums[2] = str[0]; 74 cNums[4] = str[1]; 75 cNums[6] = str[2]; 76 } 77 else 78 { 79 cNums[0] = str[str.Length-4]; 80 cNums[2] = str[str.Length-3]; 81 cNums[4] = str[str.Length-2]; 82 cNums[6] = str[str.Length-1]; 83 } 84 85 } 86 else 87 { 88 if (str.Contains('千')) 89 { 90 cNums[0] = str[str.IndexOf('千') - 1]; 91 } 92 else 93 { 94 cNums[0] = '零'; 95 } 96 if (str.Contains('百')) 97 { 98 cNums[2] = str[str.IndexOf('百') - 1]; 99 } 100 else 101 { 102 cNums[2] = '零'; 103 } 104 if (str.Contains('十')) 105 { 106 cNums[4] = str[str.IndexOf('十') - 1]; 107 } 108 else 109 { 110 cNums[4] = '零'; 111 } 112 if (str[str.Length - 1] == '千' || str[str.Length - 1] == '百' || str[str.Length - 1] == '十') 113 { 114 cNums[6] = '零'; 115 } 116 else 117 { 118 cNums[6] = str[str.Length - 1]; 119 } 120 } 121 return 1000 * CharToNum(cNums[0]) + 100 * CharToNum(cNums[2]) +10* CharToNum(cNums[4]) + CharToNum(cNums[6]); 122 123 } 124 /// <summary> 125 /// 将单个中文数字转换为阿拉伯数字 126 /// </summary> 127 /// <param name="achar"></param> 128 /// <returns></returns> 129 private int CharToNum(char achar) 130 { 131 switch (achar) 132 { 133 case '零': 134 return 0; 135 case '一': 136 return 1; 137 case '二': 138 return 2; 139 case '三': 140 return 3; 141 case '四': 142 return 4; 143 case '五': 144 return 5; 145 case '六': 146 return 6; 147 case '七': 148 return 7; 149 case '八': 150 return 8; 151 case '九': 152 return 9; 153 default: 154 return -1; 155 } 156 } 157 /// <summary> 158 /// 判断是否包含非中文数字类字符 159 /// </summary> 160 /// <param name="str"></param> 161 /// <returns></returns> 162 private bool IsCNUM(string str) 163 { 164 return Regex.IsMatch(str,@"[^'零','一','二','三','四','五','六','七','八','九','十','百','千','万']."); 165 } 166 /// <summary> 167 /// 转换9999万9999以内的数字 168 /// </summary> 169 /// <param name="str"></param> 170 /// <returns></returns> 171 private int ToNumAll(string str) 172 { 173 if (str.Contains('万')) 174 { 175 string str1 = str.Substring(0, str.IndexOf('万')); 176 string str2 = str.Substring(str.IndexOf('万')+1); 177 return ToNum1(str1) * 10000 + ToNum1(str2); 178 } 179 else if(str.Length>4) 180 { 181 string str1 = str.Substring(0, str.Length - 4); 182 string str2 = str.Substring(str.Length - 4); 183 return ToNum1(str1) * 10000 + ToNum1(str2); 184 } 185 else 186 { 187 return ToNum1(str); 188 } 189 } 190 } 191 }