C#設計一個簡單的計算器,實現兩個數的加,減,乘,除,求冪等計算,執行效果如下圖所示:
阿新 • • 發佈:2020-10-26
1.題目要求如下:
C#設計一個簡單的計算器,實現兩個數的加,減,乘,除,求冪等計算,執行效果如下圖所示:
2.這邊需要用到的是VS2019下的C#Windows窗體
3.來吧,展示:
using System; using System.Windows.Forms; namespace Calculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private voidForm1_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/