当前位置:首页 > 代码 > 正文

.net简单计算器的代码(计算器代码实现)[20240424更新]

admin 发布:2024-04-24 05:35 108


本篇文章给大家谈谈.net简单计算器的代码,以及计算器代码实现对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用.net编写win计算器,进行小数点的运算以及弹出和关闭窗口??

那个设计窗口什么的就自己添加了哈

先要知道你需要按的按钮在文本框中要怎么输出

eg:“2”当文本框为0时2取代,为空时,直接写了

private void button5_Click(object sender, EventArgs e)

{

if ("0".Equals(textBox1.Text))

{ textBox1.Text = "2"; }

else

{

textBox1.Text = textBox1.Text + "2";

}

}

等号应该算这里面复杂的一可以想象成在你已经输入的文本框中有的数字中含有+-*/

在这些符号的左右取值

可编写为

private void button14_Click(object sender, EventArgs e)

{

string s = textBox1.Text;

string[] r;

//判断符号

if (s.EndsWith("+") || s.EndsWith("-") || s.EndsWith("*") || s.EndsWith("/"))

{

textBox1.Text = textBox1.Text;

}else if(s.Contains("+")){

r = s.Split('+');

textBox1.Text = (Double.Parse(r[0]) + Double.Parse(r[1])).ToString();

}else if (s.Contains("-"))

{

r = s.Split('-');

textBox1.Text = (Double.Parse(r[0]) - Double.Parse(r[1])).ToString();

}else if (s.Contains("*"))

{

r = s.Split('*');

textBox1.Text = (Double.Parse(r[0]) * Double.Parse(r[1])).ToString();

}else if (s.Contains("/"))

{

r = s.Split('/');

textBox1.Text = (Double.Parse(r[0]) / Double.Parse(r[1])).ToString();

}

其它的应该就没有什么了吧,

您好 ,我想用vb。net编一个计算器,就是设置了几个数字键,摁一下显示一个数字,比如输入67该怎么编写

操作过程:

1获取你摁的第一个数字值,比如6,把它转换成字符串"6",设置在文本框txtNum中.

2获取你按的第二个数字值,比如7,转换成字符串,设置文本框

txtNum.text += "7" 这样就出现了"67"的显示了.

用VB.NET编的计算器程序!

Public Class SimpleCalculator

Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()

MyBase.New()

'该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写处置以清理组件列表。

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Windows 窗体设计器所必需的

Private components As System.ComponentModel.IContainer

'注意:以下过程是 Windows 窗体设计器所必需的

'可以使用 Windows 窗体设计器修改此过程。

'不要使用代码编辑器修改它。

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents Button3 As System.Windows.Forms.Button

Friend WithEvents Button4 As System.Windows.Forms.Button

Friend WithEvents Button5 As System.Windows.Forms.Button

Friend WithEvents Button6 As System.Windows.Forms.Button

Friend WithEvents Button7 As System.Windows.Forms.Button

Friend WithEvents Button8 As System.Windows.Forms.Button

Friend WithEvents Button9 As System.Windows.Forms.Button

Friend WithEvents Button10 As System.Windows.Forms.Button

Friend WithEvents Button11 As System.Windows.Forms.Button

Friend WithEvents Button12 As System.Windows.Forms.Button

Friend WithEvents Button13 As System.Windows.Forms.Button

Friend WithEvents Button14 As System.Windows.Forms.Button

Friend WithEvents Button15 As System.Windows.Forms.Button

Friend WithEvents Button16 As System.Windows.Forms.Button

System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.Button1 = New System.Windows.Forms.Button

Me.Button2 = New System.Windows.Forms.Button

Me.Button3 = New System.Windows.Forms.Button

Me.Button4 = New System.Windows.Forms.Button

Me.Button5 = New System.Windows.Forms.Button

Me.Button6 = New System.Windows.Forms.Button

Me.Button7 = New System.Windows.Forms.Button

Me.Button8 = New System.Windows.Forms.Button

Me.Button9 = New System.Windows.Forms.Button

Me.Button10 = New System.Windows.Forms.Button

Me.Button11 = New System.Windows.Forms.Button

Me.Button12 = New System.Windows.Forms.Button

Me.Button13 = New System.Windows.Forms.Button

Me.Button14 = New System.Windows.Forms.Button

Me.Button15 = New System.Windows.Forms.Button

Me.Button16 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(16, 16)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(48, 16)

Me.Label1.TabIndex = 0

Me.Label1.Text = "结果:"

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(64, 8)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(120, 21)

Me.TextBox1.TabIndex = 1

Me.TextBox1.Text = ""

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(200, 8)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(56, 24)

Me.Button1.TabIndex = 2

Me.Button1.Text = "清空"

'

'Button2

'

Me.Button2.Location = New System.Drawing.Point(48, 56)

Me.Button2.Name = "Button2"

Me.Button2.Size = New System.Drawing.Size(24, 24)

Me.Button2.TabIndex = 3

Me.Button2.Text = "1"

'

'Button3

'

Me.Button3.Location = New System.Drawing.Point(88, 56)

Me.Button3.Name = "Button3"

Me.Button3.Size = New System.Drawing.Size(24, 24)

Me.Button3.TabIndex = 4

Me.Button3.Text = "2"

'

'Button4

'

Me.Button4.Location = New System.Drawing.Point(136, 56)

Me.Button4.Name = "Button4"

Me.Button4.Size = New System.Drawing.Size(24, 24)

Me.Button4.TabIndex = 5

Me.Button4.Text = "3"

'

'Button5

'

Me.Button5.Location = New System.Drawing.Point(48, 88)

Me.Button5.Name = "Button5"

Me.Button5.Size = New System.Drawing.Size(24, 24)

Me.Button5.TabIndex = 6

Me.Button5.Text = "4"

'

'Button6

'

Me.Button6.Location = New System.Drawing.Point(88, 88)

Me.Button6.Name = "Button6"

Me.Button6.Size = New System.Drawing.Size(24, 24)

Me.Button6.TabIndex = 7

Me.Button6.Text = "5"

'

'Button7

'

Me.Button7.Location = New System.Drawing.Point(136, 88)

Me.Button7.Name = "Button7"

Me.Button7.Size = New System.Drawing.Size(24, 24)

Me.Button7.TabIndex = 8

Me.Button7.Text = "6"

'

'Button8

'

Me.Button8.Location = New System.Drawing.Point(48, 120)

Me.Button8.Name = "Button8"

Me.Button8.Size = New System.Drawing.Size(24, 24)

Me.Button8.TabIndex = 9

Me.Button8.Text = "7"

'

'Button9

'

Me.Button9.Location = New System.Drawing.Point(88, 120)

Me.Button9.Name = "Button9"

Me.Button9.Size = New System.Drawing.Size(24, 24)

Me.Button9.TabIndex = 10

Me.Button9.Text = "8"

'

'Button10

'

Me.Button10.Location = New System.Drawing.Point(136, 120)

Me.Button10.Name = "Button10"

Me.Button10.Size = New System.Drawing.Size(24, 24)

Me.Button10.TabIndex = 11

Me.Button10.Text = "9"

'

'Button11

'

Me.Button11.Location = New System.Drawing.Point(48, 152)

Me.Button11.Name = "Button11"

Me.Button11.Size = New System.Drawing.Size(24, 24)

Me.Button11.TabIndex = 12

Me.Button11.Text = "0"

'

'Button12

'

Me.Button12.Location = New System.Drawing.Point(176, 56)

Me.Button12.Name = "Button12"

Me.Button12.Size = New System.Drawing.Size(24, 24)

Me.Button12.TabIndex = 13

Me.Button12.Text = "+"

'

'Button13

'

Me.Button13.Location = New System.Drawing.Point(176, 88)

Me.Button13.Name = "Button13"

Me.Button13.Size = New System.Drawing.Size(24, 24)

Me.Button13.TabIndex = 14

Me.Button13.Text = "-"

'

'Button14

'

Me.Button14.Location = New System.Drawing.Point(176, 120)

Me.Button14.Name = "Button14"

Me.Button14.Size = New System.Drawing.Size(24, 24)

Me.Button14.TabIndex = 15

Me.Button14.Text = "*"

'

'Button15

'

Me.Button15.Location = New System.Drawing.Point(176, 152)

Me.Button15.Name = "Button15"

Me.Button15.Size = New System.Drawing.Size(24, 24)

Me.Button15.TabIndex = 16

Me.Button15.Text = "/"

'

'Button16

'

Me.Button16.Location = New System.Drawing.Point(88, 152)

Me.Button16.Name = "Button16"

Me.Button16.Size = New System.Drawing.Size(72, 24)

Me.Button16.TabIndex = 17

Me.Button16.Text = "计算"

'

'SimpleCalculator

'

Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

Me.ClientSize = New System.Drawing.Size(264, 190)

Me.Controls.Add(Me.Button16)

Me.Controls.Add(Me.Button15)

Me.Controls.Add(Me.Button14)

Me.Controls.Add(Me.Button13)

Me.Controls.Add(Me.Button12)

Me.Controls.Add(Me.Button11)

Me.Controls.Add(Me.Button10)

Me.Controls.Add(Me.Button9)

Me.Controls.Add(Me.Button8)

Me.Controls.Add(Me.Button7)

Me.Controls.Add(Me.Button6)

Me.Controls.Add(Me.Button5)

Me.Controls.Add(Me.Button4)

Me.Controls.Add(Me.Button3)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.Label1)

