C#设计一个简单的计算器,实现两个数的加,减,乘,除,求幂等计算,运行效果如下图所示:
1.题目要求如下:
C#设计一个简单的计算器,实现两个数的加,减,乘,除,求幂等计算,运行效果如下图所示:
2.这边需要用到的是VS2019下的C#Windows窗体
3.来吧,展示:
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("欢迎使用小关牌计算器!");
}
private void button1_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result = x + y;
label4.Text = textBox1.Text + "+" + textBox2.Text + "的值为" + result;
}
private void button3_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result = x / y;
label4.Text = textBox1.Text + "/" + textBox2.Text + "的值为" + result;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
MessageBox.Show("目前不支持其他运算!");
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result = x - y;
label4.Text = textBox1.Text + "-" + textBox2.Text + "的值为" + result;
}
private void button2_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result = x * y;
label4.Text = textBox1.Text + "*" + textBox2.Text + "的值为" + result;
}
private void button4_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
double result = Math.Pow(x, y);
label4.Text = textBox1.Text + "的" + textBox2.Text + "次方为" + result;
}
private void button5_Click(object sender, EventArgs e)
{
//归零
textBox1.Text = "";
textBox2.Text = "";
label4.Text = "";
}
}
}
此为小关计算器2.0简明版
4.来看看运行结果:ctrl+f5
这边自由发挥加上了弹窗提示的效果
这边演示的为9的9次方结果
希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/