哦 想到了 宏替换能获取值但是能实现表达式正确性的检测吗?

梅尚程荀
马谭杨奚
程序代码:clear all
public ga_Keys[30]
ga_Keys[1] = CREATEOBJECT("Key_t", "MC", "MC")
ga_Keys[7] = CREATEOBJECT("Key_t", "MR", "MR")
ga_Keys[13] = CREATEOBJECT("Key_t", "MS", "MS")
ga_Keys[19] = CREATEOBJECT("Key_t", "M+", "M+")
ga_Keys[25] = CREATEOBJECT("Key_t", "M-", "M-")
ga_Keys[2] = CREATEOBJECT("Key_t", "(", "(")
ga_Keys[3] = CREATEOBJECT("Key_t", ")", ")")
ga_Keys[4] = CREATEOBJECT("Key_t", "BS", "←")
ga_Keys[5] = CREATEOBJECT("Key_t", "C1", "CE")
ga_Keys[6] = CREATEOBJECT("Key_t", "C2", "C")
ga_Keys[8] = CREATEOBJECT("Key_t", "7", "7")
ga_Keys[9] = CREATEOBJECT("Key_t", "8", "8")
ga_Keys[10] = CREATEOBJECT("Key_t", "9", "9")
ga_Keys[11] = CREATEOBJECT("Key_t", "/", "/")
ga_Keys[12] = CREATEOBJECT("Key_t", "sqrt", "√")
ga_Keys[14] = CREATEOBJECT("Key_t", "4", "4")
ga_Keys[15] = CREATEOBJECT("Key_t", "5", "5")
ga_Keys[16] = CREATEOBJECT("Key_t", "6", "6")
ga_Keys[17] = CREATEOBJECT("Key_t", "*", "*")
ga_Keys[18] = CREATEOBJECT("Key_t", "%", "%")
ga_Keys[20] = CREATEOBJECT("Key_t", "1", "1")
ga_Keys[21] = CREATEOBJECT("Key_t", "2", "2")
ga_Keys[22] = CREATEOBJECT("Key_t", "3", "3")
ga_Keys[23] = CREATEOBJECT("Key_t", "-", "-")
ga_Keys[24] = CREATEOBJECT("Key_t", "inverse", "1/x")
ga_Keys[26] = CREATEOBJECT("Key_t", "0", "0")
ga_Keys[27] = CREATEOBJECT("Key_t", "sign", "+/-")
ga_Keys[28] = CREATEOBJECT("Key_t", ".", ".")
ga_Keys[29] = CREATEOBJECT("Key_t", "+", "+")
ga_Keys[30] = CREATEOBJECT("Key_t", "=", "=")
public gc_Expression, gc_Result, gc_Memory, gc_MemValue, i
gc_Expression = ""
gc_Result = ""
gc_Memory = ""
gc_MemValue = ""
i = 0
set decimals to 4
on error do errHandler with Error(), Message(), Message(1), Program(), Lineno()
Main()
on error
clear all
return
procedure errHandler(merror, mess, mess1, mprog, mlineno)
MessageBox(Str(mlineno) + "表达式错误:" + mess)
endproc
procedure Main()
local lo_MainForm
lo_MainForm = NewObject("C_Form")
lo_MainForm.show
read events
endproc
define class Key_t as Custom
ID = ""
Caption = ""
procedure Init(tc_ID, tc_Caption)
with This
.ID = tc_ID
.Caption = tc_Caption
endwith
endproc
enddefine
Define Class C_Button as CommandButton
Width = 50
Height = 50
*FontName = "Terminal"
Themes = 0
FontSize = 20
*- 键名属性
Key = ""
Procedure Init(t_Key)
With This
.Key = t_Key
.Caption = .Key.Caption
EndWith
EndProc
Procedure Get_Memory(mem_Style, flag_num)
If gc_Expression == ""
MessageBox("请先输入数据")
Else
If gc_Result == ""
i = 0
gc_temp = gc_Expression
Do while IsDigit(Right(gc_Temp, 1))
i = i + 1
gc_Temp = Left(gc_Temp, Len(gc_Temp) - 1)
EndDo
If i == 0
gc_MemValue = gc_MemValue
Else
a = Right(gc_Expression, i)
gc_temp = &mem_Style + &a * flag_num
gc_MemValue = Transform(gc_temp)
EndIf
Else
If gc_Result != Transform(&gc_Expression)
i = 0
gc_temp = gc_Expression
Do while IsDigit(Right(gc_Temp, 1))
i = i + 1
gc_Temp = Left(gc_Temp, Len(gc_Temp) - 1)
EndDo
If i == 0
gc_MemValue = gc_MemValue
Else
a = Right(gc_Expression, i)
gc_temp = &mem_Style + &a * flag_num
gc_MemValue = Transform(gc_temp)
EndIf
Else
gc_temp = &mem_Style + &gc_Expression * flag_num
gc_MemValue = Transform(gc_temp)
EndIf
EndIf
gc_Memory = "M"
MessageBox("现在存储的数据是:" + gc_MemValue)
EndIf
EndProc
Procedure Click
Do Case
Case IsDigit(This.Key.ID) .or. InList(This.Key.ID, "+", "-", "*", "/", "(", ")", ".")
gc_Expression = gc_Expression + This.Key.Caption
Case This.Key.ID == "BS"
gc_Expression = Left(gc_Expression, Len(gc_Expression) - 1)
Case This.Key.ID == "C2"
gc_Expression = ""
gc_Result = ""
Case This.Key.ID == "C1"
Do while IsDigit(Right(gc_Expression, 1))
gc_Expression = Left(gc_Expression, Len(gc_Expression) - 1)
EndDo
Case This.Key.ID == "="
gc_Result = Transform(&gc_Expression)
Case This.Key.ID == "sqrt"
gc_Result = gc_Expression
gc_Result = Transform(Sqrt(&gc_Result))
gc_Expression = gc_Result
Case This.Key.ID == "inverse"
gc_Result = gc_Expression
gc_Result = Transform(1/&gc_Expression)
gc_Expression = gc_Result
Case This.Key.ID == "%"
gc_Result = gc_Expression
gc_Result = Transform(&gc_Expression/100)
gc_Expression = gc_Result
Case This.Key.ID == "sign"
If Left(gc_Expression, 1) != '-'
gc_Expression = Padl(gc_Expression, Len(gc_Expression) + 1, "-")
Else
gc_Expression = Right(gc_Expression, Len(gc_Expression) - 1)
EndIf
Case This.Key.ID == "MS"
This.Get_Memory("", 1)
Case This.Key.ID == "M+"
This.Get_Memory(gc_MemValue, 1)
Case This.Key.ID == "M-"
This.Get_Memory(gc_MemValue, -1)
Case This.Key.ID == "MR"
gc_Expression = gc_Expression + Transform(gc_MemValue)
Case This.Key.ID == "MC"
gc_Memory = ""
gc_MemValue = "0"
MessageBox("现在存储的数是:" + gc_MemValue)
EndCase
ThisForm.Refresh
EndProc
EndDefine
define class C_Form as Form
MaxButton = .F.
Caption = " 计算器"
Icon = 'D:\VFP\ico\rr.ico'
AutoCenter = .T.
add object C_display as Container with Width = 350, Height = 50,;
BorderWidth = 2
add object C_btnGroup as Container with Width = ThisForm.C_display.Width, Height = 300,;
BorderWidth = 0
procedure C_display.Init
with This
.SpecialEffect = 1
.AddObject("mem_dpy", "Container") && mem_dpy 的意思是memory_display
.mem_dpy.Top = 5
.mem_dpy.Left = 5
.mem_dpy.Width = 40
.mem_dpy.Height = 40
.mem_dpy.SpecialEffect = 1
.AddObject("epn_dpy", "Label") && epn_dpy 的意思是expression_display
.epn_dpy.Top = .mem_dpy.Top
.epn_dpy.Left = .mem_dpy.Left * 2 + .mem_dpy.Width
.epn_dpy.Width = .Width - .mem_dpy.Width - .mem_dpy.Left * 2
.epn_dpy.Height = .Height/2 - .mem_dpy.Top
*.epn_dpy.BackColor = Rgb(0, 0, 255)
.AddObject("rst_dpy", "Label") && rst_dpy 的意思是result_display
.rst_dpy.Top = .Height/2
.rst_dpy.Left = .epn_dpy.Left
.rst_dpy.Width = .epn_dpy.Width
.rst_dpy.Height = .epn_dpy.Height
.rst_dpy.ForeColor = Rgb(255, 0, 0)
*.rst_dpy.BackColor = Rgb(255, 0, 0)
.AddObject("M_Flag", "Label")
.M_Flag.FontSize = 16
.M_Flag.Top = .mem_dpy.Top + .mem_dpy.Height/2 - .M_Flag.FontSize/2
.M_Flag.Left = .mem_dpy.Left + .mem_dpy.Width/2 - .M_Flag.FontSize/2
.M_Flag.Width = .M_Flag.FontSize
.M_Flag.Height = .M_Flag.FontSize
.M_Flag.ForeColor = Rgb(0, 0, 255)
.M_Flag.Caption = ""
.SetAll("Visible", .T.)
.SetAll("Alignment", 1)
.SetAll("BorderWidth", 2)
.SetAll("FontSize", 16)
endwith
endproc
Procedure C_display.Refresh
This.epn_dpy.Caption = gc_Expression
This.rst_dpy.Caption = gc_Result
This.M_Flag.Caption = gc_Memory
EndProc
Procedure C_btnGroup.Init
Local lc_KeyName, ln_Space
Local ln_Index, ln_Top, ln_Left
ln_Top = 0
ln_Left = 0
ln_Space = 10
For ln_Index = 1 to Alen(ga_Keys, 0)
lc_KeyName = "Key_" + Padl(ln_Index, 2, '0')
This.AddObject(lc_KeyName, "C_Button", ga_Keys[ln_Index])
With This.&lc_KeyName
If InList(ln_Index, 7, 13, 19, 25)
ln_Top = ln_Top + .Height + ln_Space
ln_Left = 0
EndIf
If InList(ln_Index, 8, 9, 10, 14, 15, 16, 20, 21, 22,;
26, 27, 28, 12, 18, 24)
.ForeColor = Rgb(0, 0, 255)
Else
.ForeColor = Rgb(255, 0, 0)
EndIf
.Top = ln_Top
.Left = ln_Left
.Visible = .T.
.Themes = 0
ln_Left = ln_Left + .Width + ln_Space
EndWith
Next
EndProc
procedure Init
with This
.C_display.Top = 35
.C_display.Left = 10
.C_btnGroup.Top = .C_display.Top + .C_display.Height + 10
.C_btnGroup.Left = .C_display.Left
.Width = .C_display.Width + 20
.Height = .C_btnGroup.Top + .C_btnGroup.Height
endwith
endproc
procedure destroy
clear events
endproc
enddefine