Me.Name = "SimpleCalculator"

Me.Text = "简单计算器"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub TextBox1_TabStopChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim btn As Button '按钮类型的变量

btn = sender '把产生该事件的按钮对象赋值给btn

TextBox1.Text = TextBox1.Text + btn.Text '把该按钮的Text属性值连接到TextBox1中

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

Dim btn As Button

btn = sender

TextBox1.Text += btn.Text

End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

Dim btn As Button

btn = sender

'在文本框的Text属性后连接一个空格、本按钮的Text属性值和一个空格

TextBox1.Text = TextBox1.Text + " " + btn.Text + " "

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = "" '清空文本框

End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click

Dim btn As Button

btn = sender

TextBox1.Text = TextBox1.Text + " " + btn.Text + " "

End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click

Dim btn As Button

btn = sender

TextBox1.Text = TextBox1.Text + " " + btn.Text + " "

End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click

Dim btn As Button

btn = sender

TextBox1.Text = TextBox1.Text + " " + btn.Text + " "

End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click

'发生错误转移到标号“ErrorPro”指定的行去执行错误处理程序

On Error GoTo ErrorPro

Dim r As Decimal '保存计算结果的变量

Dim t As String = TextBox1.Text '用于保存文本框中的算术表达式

Dim space As Integer = t.IndexOf(" ") '搜索空格位置,如果没有空格,返回值为0

