我的计算器(calculateV1 )
main ; 主循环函数
do {
do input()
quit:(num1 = "")
}
while num1'=""
quit
///输入
input() [num1,num2,operator1,operator2,result,flag,ArrPri] {
for{
for{
read !, "请输入数字:", num1
quit:num1=""
set result = num1
set flag = $$isNum(num1) ;判定算式格式是否正确
quit:flag=1
}
quit:num1=""
for{
read !, "请输入运算符:", operator1
quit:operator1=""
set flag = $$isOperator(operator1) ;判定算式格式是否正确
quit:flag=1
}
if (operator1?1"="){
write !, "运算结果为:"
write result
kill
set num1 = ""
;quit
}else{
quit
}
}
for{
for{
read !, "请输入数字:", num2
;quit:num2=""
set flag = $$isNum(num2) ;判定算式格式是否正确
quit:flag=1
}
;quit:num2=""
for{
read !, "请输入运算符:", operator2
set flag = $$isOperator(operator2) ;判定算式格式是否正确
quit:flag=1
}
do prior
if ArrPri(operator1,operator2) = "^"{
do cal1
do display
kill
set num1 = 1 ;防止循环崩溃
quit
}elseif ArrPri(operator1,operator2) = 1{
do cal1
set operator1 = operator2
;kill ArrPri
}elseif ArrPri(operator1,operator2) = 2{
do cal2
///此时运算符O1应该不变,O2应等于O3
;set operator1 = operator2
;kill ArrPri
;set num2 = "" ;防止循环崩溃
}
;set operator1 = operator2
}
}
///按顺序运算
cal1()[num1,num2,operator1,operator2,result]{
if (operator1?1"+"){
set result = result +num2
}elseif (operator1?1"-"){
set result = result -num2
}elseif (operator1?1"*"){
set result = result *num2
}elseif (operator1?1"/"){
set result = result /num2
}
kill ArrPri(operator1,operator2)
}
///先算后面*,/,后算前面+,-
cal2()[num1,num2,operator1,operator2,result,flag,ArrPri]{
set result = num2 ; 后面要把result还给num1,本次计算的结果赋给num2
for{
read !, "请输入数字:", num2
set flag = $$isNum(num2) ;判定算式格式是否正确
quit:flag=1
}
if (operator2?1"*"){
set num2 = result *num2
}elseif (operator2?1"/"){
set num2 = result /num2
}
kill ArrPri(operator1,operator2)
set result = num1
for{
read !, "请输入运算符:", operator2
set flag = $$isOperator(operator2) ;判定算式格式是否正确
quit:flag=1
}
do prior
if ArrPri(operator1,operator2) = "^"{
do cal1
do display
kill
set num1 = 1
quit
}elseif ArrPri(operator1,operator2) = 1{
do cal1
set operator1 = operator2
;kill ArrPri
}elseif ArrPri(operator1,operator2) = 2{
do cal2
///此时运算符O1应该不变,O2应等于O3
;set operator1 = operator2
;kill ArrPri
}
}
///输出
display()[num1,num2,operator1,operator2,result]{
;kill
write !, "运算结果为:", result
}
///设置相邻运算符优先级
prior()[operator1,operator2,ArrPri]{
if (operator2?1"="){
if (operator1?1"+"){ ;为相近的两个运算符设置优先级
set ArrPri("+","=") = "^" ;下标存储
///接下来应为直接运算然后输出结果
}elseif (operator1?1"-"){
set ArrPri("-","=") = "^"
}elseif (operator1?1"*"){
set ArrPri("*","=") = "^"
}elseif (operator1?1"/"){
set ArrPri("/","=") = "^"
}
}elseif(operator1?1"+")
{
if (operator2?1"+"){ ;为相近的两个运算符设置优先级
set ArrPri("+","+") = 1 ;下标存储
///接下来应为直接运算得到result
}elseif (operator2?1"-"){
set ArrPri("+","-") = 1
}elseif (operator2?1"*"){
set ArrPri("+","*") = 2
///接下来应为先算*结果存到num2,但是没有num3,不能计算,需要再次输入num和ope
}elseif (operator2?1"/"){
set ArrPri("+","/") = 2
///接下来应为先算*结果存到num2
}
}elseif(operator1?1"-")
{
if (operator2?1"+"){ ;为相近的两个运算符设置优先级
set ArrPri("-","+") = 1 ;下标存储
///接下来应为直接运算得到result
}elseif (operator2?1"-"){
set ArrPri("-","-") = 1
}elseif (operator2?1"*"){
set ArrPri("-","*") = 2
///接下来应为先算*结果存到num2
}elseif (operator2?1"/"){
set ArrPri("-","/") = 2
///接下来应为先算*结果存到num2
}
}elseif(operator1?1"*")
{
if (operator2?1"+"){ ;为相近的两个运算符设置优先级
set ArrPri("*","+") = 1 ;下标存储
///接下来应为直接运算得到result
}elseif (operator2?1"-"){
set ArrPri("*","-") = 1
}elseif (operator2?1"*"){
set ArrPri("*","*") = 1
}elseif (operator2?1"/"){
set ArrPri("*","/") = 1
}
}elseif(operator1?1"/")
{
if (operator2?1"+"){ ;为相近的两个运算符设置优先级
set ArrPri("/","+") = 1 ;下标存储
///接下来应为直接运算得到result
}elseif (operator2?1"-"){
set ArrPri("/","-") = 1
}elseif (operator2?1"*"){
set ArrPri("/","*") = 1
}elseif (operator2?1"/"){
set ArrPri("/","/") = 1
}
}
}
///判断输入的是否为数字
isNum(num){
if (num?1(1.n,1"-".n,1"+".n,1.n1".".n,1"-".n1".".n,1"+".n1".".n))
{
write "正确输入"
quit 1
}else{
write "请输入正确格式""""n,-n,+n,n.n,-n.n,+n.n"""
quit 0
}
}
///判断输入的是否为运算符
isOperator(operator){
if (operator?1(1"+",1"-",1"*",1"/",1"="))
{
write "正确输入"
quit 1
}else{
write "请输入正确格式""""+,-,*,/"""
quit 0
}
}