计算器软件设计,如何用microsoft visual basic 60设计一个简单的计算器程序加减
来源:整理 编辑:大钱队理财 2023-06-27 21:00:20
1,如何用microsoft visual basic 60设计一个简单的计算器程序加减
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text + "1"
End Sub
Private Sub Command10_Click()
Command12.Caption = Text1.Text
Text1.Text = ""
Command15.Caption = 1
End Sub
Private Sub Command11_Click()
If Command15.Caption = "" Then
Command13.Caption = Text1.Text
Text1.Text = "0"
Text1.Text = Val(Command12.Caption) + -Val(Command13.Caption)
Command13.Caption = ""
Command12.Caption = ""
ElseIf Command15.Caption = 1 Then
Command13.Caption = Text1.Text
Text1.Text = "0"
Text1.Text = Val(Command13.Caption) + Val(Command12.Caption)
Command13.Caption = ""
Command12.Caption = ""
Command15.Caption = ""
End If
End Sub
Private Sub Command14_Click()
Command12.Caption = Text1.Text
Text1.Text = ""
End Sub
Private Sub Command2_Click(Index As Integer)
Text1.Text = Text1.Text + "2"
End Sub
Private Sub Command3_Click(Index As Integer)
Text1.Text = Text1.Text + "3"
End Sub
Private Sub Command4_Click(Index As Integer)
Text1.Text = Text1.Text + "4"
End Sub
Private Sub Command5_Click(Index As Integer)
Text1.Text = Text1.Text + "5"
End Sub
Private Sub Command6_Click(Index As Integer)
Text1.Text = Text1.Text + "6"
End Sub
Private Sub Command7_Click(Index As Integer)
Text1.Text = Text1.Text + "7"
End Sub
Private Sub Command8_Click(Index As Integer)
Text1.Text = Text1.Text + "8"
End Sub
Private Sub Command9_Click()
Text1.Text = ""
End Sub
2,计算器程序设计
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jsq
{
public static void main(String[] args)
{
CalculatorFrame frame = new jsqiFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class jsqFrame extends JFrame
{
public jsqFrame()
{
setTitle("Calculator");
jsqPanel panel = new jsqPanel();
add(panel);
pack();
}
}
class jsqPanel extends JPanel
{
public jsqPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(panel, BorderLayout.CENTER);
}
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;

3,用VB程序设计一个简单计算器
界面:
text1
1 2 3 +
4 5 6 -
7 8 9 *
0 = AC /
代码:
dim newnum as boolean
dim n1,n2 op as integer
private sub ac_click()
text1.text=""
end sub
private sub command1_click(index as integer)
if newnum=true then text1=""
text1=text1&index
newnum=false
end sub
private sub command1_click(index as integer)
if op=0 then
n1=val(text1)
else
n2=val(text1)
select case op
case 1
text1=n1+n2
case 2
text1=n1-n2
case 3
text1=n1*n2
case 4
if n2<>0 then text1=n1/n2
end select
op=0
end if
if index>0 then op=index
n1=val(text1)
newnum=true
end sub布局如下
Option Explicit
Dim sum1, sum2, f, n, dian
Private Sub Command1_Click(Index As Integer)
Label1.Caption = Label1.Caption & Index
Picture1.SetFocus
End Sub
Private Sub Command11_Click(Index As Integer)
sum1 = Val(Label1.Caption)
Label1.Caption = ""
dian = 0
f = Index
Picture1.SetFocus
End Sub
Private Sub Command15_Click()
sum2 = Val(Label1.Caption)
Select Case f
Case 0
Label1.Caption = sum1 + sum2
Case 1
Label1.Caption = sum1 - sum2
Case 2
Label1.Caption = sum1 * sum2
Case Else
Label1.Caption = sum1 / sum2
End Select
Picture1.SetFocus
End Sub
Private Sub Command16_Click()
Label1.Caption = ""
Picture1.SetFocus
End Sub
Private Sub Command17_Click()
sum1 = 0
sum2 = 0
Label1.Caption = ""
Picture1.SetFocus
End Sub
Private Sub Command2_Click()
If Label1.Caption <> "" Then
Label1.Caption = Left(Label1.Caption, Len(Label1.Caption) - 1)
End If
Picture1.SetFocus
End Sub
Private Sub Command3_Click(Index As Integer)
Dim a, b
n = Index
Select Case n
Case 0
a = ""
Text1.Visible = False
Case 1
Label1.Caption = a
Case 2
a = Val(Label1.Caption)
Text1.Visible = True
Case 3
a = a + Val(Label1.Caption)
Text1.Visible = True
End Select
Picture1.SetFocus
End Sub
Private Sub Command4_Click()
Label1.Caption = Not Label1.Caption - 1
Picture1.SetFocus
End Sub
Private Sub Command6_Click()
Label1.Caption = spr(Label1.Caption)
Picture1.SetFocus
End Sub
Private Sub Command7_Click()
Label1.Caption = Val(Label1.Caption) / 100
Picture1.SetFocus
End Sub
Private Sub Command8_Click()
Label1.Caption = 1 / Val(Label1.Caption)
Picture1.SetFocus
End Sub
Private Sub Command9_Click()
If dian < 1 Then
Label1.Caption = Label1.Caption & "."
dian = dian + 1
End If
Picture1.SetFocus
End Sub
Private Sub Form_Activate()
Picture1.SetFocus
End Sub
首先建立一个command1 然后剪切复制 系统提示说已经有一个COMMAND1 是否要建立控件数组?
你点击是 这样重复9次 分别把caption该成0到9 然后建立command2 方法同上,caption为 +-*/ 然后是 command3 caption是=
然后输入代码,首先在通用里生命dim shu1,shu2
dim suanfu as string
private sub command1(index)_click
text1.text=text1.text & commmand1(index).capton
end sub
command2里 输入
shu1=text1.text
suanfu=text1.text & command(index).capiton
text1.text=""
end sub
command3里输入
shu2=text1.text
selece case suanfu
case "+"
text1.text=shu1+shu2
case "-"
text1.text=shu1-shu2
case "*"
text1.text=shu*shu2
case "/"
if shu2=0 then
msgbox "分母不能为0"
else text1.text=shu1/shu2
end if
end select
end sub
Private Sub Command1_Click()
Dim x%, y%, c$, z%
x = (Text1.Text)
y = (Text2.Text)
c = (Text3.Text)
Select Case c
Case "+"
z = x + y
Case "-"
z = x - y
Case "*"
z = x * y
Case "/"
z = x / y
End Select
Text4.Text = z
End Sub

文章TAG:
计算 软件 软件设计 计算器软件设计 如何用microsoft visual basic 60设计一个简单的计算器程序加减