C#窗体程序:设计一个能简单计算加减乘除的计算器
分类:折腾 ; 热度:3566 ; 最后更新于2020 年 02 月 14 日
C#窗体程序:设计一个能简单计算加减乘除的计算器
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)
{
}
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)
{
}private void btnJisuan_Click(object sender, EventArgs e)
{
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)
{
result = inshu1 / inshu2;
result = Round(result, 6);
}txtResult.Text = Convert.ToString(result);
}
}
}