Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. package com.example.scientificcalculator;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.ImageButton;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.  
  12. EditText input, sign;
  13. float value1, value2;
  14. String facto;
  15. int i,num;
  16. long factorial;
  17. boolean summation, subtraction, multiplication, divition, hasdot, log, ln, xn, root, not, sin, cos, tan;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. input = findViewById(R.id.input);
  24. sign = findViewById(R.id.sign);
  25. }
  26.  
  27. public void zero(View view) {
  28. input.setText(input.getText()+"0");
  29. }
  30.  
  31. public void one(View view) {
  32. input.setText(input.getText()+"1");
  33. }
  34.  
  35. public void two(View view) {
  36. input.setText(input.getText()+"2");
  37. }
  38.  
  39. public void three(View view) {
  40. input.setText(input.getText()+"3");
  41. }
  42.  
  43. public void four(View view) {
  44. input.setText(input.getText()+"4");
  45. }
  46.  
  47. public void five(View view) {
  48. input.setText(input.getText()+"5");
  49. }
  50.  
  51. public void six(View view) {
  52. input.setText(input.getText()+"6");
  53. }
  54.  
  55. public void seven(View view) {
  56. input.setText(input.getText()+"7");
  57. }
  58.  
  59. public void eight(View view) {
  60. input.setText(input.getText()+"8");
  61. }
  62.  
  63. public void nine(View view) {
  64. input.setText(input.getText()+"9");
  65. }
  66.  
  67. public void dot(View view) {
  68. if(!hasdot){
  69. if(input.getText().equals("")){
  70. input.setText("0.");
  71. }
  72. else {
  73. input.setText(input.getText()+".");
  74. }
  75. hasdot=true;
  76. }
  77. }
  78.  
  79. public void sum(View view) {
  80. try {
  81. value1 = Float.parseFloat(input.getText() + "");
  82. summation = true;
  83. sign.setText("+");
  84. input.setText("");
  85. hasdot = false;
  86. }
  87. catch (Exception e){
  88.  
  89. }
  90. }
  91.  
  92. public void sub(View view) {
  93. try {
  94. value1 = Float.parseFloat(input.getText() + "");
  95. subtraction = true;
  96. sign.setText("-");
  97. input.setText("");
  98. hasdot = false;
  99. }
  100. catch (Exception e){
  101.  
  102. }
  103. }
  104.  
  105. public void multi(View view) {
  106. try {
  107. value1 = Float.parseFloat(input.getText() + "");
  108. multiplication = true;
  109. sign.setText("*");
  110. input.setText("");
  111. hasdot = false;
  112. }
  113. catch (Exception e){
  114.  
  115. }
  116. }
  117.  
  118. public void divi(View view) {
  119. try {
  120. value1 = Float.parseFloat(input.getText() + "");
  121. divition = true;
  122. sign.setText("/");
  123. input.setText("");
  124. hasdot = false;
  125. }
  126. catch (Exception e){
  127.  
  128. }
  129. }
  130.  
  131. public void delete(View view) {
  132. try{
  133. if(input.getText().equals("")){
  134. input.setText(null);
  135. }
  136. else {
  137. int len = input.getText().length();
  138. String s = input.getText().toString();
  139. if(s.charAt(len-1)=='.'){
  140. input.setText(input.getText().subSequence(0,input.getText().length()-1));
  141. hasdot=false;
  142. }
  143. else {
  144. input.setText(input.getText().subSequence(0,input.getText().length()-1));
  145. }
  146. }
  147. }
  148. catch (Exception e){
  149.  
  150. }
  151. }
  152.  
  153. public void clear(View view) {
  154. input.setText("");
  155. sign.setText("");
  156. hasdot=false;
  157. }
  158.  
  159. public void log(View view) {
  160. log = true;
  161. sign.setText("log");
  162. input.setText("");
  163. }
  164.  
  165. public void ln(View view) {
  166. ln = true;
  167. sign.setText("ln");
  168. input.setText("");
  169. }
  170.  
  171. public void xn(View view) {
  172. try {
  173. value1 = Float.parseFloat(input.getText() + "");
  174. xn = true;
  175. sign.setText("^");
  176. input.setText("");
  177. }
  178. catch (Exception e){
  179.  
  180. }
  181. }
  182.  
  183. public void root(View view) {
  184. root = true;
  185. sign.setText("√");
  186. input.setText("");
  187. }
  188.  
  189. public void not(View view) {
  190. try {
  191. value1 = Float.parseFloat(input.getText() + "");
  192. facto = input.getText()+"";
  193. not = true;
  194. sign.setText("!");
  195. input.setText("");
  196. }
  197. catch (Exception e){
  198.  
  199. }
  200. }
  201.  
  202. public void sin(View view) {
  203. sin = true;
  204. sign.setText("sin");
  205. input.setText("");
  206. }
  207.  
  208. public void cos(View view) {
  209. cos = true;
  210. sign.setText("cos");
  211. input.setText("");
  212. }
  213.  
  214. public void tan(View view) {
  215.  
  216. tan = true;
  217. sign.setText("ten");
  218. input.setText("");
  219. }
  220.  
  221. public void equal(View view) {
  222. try{
  223. if (summation == true) {
  224. value2 = Float.parseFloat(input.getText() + "");
  225. input.setText(value1 + value2 + "");
  226. summation = false;
  227. sign.setText(null);
  228. } else if (subtraction == true) {
  229. value2 = Float.parseFloat(input.getText() + "");
  230. input.setText(value1 - value2 + "");
  231. subtraction = false;
  232. sign.setText(null);
  233. } else if (multiplication == true) {
  234. value2 = Float.parseFloat(input.getText() + "");
  235. input.setText(value1 * value2 + "");
  236. multiplication = false;
  237. sign.setText(null);
  238. } else if (divition == true) {
  239. value2 = Float.parseFloat(input.getText() + "");
  240. try {
  241. input.setText(value1 / value2 + "");
  242. divition = false;
  243. sign.setText(null);
  244. } catch (Exception e) {
  245.  
  246. }
  247. } else if (log == true) {
  248. try {
  249. value1 = Float.parseFloat(input.getText() + "");
  250. input.setText(Math.log10(value1) + "");
  251. log = false;
  252. sign.setText(null);
  253. } catch (Exception e) {
  254.  
  255. }
  256. } else if (ln == true) {
  257. try {
  258. value1 = Float.parseFloat(input.getText() + "");
  259. input.setText(Math.log(value1) + "");
  260. ln = false;
  261. sign.setText(null);
  262. } catch (Exception e) {
  263.  
  264. }
  265. } else if (xn == true) {
  266. try {
  267. value2 = Float.parseFloat(input.getText() + "");
  268. input.setText(Math.pow(value1, value2) + "");
  269. xn = false;
  270. sign.setText(null);
  271. } catch (Exception e) {
  272.  
  273. }
  274. } else if (root == true) {
  275. try {
  276. value1 = Float.parseFloat(input.getText() + "");
  277. input.setText(Math.sqrt(value1) + "");
  278. root = false;
  279. sign.setText(null);
  280. } catch (Exception e) {
  281.  
  282. }
  283. } else if (not == true) {
  284. num = Integer.parseInt(facto);
  285. factorial = 1;
  286. for (int i = 1; i <= num; ++i) {
  287. factorial *= i;
  288. }
  289. input.setText(factorial + "");
  290. sign.setText(null);
  291. not = false;
  292. } else if (sin == true) {
  293. try {
  294. value1 = Float.parseFloat(input.getText() + "");
  295. input.setText(Math.sin(value1) + "");
  296. sin = false;
  297. sign.setText(null);
  298. } catch (Exception e) {
  299.  
  300. }
  301. } else if (cos == true) {
  302. try {
  303. value1 = Float.parseFloat(input.getText() + "");
  304. input.setText(Math.cos(value1) + "");
  305. cos = false;
  306. sign.setText(null);
  307. } catch (Exception e) {
  308.  
  309. }
  310. } else if (tan == true) {
  311. try {
  312. value1 = Float.parseFloat(input.getText() + "");
  313. input.setText(Math.tan(value1) + "");
  314. tan = false;
  315. sign.setText(null);
  316. } catch (Exception e) {
  317.  
  318. }
  319. }
  320. }
  321. catch (Exception e){
  322.  
  323. }
  324.  
  325. }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement