谁帮我修改一下这个脚本
拼音转换成数字,其中(2--abc,3--def,4--ghi,5--jkl,6--mno,7--pqrs,8--tuv,9--wxyz)例如:输入:chen,输出:2436
我写的脚本:
# Pinyin convert to number.
create_input_dialog ( "Please type the pinyin" );
public function pton(pinyin)
{
pinyin = tolower(pinyin);
result = "";
for(i = 1; i <= length(pinyin); i++)
{
ls = substr(pinyin, i, 1);
if(ls == "a" | ls == "b" | ls == "c")
{result = result & "2";}
if(ls == "d" | ls == "e" | ls == "f")
{result = result & "3";}
if(ls == "g" | ls == "h" | ls == "i")
{result = result & "4";}
if(ls == "j" | ls == "k" | ls == "l")
{result = result & "5";}
if(ls == "m" | ls == "n" | ls == "o")
{result = result & "6";}
if(ls == "p" | ls == "q" | ls == "r" | ls == "s")
{result = result & "7";}
if(ls == "t" | ls == "u" | ls == "v")
{result = result & "8";}
if(ls == "w" | ls == "x" | ls == "y" | ls == "z")
{result = result & "9";}
printf(result);
}
} 建议把函数单独放一个脚本,或是把它的属性定义成编译模块。
然后再调用此函数,传入参数。
函数:
public function pton(in pinyin)
{
#定义变量
static i;
static result;
static ls;
pinyin = tolower(pinyin);
result = "";
for(i = 1; i <= length(pinyin); i++)
{
ls = substr(pinyin, i, 1);
if(ls == "a" || ls == "b" || ls == "c")
{result = result & "2";}
if(ls == "d" || ls == "e" || ls == "f")
{result = result & "3";}
if(ls == "g" || ls == "h" || ls == "i")
{result = result & "4";}
......................................
if(ls == "w" || ls == "x" || ls == "y" ||ls == "z")
{result = result & "9";}
}
printf(result); #在FOR循环外输出
}
[ 本帖最后由 shiwomyw 于 2007-2-6 20:21 编辑 ] 同意楼上的,if这块做或的判断要用||分开,最好在判断执行里面加上continue;语句,即 if(ls == "a" || ls == "b" || ls == "c")
{
result = result & "2";
continue;
}
这样判断并赋值后就可以跳出本次循环,省去后面很多不必要的判断 为了程序的运行效率,不要简单用IF语句,可以使用IF...ELSE IF ...ELSE语句,这样运行的效率高。 还是不行啊,程序没有错误了,可是不能显示结果
# Pinyin converts to number.
create_input_dialog ( "Please type the pinyin" );
public function pton(in pinyin)
{
# Var declarations.
static i;
static result;
static ls;
pinyin = tolower(pinyin);
result = "";
for(i = 1; i <= length(pinyin); i++)
{
ls = substr(pinyin, i, 1);
if(ls == "a" || ls == "b" || ls == "c")
{ result = result & "2";
continue;
}
if(ls == "d" || ls == "e" || ls == "f")
{ result = result & "3";
continue;
}
if(ls == "g" || ls == "h" || ls == "i")
{ result = result & "4";
continue;
}
if(ls == "j" || ls == "k" || ls == "l")
{ result = result & "5";
continue;
}
if(ls == "m" || ls == "n" || ls == "o")
{ result = result & "6";
continue;
}
if(ls == "p" || ls == "q" || ls == "r" || ls == "s")
{ result = result & "7";
continue;
}
if(ls == "t" || ls == "u" || ls == "v")
{ result = result & "8";
continue;
}
if(ls == "w" || ls == "x" || ls == "y" || ls == "z")
{ result = result & "9";
continue;
}
}
printf(result);
} 这是一个函数脚本不能单独运行的,需要传入参数。
你可以这样试试
可以把这个脚本单独保存,并设置属性为complied module.
例如:函数脚本保存为:e:\脚本\lib.
主脚本为e:\脚本\main,对函数进行调用
main脚本如下:
call"e:\\脚本\\lib"();
pton("AEJ");
或是另外一种处理:不写成函数形式,只是把要执行的内容写成脚本,然后在脚本属性中设置传入参数为pinyin。
在脚本中可以直接调用此脚本并传入参数。
例如主脚本可以写成:call"e:\\脚本\\lib"("AEJ");
对你的脚本有一个地方有点不明白,顺便问一下,create_input_dialog ( "Please type the pinyin" );,这条语句起什么作用?和整个脚本有什么关系? create_input_dialog 语句是得到输入字符串的,应该这样写 pinyin = create_input_dialog ( "Please type the pinyin" );
这样pinyin中得到的就是用户输入的字符串了,把这个字符串做为变量传入函数中即可。建议函数另保存为一个complied module文件,哪个脚本用到了call一下这个complied module文件,就可以直接使用里面的函数了 sdlkfj3
终于完成了,谢谢各位大虾,以后我会再接再厉。
正在努力学习脚本编写中,以后还要多多指教啊! switch语句效率会好点吧? 确实switch语句从效率和可读性方面都会更好一些。楼主可以尝试这样写:
pinyin = create_input_dialog("Please type the pinyin");
for(i = 1; i <= length(pinyin);i++)
{
ls = substr(pinyin,i,1);
switch(ls)
{
case "a": case "b": case "c":
result = result & "2";
break;
case "d": case "e": case "f":
result = result & "3";
break;
case "g": case "h": case "i":
result = result & "4";
break;
...................
}
}
[ 本帖最后由 dionysus 于 2007-2-9 22:12 编辑 ] 效率:switch 跟IF...SELSE的运行的效率一样,都是碰到符合条件的就结束,如果是单纯的IF语言,所有的语句都要运行一次。 不错的帖子,建议加精
页:
[1]