«

»

Lis 03

Wydrukuj to Wpis

Kalkulator w Visual C#

calc

Kalkulator wykonuje podstawowe operacje arytmetyczne, takie jak dodawanie, odejmowanie, mnożenie, dzielenie.

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/// 
///Omówienie Kalkulatora
///Kalkulator wykonuje podstawowe operacje arytmetyczne, takie jak 
///dodawanie, odejmowanie, mnożenie, dzielenie.
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
    public class Calc : Form
    {
        TextBox tbox;
        Button btnCE;
        Button btn1;  Button btn2;  Button btn3;  Button btnDziel;
        Button btn4;  Button btn5;  Button btn6;  Button btnMnoz;
        Button btn7;  Button btn8;  Button btn9;  Button btnOdej;
        Button btn0;  Button btnPrzec;  Button btnRow;  Button btnDod;
 
        string operat;
        bool cyfra_pierwsza =true;
        double walor1, walor2;
 
        [STAThread]
        static void Main()
        {
            Application.Run(new Calc());
        }
 
    void cyfry_CE_Click(object sender, EventArgs e)
    {
        if (((Button)sender).Text == "CE")
        {
            tbox.Text = "0";
            operat = "";
            cyfra_pierwsza = true;
        }
        else
        {
            string cyfra = ((Button)sender).Text;
            if (cyfra_pierwsza)
            {
                tbox.Text = cyfra;
                cyfra_pierwsza = false;
            }
            else
                tbox.Text += cyfra;
        }  
    }  
 
    void operat_Click(object sender, EventArgs e)
    {
        walor1 = Double.Parse(tbox.Text);
        operat = ((Button)sender).Text;
        cyfra_pierwsza = true;
    }
 
    void btn_Row_Click(object sender, EventArgs e)
    {
        double rezultat = 0;
        walor2 = Double.Parse(tbox.Text);
        switch (operat)
        {
            case "+":
                rezultat = walor1 + walor2;
                break;
            case "-":
                rezultat = walor1 - walor2;
                break;
            case "/":
                rezultat = walor1 / walor2;
                break;
            case "*":
                rezultat = walor1 * walor2;
                break;
            default:
                break;
        }
        tbox.Text = rezultat.ToString();
        walor1 = rezultat;
        cyfra_pierwsza = true;
    }
 
    public Calc()
    {
        tbox = new TextBox();
        btnCE = new Button();
        btn1 = new Button(); btn2 = new Button(); btn3 = new Button(); btnDziel = new Button();
        btn4 = new Button(); btn5 = new Button(); btn6 = new Button(); btnMnoz = new Button();
        btn7 = new Button(); btn8 = new Button(); btn9 = new Button(); btnOdej = new Button();
        btn0 = new Button(); btnPrzec = new Button(); btnRow = new Button(); btnDod = new Button();
 
        //Calc
        this.Text = "Kalkulator";
        this.Size = new Size(220, 300);
        this.MaximizeBox = false;
 
        //tbox
        tbox.Font = new Font("Microsoft Sans Serif", 12F);
        tbox.Location = new Point(12, 12);
        tbox.Size = new Size(176, 20);
        tbox.TextAlign = HorizontalAlignment.Right;
        tbox.Text = "0";
        this.Controls.Add(tbox);
 
        //btnCE
        btnCE.Size = new Size(tbox.Width, 20);
        btnCE.Location = new Point(12, tbox.Location.Y + tbox.Height + 5);
        //btnCE.TabIndex = 1;
        btnCE.Text = "CE";
        btnCE.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btnCE);
 
        //btn1
        btn1.Size = new Size(40, 40);
        btn1.Location = new Point(12, btnCE.Location.Y + btnCE.Height + 5);
        btn1.TabIndex = 1;
        btn1.Text = "1";
        btn1.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn1);
 
        //btn2
        btn2.Size = new Size(40, 40);
        btn2.Location = new Point(btn1.Location.X + btn1.Width + 5, btn1.Location.Y);
        btn2.TabIndex = 2;
        btn2.Text = "2";
        btn2.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn2);
 
        //btn3
        btn3.Size = new Size(40, 40);
        btn3.Location = new Point(btn2.Location.X + btn2.Width + 5, btn2.Location.Y);
        btn3.TabIndex = 3;
        btn3.Text = "3";
        btn3.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn3);
 
        //Dziel
        btnDziel.Size = new Size(40, 40);
        btnDziel.Location = new Point(btn3.Location.X + btn3.Width + 5, btn3.Location.Y);
        btnDziel.TabIndex = 10;
        btnDziel.Text = "/";
        btnDziel.Click += new EventHandler(operat_Click);
        this.Controls.Add(btnDziel);
 
        //btn4
        btn4.Size = new Size(40, 40);
        btn4.Location = new Point(btn1.Location.X, btn1.Location.Y + btn1.Height + 5);
        btn4.TabIndex = 4;
        btn4.Text = "4";
        btn4.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn4);
 
        //btn5
        btn5.Size = new Size(40, 40);
        btn5.Location = new Point(btn2.Location.X, btn2.Location.Y + btn2.Height + 5);
        btn5.TabIndex = 5;
        btn5.Text = "5";
        btn5.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn5);
 
        //btn6
        btn6.Size = new Size(40, 40);
        btn6.Location = new Point(btn3.Location.X, btn3.Location.Y + btn3.Height + 5);
        btn6.TabIndex = 6;
        btn6.Text = "6";
        btn6.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn6);
 
        //btnMnoz
        btnMnoz.Size = new Size(40, 40);
        btnMnoz.Location = new Point(btn6.Location.X + btn6.Width + 5, btn6.Location.Y);
        btnMnoz.TabIndex = 11;
        btnMnoz.Text = "*";
        btnMnoz.Click += new EventHandler(operat_Click);
        this.Controls.Add(btnMnoz);
 
        //btn7
        btn7.Size = new Size(40, 40);
        btn7.Location = new Point(btn4.Location.X, btn4.Location.Y + btn4.Height + 5);
        btn7.TabIndex = 7;
        btn7.Text = "7";
        btn7.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn7);
 
        //btn8
        btn8.Size = new Size(40, 40);
        btn8.Location = new Point(btn5.Location.X, btn5.Location.Y + btn5.Height + 5);
        btn8.TabIndex = 8;
        btn8.Text = "8";
        btn8.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn8);
 
        //btn9
        btn9.Size = new Size(40, 40);
        btn9.Location = new Point(btn6.Location.X, btn6.Location.Y + btn6.Height + 5);
        btn9.TabIndex = 9;
        btn9.Text = "9";
        btn9.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn9);
 
        //btnOdej
        btnOdej.Size = new Size(40, 40);
        btnOdej.Location = new Point(btn9.Location.X + btn9.Width + 5, btn9.Location.Y);
        btnOdej.TabIndex = 12;
        btnOdej.Text = "-";
        btnOdej.Click += new EventHandler(operat_Click);
        this.Controls.Add(btnOdej);
 
        //btn0
        btn0.Size = new Size(40, 40);
        btn0.Location = new Point(btn7.Location.X, btn7.Location.Y + btn7.Height + 5);
        btn0.TabIndex = 0;
        btn0.Text = "0";
        btn0.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btn0);
 
        //btnPrzec
        btnPrzec.Size = new Size(40, 40);
        btnPrzec.Location = new Point(btn8.Location.X, btn8.Location.Y + btn8.Height + 5);
        btnPrzec.TabIndex = 13;
        btnPrzec.Text = ",";
        btnPrzec.Click += new EventHandler(cyfry_CE_Click);
        this.Controls.Add(btnPrzec);
 
        //btnRow
        btnRow.Size = new Size(40, 40);
        btnRow.Location = new Point(btn9.Location.X, btn9.Location.Y + btn9.Height + 5);
        btnRow.TabIndex = 14;
        btnRow.Text = "=";
        btnRow.Click += new EventHandler(btn_Row_Click);
        this.Controls.Add(btnRow);
 
        //btnDod
        btnDod.Size = new Size(40, 40);
        btnDod.Location = new Point(btnOdej.Location.X, btnOdej.Location.Y + btnOdej.Height + 5);
        btnDod.TabIndex = 15;
        btnDod.Text = "+";
        btnDod.Click += new EventHandler(operat_Click);
        this.Controls.Add(btnDod);
    }
    }

Załączniki do pobrania

Permalink do tego artykułu: http://tmall.com.pl/kalkulator-w-visual-c/

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

Możesz użyć tych znaczników i atrybutów HTMLa: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>