51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3243|回复: 11
打印 上一主题 下一主题

谁帮我修改一下这个脚本

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2007-2-6 18:54:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
拼音转换成数字,其中(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);
        }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2007-2-6 20:17:41 | 只看该作者
建议把函数单独放一个脚本,或是把它的属性定义成编译模块。
然后再调用此函数,传入参数。

函数:
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 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2007-2-6 21:55:43 | 只看该作者
同意楼上的,if这块做或的判断要用||分开,最好在判断执行里面加上continue;语句,即 if(ls == "a" || ls == "b" || ls == "c")
{
   result = result & "2";
   continue;
}
这样判断并赋值后就可以跳出本次循环,省去后面很多不必要的判断
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2007-2-7 00:12:48 | 只看该作者
为了程序的运行效率,不要简单用IF语句,可以使用IF...ELSE IF ...ELSE语句,这样运行的效率高。
回复 支持 反对

使用道具 举报

该用户从未签到

5#
 楼主| 发表于 2007-2-7 10:15:51 | 只看该作者
还是不行啊,程序没有错误了,可是不能显示结果

# 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);
}
回复 支持 反对

使用道具 举报

该用户从未签到

6#
发表于 2007-2-7 10:39:07 | 只看该作者
这是一个函数脚本不能单独运行的,需要传入参数。
你可以这样试试
可以把这个脚本单独保存,并设置属性为complied module.
例如:函数脚本保存为:e:\脚本\lib.
         主脚本为e:\脚本\main,对函数进行调用 
   main脚本如下:
   call"e:\\脚本\\lib"();
         pton("AEJ");

或是另外一种处理:不写成函数形式,只是把要执行的内容写成脚本,然后在脚本属性中设置传入参数为pinyin。
在脚本中可以直接调用此脚本并传入参数。
例如主脚本可以写成:call"e:\\脚本\\lib"("AEJ");

对你的脚本有一个地方有点不明白,顺便问一下,create_input_dialog ( "Please type the pinyin" );,这条语句起什么作用?和整个脚本有什么关系?
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2007-2-7 12:22:19 | 只看该作者
create_input_dialog 语句是得到输入字符串的,应该这样写 pinyin = create_input_dialog ( "Please type the pinyin" );
这样pinyin中得到的就是用户输入的字符串了,把这个字符串做为变量传入函数中即可。建议函数另保存为一个complied module文件,哪个脚本用到了call一下这个complied module文件,就可以直接使用里面的函数了
回复 支持 反对

使用道具 举报

该用户从未签到

8#
 楼主| 发表于 2007-2-7 14:21:05 | 只看该作者
sdlkfj3
终于完成了,谢谢各位大虾,以后我会再接再厉。
正在努力学习脚本编写中,以后还要多多指教啊!
回复 支持 反对

使用道具 举报

该用户从未签到

9#
发表于 2007-2-9 17:54:47 | 只看该作者
switch  语句效率会好点吧?
回复 支持 反对

使用道具 举报

该用户从未签到

10#
发表于 2007-2-9 22:11:38 | 只看该作者
确实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 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

11#
发表于 2007-2-12 09:51:26 | 只看该作者
效率:switch 跟IF...SELSE的运行的效率一样,都是碰到符合条件的就结束,如果是单纯的IF语言,所有的语句都要运行一次。
回复 支持 反对

使用道具 举报

该用户从未签到

12#
发表于 2007-2-27 16:00:31 | 只看该作者
不错的帖子,建议加精
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-6-15 14:05 , Processed in 0.081083 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表