Advertisement
Guest User

Untitled

a guest
May 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.49 KB | None | 0 0
  1. package hu.blogspot.visitcatalunya.calculadora;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8.  
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.  
  12. public double total = 0; // number shown in the result textView
  13. public boolean comma = false;
  14. public boolean editNum = true;
  15. public int sign = 1;
  16. public int decimal = 0;
  17. public double num1 = 0;
  18. public double num2 = 0;
  19. public int lastButton = 1;
  20. public int operation = 0;
  21. public int saveOperation = 0;
  22.  
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. }
  29.  
  30. /*
  31.  
  32. */
  33. public void Display(View v) {
  34. TextView result = (TextView) findViewById(R.id.result);
  35. result.setText(String.valueOf(total));
  36. }
  37.  
  38. public void delete(View v) {
  39. total = 0;
  40. decimal = 0;
  41. comma = false;
  42. editNum = true;
  43. sign = 1;
  44. num1 = 0;
  45. num2 = 0;
  46. Display(v);
  47. lastButton = 1;
  48. saveOperation = 0;
  49. operation = 0;
  50. }
  51.  
  52. public void dot(View v) {
  53. comma = true;
  54. lastButton = 18;
  55. }
  56.  
  57. public void plusMinus(View v) {
  58. total = -total;
  59. sign *= -1;
  60. Display(v);
  61. lastButton = 19;
  62. }
  63.  
  64. // ===========================================================================
  65. public void calculate(View v) {
  66. if (lastButton == 20) {
  67. operation = saveOperation;
  68. } else if (lastButton == 2) {
  69. if (operation == 1 || operation == 2) {
  70. num2 = total * num1 / 100;
  71. } else {
  72. num2 = total / 100;
  73. }
  74. } else {
  75. num2 = total;
  76. }
  77.  
  78. if (operation == 1) {
  79. total = num1 + num2;
  80. num1 = total;
  81.  
  82. } else if (operation == 2) {
  83. total = num1 - num2;
  84. num1 = total;
  85. } else if (operation == 3) {
  86. total = num1 * num2;
  87. num1 = total;
  88. } else if (operation == 4) {
  89. total = num1 / num2;
  90. num1 = total;
  91. }
  92.  
  93. Display(v);
  94.  
  95. editNum = false;
  96. lastButton = 20;
  97. comma = false;
  98. decimal = 0;
  99. saveOperation = operation;
  100. operation = 0;
  101.  
  102. }
  103.  
  104.  
  105. public void sum(View v) {
  106.  
  107. if (operation == 0) {
  108. num1 = total;
  109. total = 0;
  110. operation = 1;
  111. } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
  112. operation = 1;
  113. } else {
  114. num2 = total;
  115. calculate(v);
  116. operation = 1;
  117. }
  118. decimal = 0;
  119. comma = false;
  120.  
  121. lastButton = 16;
  122.  
  123. }
  124.  
  125. public void res(View v) {
  126. if (operation == 0) { //
  127. num1 = total;
  128. total = 0;
  129. operation = 2;
  130. } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
  131. operation = 1;
  132. } else {
  133. num2 = total;
  134. calculate(v);
  135. operation = 2;
  136. }
  137.  
  138. decimal = 0;
  139. comma = false;
  140.  
  141.  
  142. lastButton = 12;
  143. }
  144.  
  145.  
  146. public void per(View v) {
  147. if (operation == 0) { //
  148. num1 = total;
  149. total = 0;
  150. operation = 3;
  151. } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
  152. operation = 3;
  153. } else {
  154. num2 = total;
  155. calculate(v);
  156. operation = 3;
  157. }
  158.  
  159. decimal = 0;
  160. comma = false;
  161.  
  162. lastButton = 8;
  163. }
  164.  
  165. public void div(View v) {
  166. if (operation == 0) {
  167. num1 = total;
  168. total = 0;
  169. operation = 4;
  170. } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
  171. operation = 4;
  172. } else {
  173. num2 = total;
  174. calculate(v);
  175. operation = 4;
  176. }
  177.  
  178. decimal = 0;
  179. lastButton = 4;
  180. }
  181.  
  182. public void percent(View v) {
  183. if (lastButton != 2) {
  184. lastButton = 2;
  185. calculate(v);
  186.  
  187.  
  188. }
  189. }
  190.  
  191. public void sqrtRoot(View v) {
  192. if (total < 0) {
  193. Toast.makeText(MainActivity.this, "Negative number, use +/- before", Toast.LENGTH_SHORT).show();
  194. } else {
  195. total = Math.sqrt(total);
  196. Display(v);
  197. }
  198. editNum = false;
  199. lastButton = 3;
  200.  
  201. }
  202.  
  203. //00000000000000000000000000000000000000000000000000000000000
  204. public void set0(View v) {
  205. if (lastButton == 20 || lastButton <= 3) {
  206. delete(v);
  207. }
  208. if (editNum) {
  209.  
  210. if (!comma) {
  211. total = total * 10;
  212. } else {
  213. decimal++;
  214. }
  215. } else {
  216. total = 0;
  217. decimal = 0;
  218. editNum = true;
  219. sign = 1;
  220. }
  221. Display(v);
  222. lastButton = 17;
  223.  
  224.  
  225. }
  226.  
  227. //11111111111111111111111111111111111111111111111111111111
  228. public void set1(View v) {
  229. if (lastButton == 20 || lastButton <= 3) {
  230. total = 0;
  231. }
  232. if (editNum) {
  233.  
  234. if (!comma) {
  235. total = total * 10 + sign;
  236. } else {
  237. decimal++;
  238. total = total + sign / (Math.pow(10, decimal));
  239. }
  240. } else {
  241. total = 1;
  242. decimal = 0;
  243. sign = 1;
  244. editNum = true;
  245. }
  246. Display(v);
  247. lastButton = 13;
  248.  
  249.  
  250. }
  251.  
  252. //222222222222222222222222222222222222222222222222222222222
  253. public void set2(View v) {
  254. if (lastButton == 20 || lastButton <= 3) {
  255. total = 0;
  256. }
  257. if (editNum) {
  258.  
  259. if (!comma) {
  260. total = total * 10 + 2 * sign;
  261. } else {
  262. decimal++;
  263. total = total + 2 * sign / (Math.pow(10, decimal));
  264. }
  265. } else {
  266. total = 2;
  267. decimal = 0;
  268. sign = 1;
  269. editNum = true;
  270. }
  271. Display(v);
  272. lastButton = 14;
  273.  
  274. }
  275.  
  276. public void set3(View v) {
  277. if (lastButton == 20 || lastButton <= 3) {
  278. total = 0;
  279. }
  280. if (editNum) {
  281.  
  282. if (!comma) {
  283. total = total * 10 + 3 * sign;
  284. } else {
  285. decimal++;
  286. total = total + 3 * sign / (Math.pow(10, decimal));
  287. }
  288. } else {
  289. total = 3;
  290. decimal = 0;
  291. sign = 1;
  292. editNum = true;
  293. }
  294. Display(v);
  295. lastButton = 15;
  296.  
  297. }
  298.  
  299. public void set4(View v) {
  300. if (editNum) {
  301.  
  302. if (!comma) {
  303. total = total * 10 + 4 * sign;
  304. } else {
  305. decimal++;
  306. total = total + 4 * sign / (Math.pow(10, decimal));
  307. }
  308. } else {
  309. total = 4;
  310. decimal = 0;
  311. sign = 1;
  312. editNum = true;
  313. }
  314. Display(v);
  315. lastButton = 9;
  316.  
  317. }
  318.  
  319. public void set5(View v) {
  320. if (editNum) {
  321.  
  322. if (!comma) {
  323. total = total * 10 + 5 * sign;
  324. } else {
  325. decimal++;
  326. total = total + 5 * sign / (Math.pow(10, decimal));
  327. }
  328. } else {
  329. total = 5;
  330. decimal = 0;
  331. sign = 1;
  332. editNum = true;
  333. }
  334. Display(v);
  335. lastButton = 10;
  336.  
  337. }
  338.  
  339. public void set6(View v) {
  340. if (editNum) {
  341.  
  342. if (!comma) {
  343. total = total * 10 + 6 * sign;
  344. } else {
  345. decimal++;
  346. total = total + 6 * sign / (Math.pow(10, decimal));
  347. }
  348. } else {
  349. total = 6;
  350. decimal = 0;
  351. sign = 1;
  352. editNum = true;
  353. }
  354. Display(v);
  355. lastButton = 11;
  356.  
  357. }
  358.  
  359. public void set7(View v) {
  360. if (editNum) {
  361.  
  362. if (!comma) {
  363. total = total * 10 + 7 * sign;
  364. } else {
  365. decimal++;
  366. total = total + 7 * sign / (Math.pow(10, decimal));
  367. }
  368. } else {
  369. total = 7;
  370. decimal = 0;
  371. sign = 1;
  372. editNum = true;
  373. }
  374. Display(v);
  375. lastButton = 5;
  376.  
  377. }
  378.  
  379. public void set8(View v) {
  380. if (editNum) {
  381.  
  382. if (!comma) {
  383. total = total * 10 + 8 * sign;
  384. } else {
  385. decimal++;
  386. total = total + 8 * sign / (Math.pow(10, decimal));
  387. }
  388. } else {
  389. total = 8;
  390. decimal = 0;
  391. sign = 1;
  392. editNum = true;
  393. }
  394. Display(v);
  395. lastButton = 6;
  396.  
  397. }
  398.  
  399. public void set9(View v) {
  400. if (editNum) {
  401.  
  402. if (!comma) {
  403. total = total * 10 + 9 * sign;
  404. } else {
  405. decimal++;
  406. total = total + 9 * sign / (Math.pow(10, decimal));
  407. }
  408. } else {
  409. total = 9;
  410. decimal = 0;
  411. sign = 1;
  412. editNum = true;
  413. }
  414. Display(v);
  415. lastButton = 7;
  416.  
  417. }
  418.  
  419.  
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement