1. 程式人生 > >C#WinForm練習——製作簡易計算器

C#WinForm練習——製作簡易計算器


using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace FormProject
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
      
        /// <summary>
        /// 等於按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //char.IsNumber判斷是否為數字
,這裡只假設都是整數的情況下
            //for (int i = 0; i < textBox1.Text.Length; i++)
            //{
            //    if (char.IsNumber(textBox1.Text,i))
            //    {
            //        int n1 = Convert.ToInt32(textBox1.Text.Trim());
            //    }
            //    else
            //    {
            //        MessageBox.Show("只支援整數運算");
            //    }
            //}
            //採集資料
            int n1 = Convert.ToInt32(textBox1.Text.Trim());
            int n2 = int.Parse(textBox2.Text.Trim());
            if (comboBox1.SelectedIndex == 0)
            {
                MessageBox.Show("請選擇一個操作符再進行運算!");
            }
            else
            {
                int s = 0;
                switch (comboBox1.Text)
                {
                    case "+":
                        s = n1 + n2;
                        break;
                    case "-":
                        s = n1 - n2;
                        break;
                    case "*":
                        s = n1 * n2;
                        break;
                    case "/":
                        if (n2 == 0)
                        {
                            MessageBox.Show("除數不能為零!");
                        }
                        else
                        {
                            s = n1 / n2;
                        }                 
                        break;
                    default:
                        MessageBox.Show("未知的運算!");
                        break;
                }
                label1.Text = s.ToString();
            }
        }
        /// <summary>
        /// 窗體載入時
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Load(object sender, EventArgs e)
        {
            //給下拉列表框設定預設值
            this.comboBox1.SelectedIndex = 0;
        }
    }

}

用面向物件來實現簡易計算器:

新增一個計算類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculate
{
    public class Cal
    {
        public double Num1
        {
            get;
            set;
        }
        public double Num2
        {
            get;
            set;
        }


        public double Calcu(string operate)
        {
            double sum = 0;
            switch(operate)
            {
                case "+":
                    sum = this.Num1 + this.Num2;
                    break;
                case "-":
                    sum = this.Num1 - this.Num2;
                    break;
                case "*":
                    sum = this.Num1 * this.Num2;
                    break;
                case "/":
                    sum = this.Num1 / this.Num2;
                    break;
            }
            return sum;
        }
    }
}

窗體直接引用這個類的例項:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Cal cal = new Cal();
            cal.Num1 = Convert.ToInt32(textBox1.Text.Trim());
            cal.Num2 = int.Parse(textBox2.Text.Trim());
            if (comboBox1.SelectedIndex == 0)
            {
                MessageBox.Show("請選擇一個操作符後再執行計算!");
            }
            else
            {
                label1.Text = cal.Calcu(comboBox1.Text).ToString();
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }
    }
}