'목록하단 광고 치환자(withSeok)
728x90

따라한 강의 : youtu.be/5yERdeEttSc

[C# 초보 강좌] 예제로 배우는 C# 06



위 if 구문을 처리하여,
새로운 숫자를 하나라도 넣었을 경우
과정을 수행한다.

실행파일 다운로드

WF_Cal_5.exe
0.01MB

단점은 정수만 계산이 가능하다는 것......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 WF_Cal_1
{
    public enum u_Operators { Add, Sub, Multi, Div }
        //enum 열거형 데이터 타입
 
    public partial class Form1 : Form
    {
        public Form1()
        { InitializeComponent(); }
 
        public int vResult = 0;
        public bool isNewNum = true;
        ublic u_Operators Opt = u_Operators.Add;
            // 형태 변수명 = 값
            // 마치 int A = 1
 
 
        private void bttnN1_Click(object sender, EventArgs e)
        {
            Button u_numBttn = (Button)sender;
            SetNum(u_numBttn.Text);
        }
 
        public void SetNum(string num)
        {
            if (isNewNum)
            {
                lblNumscreen.Text = num;
                isNewNum = false;
            }
            else if (lblNumscreen.Text == "0")
            {
                lblNumscreen.Text = num;
            }
            else
            {
                lblNumscreen.Text = lblNumscreen.Text + num;
            }
        }
 
        private void bttnPlus_Click(object sender, EventArgs e)
        {
            if (isNewNum == false//새로운 수가 입력됐을 만 처리
            {
                int num = int.Parse(lblNumscreen.Text);
 
                //아래 Opt는 이전의 연산자 계산
                if (Opt == u_Operators.Add)
                    vResult = vResult + num;
                else if (Opt == u_Operators.Sub)
                    vResult = vResult - num;
                else if (Opt == u_Operators.Multi)
                    vResult = vResult * num;
                else if (Opt == u_Operators.Div)
                    vResult = vResult / num;
 
                lblNumscreen.Text = vResult.ToString();
                isNewNum = true;
            }
            /새로운 수를 넣지 않은 경우
            //아래 구문에 의해 연산자만 변경됨.
 
 
            //아래 Opt는 연산자 미리 저장
            Button u_optBttn = (Button)sender;
            if (u_optBttn.Text == "+")
                Opt = u_Operators.Add;
            else if (u_optBttn.Text == "-")
                Opt = u_Operators.Sub;
            else if (u_optBttn.Text == "*")
                Opt = u_Operators.Multi;
            else if (u_optBttn.Text == "/")
                Opt = u_Operators.Div;
        }
 
        private void bttnClear_Click(object sender, EventArgs e)
        {
            vResult = 0;
            isNewNum = true;
            Opt = u_Operators.Add;
 
            lblNumscreen.Text = "0";
        }
    }
}
cs
Visual Studio 2019 C# 정수형 계산기 전체 파일

WF_Cal_2.zip
0.04MB

728x90

+ Recent posts