如何用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;
}
}
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
winform程序 呵呵~这个主要是大数的相乘和相除啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询