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次方结果
希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/