各位帮忙看看这个程序,里面有的内容我看不懂,要求是写一个计算器,进行正整数的输入和四则运算。(要求在运行时添加其余九个元素)
控件设置:   name       index      caption
   command1  operator   0~~4       +,-,*,/,=
   command2  number     0~~9       0~~9
   command3  clea       无定义     CE
   text1     disp       无定义     无定义    
程序:dim op1,op2    '两个操作数
     dim firstinput as boolean  '是否为首次输入,首次输入的操作数放入op1中
                                 '在这里为什么要定义这个变量
     dim opflag,lastinput        '这个也为什么要定义'建立数字数组控件略
'单击清屏按钮,消除标签框显示的数字
private   sub clea_click()
        disp=format(0,"0")     '这个格式输出函数里面的0,0表示什么呢?
        op1=0
        op2=0
        firstinput=ture
        lastinput=""
end sub'显示所按数字键,这段代码不明白是什么意思
private sub number_click(index as integer) 
   if lastinput <> "NUMS"  then 
         disp=number(index).caption
   else 
         disp=disp+number(index).caption
   end if
   lastinput="NUMS"
END SUB 
 
'下面的这段代码中,为什么要进行判断输入的是第一个数还是第二个数,是第一个数的话,把键入的运算符暂存,是第二个操作数,取暂存的运算符进行运算。在这里我觉得要进行运算肯定是要两个数,怎还要分先后顺序啊?怎么还要规定第一个数输入后不进行运算?这段代码可不可以省略呢?
private sub operator_click(index as integer)
  if firstonput=ture then 
      op1=val(disp)       '这个是什么意思?
      firstinput=false
  else
     op2=val(disp)
     select  case  opflag
             case "+"
                  op1=op1+op2
             case "-"
                  op1=op1-op2 
             case "*"
                  op1=op1*op2
             case /
                  if op2=0 then 
                    msgbox"can't divide by zero",48, "calculator"
                  else
                    op1=op1/op2
                  end if
    end select
if operator(index).caption="=" then
   disp=op1
end if
lastinput="oper"
opflag=operator(index).caption    '这个什么意思?
end sub 

解决方案 »

  1.   

    op1=val(disp) 把 disp 函数传回 op1。如果 disp = 123,那么 op1=val(disp) 就相当于 op1=123
    opflag=operator(index).caption 应该就是+ - * /的显示吧?
      

  2.   

    val就是求值,val( "9" )的结果就是9, op1=val(disp)的意思是将disp转成数值赋给op1之所以要有第一和第二的分别,是因为数字运算是有优先级的,乘除高于加减opflag=operator(index).caption     是将当前的运算符记下来,留给下一步使用,所以这样做,还是因为运算有优先级的分别。