forked from knchapagai/ProgrammingHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc#.txt
172 lines (158 loc) · 5.44 KB
/
c#.txt
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
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 WindowsFormsApplication1
{
public partial class csharp_calculator : Form
{
public csharp_calculator()
{
InitializeComponent();
}
float num1 = 0, num2 = 0;
int oprClickCount = 0;
bool isOprClick = false, isEqualClick = false;
string opr;
private void csharp_calculator_Load(object sender, EventArgs e)
{
// add button click event to all buttons
foreach(Control c in Controls)
{
if(c is Button)
{
if(c.Text != "Reset")
c.Click += new System.EventHandler(btn_click);
}
}
}
// create button click event
public void btn_click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (!isOperator(button))// if the button is a number
{
if(isOprClick)
{
num1 = float.Parse(textBox1.Text);
textBox1.Text = "";
}
// if the textbox text not contain "."
if (!textBox1.Text.Contains("."))
{
if (textBox1.Text.Equals("0") && !button.Text.Equals("."))
{
// delete the first "0"
// set button text to the textbox
// if the button text is not "."
textBox1.Text = button.Text;
isOprClick = false;
}
else
{
textBox1.Text += button.Text;
isOprClick = false;
}
}
else if (!button.Text.Equals("."))
{
textBox1.Text += button.Text;
isOprClick = false;
}
}
else // if the button is an operator [+ - / * =]
{
if(oprClickCount == 0)// if it's the first time we click on an operator
{
oprClickCount++;
// convert textbox text to float and set it into num1
num1 = float.Parse(textBox1.Text);
// get the operator from button text
opr = button.Text;
// set oprclick to true
isOprClick = true;
}
else
{
if(!button.Text.Equals("="))// if the operation is not "="
{
if(!isEqualClick)
{
num2 = float.Parse(textBox1.Text);
textBox1.Text = Convert.ToString(calc(opr, num1, num2));
num2 = float.Parse(textBox1.Text);
opr = button.Text;
isOprClick = true;
isEqualClick = false;
}else
{
isEqualClick = false;
opr = button.Text;
}
}
else
{
num2 = float.Parse(textBox1.Text);
textBox1.Text = Convert.ToString(calc(opr, num1, num2));
num1 = float.Parse(textBox1.Text);
isOprClick = true;
isEqualClick = true;
}
}
}
}
// create a function to check if the clicked button is a number(+ '.') or an operators
public bool isOperator(Button button)
{
string buttonText = button.Text;
if(buttonText.Equals("+") || buttonText.Equals("-") ||
buttonText.Equals("X") || buttonText.Equals("/") ||
buttonText.Equals("="))
{
return true;
}
else
{
return false;
}
}
// function to calculate
public float calc(string opr, float n1, float n2)
{
float result = 0;
switch (opr)
{
case"+":
result = n1 + n2;
break;
case "-":
result = n1 - n2;
break;
case "X":
result = n1 * n2;
break;
case "/":
if(n2 != 0)
result = n1 / n2;
break;
}
return result;
}
// button reset to make another calculation
private void buttonReset_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
opr = "";
isOprClick = false;
isEqualClick = false;
oprClickCount = 0;
textBox1.Text = "0";
}
}
}