안녕하세요.

간단할수도 있지만....

윈도우계산기와 같이, 입력하고 계산하는 계산기를 만들어볼까 합니다.



정말 간단하게 만들꺼라서....

화면은 위와 같습니다.

공학용 계산기도 물론 가능하겠지만, 이번에는 다루지 않도록 합니다. -.,-;;


= 과연.. 결과가 나올까...?


쨘... 120이 나왔습니다. (다행이다..)


15/8 의 결과가 제대로 나올까... ???

= 과연 결과는.....

나와주네요. 다행입니다.

일단, 여기까지 소스를 공유하고 설명하도록 하겠습니다.


우선, JScriptCodeProvider 를 사용하기 위해서 참조설정에서 Microsoft.JScript 를 추가하였습니다.


그리고, 아래와같은 클래스를 별도로 구축하였습니다.


아래가 소스의 전문입니다.

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using Microsoft.JScript;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 초기0존재시 데이터클리어
        /// </summary>
        private void ZeroClear()
        {
            if (lblSum.Text == "0") { lblSum.Text = string.Empty; }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "1";
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "2";
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "3";
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "4";
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "5";
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "6";
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "7";
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "8";
        }
 
        private void button9_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "9";
        }
 
        private void button10_Click(object sender, EventArgs e)
        {
            ZeroClear();
            lblSum.Text += "0";
        }
 
        private void button11_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text = lblSum.Text.Substring(0, lblSum.Text.Length - 1);
            }
        }
 
        private void button12_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text += "*";
            }
        }
 
        private void button13_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text += "/";
            }
        }
 
        private void button14_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text += "+";
            }
        }
 
        private void button15_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text += "-";
            }
        }
 
        private void button16_Click(object sender, EventArgs e)
        {
            if (lblSum.Text != string.Empty)
            {
                lblSum.Text = Evaluator.EvalToDouble(lblSum.Text).ToString();
            }
        }
 
        /// <summary>
        /// 클리어
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button17_Click(object sender, EventArgs e)
        {
            lblSum.Text = "0";
        }
    }
 
 
    public class Evaluator
    {
        public static int EvalToInteger(string statement)
        {
            string s = EvalToString(statement);
            return int.Parse(s.ToString());
        }
 
        public static double EvalToDouble(string statement)
        {
            string s = EvalToString(statement);
            return double.Parse(s);
        }
 
        public static string EvalToString(string statement)
        {
            object o = EvalToObject(statement);
            return o.ToString();
        }
 
        public static object EvalToObject(string statement)
        {
            return _evaluatorType.InvokeMember(
                        "Eval",
                        BindingFlags.InvokeMethod,
                        null,
                        _evaluator,
                        new object[] { statement }
                     );
        }
 
        static Evaluator()
        {
            ICodeCompiler compiler;
            compiler = new JScriptCodeProvider().CreateCompiler();
 
            CompilerParameters parameters;
            parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;
 
            CompilerResults results;
            results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);
 
            Assembly assembly = results.CompiledAssembly;
            _evaluatorType = assembly.GetType("Evaluator.Evaluator");
 
            _evaluator = Activator.CreateInstance(_evaluatorType);
        }
 
        private static object _evaluator = null;
        private static Type _evaluatorType = null;
        private static readonly string _jscriptSource =
 
            @"package Evaluator
            {
               class Evaluator
               {
                  public function Eval(expr : String) : String 
                  { 
                     return eval(expr); 
                  }
               }
            }";
    }
}
 
cs


전체소스를 첨부합니다.

CalcSample.zip




+ Recent posts