Programing C#/C# 200

005 C# 윈도우 프로그래밍 발전

junmoyo 2020. 1. 8. 14:58

목표 : 004 프로그램을 기능을 발전시킨다.

 

소스코드 : 

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 WindowsFormsApplication1
{

    public enum Arithmetic
    {
        plus,
        minus,
        multiple,
        division
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Clear();
        }

        private double GetNum(string str)
        {
            double returnValue = 0;
            if(string.IsNullOrEmpty(str))
            {
                MessageBox.Show("공백 입니다, 숫자를 입력하세요");
                this.Clear();
                throw new Exception("공백 입니다, 숫자를 입력하세요");
            }

            try
            {
                returnValue = double.Parse(str);
            }
            catch
            {
                MessageBox.Show("숫자를 입력하세요");
                this.Clear();
                throw new Exception("숫자를 입력하세요");
            }

            return returnValue;
        }

        private double GetNum1()
        {
            return GetNum(this.textNum1.Text);
        }

        private double GetNum2()
        {
            return GetNum(this.textNum2.Text);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            double num1 = GetNum1();
            double num2 = GetNum2();
            double num = Operate(num1, num2, Arithmetic.plus);

            this.textResult.Text = num.ToString();
            this.lbResult.Text = "+";
        }
        private void btnMinus_Click(object sender, EventArgs e)
        {
            double num1 = GetNum1();
            double num2 = GetNum2();
            double num = Operate(num1, num2, Arithmetic.minus);

            this.textResult.Text = num.ToString();
            this.lbResult.Text = "-";
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            double num1 = GetNum1();
            double num2 = GetNum2();
            double num = Operate(num1, num2, Arithmetic.multiple);

            this.textResult.Text = num.ToString();
            this.lbResult.Text = "*";
        }

        private void btnDivision_Click(object sender, EventArgs e)
        {
            double num1 = GetNum1();
            double num2 = GetNum2();
            double num = Operate(num1, num2, Arithmetic.division);

            this.textResult.Text = num.ToString();
            this.lbResult.Text = "/";
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.Clear();
        }

        private void Clear()
        {
            this.textNum1.Text = "";
            this.textNum2.Text = "";
            this.textResult.Text = "";
            this.lbResult.Text = "Ready";
        }

        private double Operate(double num1, double num2, Arithmetic ari)
        {
            double returnValue = 0.0;

            switch(ari)
            {
                case Arithmetic.plus:
                    returnValue = num1 + num2;
                    break;
                case Arithmetic.minus:
                    returnValue = num1 - num2;
                    break;
                case Arithmetic.multiple:
                    returnValue = num1 * num2;
                    break;
                case Arithmetic.division:                    
                    returnValue = num1 / num2;
                    break;
            }

            return returnValue;
        }


    }
}

 

결과