//等于号处理,将结果保存在m_preStr中
void
CJSQDlg::OnBtnequal()
{
if(
!m_equal
)
{
double
x
=
atof(
m_preStr
);
double
y
=
atof(
m_postStr
);
switch(
m_Fuhao
)
{
case
1:
x
=
x
+
y;
break;
case
2:
x
=
x
-
y;
break;
case
3:
x
=
x
y;
break;
case
4:
x
=
x
/
y;
break;
case
5:
x
=
(int)x
%
(int)y;
break;
}
CString
LSstr;
LSstrFormat(
"%g",
x
);//将x转化成字符串,保存在LSstr中
SetDlgItemText(
IDC_EDIT1,
LSstr
);//显示最后结果
欢迎看一下我的计算器,
有程序界面,
你可以参考一下,添加运算功能。
说明:1共有4个文本框其,其中三个是填入或输出数据的(名称分别是:txta txtb txtc),剩下的那个是符号(txtd)2共7个按钮,4个是符号按钮,一个计算,一个清零.这是我弄的程序”袖珍计算器”代码也是自己编写的,代码如下:通用声明Dim j As Integer '定义j为整型
Private Sub Text1_Change()End SubPrivate Sub Command1_Click()
txtfuhaoText = "+" '将”+”显示到文本框txtfuhaoText中
End SubPrivate Sub Command2_Click()
txtfuhaoText = "-" ''将”-”显示到文本框txtfuhaoText中
End SubPrivate Sub Command3_Click()
txtfuhaoText = "×" '将”×”显示到文本框txtfuhaoText中
End SubPrivate Sub Command4_Click()
txtfuhaoText = "÷" 将”÷”'显示到文本框txtfuhaoText中
End Sub
Private Sub Command5_Click()
Dim a, b, c As Integer
a = Val(txtaText) '将txtaText里的内容转化为数值型,然后再赋给a
b = Val(txtbText) '将txtbText里的内容转化为数值型,然后再赋给b
If txtfuhaoText = "+" Then '运算过程
c = a + b '运算过程
ElseIf txtfuhaoText = "-" Then '运算过程
c = a - b '运算过程
ElseIf txtfuhaoText = "÷" Then '运算过程
c = a / b '运算过程
ElseIf txtfuhaoText = "×" Then '运算过程
c = a b '运算过程
Else
j = MsgBox("您输入的符号不正确", vbOKOnly, "错误信息")
End If
txtcText = c '将运算结果c输出到文本框txtc中
End SubPrivate Sub Command6_Click()
txtaText = "" '将空字符输入到文本框内(刷新)
txtbText = "" '将空字符输入到文本框内(刷新)
txtcText = "" '将空字符输入到文本框内(刷新)
txtfuhaoText = "" '将空字符输入到文本框内(刷新)
End SubPrivate Sub Command7_Click()
End '结束程序
End SubPrivate Sub Form_Load()End S
'这个很简单的了,你用记事本把这段代码复制过去,然后另存为frm就可以了
'然后用vb打开。看看效果。
'以下保存为 任务名称frm
VERSION 500
Begin VBForm Form1
Caption = "计算机"
ClientHeight = 4230
ClientLeft = 60
ClientTop = 750
ClientWidth = 3165
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4230
ScaleWidth = 3165
StartUpPosition = 2 '屏幕中心
Begin VBFrame Frame1
Height = 3495
Left = 120
TabIndex = 1
Top = 600
Width = 2895
Begin VBCommandButton cmdResult
Caption = "="
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1080
TabIndex = 20
Top = 3000
Width = 1695
End
Begin VBCommandButton cmdOff
Caption = "OFF"
Height = 375
Left = 1560
TabIndex = 19
Top = 240
Width = 495
End
Begin VBCommandButton cmdCE
Caption = "CE"
Height = 375
Left = 2280
TabIndex = 18
Top = 240
Width = 495
End
Begin VBCommandButton cmdchu
Caption = "/"
Height = 375
Left = 2280
TabIndex = 17
Top = 2520
Width = 495
End
Begin VBCommandButton cmdjia
Caption = "+"
Height = 375
Left = 2280
TabIndex = 16
Top = 720
Width = 495
End
Begin VBCommandButton cmd0
Caption = "0"
Height = 375
Left = 840
TabIndex = 15
Top = 2520
Width = 495
End
Begin VBCommandButton cmd6
Caption = "6"
Height = 375
Left = 1560
TabIndex = 14
Top = 1320
Width = 495
End
Begin VBCommandButton cmd8
Caption = "8"
Height = 375
Left = 840
TabIndex = 13
Top = 720
Width = 495
End
Begin VBCommandButton cmd4
Caption = "4"
Height = 375
Left = 120
TabIndex = 12
Top = 1320
Width = 495
End
Begin VBCommandButton cmd9
Caption = "9"
Height = 375
Left = 1560
TabIndex = 11
Top = 720
Width = 495
End
Begin VBCommandButton cmd2
Caption = "2"
Height = 375
Left = 840
TabIndex = 10
Top = 1920
Width = 495
End
Begin VBCommandButton cmdjian
Caption = "-"
Height = 375
Left = 2280
TabIndex = 9
Top = 1320
Width = 495
End
Begin VBCommandButton cmdPoint
Caption = ""
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1560
TabIndex = 8
Top = 2520
Width = 495
End
Begin VBCommandButton cmdcheng
Caption = ""
Height = 375
Left = 2280
TabIndex = 7
Top = 1920
Width = 495
End
Begin VBCommandButton cmd5
Caption = "5"
Height = 375
Left = 840
TabIndex = 6
Top = 1320
Width = 495
End
Begin VBCommandButton cmd3
Caption = "3"
Height = 375
Left = 1560
TabIndex = 5
Top = 1905
Width = 495
End
Begin VBCommandButton cmd1
Caption = "1"
Height = 375
Left = 120
TabIndex = 4
Top = 1920
Width = 495
End
Begin VBCommandButton cmd7
Caption = "7"
Height = 375
Left = 120
TabIndex = 3
Top = 720
Width = 495
End
Begin VBCommandButton cmdChange
Caption = "+/-"
Height = 375
Left = 120
TabIndex = 2
Top = 2520
Width = 495
End
End
Begin VBTextBox txt
Alignment = 1 'Right Justify
Enabled = 0 'False
BeginProperty Font
Name = "宋体"
Size = 105
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 120
Locked = -1 'True
TabIndex = 0
Top = 120
Width = 2895
End
Begin VBMenu meuFile
Caption = "文件(&F)"
Begin VBMenu meuExit
Caption = "退出(&X)"
Shortcut = ^X
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim XiaoShu As Boolean
'在窗体变量中声明一个布尔值来判断是否按下了小数点键,防止txt中出现多个小数。如果已经输入了小数点则Xiaoshu为True,否则为False
Dim FuHao As Byte
'声明一个Byte来记录我们按下了什么符号,1为"+",2为"-",3为"",4为"/"
Dim FirstNumber As Double
'声明一个变量来记录 *** 作数的第一个数,如A+B,那么FirstNumber来记下A的值。
Dim DengHao As Boolean
'声明一个变量来记录是否按下了=号,如果按下了则为True
Private Sub cmd0_Click()
If txtText <> "" Then txtText = txtText + "0" '防止多个0在文本中出现
End Sub
Private Sub cmd1_Click() '按下1时
If Not DengHao Then '没按下等号
txtText = txtText + "1"
Else
txtText = "1" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd2_Click() '按下2时
If Not DengHao Then '没按下等号
txtText = txtText + "2"
Else
txtText = "2" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd3_click() '按下3时
If Not DengHao Then '没按下等号
txtText = txtText + "3"
Else
txtText = "3" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd4_Click() '按下4时
If Not DengHao Then '没按下等号
txtText = txtText + "4"
Else
txtText = "4" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd5_Click() '按下5时
If Not DengHao Then '没按下等号
txtText = txtText + "5"
Else
txtText = "5" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd6_Click() '按下6时
If Not DengHao Then '没按下等号
txtText = txtText + "6"
Else
txtText = "6" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd7_Click() '按下7时
If Not DengHao Then '没按下等号
txtText = txtText + "7"
Else
txtText = "7" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd8_Click() '按下8时
If Not DengHao Then '没按下等号
txtText = txtText + "8"
Else
txtText = "8" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmd9_Click() '按下9时
If Not DengHao Then '没按下等号
txtText = txtText + "9"
Else
txtText = "9" '按下了等号
DengHao = False
End If
End Sub
Private Sub cmdCE_Click()
txtText = ""
XiaoShu = False
End Sub
Private Sub cmdChange_Click()
If (Sgn(Val(txtText))) = 1 Then txtText = "-" + txtText: Exit Sub '用Sgn来判断txt文本框内的值的符号。大于0的话就在前面加上"-"
If (Sgn(Val(txtText))) = -1 Then txtText = Abs(Val(txtText)) 'txt文本框的文本为大于0的数txt前面的"-"去掉。
End Sub
Private Sub cmdcheng_Click()
FirstNumber = Val(txtText)
FuHao = 3
txtText = ""
XiaoShu = False
End Sub
Private Sub cmdchu_Click()
FirstNumber = Val(txtText)
FuHao = 4
txtText = ""
XiaoShu = False
End Sub
Private Sub cmdjia_Click()
FirstNumber = Val(txtText)
FuHao = 1
txtText = ""
XiaoShu = False
End Sub
Private Sub cmdjian_Click()
FirstNumber = Val(txtText)
FuHao = 2
txtText = ""
XiaoShu = False
End Sub
Private Sub cmdOff_Click()
End
End Sub
Private Sub cmdPoint_Click()
If txtText = "" Then txtText = "0"
If Not XiaoShu Then '当我们以前没有按下小数点键时在txt中加上小数点
txtText = txtText + ""
XiaoShu = True
End If
End Sub
Private Sub cmdResult_Click()
If FuHao = 1 Then '按下了+号
txtText = FirstNumber + Val(txtText)
End If
If FuHao = 2 Then '按下了-号
txtText = FirstNumber - Val(txtText)
End If
If FuHao = 3 Then '按下了号
txtText = FirstNumber Val(txtText)
End If
If FuHao = 4 Then '按下了、号
txtText = FirstNumber / Val(txtText)
End If
DengHao = True '记录下按了=号
XiaoShu = False
End Sub
Private Sub meuExit_Click()
End
End Sub
以上就是关于用vb6.0编写计算器程序全部的内容,包括:用vb6.0编写计算器程序、用VB 6.0 怎么编写一个计算器、用VB设计一个计算器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)