'字符串的取子符方法,第二个参数小于0,则将产生错误号为5的异常,即过程参数不正确

Dim s1 As String = t.Substring(0, space) '通过取子串方法获得第一个运算数

Dim op As String = t.Substring(space + 1, 1) '通过取子串方法获得运算符

Dim s2 As String = t.Substring(space + 3) '通过取子串方法获得第二个运算数

Dim arg1, arg2 As Integer

arg1 = Val(s1) : arg2 = Val(s2)

Select Case op

Case "+" : r = arg1 + arg2

Case "-" : r = arg1 - arg2

Case "*" : r = arg1 * arg2

Case "/" : r = arg1 / arg2

Case Else

MsgBox("输入的运算符有误!")

Exit Sub

End Select

TextBox1.Text = CStr(r) '显示结果

Exit Sub '退出过程

ErrorPro: '错误处理程序块

Select Case Err.Number

Case 6 '除数为零时,或运算溢出时的错误号

MsgBox("算术运算溢出!", , "溢出提示")

TextBox1.Focus()

Exit Sub

Case 5 ' Substring过程的参数不符合要求的错误号

MsgBox("必须输入运算符和第二个运算数!", , "运算数少")

Exit Sub

Case Else

'其它情况显示错误号和错误原因

MsgBox("错误号为" Err.Number Chr(10) Chr(13) "错误原因:" Err.Description)

Exit Sub

End Select

End Sub

End Class

用ASP.NET怎么编写一个简易的计算器。有加 减 乘 除 数字和等号15个按钮的还一个输出,简单的后台代码谢谢

晕!你 自己到CSDB 这个网站上面去下载一个啊! 上面很多的~

这个网站上面有

自己参考

求ASP.net计算器应用程序代码(用C#语言 Session对象)

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

txtSum.Text = "0"; //先给计算器赋一个初始值0

}

//单击数字键

protected void btn0_Click(object sender, EventArgs e) {

if (txtSum.Text == "0")

{

txtSum.Text = "0";

}

else

{

txtSum.Text += "0";

}

}

protected void btn1_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "1";

}

else

{

txtSum.Text += "1";

}

}

protected void btn2_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "2";

}

else

{

txtSum.Text += "2";

}

}

protected void btn3_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "3";

}

else

{

txtSum.Text += "3";

}

}

protected void btn4_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "4";

}

else

{

txtSum.Text += "4";

}

}

protected void btn5_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "5";

}

else

{

txtSum.Text += "5";

}

}

protected void btn6_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "6";

}

else

{

txtSum.Text += "6";

}

}

protected void btn7_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "7";

}

else

{

txtSum.Text += "7";

}

}

protected void btn8_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "8";

}

else

{

txtSum.Text += "8";

}

}

protected void btn9_Click(object sender, EventArgs e)

