如何用C#编写简易计算器
3个回答
展开全部
要新建一个项目,类型是 window窗体应用程序 ,然后使用工具箱中的组件布局计算器窗口,然后双击每个组件,为其添加代码,我写了一个简单计算器代码,我写的这个实现的功能就是整数或小数的四则运算,输错可以清零重新输入。你也不用和我的一模一样,我只是为你提供一个思路,具体情况你自己试着做吧,这个为题不难,我的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
char fh;
double number,temp;
private void Form1_Load(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void but1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void but2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void but3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void but4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void but5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void but6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void but7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void but8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void but9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void but0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void but10_Click_1(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void butAdd_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '+';
textBox1.Text = null;
}
private void butMin_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '-';
textBox1.Text = null;
}
private void butMul_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '*';
textBox1.Text = null;
}
private void butDiv_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '/';
textBox1.Text = null;
}
private void butRev_Click(object sender, EventArgs e)
{
fh = ' ';
number = 0;
temp = 0;
textBox1.Text = null;
}
private void butApp_Click(object sender, EventArgs e)
{
temp = double.Parse(textBox1.Text);
switch (fh)
{
case '+':
number += temp;
break;
case '-':
number -= temp;
break;
case '*':
number *= temp;
break;
case '/':
number /= temp;
break;
}
textBox1.Text = number.ToString();
fh = ' ';
number = 0;
temp = 0;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
char fh;
double number,temp;
private void Form1_Load(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void but1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void but2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void but3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void but4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void but5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void but6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void but7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void but8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void but9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void but0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void but10_Click_1(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void butAdd_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '+';
textBox1.Text = null;
}
private void butMin_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '-';
textBox1.Text = null;
}
private void butMul_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '*';
textBox1.Text = null;
}
private void butDiv_Click(object sender, EventArgs e)
{
number = double.Parse(textBox1.Text);
fh = '/';
textBox1.Text = null;
}
private void butRev_Click(object sender, EventArgs e)
{
fh = ' ';
number = 0;
temp = 0;
textBox1.Text = null;
}
private void butApp_Click(object sender, EventArgs e)
{
temp = double.Parse(textBox1.Text);
switch (fh)
{
case '+':
number += temp;
break;
case '-':
number -= temp;
break;
case '*':
number *= temp;
break;
case '/':
number /= temp;
break;
}
textBox1.Text = number.ToString();
fh = ' ';
number = 0;
temp = 0;
}
}
}
展开全部
简单的加减乘除计算器示例(Console模式):
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("请输入数字A:");
string strNumberA = Console.ReadLine();
Console.Write("请选择运算符号(+、-、*、/):");
string strOperate = Console.ReadLine();
Console.Write("请输入数字B:");
string strNumberB = Console.ReadLine();
string strResult = "";
switch (strOperate)
{
case "+":
strResult = Convert.ToString(Convert.ToDouble(strNumberA) + Convert.ToDouble(strNumberB));
break;
case "-":
strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB));
break;
case "*":
strResult = Convert.ToString(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB));
break;
case "/":
if (strNumberB != "0")
strResult = Convert.ToString(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB));
else
strResult = "除数不能为0";
break;
}
Console.WriteLine("结果是:" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("您的输入有错:" + ex.Message);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-11-23
展开全部
你要做什么样的计算器?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询