PowerBuilder——实现⼀个简单计算器计算器布局如下:
实现步骤
1.定义实例变量(由于运算符有挂在窗⼝,所以将变量定义为全局变量,以便菜单项也可以使⽤)
decimal num //存放操作数,decimal带符号数据,18位有效数字
character operator //存放操作符,character单个字符
int flag //标志位,判断⼀次计算是否成功
string sle //记录运算过程,连续运算时将输⼊的过程显⽰在⽂本框1中
2.编写代码,实现当点击数字或⼩数点按钮时,其值能显⽰到⽂本框中
if flag=0 then //第⼀次输⼊,将⽂本框显⽰的初始值赋空
= ""
flag = 1
end if
= + //若不是第⼀次输⼊,就将该数字按键的值追加在⽂本框中
sle =
3.实现运算符(+ 、-、*、/),计算功能
string()可将任意的数据类型转换成字符串类型
dec()把获取的字符型数据转换成数字
mid(string,start,length) 截取字符串的⼦串
match(str,textpattern) 确定字符串中是否包含指定模式的字符
//加减运算,不会出现+++的情况
choose case operator
case '+'
= string(dec()+num)
=
// = string(num)+operator+
case '-'用java编写一个简单的计算器
= string(num - dec())
=
case '*'
= string(num * dec())
=
case '/'
= string(num/dec())
=
end choose
num = dec() //保留中间结果
operator = //保留本次运算,当前输⼊的运算符
flag = 0 //准备输⼊下⼀个操作数
// = +
sle =
if mid(sle,len(sle)-1,1)= then
sle = mid(sle,0,len(sle)-1)
end if
= sle
/
/乘除运算,还存在问题:连按*或/按钮时,⽂本跨会显⽰****或/////
choose case operator
case '+'
= string(dec()+num)
=
case '-'
= string(num - dec())
=
case '*'
if match(,"^[0-9]$") = true then //出现***后,匹配传过来以便下次运算的的内容是否由数字组成
= string(num * dec())
elseif match(,"^[0-9]$") = false then
//将的值改为1,进⾏运算。
= string(num)
end if
=
case '/'
if match(,"^[0-9]$") = true then //出现////后,匹配传过来以便下次运算的的内容是否由数字组成
= string(num/dec())
elseif match(,"^[0-9]$") = false then
//将的值改为1,进⾏运算。这样连按//,不会出现逻辑错误
= string(num)
end if
=
end choose
num = dec() //保留中间结果
operator = //保留本次运算,当前输⼊的运算符
flag = 0 //准备输⼊下⼀个操作数
sle =
if mid(sle,len(sle)-1)= then
sle = mid(sle,0,len(sle)-2)
end if
= sle
4.实现点击=按钮,显⽰运算的最终结果
choose case operator
case '+'
= string(dec()+num)
case '-'
= string(num - dec())
case '*'
= string(dec() * num)
case '/'
if ="0" then
= "不能除以0"
else
= string(num/dec())
end if
// = string(num)+operator+
end choose
num = dec() //保留此次计算的数据,即输⼊的后⼀个数字,即A+B中的B
operator = " " //保留本次运算,当前输⼊的运算符
flag = 0 //准备下⼀个操作数的输⼊
sle = sle+operator
= sle
5.实现清空和退格删除功能
= " "
= " "
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
6.添加菜单(运算符),可通过菜单项的运算符来实现计算器的运算功能
//加、减运算符
choose case operator
case '+'
w_operator. = string(dec(w_operator.)+num)
w_operator. = w_operator.
case '-'
w_operator. = string(num - dec(w_operator.))
w_operator. = w_operator.
case '*'
w_operator. = string(num*dec(w_operator.))
w_operator. = w_operator.
case '/'
w_operator. = string(num/dec(w_operator.))
w_operator. = w_operator.
end choose
num = dec(w_operator.) //保留中间结果 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
//乘、除运算符
choose case operator
case '+'
w_operator. = string(dec(w_operator.)+num)
w_operator. = w_operator.
case '-'
w_operator. = string(num - dec(w_operator.))
w_operator. = w_operator.
case '*'
if match(w_operator.,"^[0-9]$") = true then //出现**后,匹配传过来以便下次运算的的内容是否由数字组成 w_operator. = string(num*dec(w_operator.))
elseif match(w_operator.,"^[0-9]$") = false then
// ** 后 =A*B*C即⽂本框的内容,不能转换成数字类型进⾏运算计算
//将的值改为1,进⾏运算。这样连按**,不会出现计算错误
w_operator. = string(num)
end if
w_operator. = w_operator.
case '/'
w_operator. = string(num/dec(w_operator.))
w_operator. = w_operator.
end choose
num = dec(w_operator.) //保留输⼊的前⼀个数
operator = "*" //保留本次运算,当前输⼊的运算符
flag = 0 //准备下⼀个操作数的输⼊
sle = sle+"*"
if mid(sle,len(sle)-1)= "*" then
sle = mid(sle,0,len(sle)-2)
// = "1"
else
w_operator. = sle
end if
显⽰问题
1.按了 = 按钮后,不能接着上次的计算过程进⾏后续运算
2. 第⼀次输⼊,先按-号再输数字,如-2,⽂本框只会显⽰数字2,不影响后续输⼊的加减计算,但是若后续运算中由乘除运算则会出
错
3.按下⼩数点按钮,不能在⼩数点前加上0,显⽰0.n,在后续运算中若直接先输⼊⼩数点,仍会只显⽰.n,不影响啊计算
4.按照⽤户输⼊的顺序进⾏计算和显⽰(输⼊⼀个数计算⼀个数),不能正确计算输⼊的混合算式
5.连按乘或除/按钮是,⽂本跨中会连续显⽰**或////,但不影响计算功能
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论