用C#编写计算器

零有点问题,而且目前只能做一些简单的运算,+、-、*、/、平方、开根号

希望有大佬指正我的错误

感谢

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

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
double a;//第一个运算数
double b;//第二个运算数
string d;//结果
public Form1()
{
InitializeComponent();
}

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

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

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

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

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

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

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

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

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

private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
if (d == "/")
{
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void button14_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);//parse 解析,把字符串转化为整型,转换数据类型
textBox1.Text = "";
d = "+";
}

private void button15_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
textBox1.Text = "";
d = "-";
}

private void button12_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
textBox1.Text = "";
d = "*";
}

private void button13_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
textBox1.Text = "";
d = "/";
}

private void button10_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
textBox1.Text = "";
d = "pow";
}

private void button11_Click(object sender, EventArgs e)
{
b = double.Parse(textBox1.Text);
textBox1.Text = "";
d = "sqrt";
}

private void button18_Click(object sender, EventArgs e)
{
switch (d)
{
case "+": a=b+ double.Parse(textBox1.Text);
break;
case "-": a = b - double.Parse(textBox1.Text);
break;
case "*": a = b * double.Parse(textBox1.Text);
break;
case "/": a = b / double.Parse(textBox1.Text);
break;
case "pow": a = Math.Pow(b,2.0);
break;
case "sqrt": a = Math.Sqrt(b);
break;
}
textBox1.Text = a + "";
}

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

private void button17_Click(object sender, EventArgs e)//小数点
{
if (textBox1.Text != "")
{
textBox1.Text += ".";
}
}
}
}

posted on 2018-12-07 18:44  萌新菜鸟  阅读(5614)  评论(0编辑  收藏  举报

导航