IT/C#

11. C # 계산기 (Win Form)

Clair_de_Lune 2023. 12. 19. 13:55
728x90

11. C # 계산기 (Win Form)

 

 

  •  
  • 스트링 문자열 더qmf 변환 활용
  • Button button = (Button)sender; //버튼 가져오기 활용
  •  currentNumber += button.Text; // 텍스트 currentNumber에 추가
  •   Txt.Text = currentNumber; 출력
  • Number += button.Text; // 텍스트 가져오기
  • 시간나면 C# 열거자 학습
  •  

 

C# 계산기


 

 private string currentNumber; 
   private string operation;
      private double result;

 (변수 선언합시다.)

 

(버튼 1: 숫 )

   private void button1_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;  // 클릭된 버튼 가져오기
            currentNumber += button.Text; // 텍스트 currentNumber에 추가
            Txt.Text = currentNumber;
        }

(버튼 2 : - 빼 )

 private void button2_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            operation = button.Text;  //텍스트  저장
            result = double.Parse(currentNumber); //double로 바구고  result에 저장
            currentNumber = "";
            //빼기
        }

(버튼 3 : = 등)

 private void button3_Click(object sender, EventArgs e)
        {
            double Number = double.Parse(currentNumber); // double변환해

            switch (operation)
            {
                case "+":
                    result += Number; // 덧+
                    break;
                case "-":
                    result -= Number; // 뺄-
                    break;
                case "*":
                    result *= Number; // 곱*
                    break;
                case "/":
                    result /= Number; // 나누기 /
                    break;
            }

            Txt.Text = result.ToString(); // 결과: 문자열 바꾸고 출력
            currentNumber = "";
        }

(버튼 4 : C 초기화)

currentNumber = "";
            operation = "";
            result = 0; // 초기호ㅏ
            Txt.Text = "";


 

728x90