其他分享
首页 > 其他分享> > PowerBuilder——实现一个简单计算器

PowerBuilder——实现一个简单计算器

作者:互联网

      1.定义实例变量(由于运算符有挂在窗口,所以将变量定义为全局变量,以便菜单项也可以使用)
      
        decimal num //存放操作数,decimal带符号数据,18位有效数字
	character operator //存放操作符,character单个字符
	int flag //标志位,判断一次计算是否成功
	string sle //记录运算过程,连续运算时将输入的过程显示在文本框1中 
      
      
      2.编写代码,实现当点击数字或小数点按钮时,其值能显示到文本框中
     
     if flag=0 then    //第一次输入,将文本框显示的初始值赋空
		sle_1.text = "" 
		flag = 1  
	end if
	sle_1.text = sle_1.text+this.text   //若不是第一次输入,就将该数字按键的值追加在文本框中
	sle = sle+this.text
     
     
      3.实现运算符(+ 、-、*、/),计算功能
              string()可将任意的数据类型转换成字符串类型
 	      dec()把获取的字符型数据转换成数字
	      mid(string,start,length)  截取字符串的子串
              match(str,textpattern) 确定字符串中是否包含指定模式的字符 
     
   //加减运算,不会出现+++的情况
      choose case operator
	   case '+'
		sle_2.text = string(dec(sle_1.text)+num)  
		sle_1.text = sle_2.text
		//sle_1.text = string(num)+operator+sle_1.text
           case '-'
		sle_2.text = string(num - dec(sle_1.text))
		sle_1.text = sle_2.text
	   case '*'
		sle_2.text = string(num * dec(sle_1.text))
		sle_1.text = sle_2.text
           case '/'
		sle_2.text = string(num/dec(sle_1.text))
		sle_1.text = sle_2.text
      end choose
      num = dec(sle_1.text)   //保留中间结果
      operator = this.text       //保留本次运算,当前输入的运算符
      flag = 0          //准备输入下一个操作数
      //sle_1.text = sle_1.text + this.text
      sle = sle+this.text
      if mid(sle,len(sle)-1,1)= this.text  then 
	   sle = mid(sle,0,len(sle)-1)
      end if
      sle_1.text =  sle
     
      //乘除运算,还存在问题:连按*或/按钮时,文本跨会显示****或/////
      choose case operator
	   case '+'
		sle_2.text = string(dec(sle_1.text)+num)  
		sle_1.text = sle_2.text
	   case '-'
		sle_2.text = string(num - dec(sle_1.text))
		sle_1.text = sle_2.text
	   case '*'
		if match(sle_1.text,"^[0-9]$") = true then  //出现***后,匹配传过来以便下次运算的sle_1.text的内容是否由数字组成
			sle_2.text = string(num * dec(sle_1.text))
		elseif  match(sle_1.text,"^[0-9]$") = false then
		//将sle_1.text的值改为1,进行运算。
			sle_2.text = string(num)
		end if 	
		sle_1.text = sle_2.text
	   case '/'
		if match(sle_1.text,"^[0-9]$") = true then  //出现////后,匹配传过来以便下次运算的sle_1.text的内容是否由数字组成
			sle_2.text = string(num/dec(sle_1.text))
		elseif  match(sle_1.text,"^[0-9]$") = false then
		//将sle_1.text的值改为1,进行运算。这样连按//,不会出现逻辑错误
			sle_2.text = string(num)
	        end if 	
		sle_1.text = sle_2.text
      end choose
      num = dec(sle_1.text)   //保留中间结果
      operator = this.text       //保留本次运算,当前输入的运算符
      flag = 0          //准备输入下一个操作数
      sle = sle+this.text
      if mid(sle,len(sle)-1)= this.text then 
	      sle = mid(sle,0,len(sle)-2)  
      end if
      sle_1.text = sle
      

      4.实现点击=按钮,显示运算的最终结果
      choose case operator
           case '+'
		sle_2.text = string(dec(sle_1.text)+num) 
           case '-'
		sle_2.text = string(num - dec(sle_1.text))
           case '*'
		sle_2.text = string(dec(sle_1.text) * num)
           case '/'
		if sle_1.text ="0" then 
			sle_2.text = "不能除以0"
		else
			sle_2.text = string(num/dec(sle_1.text))
		end if
		//sle_1.text = string(num)+operator+sle_1.text 
     end choose    
     num = dec(sle_1.text)   //保留此次计算的数据,即输入的后一个数字,即A+B中的B
     operator = " "      //保留本次运算,当前输入的运算符
     flag = 0          //准备下一个操作数的输入
     sle = sle+operator
     sle_1.text =  sle


      5.实现清空和退格删除功能
      sle_1.text = " "    
      sle_2.text = " "
      flag = 0
      operator = ''
      num = 0   //清空,需将两个文本框的内容赋空即可    
      if len(mid(sle,0,len(sle)-1)) = 0 then  //判断显示的文本框是否为空,
	      sle = ""
      else
	      sle = mid(sle,0,len(sle)-1)  //退格,删除一个数字或是运算符
      end if 
      //messagebox("sle",sle)   
      sle_1.text = sle 


      6.添加菜单(运算符),可通过菜单项的运算符来实现计算器的运算功能
      //加、减运算符
      choose case operator
           case '+'
		w_operator.sle_2.text = string(dec(w_operator.sle_1.text)+num)   
		w_operator.sle_1.text = w_operator.sle_2.text
           case '-'
		w_operator.sle_2.text = string(num - dec(w_operator.sle_1.text))
		w_operator.sle_1.text = w_operator.sle_2.text
           case '*'
		w_operator.sle_2.text = string(num*dec(w_operator.sle_1.text))
		w_operator.sle_1.text = w_operator.sle_2.text
           case '/'
		w_operator.sle_2.text = string(num/dec(w_operator.sle_1.text))
		w_operator.sle_1.text = w_operator.sle_2.text
      end choose
      num = dec(w_operator.sle_1.text)   //保留中间结果  num=A 或是 A+B的结果
      operator = "+"       //保留本次运算,当前输入的运算符
      flag = 0          //准备下一个操作数的输入
      sle = sle+ "+"   //若出现++,则截断最后多的+后再往下计算
      if mid(sle,len(sle)-1,1)= "+"  then 
            sle = mid(sle,0,len(sle)-1)
      end if
      w_operator.sle_1.text =  sle

      //乘、除运算符
      choose case operator
           case '+'
		w_operator.sle_2.text = string(dec(w_operator.sle_1.text)+num)   
		w_operator.sle_1.text = w_operator.sle_2.text
           case '-'
		w_operator.sle_2.text = string(num - dec(w_operator.sle_1.text))
		w_operator.sle_1.text = w_operator.sle_2.text
           case '*'
		if match(w_operator.sle_1.text,"^[0-9]$") = true then  //出现**后,匹配传过来以便下次运算的sle_1.text的内容是否由数字组成
			w_operator.sle_2.text = string(num*dec(w_operator.sle_1.text))
		elseif  match(w_operator.sle_1.text,"^[0-9]$") = false then
		//      **  后 sle_1.text=A*B*C即文本框的内容, 不能转换成数字类型进行运算计算
		//将sle_1.text的值改为1,进行运算。这样连按**,不会出现计算错误
			w_operator.sle_2.text = string(num)
		end if 
		w_operator.sle_1.text = w_operator.sle_2.text
           case '/'
		w_operator.sle_2.text = string(num/dec(w_operator.sle_1.text))
		w_operator.sle_1.text = w_operator.sle_2.text
      end choose
      num = dec(w_operator.sle_1.text)   //保留输入的前一个数
      operator = "*"       //保留本次运算,当前输入的运算符
      flag = 0          //准备下一个操作数的输入
      sle = sle+"*" 
      if mid(sle,len(sle)-1)= "*"  then 
		sle = mid(sle,0,len(sle)-2)
		//sle_1.text = "1"
      else
		w_operator.sle_1.text = sle
      end if

标签:string,PowerBuilder,text,计算器,num,简单,operator,sle,dec
来源: https://www.cnblogs.com/mydiray/p/14015675.html