用vb做简易计算器

2024-12-31 05:16:34
推荐回答(2个)
回答1:

Dim jsff
Dim f_num As Single

Private Sub Text1_KeyPress(KeyAscii As Integer)

' If jsff <> "" Then
' Text1.Text = ""
' Else
' Exit Sub
' End If

End Sub

Private Sub Form_Load()

f_num = 0
jsff = ""
Text1.Text = ""

End Sub

Private Sub js()
Select Case jsff
Case "+"
f_num = f_num + Val(Text1.Text)
Case "-"
f_num = f_num - Text1.Text
Case "*"
f_num = f_num * Text1.Text
Case "/"
f_num = f_num / Text1.Text
Case "/"
f_num = f_num / Text1.Text

End Select

End Sub

Sub Command1_Click()

If Text1.Text = "" Then
Text1.SetFocus
Exit Sub
End If

If jsff = "" Then
f_num = Val(Text1.Text)
Else
js
End If
jsff = "+"
Text1.Text = ""
Text1.SetFocus

End Sub

Private Sub Command2_Click()

If Text1.Text = "" Then
Text1.SetFocus
Exit Sub
End If

If jsff = "" Then
f_num = Val(Text1.Text)
Else
js
End If
jsff = "-"
Text1.Text = ""
Text1.SetFocus

End Sub

Private Sub Command3_Click()

If Text1.Text = "" Then
Exit Sub
End If

If jsff = "" Then
f_num = Val(Text1.Text)
Else
js
End If
jsff = "*"
Text1.Text = ""
Text1.SetFocus

End Sub

Private Sub Command4_Click()

If Text1.Text = "" Then
Exit Sub
End If

If jsff = "" Then
f_num = Val(Text1.Text)
Else
js
End If
jsff = "/"
Text1.Text = ""
Text1.SetFocus

End Sub

Private Sub Command6_Click()

If Text1.Text = "" Then
Text1.SetFocus
Exit Sub
End If

x = Round(Sin(Val(Text1.Text)), 4)
If Left(x, 2) = "-." Then
x = "-0" & Right(x, Len(x) - 1)
ElseIf Left(x, 1) = "." Then
x = "0" & x
End If
Text1.Text = x

End Sub

Private Sub Command7_Click()

If Text1.Text = "" Then
Text1.SetFocus
Exit Sub
End If

x = Round(Cos(Val(Text1.Text)), 4)
If Left(x, 2) = "-." Then
x = "-0" & Right(x, Len(x) - 1)
ElseIf Left(x, 1) = "." Then
x = "0" & x
End If
Text1.Text = x

End Sub

Private Sub Command8_Click()

If Val(Text1.Text) < 0 Then
MsgBox "请不要对负数做开方运算"
Text1.SetFocus
Exit Sub
End If
x = Sqr(Val(Text1.Text))
If Left(x, 1) = "." Then x = "0" & x
Text1.Text = x

End Sub

Private Sub Command9_Click()

If Text1.Text = 0 Then
MsgBox "请不要对0做倒数运算"
Text1.SetFocus
Exit Sub
End If

x = Val(1 / Text1.Text)
If Left(x, 2) = "-." Then
x = "-0" & Right(x, Len(x) - 1)
ElseIf Left(x, 1) = "." Then
x = "0" & x
End If
Text1.Text = x

End Sub

Private Sub Command5_Click()

If Text1.Text = "" And jsff = "" Then
Text1.SetFocus
Exit Sub
End If

If Text1.Text = "" And jsff = "+" Then
MsgBox "请输入加数"
Text1.SetFocus
Exit Sub
ElseIf Text1.Text = "" And jsff = "-" Then
MsgBox "请输入减数"
Text1.SetFocus
Exit Sub
ElseIf Text1.Text = "" And jsff = "*" Then
MsgBox "请输入乘数"
Text1.SetFocus
Exit Sub
ElseIf Text1.Text = "" And jsff = "/" Then
MsgBox "请输入除数"
Text1.SetFocus
Exit Sub
ElseIf Text1.Text = 0 And jsff = "/" Then
MsgBox "请不要用0做除数"
Text1.SetFocus
Exit Sub
End If

js
x = f_num
If Left(x, 2) = "-." Then
x = "-0" & Right(x, Len(x) - 1)
ElseIf Left(x, 1) = "." Then
x = "0" & x
End If
Text1.Text = x
jsff = ""
f_num = 0

End Sub

Private Sub Command10_Click()

Text1.Text = ""
f_num = 0
jsff = ""
Text1.SetFocus

End Sub

回答2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

double x, y;
int i;

private void button6_Click(object sender, EventArgs e)
{

textBox1.Text += "5";
}

private void button13_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ }
else
textBox1.Text += "0";
}

private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}

private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}

private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}

private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}

private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}

private void button4_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i++;
}

private void button8_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 2;
}

private void button12_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 3;
}

private void button16_Click(object sender, EventArgs e)
{
try
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 4;
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }
}

private void button15_Click(object sender, EventArgs e)

{
try
{
y = double.Parse(textBox1.Text);
textBox1.Text = "";
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }
if (i == 1)
{
x = x + y;
textBox1.Text += x.ToString();
i--;
}
else if (i == 2)
{

x = x - y;
textBox1.Text += x.ToString();
i -= 2;

}
else if (i == 3)
{
x = x * y;
textBox1.Text += x.ToString();
i -= 3;

}
else if (i == 4)
{
try
{
x = x / y;
textBox1.Text += x.ToString();
i -= 4;
}

}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }

}

}

private void button17_Click(object sender, EventArgs e)
{
x = y = 0;
i = 0;
textBox1.Text="";
}

private void button14_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ }
else
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
}
}