본문 바로가기
PROGRAMMING CODE/C#

윈도우 계산기 프로그램 _ C#

by daye_ 2021. 10. 14.

 

 

기본 동작과 메모리 정도만 구현했음

 

 

 

5+40 =

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 윈도우_계산기_프로그램
{
    public partial class Form1 : Form
    {
        public bool isNewNum = true;
        private double savedValue = 0;
        private string myOperator=null;
        public bool firstOpt = true;
        private string memory = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "0";
        }

        private void numButton_Click (object sender, EventArgs e)
        {
            Button numButton = (Button) sender;
            setNum(numButton.Text);
        }

        private void setNum (String num)
        {
            if((isNewNum==true)||(textBox1.Text == "0"))
            {
                textBox1.Text = num;
                isNewNum = false;
            }
            else
            {
                textBox1.Text = textBox1.Text + num;
            }
        }
        private void Dot_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains(".") == false)
                textBox1.Text += ".";
        }
        private void Opt_Click(object sender, EventArgs e)
        {
            if (firstOpt == false) Result();
            Button Opt = (Button)sender;
            savedValue = double.Parse(textBox1.Text);
            myOperator = (Opt.Text).ToString();
            textBox2.Text = myOperator; //
            isNewNum = true;
            firstOpt = false;   
        }

        private void OptEqual_Click(object sender, EventArgs e) { Result(); }

        void Result()
        {
            if (myOperator == "+") textBox1.Text = (savedValue + double.Parse(textBox1.Text)).ToString();
            else if (myOperator == "-") textBox1.Text = (savedValue - double.Parse(textBox1.Text)).ToString();
            else if (myOperator == "X") textBox1.Text = (savedValue * double.Parse(textBox1.Text)).ToString();
            else if (myOperator == "/") textBox1.Text = (savedValue / double.Parse(textBox1.Text)).ToString();
            else if (myOperator == "%") textBox1.Text = (savedValue % double.Parse(textBox1.Text)).ToString();
            myOperator = "=";
            textBox2.Text = myOperator;
        }

        private void Cancle_click(object sender, EventArgs e)
        {
            Button myCancle = (Button)sender;

            if(myCancle.Text == "C")
            {
                textBox1.Text = "0";
                textBox2.Text = null;
                myOperator = null;
                savedValue = 0;
                isNewNum = true;
                firstOpt = true;
            }
            else if(myCancle.Text == "CE"){
                textBox1.Text = "0";
            }
        }

        private void memory_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox2.Text = btn.Text;
            if (btn.Text == "MS")
            {
                if (myOperator == null) memory = savedValue.ToString();
                else if (myOperator != null) memory = textBox1.Text;
                
            }
            else if (btn.Text == "MR")
            {
                if (myOperator == null)
                {
                    savedValue = double.Parse(memory);
                    textBox1.Text = memory;
                }
                else if (myOperator != null) textBox1.Text = memory;
            }
        }
    }
}

 

 

; 

'PROGRAMMING CODE > C#' 카테고리의 다른 글

[Oracle & C#] 메모장 만들기  (0) 2021.10.20
윈도우 계산기 프로그램 _ C# (망한코드)  (0) 2021.10.14