用c#编写计算器代码,只要加减乘除就行了
推荐于2018-12-31
展开全部
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace jisuan
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(312, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(448, 72);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(88, 21);
this.textBox3.TabIndex = 2;
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"+",
"-",
"*",
"/"});
this.comboBox1.Location = new System.Drawing.Point(152, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 4;
this.button1.Text = "计算";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "清除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 6;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 366);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public double jia(double a,double b)
{
return a+b;
}
public double jian(double a,double b)
{
return a-b;
}
public double cheng(double a,double b)
{
return a*b;
}
public double chu(double a,double b)
{
return a/b;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
string i=this.comboBox1.SelectedItem.ToString();
switch(i)
{
case "+":this.textBox3.Text=this.jia(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "-":this.textBox3.Text=this.jian(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "*":this.textBox3.Text=this.cheng(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case"/" :this.textBox3.Text=this.chu(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=null;
this.textBox2.Text=null;
this.textBox3.Text = null;
}
private void button3_Click(object sender, EventArgs e)
{
//this.Hide();
Application.Exit();
//this.Close();
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace jisuan
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(312, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(448, 72);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(88, 21);
this.textBox3.TabIndex = 2;
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"+",
"-",
"*",
"/"});
this.comboBox1.Location = new System.Drawing.Point(152, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 4;
this.button1.Text = "计算";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "清除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 6;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 366);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public double jia(double a,double b)
{
return a+b;
}
public double jian(double a,double b)
{
return a-b;
}
public double cheng(double a,double b)
{
return a*b;
}
public double chu(double a,double b)
{
return a/b;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
string i=this.comboBox1.SelectedItem.ToString();
switch(i)
{
case "+":this.textBox3.Text=this.jia(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "-":this.textBox3.Text=this.jian(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "*":this.textBox3.Text=this.cheng(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case"/" :this.textBox3.Text=this.chu(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=null;
this.textBox2.Text=null;
this.textBox3.Text = null;
}
private void button3_Click(object sender, EventArgs e)
{
//this.Hide();
Application.Exit();
//this.Close();
}
}
}
展开全部
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 Main : Form
{
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
}
private void txtInshu1_TextChanged(object sender, EventArgs e)
{
}
private void txtInshu1_KeyPress(object sender, KeyPressEventArgs e)
{
OnlyEnterNumber(sender, e);
}
//// <summary>
/// 只能输入数字(含负号小数点)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnlyEnterNumber(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
{
e.Handled = true;
}
// 输入为负号时,只能输入一次且只能输入一次
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
}
/*
* 参数:d表示要四舍五入的数;i表示要保留的小数点后位数。
* 正负数都四舍五入,适合数据统计的显示
*/
double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);
return d;
}
private void txtInshu2_TextChanged(object sender, EventArgs e)
{
}
private void txtInshu2_KeyPress_1(object sender, KeyPressEventArgs e)
{
OnlyEnterNumber(sender, e);
}
private void btnJisuan_Click(object sender, EventArgs e)
{
if (txtInshu1.Text == "") {
MessageBox.Show("因数1不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (txtInshu2.Text == "")
{
MessageBox.Show("因数2不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
double inshu1 = Convert.ToDouble(txtInshu1.Text);
double inshu2 = Convert.ToDouble(txtInshu2.Text);
double result = 0.0;
if (radioBtnJia.Checked) {
result = inshu1 + inshu2;
}
if (radioBtnJian.Checked)
{
result = inshu1 - inshu2;
}
if (radioBtnCheng.Checked)
{
result = inshu1 * inshu2;
}
if (radioBtnChu.Checked)
{
if (0 == inshu2)
{
MessageBox.Show("因数2做除数不能为0!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
result = inshu1 / inshu2;
result = Round(result, 6);
}
txtResult.Text = Convert.ToString(result);
}
}
}
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 Main : Form
{
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
}
private void txtInshu1_TextChanged(object sender, EventArgs e)
{
}
private void txtInshu1_KeyPress(object sender, KeyPressEventArgs e)
{
OnlyEnterNumber(sender, e);
}
//// <summary>
/// 只能输入数字(含负号小数点)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnlyEnterNumber(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
{
e.Handled = true;
}
// 输入为负号时,只能输入一次且只能输入一次
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
}
/*
* 参数:d表示要四舍五入的数;i表示要保留的小数点后位数。
* 正负数都四舍五入,适合数据统计的显示
*/
double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);
}
string strd = prestr + "." + poststr;
d = Double.Parse(strd);
return d;
}
private void txtInshu2_TextChanged(object sender, EventArgs e)
{
}
private void txtInshu2_KeyPress_1(object sender, KeyPressEventArgs e)
{
OnlyEnterNumber(sender, e);
}
private void btnJisuan_Click(object sender, EventArgs e)
{
if (txtInshu1.Text == "") {
MessageBox.Show("因数1不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (txtInshu2.Text == "")
{
MessageBox.Show("因数2不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
double inshu1 = Convert.ToDouble(txtInshu1.Text);
double inshu2 = Convert.ToDouble(txtInshu2.Text);
double result = 0.0;
if (radioBtnJia.Checked) {
result = inshu1 + inshu2;
}
if (radioBtnJian.Checked)
{
result = inshu1 - inshu2;
}
if (radioBtnCheng.Checked)
{
result = inshu1 * inshu2;
}
if (radioBtnChu.Checked)
{
if (0 == inshu2)
{
MessageBox.Show("因数2做除数不能为0!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
result = inshu1 / inshu2;
result = Round(result, 6);
}
txtResult.Text = Convert.ToString(result);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-21
展开全部
我有这个程序,如果需要,请继续追问,并留下你的邮箱号码。满意后请采纳,万分感谢~~~!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-21
展开全部
你可以把联系我 或者留下你的邮箱 我把做好的程序和代码都可以发给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-21
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _604010013_3计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string kh = txtbds.Text; string fh = ""; string ths = "";
string[] sz = kh.Split(new char[] { '+', '-','*','/' });
for (int i = 0; i < kh.Length; i++)
{ if ((kh[i] == '*') || (kh[i] == '/') || (kh[i] == '+') || (kh[i] == '-')) fh += kh[i];
}
int kjs = 0;
for (int i = 0; i < fh.Length; i++)
{
if (fh[i] == '*')
{
kjs = int.Parse(sz[i]) * int.Parse(sz[i + 1]);
ths = sz[i] + "*" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
if (fh[i] == '/')
{
kjs = int.Parse(sz[i]) / int.Parse(sz[i + 1]);
ths = sz[i] + "/" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString()); if (fh[i] == '+')
{
kjs = int.Parse(sz[i]) + int.Parse(sz[i + 1]);
ths = sz[i] + "+" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
if (fh[i] == '-')
{
kjs = int.Parse(sz[i]) - int.Parse(sz[i + 1]);
ths = sz[i] + "-" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
} string jzfh = ""; int jxls = 0;
string[] jzsz = kh.Split(new char[] { '+', '-','*','/' });
for (int k = 0; k < kh.Length; k++)
{ if ((kh[k] == '+') || (kh[k] == '-') || (kh[k] == '*') || (kh[k] == '/'))jzfh += kh[k]; }
if (jzfh.Length!= 0)
{
for (int k = 0; k < jzfh.Length; k++)
{
if (jzfh[k] == '*') { jxls = int.Parse(jzsz[k]) * int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '/') { jxls = int.Parse(jzsz[k]) / int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '+') { jxls = int.Parse(jzsz[k]) + int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '-') { jxls = int.Parse(jzsz[k]) - int.Parse(jzsz[k + 1]); }
} txtjg.Text = jxls.ToString(); }
else { txtjg.Text = kh; }
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _604010013_3计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string kh = txtbds.Text; string fh = ""; string ths = "";
string[] sz = kh.Split(new char[] { '+', '-','*','/' });
for (int i = 0; i < kh.Length; i++)
{ if ((kh[i] == '*') || (kh[i] == '/') || (kh[i] == '+') || (kh[i] == '-')) fh += kh[i];
}
int kjs = 0;
for (int i = 0; i < fh.Length; i++)
{
if (fh[i] == '*')
{
kjs = int.Parse(sz[i]) * int.Parse(sz[i + 1]);
ths = sz[i] + "*" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
if (fh[i] == '/')
{
kjs = int.Parse(sz[i]) / int.Parse(sz[i + 1]);
ths = sz[i] + "/" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString()); if (fh[i] == '+')
{
kjs = int.Parse(sz[i]) + int.Parse(sz[i + 1]);
ths = sz[i] + "+" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
if (fh[i] == '-')
{
kjs = int.Parse(sz[i]) - int.Parse(sz[i + 1]);
ths = sz[i] + "-" + sz[i + 1];
kh = kh.Replace(ths, kjs.ToString());
}
} string jzfh = ""; int jxls = 0;
string[] jzsz = kh.Split(new char[] { '+', '-','*','/' });
for (int k = 0; k < kh.Length; k++)
{ if ((kh[k] == '+') || (kh[k] == '-') || (kh[k] == '*') || (kh[k] == '/'))jzfh += kh[k]; }
if (jzfh.Length!= 0)
{
for (int k = 0; k < jzfh.Length; k++)
{
if (jzfh[k] == '*') { jxls = int.Parse(jzsz[k]) * int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '/') { jxls = int.Parse(jzsz[k]) / int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '+') { jxls = int.Parse(jzsz[k]) + int.Parse(jzsz[k + 1]); }
if (jzfh[k] == '-') { jxls = int.Parse(jzsz[k]) - int.Parse(jzsz[k + 1]); }
} txtjg.Text = jxls.ToString(); }
else { txtjg.Text = kh; }
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询