试编一程序判断输入的字符串(以#结束)中左右括号是否配对,
  提示:左右括号配对指的是有多少个左括号出现,就有多少个右括号出现。
要求:一次性让用户赋值,不使用循环结构。
困扰了我很久的问题

解决方案 »

  1.   

    function test(byval s as string) as boolean
        test=(ubound(split(s,")"))=ubound(split(s,"(")))
    exit function'调用
    dim s as string
    s="a(bc)ed"
    msgbox test(s)
      

  2.   


    function test(byval s as string) as boolean
        test=(ubound(split(s,")"))=ubound(split(s,"(")))
    end function'调用
    dim s as string
    s="a(bc)ed"
    msgbox test(s)
      

  3.   

    就我的理解给你答复一下:
    public function ifmatch(str as string) as boolean
       n=0; 
       endpos=instr(str,"#") '求字符串的结束位置
       for i=1 to endpos
           c=mid(str,i,1)
           if c="(" then
              n++;  
           end if 
           if c=")" then
              n--;
           end if
       next i
       if n=0 then
          ismatch=true
       else
          ismatch=false
       end if
    end function'函数的实现是通过一个变量n,依次读各个字符,若为"("则n++,若为")"则n--,最后若n为0说明是匹配的.
      

  4.   

    1、
       可能是我没有描述清楚,我的意思是采用'inputbox("请输入字符串:")'方式获得的字符串。
    然后,象c中类似的定义数组char ch[]="ajigljj",
    if(ch[2]=='#')
      return true;2、
       能解释一下,代码吗?
    刚开始学vb,有好多的不明白。。
    eg. ubound ,split 
      

  5.   


    function test(byval s as string) as boolean
        test=(ubound(split(s,")"))=ubound(split(s,"(")))
    end function'调用
    dim s as string
    s="a(bc)ed"
    msgbox test(s)