본문 바로가기

C#/개발

[C#] 계산기

기본중의 기본

 

 

코드

 

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 partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Clear();
        }

        private void Clear()
        {
            textNum1.Clear();
            textNum2.Clear();
            textResult.Clear();
        }

        private void Calc(String op)
        {
            try
            {
                if(string.IsNullOrEmpty(textNum1.Text) || string.IsNullOrEmpty(textNum2.Text)){
                    MessageBox.Show("공백이 입력되었습니다.");
                    Clear();
                }
                else{
                    switch(op){
                        case "+": textResult.Text = (double.Parse(textNum1.Text)+double.Parse(textNum2.Text)).ToString(); break;
                        case "-": textResult.Text = (double.Parse(textNum1.Text) - double.Parse(textNum2.Text)).ToString(); break;
                        case "*": textResult.Text = (double.Parse(textNum1.Text) * double.Parse(textNum2.Text)).ToString(); break;
                        case "/": textResult.Text = (double.Parse(textNum1.Text) / double.Parse(textNum2.Text)).ToString(); break;
                    }
                    lbResult.Text = op;
            }
            }
            catch { MessageBox.Show("숫자를 입력하세요."); Clear(); };
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            Calc("+");
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            Calc("-");
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            Calc("*");
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            Calc("/");
        }
    }
}

 

 

이건 연복리 계산기

 

 

코드

 

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 Bank
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnCalc_Click(object sender, EventArgs e)
        {
            this.lbResult.Text = "" + Calc(Decimal.ToDouble(this.nudAmount.Value),
                Decimal.ToDouble(this.nudRate.Value), Decimal.ToInt32(this.nudYear.Value)).ToString();
        }
        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.nudAmount.Value = (decimal)(double)200000;
            this.nudRate.Value = (decimal)4.5M;
            this.nudYear.Value = (decimal)2M;
            this.lbResult.Text = "";
        }
        public static int Calc(double a, double r, int n)
        {
            double result = a;
            double tmp = a;
            for (int i = 0; i < n; i++)
            {
                result*=(100.0 + r)/100.0;
            }
            return (int)result;
        }
    }
}

'C# > 개발' 카테고리의 다른 글

[C#] RSA 암호화 & 복호화  (0) 2022.08.05
[C#] 로또  (0) 2022.08.05
[C#] 웹 이미지 다운로더  (0) 2022.08.05
[C#] 시리얼 통신과 장비조작  (0) 2022.08.05
[C#] 그래픽  (0) 2022.08.05