用vb做简易计算器

三、简单的计算器(30分)
1. 界面设计(5分)
(1)窗口标题:简易计算器;
(2)文本框:用于显示输入的数值和计算结果;
(3)数字按钮0、1、2、……、9;小数点按钮;运算符按钮+(加)、-(减)、*(乘)、/(除)、=(等号);
(4)BackSpace按钮(功能同键盘上的BackSpace键);
(5)清除按钮;关闭按钮;
2.功能要求(25分)
(1)输入方法:表达式中的操作数和运算符既可以使用键盘输入或鼠标单击按钮输入,也可以混合使用。
(2)文本框中只显示操作数和计算结果,不显示运算符。例如,要计算34+6=,应先输入34,显示;输入+,不显示;输入6,显示,但不再显示34;输入=,不显示;显示计算结果40,但不再显示6。
(3)计算时能够判断优先级。例如,输入34+6*2=,计算结果应该为:46。
(4)“BackSpace”按钮的功能:键盘上的BackSpace键。
(5)“清除”按钮的功能:清除文本框中显示的内容,准备开始下一次计算。
(6)“关闭”按钮的功能:结束程序的运行,退出。

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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-15
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);
}
}
}
}
第2个回答  2011-06-09
我也需要做这个本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网