(功能尽量强大点)
建立14个按钮,按钮的名字都要Command1,设置Index属性,0~9分别对应数字0~9,10为小数点,11为等号,12~14分别对应+、-、*和/。
2、输入如下代码:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean
Dim PointFlag As Boolean
Dim Runsign As Integer
Dim SignFlag As Boolean
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(Str(Index))
End If
Text1.Text = Val(StrNum1)
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(".")
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = Val(StrNum1)
Case 12 To 15
FirstNum = True
PointFlag = False
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
On Error Resume Next
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
If App.PrevInstance Then MsgBox "请不要多次运行此程序,谢谢!", vbAbortRetryIgnore: Unload Me
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Sub Run()
On Error Resume Next
Dim equal As Single
Dim equal2 As Double
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 + Num2
End If
Case 2
equal = Num1 - Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 - Num2
End If
Case 3
equal = Num1 * Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 * Num2
End If
Case 4
equal = Num1 / Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 / Num2
End If
End Select
If equal = 0 Then a = equal2 Else a = equal
StrNum2 = Str(a)
StrNum1 = StrNum2
Text1.Text = Val(StrNum2)
End Sub
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = 0
End Sub
2、输入如下代码:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean
Dim PointFlag As Boolean
Dim Runsign As Integer
Dim SignFlag As Boolean
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(Str(Index))
End If
Text1.Text = Val(StrNum1)
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(".")
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = Val(StrNum1)
Case 12 To 15
FirstNum = True
PointFlag = False
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
On Error Resume Next
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
If App.PrevInstance Then MsgBox "请不要多次运行此程序,谢谢!", vbAbortRetryIgnore: Unload Me
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Sub Run()
On Error Resume Next
Dim equal As Single
Dim equal2 As Double
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 + Num2
End If
Case 2
equal = Num1 - Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 - Num2
End If
Case 3
equal = Num1 * Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 * Num2
End If
Case 4
equal = Num1 / Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 / Num2
End If
End Select
If equal = 0 Then a = equal2 Else a = equal
StrNum2 = Str(a)
StrNum1 = StrNum2
Text1.Text = Val(StrNum2)
End Sub
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = 0
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-09-14
我有JAVA编的 比VB好
public class CPU {
StringBuffer hc1 = new StringBuffer(); //缓冲区1
StringBuffer hc2 = new StringBuffer(); //缓冲区2
int JIAFA = 0;
int JIANFA = 1;
int CHENGFA = 2;
int CHUFA = 3;
int MEIYOU = -1;
int state = MEIYOU; //计算状态
/**
* 计算方法
* 返回计算结果
*/
public String jisuan() {
int firstNUM = Integer.parseInt(hc1.toString());
int secondNUM = Integer.parseInt(hc2.toString());
int jieguo = 0;
switch (state) {
case -1:
break;
case 0:
jieguo = firstNUM + secondNUM;
break;
case 1:
jieguo = firstNUM - secondNUM;
break;
case 2:
jieguo = firstNUM * secondNUM;
break;
case 3:
jieguo = firstNUM / secondNUM;
break;
}
hc1.delete(0, hc1.length());
hc2.delete(0,hc2.length());
state = MEIYOU ;
return hc1.append(jieguo).toString();
}
/**
* 更改状态
*/
public void jisuanState(int s) {
state = s;
}
/**
* 写入缓存
*/
public void write(String s) {
if (state == MEIYOU) {
hc1.append(s);
} else {
hc2.append(s);
}
}
//初始化
public void init() {
hc1.delete(0, hc1.length());
hc2.delete(0, hc2.length());
state = MEIYOU ;
}
/**
* 得到输入屏幕的内容
*/
public String getJG() {
String sss = "";
if (state == MEIYOU) {
sss = hc1.toString();
} else {
sss = hc2.toString();
}
return sss;
}
}
创建对象
数字键
c.write("7");
jTextField1.setText(c.getJG());
等于
jTextField1.setText(c.jisuan());
初始化
c.init();
jTextField1.setText("0");
计算符号
c.jisuanState(c.JIAFA);
public class CPU {
StringBuffer hc1 = new StringBuffer(); //缓冲区1
StringBuffer hc2 = new StringBuffer(); //缓冲区2
int JIAFA = 0;
int JIANFA = 1;
int CHENGFA = 2;
int CHUFA = 3;
int MEIYOU = -1;
int state = MEIYOU; //计算状态
/**
* 计算方法
* 返回计算结果
*/
public String jisuan() {
int firstNUM = Integer.parseInt(hc1.toString());
int secondNUM = Integer.parseInt(hc2.toString());
int jieguo = 0;
switch (state) {
case -1:
break;
case 0:
jieguo = firstNUM + secondNUM;
break;
case 1:
jieguo = firstNUM - secondNUM;
break;
case 2:
jieguo = firstNUM * secondNUM;
break;
case 3:
jieguo = firstNUM / secondNUM;
break;
}
hc1.delete(0, hc1.length());
hc2.delete(0,hc2.length());
state = MEIYOU ;
return hc1.append(jieguo).toString();
}
/**
* 更改状态
*/
public void jisuanState(int s) {
state = s;
}
/**
* 写入缓存
*/
public void write(String s) {
if (state == MEIYOU) {
hc1.append(s);
} else {
hc2.append(s);
}
}
//初始化
public void init() {
hc1.delete(0, hc1.length());
hc2.delete(0, hc2.length());
state = MEIYOU ;
}
/**
* 得到输入屏幕的内容
*/
public String getJG() {
String sss = "";
if (state == MEIYOU) {
sss = hc1.toString();
} else {
sss = hc2.toString();
}
return sss;
}
}
创建对象
数字键
c.write("7");
jTextField1.setText(c.getJG());
等于
jTextField1.setText(c.jisuan());
初始化
c.init();
jTextField1.setText("0");
计算符号
c.jisuanState(c.JIAFA);
第2个回答 2013-09-14
建立14个按钮,按钮的名字都要Command1,设置Index属性,0~9分别对应数字0~9,10为小数点,11为等号,12~14分别对应+、-、*和/。
第3个回答 2013-09-14
不会用