{

if (txtSum.Text == "0")

{

txtSum.Text = "9";

}

else

{

txtSum.Text += "9";

}

}

//单击运算符号,先判断计算器值是否为0

protected void btnAdd_Click(object sender, EventArgs e)

{

if (txtSum.Text != "0")

{

string sum = txtSum.Text; //定义一个Sum保存第一个要运算的数

Session["Sum"] = sum; //使用Session对象将第一个数保存

string symbols = "+"; //定义一个运算符

Session["Symbols"] = symbols; //使用Session对象将运算符保存

txtSum.Text = "0"; //然后将计算器清空,输入第二个数

}

}

//单击运算符号,先判断计算器值是否为0

protected void btnReduce_Click(object sender, EventArgs e)

{

if (txtSum.Text != "0")

{

string sum = txtSum.Text; // 定义一个Sum保存第一个要运算的数

Session["Sum"] = sum;//使用Session对象将第一个数保存

string symbols = "-";//定义一个运算符

Session["Symbols"] = symbols; //使用Session对象将运算符保存

txtSum.Text = "0";//然后将计算器清空,输入第二个数

}

}

//下面的都是依次像上面解析

protected void btnBy_Click(object sender, EventArgs e)

{

if (txtSum.Text != "0")

{

string sum = txtSum.Text;

Session["Sum"] = sum;

string symbols = "*";

Session["Symbols"] = symbols;

txtSum.Text = "0";

}

}

protected void btnDesides_Click(object sender, EventArgs e)

{

if (txtSum.Text != "0")

{

string sum = txtSum.Text;

Session["Sum"] = sum;

string symbols = "/";

Session["Symbols"] = symbols;

txtSum.Text = "0";

}

}

protected void btnSum_Click(object sender, EventArgs e)

{

string sum = ""; //重新定义一个字符串,提取第一个数

string symbols =(string)Session["Symbols"];//重新定义一个运算符,提取Session里的运算符

int i; //定义一个变量 求和

switch (symbols)

{

case "+":

sum = (string)Session["Sum"];

i = Convert.ToInt32(sum) + Convert.ToInt32(txtSum.Text);

txtSum.Text = Convert.ToString(i);

break;

case "-":

sum = (string)Session["Sum"];

i = Convert.ToInt32(sum) - Convert.ToInt32(txtSum.Text);

txtSum.Text = Convert.ToString(i);

break;

case "*":

sum = (string)Session["Sum"];

i = Convert.ToInt32(sum) * Convert.ToInt32(txtSum.Text);

txtSum.Text = Convert.ToString(i);

break;

case "/":

sum = (string)Session["Sum"];

i = Convert.ToInt32(sum) / Convert.ToInt32(txtSum.Text);

txtSum.Text = Convert.ToString(i);

break;

default:

txtSum.Text = "0";

break;

}

}

//单击清空按钮

protected void btnC_Click(object sender, EventArgs e)

{

txtSum.Text = "0"; //清空计算器

Session["Sum"] = "0"; //清空Session["Sum]

Session["symbols"]="0"; //清空Session["symbols"]

}

简易计算器代码

我的计算器 Option Explicit Dim FocusText As VB.TextBox Dim pd As Boolean Dim x As Double Dim y As Double Dim ch As Integer Private Sub Command1_Click(Index As Integer) On Error Resume Next FocusText.SetFocus If Index SendKeys Index ElseIf Index = 10 Then ch = Index - 10 x = Val(Text1.Text) Text1.Text = "" ElseIf Index = 14 Then y = Val(Text1.Text) Select Case ch Case 0 Text1.Text = x + y Case 1 Text1.Text = x - y Case 2 Text1.Text = x * y Case 3 Text1.Text = x / y End Select ElseIf Index = 15 Then FocusText.SetFocus SendKeys "{BS}" ElseIf Index = 16 Then FocusText.SetFocus SendKeys "." End If End Sub '防止输入多个小数点以及出数字外的其他字符 Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii 57) And KeyAscii 46 And KeyAscii 8 Then KeyAscii = 0 End If If pd = True And KeyAscii = 46 Then KeyAscii = 0 End If If pd = False And KeyAscii = 46 Then pd = True KeyAscii = 46 End If End Sub '防止 Ctrl + v 粘贴 Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyV And Shift = vbCtrlMask Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub '防止右键粘贴 Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbRightButton Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub Private Sub Text1_LostFocus() Set FocusText = Text1 End Sub 界面如下 0为Command1(0)1为Command1(1)依次类推 ←为Command1(15) 小数点为Command1(16) 贴子相关图片: 界面可以到

关于.net简单计算器的代码和计算器代码实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/414.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载