-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.cs
More file actions
211 lines (190 loc) · 8.3 KB
/
MainActivity.cs
File metadata and controls
211 lines (190 loc) · 8.3 KB
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
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using AndroidX.AppCompat.App;
using System;
using System.Xml;
using static Android.Resource;
namespace Calculator
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
// הגדרת כפתורים ומשתנים
TextView txt1;
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnac, btnmulti, btnplus, btndiv, btnminus, btndot, btnequal, btntan, btncos, btnsin;
string current_num = "", op = "";
double num1 = 0, num2 = 0, radians = 0, degrees = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
// יוצרים את כל הכפתורים
txt1 = FindViewById<TextView>(Resource.Id.txt1);
btn0 = FindViewById<Button>(Resource.Id.btn0);
btn1 = FindViewById<Button>(Resource.Id.btn1);
btn2 = FindViewById<Button>(Resource.Id.btn2);
btn3 = FindViewById<Button>(Resource.Id.btn3);
btn4 = FindViewById<Button>(Resource.Id.btn4);
btn5 = FindViewById<Button>(Resource.Id.btn5);
btn6 = FindViewById<Button>(Resource.Id.btn6);
btn7 = FindViewById<Button>(Resource.Id.btn7);
btn8 = FindViewById<Button>(Resource.Id.btn8);
btn9 = FindViewById<Button>(Resource.Id.btn9);
btnac = FindViewById<Button>(Resource.Id.btnac);
btnmulti = FindViewById<Button>(Resource.Id.btnmulti);
btnplus = FindViewById<Button>(Resource.Id.btnplus);
btnminus = FindViewById<Button>(Resource.Id.btnminus);
btndiv = FindViewById<Button>(Resource.Id.btndiv);
btndot = FindViewById<Button>(Resource.Id.btndot);
btnequal = FindViewById<Button>(Resource.Id.btnequal);
btntan = FindViewById<Button>(Resource.Id.btntan);
btnsin = FindViewById<Button>(Resource.Id.btnsin);
btncos = FindViewById<Button>(Resource.Id.btncos);
// קריאה לכל הכפתורים כאשר הם נלחצים
btn0.Click += OnbuttonClicked;
btn1.Click += OnbuttonClicked;
btn2.Click += OnbuttonClicked;
btn3.Click += OnbuttonClicked;
btn4.Click += OnbuttonClicked;
btn5.Click += OnbuttonClicked;
btn6.Click += OnbuttonClicked;
btn7.Click += OnbuttonClicked;
btn8.Click += OnbuttonClicked;
btn9.Click += OnbuttonClicked;
btnac.Click += OnbuttonClicked;
btnplus.Click += OnplusClicked;
btnminus.Click += OnminusClicked;
btnmulti.Click += OnmultiplicationClicked;
btndiv.Click += OndivisonClicked;
btndot.Click += OndotClicked;
btnsin.Click += OnsintClicked;
btncos.Click += OncostClicked;
btntan.Click += OntantClicked;
btnequal.Click += OnequalClicked;
}
// פונקציה לחיצה על הכפתורים 0-9 ומחיקה
public void OnbuttonClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
if (btnac == btn)
{
txt1.Text = "0";
current_num = "";
}
if (btn0 == btn) current_num += 0;
if (btn1 == btn) current_num += 1;
if (btn2 == btn) current_num += 2;
if (btn3 == btn) current_num += 3;
if (btn4 == btn) current_num += 4;
if (btn5 == btn) current_num += 5;
if (btn6 == btn) current_num += 6;
if (btn7 == btn) current_num += 7;
if (btn8 == btn) current_num += 8;
if (btn9 == btn) current_num += 9;
if (btnac != btn) txt1.Text = current_num;
}
// פונקציה לחיצה על כפתור שווה
public void OnequalClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
if (op == "+" || op == "-" || op == "÷" || op == "*")
{
num2 = Convert.ToDouble(current_num);
if (op == "+") txt1.Text = Convert.ToString(num1 + num2);
if (op == "-") txt1.Text = Convert.ToString(num1 - num2);
if (op == "÷") txt1.Text = Convert.ToString(num1 / num2);
if (op == "*") txt1.Text = Convert.ToString(num1 * num2);
current_num = txt1.Text;
}
if (op == "sin" || op == "cos" || op == "tan")
{
current_num = "";
current_num = txt1.Text.Remove(0, 3);
num1 = Convert.ToDouble(current_num);
if (op == "sin") radians = Math.Sin(num1);
if (op == "cos") radians = Math.Cos(num1);
if (op == "tan") radians = Math.Tan(num1);
degrees = radians * 180 / Math.PI;
degrees = Math.Round(degrees, 2);
current_num = Convert.ToString(degrees);
txt1.Text = current_num;
}
}
// פונקציה לחיצה על כפתור פלוס
public void OnplusClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
num1 = Convert.ToDouble(current_num);
current_num = "";
op = "+";
txt1.Text = "+";
}
// פונקציה לחיצה על כפתור מינוס
public void OnminusClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
num1 = Convert.ToDouble(current_num);
current_num = "";
op = "-";
txt1.Text = "-";
}
// פונקציה לחיצה על כפתור חלוקה
public void OndivisonClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
num1 = Convert.ToDouble(current_num);
current_num = "";
op = "÷";
txt1.Text = "÷";
}
// פונקציה לחיצה על כפתור כפל
public void OnmultiplicationClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
num1 = Convert.ToDouble(current_num);
current_num = "";
op = "*";
txt1.Text = "x";
}
// פונקציה לחיצה על כפתור נקודה
public void OndotClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
current_num = txt1.Text + ".";
txt1.Text = current_num;
}
// פונקציה לחיצה על כפתור סינוס
public void OnsintClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
txt1.Text = "sin";
current_num = "sin";
op = "sin";
}
// פונקציה לחיצה על כפתור קוסינוס
public void OncostClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
txt1.Text = "cos";
current_num = "cos";
op = "cos";
}
// פונקציה לחיצה על כפתור טאנגס
public void OntantClicked(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
txt1.Text = "tan";
current_num = "tan";
op = "tan";
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}