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) { try { string s1 = textBox1.Text; string s2 = textBox2.Text; int i1, i2; i1 = (int)Convert.ToInt64(s1); i2 = (int)Convert.ToInt64(s2); string s3 = (i1 i2).ToString(); MessageBox.Show(s3); } catch { MessageBox.Show("错误,请检查!"); } } private void button3_Click(object sender, EventArgs e) { try { string s1 = textBox1.Text; string s2 = textBox2.Text; double i1, i2; i1 = (int)Convert.ToInt64(s1); i2 = (int)Convert.ToInt64(s2); string s3 = (i1 / i2).ToString(); MessageBox.Show(s3); } catch { MessageBox.Show("错误,请检查!"); } } 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) { try { string s1 = textBox1.Text; string s2 = textBox2.Text; int i1, i2; i1 = (int)Convert.ToInt64(s1); i2 = (int)Convert.ToInt64(s2); string s3 = (i1 - i2).ToString(); MessageBox.Show(s3); } catch { MessageBox.Show("错误,请检查!"); } } private void button2_Click(object sender, EventArgs e) { try { string s1 = textBox1.Text; string s2 = textBox2.Text; int i1, i2; i1 = (int)Convert.ToInt64(s1); i2 = (int)Convert.ToInt64(s2); string s3 = (i1 * i2).ToString(); MessageBox.Show(s3); } catch { MessageBox.Show("错误,请检查!"); } } private void button4_Click(object sender, EventArgs e) { try { string s1 = textBox1.Text; string s2 = textBox2.Text; int i1, i2; i1 = (int)Convert.ToInt64(s1); i2 = (int)Convert.ToInt64(s2); double s3 = Math.Pow(i1, i2); string s4 = Convert.ToString(s3); MessageBox.Show(s4); } catch { MessageBox.Show("错误,请检查!"); } } private void button5_Click(object sender, EventArgs e) { //归零 textBox1.Text = string.Empty; textBox2.Text = string.Empty; } }}
这个有140多行代码稍微有点长,有待改善
4.来看看运行结果:ctrl f5
这边自由发挥加上了弹窗提示的效果
这边演示的为9的9次方结果
赞 (0)