首页 > Windows开发 > 详细

用C#编写计算器

时间:2018-12-07 19:16:49      阅读:206      评论:0      收藏:0      [点我收藏+]

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

希望有大佬指正我的错误

感谢

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 += ".";
}
}
}
}

用C#编写计算器

原文:https://www.cnblogs.com/zhang-1999/p/10084499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!