-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
251 lines (215 loc) · 9.51 KB
/
MainActivity.java
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
246
247
248
249
250
251
package com.example.apoorva.myapplicationcalc;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.os.*;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final Button b1, b2, b3, bmul, b5;
final Button b6, b7, bsub, b9, b4, b8, badd, bdiv, bdec, b0, beql;
final TextView edt1;
edt1 = findViewById(R.id.ed1);
b7 = findViewById(R.id.but1);
b8 = findViewById(R.id.but2);
b9 = findViewById(R.id.but3);
bmul = findViewById(R.id.but4);
b4 = findViewById(R.id.but5);
b5 = findViewById(R.id.but6);
b6 = findViewById(R.id.but7);
bsub = findViewById(R.id.but8);
b1 = findViewById(R.id.but9);
b2 = findViewById(R.id.but10);
b3 = findViewById(R.id.but11);
badd = findViewById(R.id.but12);
bdiv = findViewById(R.id.but13);
b0 = findViewById(R.id.but14);
bdec = findViewById(R.id.but15);
beql = findViewById(R.id.but16);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "1");
}
}
);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "2");
}
}
);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "3");
}
}
);
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "4");
}
}
);
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "5");
}
}
);
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "6");
}
}
);
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "7");
}
}
);
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "8");
}
}
);
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "9");
}
}
);
bmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "X");
}
}
);
badd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = edt1.getText().toString();
edt1.setText(edt1.getText() + "+");
}
}
);
bsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "-");
}
}
);
bdec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + ".");
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt1.setText(edt1.getText() + "0");
}
}
);
bdiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
edt1.setText(edt1.getText() + "/");
}
}
);
beql.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int b;
//boolean b;
String str = edt1.getText().toString();
if(str.contains("..")||str.contains("++")||str.contains("--")||str.contains("**")||str.contains("//")||str.contains("+-")||str.contains("*-")||str.contains("/-")||str.contains("/+")||str.contains("/*")||str.contains("+++"))
{
Toast.makeText(getApplicationContext(),"Invalid Expression",Toast.LENGTH_LONG).show();
}
else {
double p1 = bodmas(str);
String p = Double.toString(p1);
edt1.setText(p);
}
}
});
}
//bodmas
static double bodmas(String equation){
double result = 0.0;
String noMinus = equation.replace("-", "+-");
String[] byPluses = noMinus.split("\\+");//split by addition
for (String multipl : byPluses) {
String[] byMultipl = multipl.split("X");//split by multiplication
double multiplResult = 1.0;
for (String operand : byMultipl) {
if (operand.contains("/")) {
String[] division = operand.split("\\/");//split by division
double divident = Double.parseDouble(division[0]);//division
for (int i = 1; i < division.length; i++) {
divident /= Double.parseDouble(division[i]);
}
multiplResult *= divident;//multiplication ke result me 1 1 karke multiply hoga
} else {
multiplResult *= Double.parseDouble(operand);
}
}
result += multiplResult;
}
return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id=item.getItemId();
//noinspection SimplifiableIfStatement
if(id==R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
}