这个题目是这样的: 需要将 中文的数字字符串 经过转换为数字
也就是说 一万二千三百四十五 结果为 12345 是这样的 比比谁的算法最效率!
也就是说 一万二千三百四十五 结果为 12345 是这样的 比比谁的算法最效率!
解决方案 »
- “System.Net.Dns.Resolve(string)”已过时
- SQL语句命令行参数添加的问题?
- DataGridView控件中怎么实现日期单元格列出现日期选择
- PaintEventArgse函数如何传入?
- picturebox_mousemove()里参数获取问题
- 请问如何在类里面得到登陆页面的sesssion参数
- .net下有宽字符和窄字符的问题吗?
- 如何用C#下的Raw Socket编程实现网络封包监视
- Array.BinarySearch()用来查找字符串为什么找不到。
- 软件公司哪个开发的比较多,b/s 还是C/s结构
- 问下名位.net里事件处理的顺序,很简单的
- C#Form的图标如何在通知区域里显示???
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
private static string str;
private static string[] info={"百","千"};
private static string [] hanzi={"十","一","二","三","四","五","六","七","八","九","零"};
private static string result="";
private static string all="";
private static string doout="";
private static string[] tage={"万","亿"};
private static bool b=true;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Console.Write("请输入中文数字:");
str=System.Console.ReadLine();
string s=str;
string alls=str;
for(int i=0;i<tage.Length;i++)
{
b=true;
all="";
if(alls.IndexOf(tage[i])==alls.Length-1)
{
if(i==0)
{
s="*";
}
else if(i==1)
{
s="**";
}
alls=alls.Remove(alls.IndexOf(tage[i]),1);
dodo(s);
doout=all+doout;
}
else if(alls.IndexOf(tage[i])>0 && alls.IndexOf(tage[i])!=alls.Length-1)
{
s=alls.Substring(alls.IndexOf(tage[i])+1);
alls=alls.Remove(alls.IndexOf(tage[i]),s.Length+1);
dodo(s);
doout=all+doout;
}
}
all="";
dodo(alls);
doout=all+doout; System.Console.WriteLine("结果是:"+int.Parse(doout).ToString());
System.Console.ReadLine();
} public static void dodo(string s)
{
int i=0;
string todo="";
if(s=="*")
{
todo="零零零零";
make(todo);
}
else if(s=="**")
{
todo="零零零零零零零零";
make(todo);
}
else
{
while (s.Length>0)
{
for(i=0;i<info.Length;i++)
{
if(s.IndexOf(info[i])>0)
{
todo=s.Substring(s.IndexOf(info[i])+1);
s=s.Remove(s.IndexOf(info[i]),todo.Length+1);
if(todo=="")
{
if(i==0)
{
todo="零零";
}
else if(i==1)
{
todo="零零零";
}
else
{
todo="零";
}
}
b=false;
break;
}
}
if(i==info.Length)
{
make(s);
break;
}
make(todo);
}
}
} public static void make(string s)
{
result="";
if(s.Equals("十"))
{
result="10";
}
else
{
foreach(char c in s)
{
for(int i=0;i<hanzi.Length;i++)
{
if(c.ToString()==hanzi[i])
{
if(i==0)
{
if(s.IndexOf(hanzi[i])+1==s.Length)
{
result+=i.ToString();
}
}
else if(i==10)
{
result+="0";
}
else
{
result+=i.ToString();
}
}
}
}
}
for(int k=result.Length;k<4&& b ;k++)
{
result="0"+result;
}
all=result+all;
}
}
}
struct Number
{
public int this[char c]
{
get
{
switch (c)
{
case '一':
return 1;
case '二':
return 2;
case '三':
return 3;
case '四':
return 4;
case '五':
return 5;
case '六':
return 6;
case '七':
return 7;
case '八':
return 8;
case '九':
return 9;
case '零':
return 0;
default:
return -1;
}
}
}
}
struct Unit
{
public int this[char c]
{
get
{
switch (c)
{
case '十':
return 10;
case '百':
return 100;
case '千':
return 1000;
case '万':
return 10000;
case '亿':
return 100000000;
default:
return 1;
}
}
}
}
//下面是具体算法部分,思路非常简单就是从右至左分析中文的数字,遇到'位'计记录下来,非'位'就乘以位并加入到结果result中.firstUnit单表第一级位,second代表二级位,例如:三十八万 十是secondUnit,万是firsUnit
class Parser_CNum
{
public long Parse(string cnum)
{
Unit unit;
Number number;
cnum = Regex.Replace(cnum, "\\s+", "");
long firstUnit = 1; //一级单位
long secondUnit = 1; //二级单位
long tmpUnit = 1;
long result = 0; for (int i = cnum.Length - 1; i > -1; --i)
{
tmpUnit = unit[cnum[i]];
if (tmpUnit > firstUnit)
{
firstUnit = tmpUnit;
secondUnit = 1;
continue;
}
else if (tmpUnit > secondUnit)
{
secondUnit = tmpUnit;
continue;
}
result += firstUnit * secondUnit * number[cnum[i]];
}
return result;
}
}
//最后测试
public static void Main()
{
Parser_CNum parser = new Parser_CNum();
Debug.Assert(parser.Parse("一万二千三百四十五") == 12345, "一万二千三百四十五 解析错误,为:" + parser.Parse("一万二千三百四十五"));
Debug.Assert(parser.Parse("一千二百零三") == 1203, "一千二百零三 解析错误,为:" + parser.Parse("一千二百零三"));
Debug.Assert(parser.Parse("一千零三十二") == 1032, "一千零三十二 解析错误,为:" + parser.Parse("一千零三十二"));
Debug.Assert(parser.Parse("一千二百三十四亿 五千六百七十八万 九千零一") == 123456789001, "一千二百三十四亿 五千六百七十八万 九千零一 解析错误,为:" + parser.Parse("一千二百三十四亿 五千六百七十八万 九千零一"));
}注:因为使用了long类型性能会有损失
public static void Main()
{
int [] arr1 =new arr1 [9];
char number ;
int j =0;
string sum ="一万三千四百二十七" ;
char [] arr2 = sum.ToChar() ;
for (i = 0 ; i < arr2.Length ;i++)
{
number = arr2[i] ;
swtch(number)
{
case '一':
arr[j] = 1 ;
j++;
break;
case '二':
arr[j] = 2 ;
j++;
break;
case '三':
arr[j] = 3 ;
j++;
break;
case '四':
arr[j] = 4 ;
j++;
break;
case '五':
arr[j] = 5 ;
j++;
break;
case '六':
arr[j] = 6 ;
j++;
break;
case '七':
arr[j] = 7 ;
j++;
break;
case '八':
arr[j] = 8 ;
j++;
break;
case '九':
arr[j] = 9 ;
j++;
break;
case '十':
break;
case '百':
break;
case '千':
break;
case '万':
break;
case '亿':
break;
}
}
}
虽然都是 if 语句 但是谁的 if 语句更有效率呢?
swtch case 不比if 有效率 只是代码写起来简单而已.....
Debug.Assert(parser.Parse("十") == 10, "十 解析错误,为:" + parser.Parse("一万二千三百四十五"));
这个转换 你不能实现 BUG!!!
没注意还有单个“十”,“十五”这种,这个非常好解决用正则是补一个“一”就行了
cnum = Regex.Replace(cnum, "^[十百千万亿]", delegate(Match m) { return "一" + m.Value; });
double Tras(string str1) //最长处理数据为万亿即9999999999999.99
{
if (str1.Length > 16)
return -1; //传递过来的值不合格
double dblSum = 0;
string strInclude = "(亿|万|仟|千|百|佰|零|壹|一|貮|二|叁|三|肆|四|伍|五|陆|六|柒|七|捌|八|玖|九|拾|十|点|元|块|圆|毛|角|分)"; //只允许字符串出现这些数字
if (Regex.Replace(str1, strInclude, "").Length > 0)
return -1; //传递过来的值不合格
dblSum = getNumber(getspliter(".+?亿", ref str1)) * 100000000;
dblSum += getNumber(getspliter(".+?万", ref str1)) * 10000;
dblSum += getNumber(str1);
dblSum += getNumber(getspliter(".+?(点|元|块|圆)", ref str1));
dblSum += getNumber(getspliter(".+?(角|毛)", ref str1)) * 0.1;
dblSum += getNumber(getspliter(".+分", ref str1)) * 0.01;
return dblSum;
}
string getspliter(string regSring,ref string inputstring)
{
Regex reg = new Regex(regSring);
Match mat = reg.Match(inputstring);
if (mat.Success)
{
inputstring = Regex.Replace(inputstring, regSring, "");
return mat.Value.Substring(0,mat.Value.Length-1);
}
else
return "";
} int getNumber(string str1) //传过来最长4位数字符,转成数字
{
int i = 0;
i += getNumberByUnicode(getspliter(".+?万", ref str1)) * 10000;
i += getNumberByUnicode(getspliter(".+?(仟|千)", ref str1))*1000;
i += getNumberByUnicode(getspliter(".+?(百|佰)", ref str1)) * 100;
i += getNumberByUnicode(getspliter(".+?(拾|十)", ref str1)) * 10;
i += getNumberByUnicode(str1);
return i;
}
int getNumberByUnicode(string c1)
{
switch (c1)
{
case "":
return 0; case "壹":
case "一":
return 1; case "贰":
case "二":
return 2; case "叁":
case "三":
return 3; case "肆":
case "四":
return 4; case "伍":
case "五":
return 5; case "陆":
case "六":
return 6; case "柒":
case "七":
return 7; case "捌":
case "八":
return 8; case "玖":
case "九":
return 9; default:
return -1;
}
}调用例子
string strT = "一十一万二千三百四十五元四毛三分";
this.Text = Tras(strT).ToString();注意:112345.45的写法,不能直接写成十一万,而应写成一十一万(有点类似于写支票,呵呵)
仅提供思路,解决问题按实际需要。
return -1; //传递过来的值不合格 这个判断可以去掉
楼主的算法:5.8秒 ~ 6秒
我的算法: 470毫秒 ~ 500毫秒 Stopwatch sw1 = new Stopwatch();
sw1.Start();
for (int i = 0; i < 100000; ++i)
{
Parse("一万二千三百四十五");
Parse("一千二百零三");
Parse("一千零三十二");
Parse("一千零二");
Parse("一千二百");
Parse("一千二百三十四亿 五千六百七十八万 九千零一");
Parse("十五");
Parse("五十");
}
sw1.Stop();
Console.WriteLine(sw1.ElapsedMilliseconds);楼主你算法慢的主要原因就是使用了string进行计算最后再做转型,可能改用StringBuilder要好一些
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//开始转换
//
if (this.textBox1.Text == "")
this.label1.Text = "请先输入大写数字";
else
{
string str_Number = this.textBox1.Text;
this.label1.Text = "";
for (int i = 0; i < str_Number.Length; i += 2)
{
//this.label1.Text += str_Number[i];
switch (str_Number[i])
{
case '一':
this.label1.Text += "1";
break;
case '二':
this.label1.Text += "2";
break; case '三':
this.label1.Text += "3";
break; case '四':
this.label1.Text += "4";
break; case '五':
this.label1.Text += "5";
break; case '六':
this.label1.Text += "6";
break; case '七':
this.label1.Text += "7";
break; case '八':
this.label1.Text += "8";
break; case '九':
this.label1.Text += "9";
break; case '零':
this.label1.Text += "0";
break; default:
this.label1.Text = "您输入了错误字符";
i = str_Number.Length;
break;
}
}
}
}
}
}
public double DecodeMoneyCn(string AText)
{
AText = AText.Replace("亿亿", "兆");
AText = AText.Replace("万万", "亿");
AText = AText.Replace("点", "元");
AText = AText.Replace("块", "元");
AText = AText.Replace("毛", "角");
double vResult = 0;
double vNumber = 0; // 当前数字
double vTemp = 0;
int vDecimal = 0; // 是否出现小数点
foreach (char vChar in AText)
{
int i = "零一二三四五六七八九".IndexOf(vChar);
if (i < 0) i = "洞幺两三四五六拐八勾".IndexOf(vChar);
if (i < 0) i = "零壹贰叁肆伍陆柒捌玖".IndexOf(vChar);
if (i > 0)
{
vNumber = i;
if (vDecimal > 0)
{
vResult += vNumber * Math.Pow(10, -vDecimal);
vDecimal++;
vNumber = 0;
}
}
else
{
i = "元十百千万亿".IndexOf(vChar);
if (i < 0) i = "整拾佰仟万亿兆".IndexOf(vChar);
if (i == 5) i = 8;
if (i == 6) i = 12;
if (i > 0)
{
if (i >= 4)
{
vTemp += vNumber;
if (vTemp == 0) vTemp = 1;
vResult += vTemp * Math.Pow(10, i);
vTemp = 0;
}
else vTemp += vNumber * Math.Pow(10, i);
}
else
{
i = "元角分".IndexOf(vChar);
if (i > 0)
{
vTemp += vNumber;
vResult += vTemp * Math.Pow(10, -i);
vTemp = 0;
}
else if (i == 0)
{
vTemp += vNumber;
vResult += vTemp;
vDecimal = 1;
vTemp = 0;
}
}
vNumber = 0;
}
}
return vResult + vTemp + vNumber;
}private void button1_Click(object sender, EventArgs e)
{
string[] vTestText = {
"十二点五六",
"一亿零一万零五",
"四万万",
"九十八亿七千六百五十四万三千二百一十",
"五元一角四分", "壹佰元整",
"三千五百万",
"九块二毛"};
foreach (string vText in vTestText)
{
Console.WriteLine("DecodeMoneyCn(\"{0}\")={1}", vText,
DecodeMoneyCn(vText));
}
//输出
//DecodeMoneyCn("十二点五六")=2.56
//DecodeMoneyCn("一亿零一万零五")=100010005
//DecodeMoneyCn("四万万")=400000000
//DecodeMoneyCn("九十八亿七千六百五十四万三千二百一十")=9876543210
//DecodeMoneyCn("五元一角四分")=5.14
//DecodeMoneyCn("壹佰元整")=100
//DecodeMoneyCn("三千五百万")=35000000
//DecodeMoneyCn("九块二毛")=9.2
//DecodeMoneyCn("一兆")=1000000000000
}
public double DecodeMoneyCn(string AText)
{
if (AText.IndexOf("十") == 0) AText = "一" + AText; // 勘误
AText = AText.Replace("亿亿", "兆");
AText = AText.Replace("万万", "亿");
AText = AText.Replace("点", "元");
AText = AText.Replace("块", "元");
AText = AText.Replace("毛", "角");
double vResult = 0;
double vNumber = 0; // 当前数字
double vTemp = 0;
int vDecimal = 0; // 是否出现小数点
foreach (char vChar in AText)
{
int i = "零一二三四五六七八九".IndexOf(vChar);
if (i < 0) i = "洞幺两三四五六拐八勾".IndexOf(vChar);
if (i < 0) i = "零壹贰叁肆伍陆柒捌玖".IndexOf(vChar);
if (i > 0)
{
vNumber = i;
if (vDecimal > 0)
{
vResult += vNumber * Math.Pow(10, -vDecimal);
vDecimal++;
vNumber = 0;
}
}
else
{
i = "元十百千万亿".IndexOf(vChar);
if (i < 0) i = "整拾佰仟万亿兆".IndexOf(vChar);
if (i == 5) i = 8;
if (i == 6) i = 12;
if (i > 0)
{
if (i >= 4)
{
vTemp += vNumber;
if (vTemp == 0) vTemp = 1;
vResult += vTemp * Math.Pow(10, i);
vTemp = 0;
}
else vTemp += vNumber * Math.Pow(10, i);
}
else
{
i = "元角分".IndexOf(vChar);
if (i > 0)
{
vTemp += vNumber;
vResult += vTemp * Math.Pow(10, -i);
vTemp = 0;
}
else if (i == 0)
{
vTemp += vNumber;
vResult += vTemp;
vDecimal = 1;
vTemp = 0;
}
}
vNumber = 0;
}
}
return vResult + vTemp + vNumber;
}
private System.Windows.Forms.TextBox txtInfo;
private System.Windows.Forms.Label lblInfo;
private System.Windows.Forms.Label lblText;
private System.Windows.Forms.TextBox txtText; private string []tag={"万","亿"};
private string []unit={"十","百","千"};
private string []huan={"零","一","二","三","四","五","六","七","八","九"};
private Decimal d=0;
private string todo="";
private bool cou=true;
private void btnMake_Click(object sender, System.EventArgs e)
{
if(this.txtInfo.Text!=string.Empty)
{
d=0;
todo=this.txtInfo.Text;
makeTag(todo);
this.txtText.Text=d.ToString();
MessageBox.Show("用了:"+(a-b));
}
else
{
MessageBox.Show("请输入要转换的中文钱串(必须是规范的书写格式)");
}
}
private void makeTag(string s)
{
string make2="";
for(int i=0;i<tag.Length;i++)
{
if(s.IndexOf(tag[i])>=0)
{
make2=s.Substring(s.IndexOf(tag[i]));
s=s.Remove(s.IndexOf(tag[i]),make2.Length);
calculateResult(make2,i,true);
}
}
for(int k=(tag.Length-1);k>=0;k--)
{
if(todo.IndexOf(tag[k])>=0)
{
calculateResult(s,k+1,false);
break;
}
}
if(cou)
{
calculateResult(s);
}
}
private void calculateResult(string s,int w,bool pd)
{
if(s.IndexOf("零")>=0)
{
string zero=s.Substring(s.IndexOf("零")+1);
calculateResult(zero);
s=s.Remove(s.IndexOf("零"),zero.Length+1);
}
bool how=true;
cou=false;
int small=0;
string num="";
if(w<tag.Length && pd)
{
s=s.Remove(s.IndexOf(tag[w]),tag[w].Length);
}
for(int i=0;i<unit.Length;i++)
{
if(s.IndexOf(unit[i])>=0)
{
how=false;
if(i==0 && s.IndexOf(unit[0])<s.Length-1)
{
num=s.Substring(s.IndexOf(unit[i])+1,1);
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=j;
}
}
}
try
{
num=s.Substring(s.IndexOf(unit[i])-1,1);
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=int.Parse(Math.Pow(10,i+1).ToString()) * j;
}
}
}
catch(Exception e)
{
small+=int.Parse(Math.Pow(10,i+1).ToString()) * 1;
}
}
}
if(how)
{
num=s;
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=j;
}
}
}
d+=small* Decimal.Parse(Math.Pow(10000,w).ToString());
}
private void calculateResult(string s)
{
if(s.IndexOf("零")>=0)
{
string zero=s.Substring(s.IndexOf("零")+1);
calculateResult(zero);
s=s.Remove(s.IndexOf("零"),zero.Length+1);
}
bool how=true;
int small=0;
string num="";
for(int i=0;i<unit.Length;i++)
{
if(s.IndexOf(unit[i])>=0)
{
how=false;
if(i==0 && s.IndexOf(unit[0])<s.Length-1)
{
num=s.Substring(s.IndexOf(unit[i])+1,1);
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=j;
}
}
}
try
{
num=s.Substring(s.IndexOf(unit[i])-1,1);
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=int.Parse(Math.Pow(10,i+1).ToString()) * j;
}
}
}
catch(Exception e)
{
small+=int.Parse(Math.Pow(10,i+1).ToString()) * 1;
}
}
}
if(how)
{
num=s;
for(int j=0;j<huan.Length;j++)
{
if(num==huan[j])
{
small+=j;
}
}
}
d+=small;
}
}
}
if(a==1)
{
}else if(a==2)
{
}else if(a==3)
{
}else if(a==4)
{
}
和
switch(a)
{
case 1:
case 2:
case 3:
case 4:
}
谁更效率呢?
{
long returnValue=0;
long tempLong = 0;
int intCase = 0;
for (int i = 0; i < strLong.Length; i++)
{
switch (strLong[i].ToString())
{
case "一":
intCase = 1;
break;
case "二":
intCase = 2;
break;
case "三":
intCase = 3;
break;
case "四":
intCase = 4;
break;
case "五":
intCase = 5;
break;
case "六":
intCase = 6;
break;
case "七":
intCase = 7;
break;
case "八":
intCase = 8;
break;
case "九":
intCase = 9;
break;
case "十":
tempLong = tempLong + intCase * 10;
intCase = 0;
break;
case "百":
tempLong = tempLong + intCase * 100;
intCase = 0;
break;
case "千":
tempLong = tempLong + intCase * 1000;
intCase = 0;
break;
case "万":
returnValue = returnValue + (tempLong + intCase) * 10000;
tempLong = 0;
intCase = 0;
break;
case "亿":
returnValue = returnValue + (tempLong + intCase) * 100000000;
tempLong = 0;
intCase = 0;
break;
default:
break;
}
}
return returnValue+tempLong + intCase;
}十万,需要写成一十万 其他的未知判断没写
效率如何也没测。应该是可以实现的
if (intCase == 0)
{
returnValue = returnValue * 10000 + (tempLong + intCase) * 10000;
}
else
{
returnValue = returnValue + (tempLong + intCase) * 10000;
}
tempLong = 0;
intCase = 0;
break;
case "亿":
if (intCase == 0)
{
returnValue = returnValue * 100000000 + (tempLong + intCase) * 100000000;
}
else
{
returnValue = returnValue + (tempLong + intCase) * 100000000;
}
tempLong = 0;
intCase = 0;
break;
{
static string[][] nums = new string[][] { new string[]{ "一", "1" }, new string[]{ "二", "2" }, new string[]{ "三", "3" }, new string[]{ "四", "4" }, new string[]{ "五", "5" }, new string[]{ "六", "6" }, new string[]{ "七", "7" }, new string[]{ "八", "8" }, new string[]{ "九", "9" } };
static string[][] units = new string[][] { new string[]{ "十", "1" }, new string[]{ "百", "2" }, new string[]{ "千", "3" }, new string[]{ "万", "4" }, new string[]{ "十万", "5" }, new string[]{ "百万", "6" }, new string[]{ "千万", "7" }, new string[]{ "億", "8" }, new string[]{ "十億", "9" }, new string[]{ "百億", "10" }, new string[]{ "千億", "11" }, new string[]{ "万億", "12" } };
static int[] intUnits = new int[20];
static void Main(string[] args)
{
string test = "一千零二";
string ans = "";
int index = 0;
bool flag = false;
string zeros = "";
string tempNum = "";
for (int i = 0; i < test.Length; i++)
{
if (i == test.Length - 1&&flag)
{
for (int j = 0; j < intUnits[index - 1] - 1; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
} if (test[i].ToString().Equals("零"))
{
flag = true;
continue;
}
string temp=GetNum(test[i].ToString()); if (!temp.Equals("error"))
{
if (!flag)
ans += temp;
else
tempNum = temp;
}
else
{
intUnits[index] = GetUnit(test[i].ToString());
index++; if (flag)
{
for (int j = 0; j < intUnits[index - 2] - intUnits[index-1]-1; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
}
if(i == test.Length - 1)
{
for (int j = 0; j < intUnits[index - 1] - intUnits[index] ; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
}
}
}
Console.WriteLine(ans);
Console.Read();
} static string GetNum(string a)
{
foreach (string[] num in nums)
{
if (a.Equals(num[0]))
return num[1];
}
return "error";
} static int GetUnit(string a)
{
foreach (string[] unit in units)
{
if (a.Equals(unit[0]))
return Int32.Parse(unit[1]);
}
return -1;
}
}写得太丑陋了,不过你的测试用例都通过了
public int tt(string input)
{
char[] word = input.ToCharArray();
int[] sta = new int[2];
int output = 0;
int flag = 0;
int num = 0;
int wei = 1;
if (word.Length == 1)
{
switch (word[0])
{
case '零':
return 0;
case '一':
return 1;
case '二':
return 2;
case '三':
return 3;
case '四':
return 4;
case '五':
return 5;
case '六':
return 6;
case '七':
return 7;
case '八':
return 8;
case '九':
return 9; }
} int j = 0;
for(int i = 0; i < word.Length; i++)
{
switch (word[i])
{
case '零':
wei = 0;
break;
case '一':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 1;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 1;
flag = 1;
}
else
{
num = 1;
flag = 0;
}
}
break;
case '二':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 2;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 2;
flag = 1;
}
else
{
num = 2;
flag = 0;
}
}
break;
case '三':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 3;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 3;
flag = 1;
}
else
{
num = 3;
flag = 0;
}
}
break;---------------------省略N字-------------------------------
···························································
case '九':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千')&& (word[i + 1] != '亿')))
{
output = output + 9;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 9;
flag = 1;
}
else
{
num = 9;
flag = 0;
}
}
break;
case '十':
wei = 10;
if (num != 0)
{
output = output + num * wei;
}
else
{
output = output + 10;
}
flag = 1;
break;
case '百':
wei = 100;
output = output + num * wei;
flag = 1;
break;
case '千':
wei = 1000;
output = output + num * wei;
flag = 1;
break;
case '万':
wei = 10000;
if (flag == 0)
{
output = output + num * wei;
}
else if (flag == 2)
{
output = (output + num) * wei;
}
else if (flag == 1)
{
output = output * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
}
flag = 1;
break;
case '亿':
wei = 100000000;
output = num * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
break;
default:
break;
}
} for (int i = 0; i < sta.Length; i++)
{
output += sta[i];
} return output;
}
public int tt(string input)
{
char[] word = input.ToCharArray();
int[] sta = new int[2];
int output = 0;
int flag = 0;
int num = 0;
int wei = 1;
if (word.Length == 1)
{
switch (word[0])
{
case '零':
return 0;
case '一':
return 1;
case '二':
return 2;
case '三':
return 3;
case '四':
return 4;
case '五':
return 5;
case '六':
return 6;
case '七':
return 7;
case '八':
return 8;
case '九':
return 9; }
} int j = 0;
for(int i = 0; i < word.Length; i++)
{
switch (word[i])
{
case '零':
wei = 0;
break;
case '一':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 1;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 1;
flag = 1;
}
else
{
num = 1;
flag = 0;
}
}
break;
case '二':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 2;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 2;
flag = 1;
}
else
{
num = 2;
flag = 0;
}
}
break;
case '三':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 3;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 3;
flag = 1;
}
else
{
num = 3;
flag = 0;
}
}
break;---------------------省略N字-------------------------------
···························································
case '九':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千')&& (word[i + 1] != '亿')))
{
output = output + 9;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 9;
flag = 1;
}
else
{
num = 9;
flag = 0;
}
}
break;
case '十':
wei = 10;
if (num != 0)
{
output = output + num * wei;
}
else
{
output = output + 10;
}
flag = 1;
break;
case '百':
wei = 100;
output = output + num * wei;
flag = 1;
break;
case '千':
wei = 1000;
output = output + num * wei;
flag = 1;
break;
case '万':
wei = 10000;
if (flag == 0)
{
output = output + num * wei;
}
else if (flag == 2)
{
output = (output + num) * wei;
}
else if (flag == 1)
{
output = output * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
}
flag = 1;
break;
case '亿':
wei = 100000000;
output = num * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
break;
default:
break;
}
} for (int i = 0; i < sta.Length; i++)
{
output += sta[i];
} return output;
}
public int tt(string input)
{
char[] word = input.ToCharArray();
int[] sta = new int[2];
int output = 0;
int flag = 0;
int num = 0;
int wei = 1;
if (word.Length == 1)
{
switch (word[0])
{
case '零':
return 0;
case '一':
return 1;
case '二':
return 2;
case '三':
return 3;
case '四':
return 4;
case '五':
return 5;
case '六':
return 6;
case '七':
return 7;
case '八':
return 8;
case '九':
return 9; }
} int j = 0;
for(int i = 0; i < word.Length; i++)
{
switch (word[i])
{
case '零':
wei = 0;
break;
case '一':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 1;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 1;
flag = 1;
}
else
{
num = 1;
flag = 0;
}
}
break;
case '二':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 2;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 2;
flag = 1;
}
else
{
num = 2;
flag = 0;
}
}
break;
case '三':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 3;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 3;
flag = 1;
}
else
{
num = 3;
flag = 0;
}
}
break;---------------------省略N字-------------------------------
···························································
case '九':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千')&& (word[i + 1] != '亿')))
{
output = output + 9;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 9;
flag = 1;
}
else
{
num = 9;
flag = 0;
}
}
break;
case '十':
wei = 10;
if (num != 0)
{
output = output + num * wei;
}
else
{
output = output + 10;
}
flag = 1;
break;
case '百':
wei = 100;
output = output + num * wei;
flag = 1;
break;
case '千':
wei = 1000;
output = output + num * wei;
flag = 1;
break;
case '万':
wei = 10000;
if (flag == 0)
{
output = output + num * wei;
}
else if (flag == 2)
{
output = (output + num) * wei;
}
else if (flag == 1)
{
output = output * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
}
flag = 1;
break;
case '亿':
wei = 100000000;
output = num * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
break;
default:
break;
}
} for (int i = 0; i < sta.Length; i++)
{
output += sta[i];
} return output;
}
public int tt(string input)
{
char[] word = input.ToCharArray();
int[] sta = new int[2];
int output = 0;
int flag = 0;
int num = 0;
int wei = 1;
if (word.Length == 1)
{
switch (word[0])
{
case '零':
return 0;
case '一':
return 1;
case '二':
return 2;
case '三':
return 3;
case '四':
return 4;
case '五':
return 5;
case '六':
return 6;
case '七':
return 7;
case '八':
return 8;
case '九':
return 9; }
} int j = 0;
for(int i = 0; i < word.Length; i++)
{
switch (word[i])
{
case '零':
wei = 0;
break;
case '一':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 1;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 1;
flag = 1;
}
else
{
num = 1;
flag = 0;
}
}
break;
case '二':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 2;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 2;
flag = 1;
}
else
{
num = 2;
flag = 0;
}
}
break;
case '三':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千') && (word[i + 1] != '亿')))
{
output = output + 3;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 3;
flag = 1;
}
else
{
num = 3;
flag = 0;
}
}
break;---------------------省略N字-------------------------------
···························································
case '九':
if ((i == word.Length - 1) || ((word[i + 1] != '十') && (word[i + 1] != '百') && (word[i + 1] != '千')&& (word[i + 1] != '亿')))
{
output = output + 9;
flag = 1;
}
else
{
if (wei == 10)
{
output = output + 9;
flag = 1;
}
else
{
num = 9;
flag = 0;
}
}
break;
case '十':
wei = 10;
if (num != 0)
{
output = output + num * wei;
}
else
{
output = output + 10;
}
flag = 1;
break;
case '百':
wei = 100;
output = output + num * wei;
flag = 1;
break;
case '千':
wei = 1000;
output = output + num * wei;
flag = 1;
break;
case '万':
wei = 10000;
if (flag == 0)
{
output = output + num * wei;
}
else if (flag == 2)
{
output = (output + num) * wei;
}
else if (flag == 1)
{
output = output * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
}
flag = 1;
break;
case '亿':
wei = 100000000;
output = num * wei;
sta[j] = output;
j++;
output = 0;
num = 0;
wei = 0;
break;
default:
break;
}
} for (int i = 0; i < sta.Length; i++)
{
output += sta[i];
} return output;
}
===================================================改进了一下,把regex改成了if就可以跑到150毫秒了
而且 1000000 万次 switch 为什么会比 if else 要快?
{
static string[][] nums = new string[][] { new string[] { "一", "1" }, new string[] { "二", "2" }, new string[] { "三", "3" }, new string[] { "四", "4" }, new string[] { "五", "5" }, new string[] { "六", "6" }, new string[] { "七", "7" }, new string[] { "八", "8" }, new string[] { "九", "9" } };
static string[][] units = new string[][] { new string[] { "十", "1" }, new string[] { "百", "2" }, new string[] { "千", "3" }, new string[] { "万", "4" }, new string[] { "十万", "5" }, new string[] { "百万", "6" }, new string[] { "千万", "7" }, new string[] { "億", "8" }, new string[] { "十億", "9" }, new string[] { "百億", "10" }, new string[] { "千億", "11" }, new string[] { "万億", "12" } };
static int[] intUnits = new int[20];
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
sw1.Start();
for (int i = 0; i < 100000; ++i)
{
Parse("一万二千三百四十五");
Parse("一千二百零三");
Parse("一千零三十二");
Parse("一千零二");
Parse("一千二百");
Parse("一千二百三十四億五千六百七十八万 九千零一");
Parse("十五");
Parse("五十");
}
sw1.Stop();
Console.WriteLine(sw1.ElapsedMilliseconds);
Console.Read();
} static Decimal Parse(string test)
{
string ans = "";
int index = 0;
bool flag = false;
string zeros = "";
string tempNum = "";
for (int i = 0; i < test.Length; i++)
{
if (i == test.Length - 1 && flag)
{
for (int j = 0; j < intUnits[index - 1] - 1; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
} if (test[i].ToString().Equals("零"))
{
flag = true;
continue;
}
string temp = GetNum(test[i].ToString()); if (!temp.Equals("error"))
{
if (!flag)
ans += temp;
else
tempNum = temp;
}
else
{
intUnits[index] = GetUnit(test[i].ToString());
index++; if (flag)
{
for (int j = 0; j < intUnits[index - 2] - intUnits[index - 1] - 1; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
}
if (i == test.Length - 1)
{
for (int j = 0; j < intUnits[index - 1] - intUnits[index]; j++)
{
zeros += "0";
}
ans += zeros + tempNum;
tempNum = "";
flag = false;
}
}
}
return Decimal.Parse(ans);
} static string GetNum(string a)
{
foreach (string[] num in nums)
{
if (a.Equals(num[0]))
return num[1];
}
return "error";
} static int GetUnit(string a)
{
foreach (string[] unit in units)
{
if (a.Equals(unit[0]))
return Int32.Parse(unit[1]);
}
return -1;
}
}用24楼测试数据测三次 2094ms 2171ms 2144ms
24楼的代码在我机子上耗时1450ms左右
24楼的代码在我机子上耗时1450ms左右
==========================================
什么配置机器,是release下吗
我自己的代码不是还跑了2000多ms么。。
看不过去了,所以把你的代码跑下参照一下速度
还有就是 清洁工大哥那代码 好象也很简洁的
{
string _bigNumber = textBox1.Text;
string _smallNumber = "";
int j=0;
long sum=0; for (int i = 0; i < _bigNumber.Length; i++)
{
if (_bigNumber[i] == '亿')
{
long partSum = 0;
long[] flag = new long[i - 1];
for (j = 0; j < i; j++)
{
switch (_bigNumber[j])
{
case '一':
flag[j] = 1;
break;
case '二':
flag[j] = 2;
break;
case '三':
flag[j] = 3;
break;
case '四':
flag[j] = 4;
break;
case '五':
flag[j] = 5;
break;
case '六':
flag[j] = 6;
break;
case '七':
flag[j] = 7;
break;
case '八':
flag[j] = 8;
break;
case '九':
flag[j] = 9;
break;
case '亿':
flag[j] = 100000000;
break;
case '万':
flag[j] = 10000;
break;
case '千':
flag[j] = 1000;
break;
case '百':
flag[j] = 100;
break;
case '十':
flag[j] = 10;
break;
default:
j++;
break;
}
}
for (int k = 0; k < j; k++)
{
if (flag[k] >= 10)
{
partSum += flag[k - 1] * flag[k];
}
else if ((k == j - 1) && (flag[k] < 10))
{
partSum += flag[k];
}
}
sum += partSum * 100000000;
}
else if (_bigNumber[i] == '万')
{
long partSum = 0;
long[] flag = new long[i - j - 1];
for (int k = 0; k < i - j - 1; k++)
{
switch (_bigNumber[k])
{
case '一':
flag[k] = 1;
break;
case '二':
flag[k] = 2;
break;
case '三':
flag[k] = 3;
break;
case '四':
flag[k] = 4;
break;
case '五':
flag[k] = 5;
break;
case '六':
flag[k] = 6;
break;
case '七':
flag[k] = 7;
break;
case '八':
flag[k] = 8;
break;
case '九':
flag[k] = 9;
break;
case '千':
flag[j] = 1000;
break;
case '百':
flag[j] = 100;
break;
case '十':
flag[j] = 10;
break;
default:
j++;
break;
}
}
for (int k = 0; k < i - j - 1; k++)
{
if (flag[k] >= 10)
{
partSum += flag[k - 1] * flag[k];
}
else if ((k == j - 1) && (flag[k] < 10))
{
partSum += flag[k];
}
}
sum += partSum * 10000;
}
else//千
{
}
后面的实现也跟上面差的不多.
else
{
switch()
{
case '':
....
..........
......
}
我的代码在你机器上跑了吗,效果怎么样
周末结贴