当前位置:   article > 正文

【安卓】Android Studio简易计算器(实现加减乘除,整数小数运算,正数负数运算)

【安卓】Android Studio简易计算器(实现加减乘除,整数小数运算,正数负数运算)

目录

前言

运算效果

一、创建一个新的项目

二、编写xml文件(计算器显示页面)

三、实现Java运算逻辑

​编辑

完整代码

xml文件代码:

 Java文件代码:

注:


前言

随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是学习Android Studio开发的一个很好的开始。

Android Studio是一款Google开发的用于创建安卓应用的集成开发环境(IDE), 它可以帮助开发者快速设计、开发和测试应用程序。接下来我将为大家介绍如何使用Android Studio创建一个简易的计算器应用程序。

运算效果

一、创建一个新的项目

完成上面步骤以后,点击Finish,等待加载好项目就可以继续下面的步骤了

二、编写xml文件(计算器显示页面)

打开activity_main.xml文件: res --> layout --> activity_main.xml

可以模仿主流计算器或者自己手机上面的计算器的版式来设计你的计算页面样式。

xml文件代码附到最后啦,根据自己需要自行截取复制。

然后页面显示的样子就是下面这样的:

三、实现Java运算逻辑

写好基本的显示页面后,咱就得来完成逻辑运算和点击事件了

点开MainActivity.java文件:

Java逻辑要完成的主要是如何设计点击事件以及如何实现加减乘除的逻辑

完整代码

xml文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. app:circularflow_angles="20"
  8. tools:context=".MainActivity">
  9. <!--csdn 波士顿o泡果奶 版权所有 -->
  10. <androidx.constraintlayout.widget.ConstraintLayout
  11. android:id="@+id/constraintLayout"
  12. android:layout_width="match_parent"
  13. android:layout_height="80dp"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent">
  16. <ImageView
  17. android:id="@+id/imageView"
  18. android:layout_width="29dp"
  19. android:layout_height="25dp"
  20. android:layout_marginTop="18dp"
  21. android:layout_marginEnd="10dp"
  22. app:layout_constraintEnd_toStartOf="@+id/imageView2"
  23. app:layout_constraintTop_toTopOf="parent"
  24. app:srcCompat="@drawable/img" />
  25. <ImageView
  26. android:id="@+id/imageView2"
  27. android:layout_width="32dp"
  28. android:layout_height="29dp"
  29. android:layout_marginTop="16dp"
  30. android:layout_marginEnd="16dp"
  31. app:layout_constraintEnd_toEndOf="parent"
  32. app:layout_constraintTop_toTopOf="parent"
  33. app:srcCompat="@drawable/img_1" />
  34. <TextView
  35. android:id="@+id/textView"
  36. android:layout_width="65dp"
  37. android:layout_height="41dp"
  38. android:layout_marginStart="16dp"
  39. android:layout_marginTop="7dp"
  40. android:text="计算"
  41. android:textColor="#010101"
  42. android:textSize="31dp"
  43. android:textStyle="bold"
  44. app:autoSizeTextType="none"
  45. app:layout_constraintStart_toStartOf="parent"
  46. app:layout_constraintTop_toTopOf="parent" />
  47. <TextView
  48. android:id="@+id/textView2"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginStart="9dp"
  52. android:layout_marginTop="14dp"
  53. android:text="汇率"
  54. android:textColor="#B2B2B2"
  55. android:textSize="25dp"
  56. android:textStyle="bold"
  57. app:layout_constraintStart_toEndOf="@+id/textView"
  58. app:layout_constraintTop_toTopOf="parent" />
  59. <androidx.constraintlayout.widget.ConstraintLayout
  60. android:layout_width="57dp"
  61. android:layout_height="3dp"
  62. android:layout_marginStart="19dp"
  63. android:background="#FF0101"
  64. app:layout_constraintStart_toStartOf="parent"
  65. app:layout_constraintTop_toBottomOf="@+id/textView">
  66. </androidx.constraintlayout.widget.ConstraintLayout>
  67. </androidx.constraintlayout.widget.ConstraintLayout>
  68. <androidx.constraintlayout.widget.ConstraintLayout
  69. android:id="@+id/constraintLayout2"
  70. android:layout_width="410dp"
  71. android:layout_height="178dp"
  72. android:background="#FFFFFF"
  73. app:layout_constraintStart_toStartOf="parent"
  74. app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
  75. <EditText
  76. android:id="@+id/editTextText2"
  77. android:layout_width="409dp"
  78. android:layout_height="83dp"
  79. android:layout_marginStart="2dp"
  80. android:layout_marginTop="76dp"
  81. android:ems="10"
  82. android:inputType="text"
  83. android:textSize="50sp"
  84. app:layout_constraintStart_toStartOf="parent"
  85. app:layout_constraintTop_toTopOf="parent" />
  86. </androidx.constraintlayout.widget.ConstraintLayout>
  87. <androidx.constraintlayout.widget.ConstraintLayout
  88. android:layout_width="match_parent"
  89. android:layout_height="466dp"
  90. app:layout_constraintStart_toStartOf="parent"
  91. app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">
  92. <androidx.constraintlayout.widget.ConstraintLayout
  93. android:id="@+id/constraintLayout51"
  94. android:layout_width="409dp"
  95. android:layout_height="89.8dp"
  96. android:background="#FFFFFF"
  97. app:layout_constraintStart_toStartOf="parent"
  98. app:layout_constraintTop_toTopOf="parent">
  99. <androidx.cardview.widget.CardView
  100. android:id="@+id/cardView2"
  101. android:layout_width="80dp"
  102. android:layout_height="80dp"
  103. android:layout_marginTop="5dp"
  104. app:cardBackgroundColor="#F2F1F1"
  105. app:cardCornerRadius="45dp"
  106. app:layout_constraintEnd_toStartOf="@+id/cardView"
  107. app:layout_constraintHorizontal_bias="0.5"
  108. app:layout_constraintStart_toStartOf="parent"
  109. app:layout_constraintTop_toTopOf="parent">
  110. <androidx.constraintlayout.widget.ConstraintLayout
  111. android:id="@+id/AC"
  112. android:layout_width="match_parent"
  113. android:layout_height="match_parent">
  114. <TextView
  115. android:id="@+id/textView12"
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content"
  118. android:layout_marginStart="18dp"
  119. android:layout_marginTop="16dp"
  120. android:text="AC"
  121. android:textSize="35sp"
  122. app:layout_constraintStart_toStartOf="parent"
  123. app:layout_constraintTop_toTopOf="parent" />
  124. </androidx.constraintlayout.widget.ConstraintLayout>
  125. </androidx.cardview.widget.CardView>
  126. <androidx.cardview.widget.CardView
  127. android:id="@+id/cardView"
  128. android:layout_width="80dp"
  129. android:layout_height="80dp"
  130. android:layout_marginTop="5dp"
  131. app:cardBackgroundColor="#F2F1F1"
  132. app:cardCornerRadius="45dp"
  133. app:layout_constraintEnd_toStartOf="@+id/cardView3"
  134. app:layout_constraintHorizontal_bias="0.5"
  135. app:layout_constraintStart_toEndOf="@+id/cardView2"
  136. app:layout_constraintTop_toTopOf="parent">
  137. <androidx.constraintlayout.widget.ConstraintLayout
  138. android:id="@+id/dl"
  139. android:layout_width="match_parent"
  140. android:layout_height="match_parent">
  141. <ImageView
  142. android:id="@+id/imageView3"
  143. android:layout_width="48dp"
  144. android:layout_height="30dp"
  145. android:layout_marginStart="14dp"
  146. android:layout_marginTop="28dp"
  147. app:layout_constraintStart_toStartOf="parent"
  148. app:layout_constraintTop_toTopOf="parent"
  149. app:srcCompat="@drawable/img_4" />
  150. </androidx.constraintlayout.widget.ConstraintLayout>
  151. </androidx.cardview.widget.CardView>
  152. <androidx.cardview.widget.CardView
  153. android:id="@+id/cardView3"
  154. android:layout_width="80dp"
  155. android:layout_height="80dp"
  156. android:layout_marginTop="5dp"
  157. android:background="#F2F1F1"
  158. app:cardBackgroundColor="#F2F1F1"
  159. app:cardCornerRadius="45dp"
  160. app:layout_constraintEnd_toStartOf="@+id/cardView4"
  161. app:layout_constraintHorizontal_bias="0.5"
  162. app:layout_constraintStart_toEndOf="@+id/cardView"
  163. app:layout_constraintTop_toTopOf="parent">
  164. <androidx.constraintlayout.widget.ConstraintLayout
  165. android:layout_width="match_parent"
  166. android:layout_height="match_parent">
  167. <TextView
  168. android:id="@+id/textView14"
  169. android:layout_width="wrap_content"
  170. android:layout_height="wrap_content"
  171. android:layout_marginStart="16dp"
  172. android:layout_marginTop="16dp"
  173. android:text="+/-"
  174. android:textSize="35sp"
  175. app:layout_constraintStart_toStartOf="parent"
  176. app:layout_constraintTop_toTopOf="parent" />
  177. </androidx.constraintlayout.widget.ConstraintLayout>
  178. </androidx.cardview.widget.CardView>
  179. <androidx.cardview.widget.CardView
  180. android:id="@+id/cardView4"
  181. android:layout_width="80dp"
  182. android:layout_height="80dp"
  183. android:layout_marginTop="5dp"
  184. android:background="#F2F1F1"
  185. app:cardBackgroundColor="#F6E9E8"
  186. app:cardCornerRadius="45dp"
  187. app:layout_constraintEnd_toEndOf="parent"
  188. app:layout_constraintHorizontal_bias="0.5"
  189. app:layout_constraintStart_toEndOf="@+id/cardView3"
  190. app:layout_constraintTop_toTopOf="parent">
  191. <androidx.constraintlayout.widget.ConstraintLayout
  192. android:id="@+id/D"
  193. android:layout_width="match_parent"
  194. android:layout_height="match_parent">
  195. <TextView
  196. android:id="@+id/textView15"
  197. android:layout_width="wrap_content"
  198. android:layout_height="wrap_content"
  199. android:layout_marginStart="30dp"
  200. android:layout_marginTop="10dp"
  201. android:text="÷ "
  202. android:textSize="42sp"
  203. app:layout_constraintStart_toStartOf="parent"
  204. app:layout_constraintTop_toTopOf="parent" />
  205. </androidx.constraintlayout.widget.ConstraintLayout>
  206. </androidx.cardview.widget.CardView>
  207. </androidx.constraintlayout.widget.ConstraintLayout>
  208. <androidx.constraintlayout.widget.ConstraintLayout
  209. android:id="@+id/constraintLayout54"
  210. android:layout_width="409dp"
  211. android:layout_height="89.8dp"
  212. app:layout_constraintStart_toStartOf="parent"
  213. app:layout_constraintTop_toBottomOf="@+id/constraintLayout51">
  214. <androidx.cardview.widget.CardView
  215. android:id="@+id/cardView2"
  216. android:layout_width="80dp"
  217. android:layout_height="80dp"
  218. android:layout_marginTop="5dp"
  219. app:cardBackgroundColor="#F2F1F1"
  220. app:cardCornerRadius="45dp"
  221. app:layout_constraintEnd_toStartOf="@+id/cardView"
  222. app:layout_constraintHorizontal_bias="0.5"
  223. app:layout_constraintStart_toStartOf="parent"
  224. app:layout_constraintTop_toTopOf="parent">
  225. <androidx.constraintlayout.widget.ConstraintLayout
  226. android:id="@+id/seven"
  227. android:layout_width="match_parent"
  228. android:layout_height="match_parent">
  229. <TextView
  230. android:id="@+id/textView16"
  231. android:layout_width="wrap_content"
  232. android:layout_height="wrap_content"
  233. android:layout_marginStart="30dp"
  234. android:layout_marginTop="17dp"
  235. android:text="7"
  236. android:textSize="35sp"
  237. app:layout_constraintStart_toStartOf="parent"
  238. app:layout_constraintTop_toTopOf="parent" />
  239. </androidx.constraintlayout.widget.ConstraintLayout>
  240. </androidx.cardview.widget.CardView>
  241. <androidx.constraintlayout.widget.ConstraintLayout
  242. android:layout_width="match_parent"
  243. android:layout_height="match_parent" />
  244. <androidx.cardview.widget.CardView
  245. android:id="@+id/cardView"
  246. android:layout_width="80dp"
  247. android:layout_height="80dp"
  248. android:layout_marginTop="5dp"
  249. app:cardBackgroundColor="#F2F1F1"
  250. app:cardCornerRadius="45dp"
  251. app:layout_constraintEnd_toStartOf="@+id/cardView3"
  252. app:layout_constraintHorizontal_bias="0.5"
  253. app:layout_constraintStart_toEndOf="@+id/cardView2"
  254. app:layout_constraintTop_toTopOf="parent">
  255. <androidx.constraintlayout.widget.ConstraintLayout
  256. android:id="@+id/eight"
  257. android:layout_width="match_parent"
  258. android:layout_height="match_parent">
  259. <TextView
  260. android:id="@+id/textView17"
  261. android:layout_width="wrap_content"
  262. android:layout_height="wrap_content"
  263. android:layout_marginStart="30dp"
  264. android:layout_marginTop="17dp"
  265. android:text="8"
  266. android:textSize="35sp"
  267. app:layout_constraintStart_toStartOf="parent"
  268. app:layout_constraintTop_toTopOf="parent" />
  269. </androidx.constraintlayout.widget.ConstraintLayout>
  270. </androidx.cardview.widget.CardView>
  271. <androidx.constraintlayout.widget.ConstraintLayout
  272. android:layout_width="match_parent"
  273. android:layout_height="match_parent" />
  274. <androidx.cardview.widget.CardView
  275. android:id="@+id/cardView3"
  276. android:layout_width="80dp"
  277. android:layout_height="80dp"
  278. android:layout_marginTop="5dp"
  279. android:background="#F2F1F1"
  280. app:cardBackgroundColor="#F2F1F1"
  281. app:cardCornerRadius="45dp"
  282. app:layout_constraintEnd_toStartOf="@+id/cardView4"
  283. app:layout_constraintHorizontal_bias="0.5"
  284. app:layout_constraintStart_toEndOf="@+id/cardView"
  285. app:layout_constraintTop_toTopOf="parent">
  286. <androidx.constraintlayout.widget.ConstraintLayout
  287. android:id="@+id/nine"
  288. android:layout_width="match_parent"
  289. android:layout_height="match_parent">
  290. <TextView
  291. android:id="@+id/textView18"
  292. android:layout_width="wrap_content"
  293. android:layout_height="wrap_content"
  294. android:layout_marginStart="30dp"
  295. android:layout_marginTop="17dp"
  296. android:text="9"
  297. android:textSize="35sp"
  298. app:layout_constraintStart_toStartOf="parent"
  299. app:layout_constraintTop_toTopOf="parent" />
  300. </androidx.constraintlayout.widget.ConstraintLayout>
  301. </androidx.cardview.widget.CardView>
  302. <androidx.constraintlayout.widget.ConstraintLayout
  303. android:layout_width="match_parent"
  304. android:layout_height="match_parent" />
  305. <androidx.cardview.widget.CardView
  306. android:id="@+id/cardView4"
  307. android:layout_width="80dp"
  308. android:layout_height="80dp"
  309. android:layout_marginTop="5dp"
  310. android:background="#F2F1F1"
  311. app:cardBackgroundColor="#F6E9E8"
  312. app:cardCornerRadius="45dp"
  313. app:layout_constraintEnd_toEndOf="parent"
  314. app:layout_constraintHorizontal_bias="0.5"
  315. app:layout_constraintStart_toEndOf="@+id/cardView3"
  316. app:layout_constraintTop_toTopOf="parent">
  317. <androidx.constraintlayout.widget.ConstraintLayout
  318. android:id="@+id/X"
  319. android:layout_width="match_parent"
  320. android:layout_height="match_parent">
  321. <TextView
  322. android:id="@+id/textView19"
  323. android:layout_width="wrap_content"
  324. android:layout_height="wrap_content"
  325. android:layout_marginStart="31dp"
  326. android:layout_marginTop="14dp"
  327. android:text="x"
  328. android:textSize="35sp"
  329. app:layout_constraintStart_toStartOf="parent"
  330. app:layout_constraintTop_toTopOf="parent" />
  331. </androidx.constraintlayout.widget.ConstraintLayout>
  332. </androidx.cardview.widget.CardView>
  333. <androidx.constraintlayout.widget.ConstraintLayout
  334. android:layout_width="match_parent"
  335. android:layout_height="match_parent" />
  336. </androidx.constraintlayout.widget.ConstraintLayout>
  337. <androidx.constraintlayout.widget.ConstraintLayout
  338. android:id="@+id/constraintLayout52"
  339. android:layout_width="409dp"
  340. android:layout_height="89.8dp"
  341. app:layout_constraintStart_toStartOf="parent"
  342. app:layout_constraintTop_toBottomOf="@+id/constraintLayout54">
  343. <androidx.cardview.widget.CardView
  344. android:id="@+id/cardView2"
  345. android:layout_width="80dp"
  346. android:layout_height="80dp"
  347. android:layout_marginTop="5dp"
  348. app:cardBackgroundColor="#F2F1F1"
  349. app:cardCornerRadius="45dp"
  350. app:layout_constraintEnd_toStartOf="@+id/cardView"
  351. app:layout_constraintHorizontal_bias="0.5"
  352. app:layout_constraintStart_toStartOf="parent"
  353. app:layout_constraintTop_toTopOf="parent">
  354. <androidx.constraintlayout.widget.ConstraintLayout
  355. android:id="@+id/four"
  356. android:layout_width="match_parent"
  357. android:layout_height="match_parent">
  358. <TextView
  359. android:id="@+id/textView20"
  360. android:layout_width="wrap_content"
  361. android:layout_height="wrap_content"
  362. android:layout_marginStart="30dp"
  363. android:layout_marginTop="16dp"
  364. android:text="4"
  365. android:textSize="35sp"
  366. app:layout_constraintStart_toStartOf="parent"
  367. app:layout_constraintTop_toTopOf="parent" />
  368. </androidx.constraintlayout.widget.ConstraintLayout>
  369. </androidx.cardview.widget.CardView>
  370. <androidx.constraintlayout.widget.ConstraintLayout
  371. android:layout_width="match_parent"
  372. android:layout_height="match_parent" />
  373. <androidx.cardview.widget.CardView
  374. android:id="@+id/cardView"
  375. android:layout_width="80dp"
  376. android:layout_height="80dp"
  377. android:layout_marginTop="5dp"
  378. app:cardBackgroundColor="#F2F1F1"
  379. app:cardCornerRadius="45dp"
  380. app:layout_constraintEnd_toStartOf="@+id/cardView3"
  381. app:layout_constraintHorizontal_bias="0.5"
  382. app:layout_constraintStart_toEndOf="@+id/cardView2"
  383. app:layout_constraintTop_toTopOf="parent">
  384. <androidx.constraintlayout.widget.ConstraintLayout
  385. android:id="@+id/five"
  386. android:layout_width="match_parent"
  387. android:layout_height="match_parent">
  388. <TextView
  389. android:id="@+id/textView21"
  390. android:layout_width="wrap_content"
  391. android:layout_height="wrap_content"
  392. android:layout_marginStart="30dp"
  393. android:layout_marginTop="16dp"
  394. android:text="5"
  395. android:textSize="35sp"
  396. app:layout_constraintStart_toStartOf="parent"
  397. app:layout_constraintTop_toTopOf="parent" />
  398. </androidx.constraintlayout.widget.ConstraintLayout>
  399. </androidx.cardview.widget.CardView>
  400. <androidx.constraintlayout.widget.ConstraintLayout
  401. android:layout_width="match_parent"
  402. android:layout_height="match_parent" />
  403. <androidx.cardview.widget.CardView
  404. android:id="@+id/cardView3"
  405. android:layout_width="80dp"
  406. android:layout_height="80dp"
  407. android:layout_marginTop="5dp"
  408. android:background="#F2F1F1"
  409. app:cardBackgroundColor="#F2F1F1"
  410. app:cardCornerRadius="45dp"
  411. app:layout_constraintEnd_toStartOf="@+id/cardView4"
  412. app:layout_constraintHorizontal_bias="0.5"
  413. app:layout_constraintStart_toEndOf="@+id/cardView"
  414. app:layout_constraintTop_toTopOf="parent">
  415. <androidx.constraintlayout.widget.ConstraintLayout
  416. android:id="@+id/six"
  417. android:layout_width="match_parent"
  418. android:layout_height="match_parent">
  419. <TextView
  420. android:id="@+id/textView22"
  421. android:layout_width="wrap_content"
  422. android:layout_height="wrap_content"
  423. android:layout_marginStart="30dp"
  424. android:layout_marginTop="16dp"
  425. android:text="6"
  426. android:textSize="35sp"
  427. app:layout_constraintStart_toStartOf="parent"
  428. app:layout_constraintTop_toTopOf="parent" />
  429. </androidx.constraintlayout.widget.ConstraintLayout>
  430. </androidx.cardview.widget.CardView>
  431. <androidx.constraintlayout.widget.ConstraintLayout
  432. android:layout_width="match_parent"
  433. android:layout_height="match_parent" />
  434. <androidx.cardview.widget.CardView
  435. android:id="@+id/cardView4"
  436. android:layout_width="80dp"
  437. android:layout_height="80dp"
  438. android:layout_marginTop="5dp"
  439. android:background="#F6E9E8"
  440. app:cardBackgroundColor="#F6E9E8"
  441. app:cardCornerRadius="45dp"
  442. app:layout_constraintEnd_toEndOf="parent"
  443. app:layout_constraintHorizontal_bias="0.5"
  444. app:layout_constraintStart_toEndOf="@+id/cardView3"
  445. app:layout_constraintTop_toTopOf="parent">
  446. <androidx.constraintlayout.widget.ConstraintLayout
  447. android:id="@+id/sub"
  448. android:layout_width="match_parent"
  449. android:layout_height="match_parent">
  450. <TextView
  451. android:id="@+id/textView23"
  452. android:layout_width="wrap_content"
  453. android:layout_height="wrap_content"
  454. android:layout_marginStart="35dp"
  455. android:layout_marginTop="16dp"
  456. android:text="-"
  457. android:textSize="35sp"
  458. app:layout_constraintStart_toStartOf="parent"
  459. app:layout_constraintTop_toTopOf="parent" />
  460. </androidx.constraintlayout.widget.ConstraintLayout>
  461. </androidx.cardview.widget.CardView>
  462. <androidx.constraintlayout.widget.ConstraintLayout
  463. android:layout_width="match_parent"
  464. android:layout_height="match_parent" />
  465. </androidx.constraintlayout.widget.ConstraintLayout>
  466. <androidx.constraintlayout.widget.ConstraintLayout
  467. android:id="@+id/constraintLayout53"
  468. android:layout_width="409dp"
  469. android:layout_height="89.8dp"
  470. app:layout_constraintStart_toStartOf="parent"
  471. app:layout_constraintTop_toBottomOf="@+id/constraintLayout52">
  472. <androidx.cardview.widget.CardView
  473. android:id="@+id/cardView2"
  474. android:layout_width="80dp"
  475. android:layout_height="80dp"
  476. android:layout_marginTop="5dp"
  477. app:cardBackgroundColor="#F2F1F1"
  478. app:cardCornerRadius="45dp"
  479. app:layout_constraintEnd_toStartOf="@+id/cardView"
  480. app:layout_constraintHorizontal_bias="0.5"
  481. app:layout_constraintStart_toStartOf="parent"
  482. app:layout_constraintTop_toTopOf="parent">
  483. <androidx.constraintlayout.widget.ConstraintLayout
  484. android:id="@+id/one"
  485. android:layout_width="match_parent"
  486. android:layout_height="match_parent">
  487. <TextView
  488. android:id="@+id/textView24"
  489. android:layout_width="wrap_content"
  490. android:layout_height="wrap_content"
  491. android:layout_marginStart="30dp"
  492. android:layout_marginTop="16dp"
  493. android:text="1"
  494. android:textSize="35sp"
  495. app:layout_constraintStart_toStartOf="parent"
  496. app:layout_constraintTop_toTopOf="parent" />
  497. </androidx.constraintlayout.widget.ConstraintLayout>
  498. </androidx.cardview.widget.CardView>
  499. <androidx.constraintlayout.widget.ConstraintLayout
  500. android:layout_width="match_parent"
  501. android:layout_height="match_parent" />
  502. <androidx.cardview.widget.CardView
  503. android:id="@+id/cardView"
  504. android:layout_width="80dp"
  505. android:layout_height="80dp"
  506. android:layout_marginTop="5dp"
  507. app:cardBackgroundColor="#F2F1F1"
  508. app:cardCornerRadius="45dp"
  509. app:layout_constraintEnd_toStartOf="@+id/cardView3"
  510. app:layout_constraintHorizontal_bias="0.5"
  511. app:layout_constraintStart_toEndOf="@+id/cardView2"
  512. app:layout_constraintTop_toTopOf="parent">
  513. <androidx.constraintlayout.widget.ConstraintLayout
  514. android:id="@+id/two"
  515. android:layout_width="match_parent"
  516. android:layout_height="match_parent">
  517. <TextView
  518. android:id="@+id/textView25"
  519. android:layout_width="wrap_content"
  520. android:layout_height="wrap_content"
  521. android:layout_marginStart="30dp"
  522. android:layout_marginTop="16dp"
  523. android:text="2"
  524. android:textSize="35sp"
  525. app:layout_constraintStart_toStartOf="parent"
  526. app:layout_constraintTop_toTopOf="parent" />
  527. </androidx.constraintlayout.widget.ConstraintLayout>
  528. </androidx.cardview.widget.CardView>
  529. <androidx.constraintlayout.widget.ConstraintLayout
  530. android:layout_width="match_parent"
  531. android:layout_height="match_parent" />
  532. <androidx.cardview.widget.CardView
  533. android:id="@+id/cardView3"
  534. android:layout_width="80dp"
  535. android:layout_height="80dp"
  536. android:layout_marginTop="5dp"
  537. android:background="#F2F1F1"
  538. app:cardBackgroundColor="#F2F1F1"
  539. app:cardCornerRadius="45dp"
  540. app:layout_constraintEnd_toStartOf="@+id/cardView4"
  541. app:layout_constraintHorizontal_bias="0.5"
  542. app:layout_constraintStart_toEndOf="@+id/cardView"
  543. app:layout_constraintTop_toTopOf="parent">
  544. <androidx.constraintlayout.widget.ConstraintLayout
  545. android:id="@+id/three"
  546. android:layout_width="match_parent"
  547. android:layout_height="match_parent">
  548. <TextView
  549. android:id="@+id/textView26"
  550. android:layout_width="wrap_content"
  551. android:layout_height="wrap_content"
  552. android:layout_marginStart="30dp"
  553. android:layout_marginTop="16dp"
  554. android:text="3"
  555. android:textSize="35sp"
  556. app:layout_constraintStart_toStartOf="parent"
  557. app:layout_constraintTop_toTopOf="parent" />
  558. </androidx.constraintlayout.widget.ConstraintLayout>
  559. </androidx.cardview.widget.CardView>
  560. <androidx.constraintlayout.widget.ConstraintLayout
  561. android:layout_width="match_parent"
  562. android:layout_height="match_parent" />
  563. <androidx.cardview.widget.CardView
  564. android:id="@+id/cardView4"
  565. android:layout_width="80dp"
  566. android:layout_height="80dp"
  567. android:layout_marginTop="5dp"
  568. android:background="#F2F1F1"
  569. app:cardBackgroundColor="#F6E9E8"
  570. app:cardCornerRadius="45dp"
  571. app:layout_constraintEnd_toEndOf="parent"
  572. app:layout_constraintHorizontal_bias="0.5"
  573. app:layout_constraintStart_toEndOf="@+id/cardView3"
  574. app:layout_constraintTop_toTopOf="parent">
  575. <androidx.constraintlayout.widget.ConstraintLayout
  576. android:id="@+id/add"
  577. android:layout_width="match_parent"
  578. android:layout_height="match_parent">
  579. <TextView
  580. android:id="@+id/textView27"
  581. android:layout_width="wrap_content"
  582. android:layout_height="wrap_content"
  583. android:layout_marginStart="30dp"
  584. android:layout_marginTop="16dp"
  585. android:text="+"
  586. android:textSize="35sp"
  587. app:layout_constraintStart_toStartOf="parent"
  588. app:layout_constraintTop_toTopOf="parent" />
  589. </androidx.constraintlayout.widget.ConstraintLayout>
  590. </androidx.cardview.widget.CardView>
  591. <androidx.constraintlayout.widget.ConstraintLayout
  592. android:layout_width="match_parent"
  593. android:layout_height="match_parent" />
  594. </androidx.constraintlayout.widget.ConstraintLayout>
  595. <androidx.constraintlayout.widget.ConstraintLayout
  596. android:layout_width="409dp"
  597. android:layout_height="89.8dp"
  598. app:layout_constraintStart_toStartOf="parent"
  599. app:layout_constraintTop_toBottomOf="@+id/constraintLayout53">
  600. <androidx.cardview.widget.CardView
  601. android:id="@+id/cardView2"
  602. android:layout_width="80dp"
  603. android:layout_height="80dp"
  604. android:layout_marginTop="5dp"
  605. app:cardBackgroundColor="#F2F1F1"
  606. app:cardCornerRadius="45dp"
  607. app:layout_constraintEnd_toStartOf="@+id/cardView"
  608. app:layout_constraintHorizontal_bias="0.5"
  609. app:layout_constraintStart_toStartOf="parent"
  610. app:layout_constraintTop_toTopOf="parent">
  611. <androidx.constraintlayout.widget.ConstraintLayout
  612. android:id="@+id/yu"
  613. android:layout_width="match_parent"
  614. android:layout_height="match_parent">
  615. <TextView
  616. android:id="@+id/textView28"
  617. android:layout_width="wrap_content"
  618. android:layout_height="wrap_content"
  619. android:layout_marginStart="27dp"
  620. android:layout_marginTop="17dp"
  621. android:text="%"
  622. android:textSize="35sp"
  623. app:layout_constraintStart_toStartOf="parent"
  624. app:layout_constraintTop_toTopOf="parent" />
  625. </androidx.constraintlayout.widget.ConstraintLayout>
  626. </androidx.cardview.widget.CardView>
  627. <androidx.constraintlayout.widget.ConstraintLayout
  628. android:layout_width="match_parent"
  629. android:layout_height="match_parent" />
  630. <androidx.cardview.widget.CardView
  631. android:id="@+id/cardView"
  632. android:layout_width="80dp"
  633. android:layout_height="80dp"
  634. android:layout_marginTop="5dp"
  635. app:cardBackgroundColor="#F2F1F1"
  636. app:cardCornerRadius="45dp"
  637. app:layout_constraintEnd_toStartOf="@+id/cardView3"
  638. app:layout_constraintHorizontal_bias="0.5"
  639. app:layout_constraintStart_toEndOf="@+id/cardView2"
  640. app:layout_constraintTop_toTopOf="parent">
  641. <androidx.constraintlayout.widget.ConstraintLayout
  642. android:id="@+id/zero"
  643. android:layout_width="match_parent"
  644. android:layout_height="match_parent">
  645. <TextView
  646. android:id="@+id/textView29"
  647. android:layout_width="wrap_content"
  648. android:layout_height="wrap_content"
  649. android:layout_marginStart="30dp"
  650. android:layout_marginTop="17dp"
  651. android:text="0"
  652. android:textSize="35sp"
  653. app:layout_constraintStart_toStartOf="parent"
  654. app:layout_constraintTop_toTopOf="parent" />
  655. </androidx.constraintlayout.widget.ConstraintLayout>
  656. </androidx.cardview.widget.CardView>
  657. <androidx.constraintlayout.widget.ConstraintLayout
  658. android:layout_width="match_parent"
  659. android:layout_height="match_parent" />
  660. <androidx.cardview.widget.CardView
  661. android:id="@+id/cardView3"
  662. android:layout_width="80dp"
  663. android:layout_height="80dp"
  664. android:layout_marginTop="5dp"
  665. android:background="#F2F1F1"
  666. app:cardBackgroundColor="#F2F1F1"
  667. app:cardCornerRadius="45dp"
  668. app:layout_constraintEnd_toStartOf="@+id/cardView4"
  669. app:layout_constraintHorizontal_bias="0.5"
  670. app:layout_constraintStart_toEndOf="@+id/cardView"
  671. app:layout_constraintTop_toTopOf="parent">
  672. <androidx.constraintlayout.widget.ConstraintLayout
  673. android:id="@+id/point"
  674. android:layout_width="match_parent"
  675. android:layout_height="match_parent">
  676. <TextView
  677. android:id="@+id/textView30"
  678. android:layout_width="wrap_content"
  679. android:layout_height="wrap_content"
  680. android:layout_marginStart="36dp"
  681. android:layout_marginTop="17dp"
  682. android:text="."
  683. android:textSize="35sp"
  684. app:layout_constraintStart_toStartOf="parent"
  685. app:layout_constraintTop_toTopOf="parent" />
  686. </androidx.constraintlayout.widget.ConstraintLayout>
  687. </androidx.cardview.widget.CardView>
  688. <androidx.constraintlayout.widget.ConstraintLayout
  689. android:layout_width="match_parent"
  690. android:layout_height="match_parent" />
  691. <androidx.cardview.widget.CardView
  692. android:id="@+id/cardView4"
  693. android:layout_width="80dp"
  694. android:layout_height="80dp"
  695. android:layout_marginTop="5dp"
  696. android:background="#F2F1F1"
  697. app:cardBackgroundColor="#F85955"
  698. app:cardCornerRadius="45dp"
  699. app:layout_constraintEnd_toEndOf="parent"
  700. app:layout_constraintHorizontal_bias="0.5"
  701. app:layout_constraintStart_toEndOf="@+id/cardView3"
  702. app:layout_constraintTop_toTopOf="parent">
  703. <androidx.constraintlayout.widget.ConstraintLayout
  704. android:id="@+id/equal"
  705. android:layout_width="match_parent"
  706. android:layout_height="match_parent">
  707. <TextView
  708. android:id="@+id/textView32"
  709. android:layout_width="wrap_content"
  710. android:layout_height="wrap_content"
  711. android:layout_marginStart="31dp"
  712. android:layout_marginTop="16dp"
  713. android:text="="
  714. android:textSize="35sp"
  715. app:layout_constraintStart_toStartOf="parent"
  716. app:layout_constraintTop_toTopOf="parent" />
  717. </androidx.constraintlayout.widget.ConstraintLayout>
  718. </androidx.cardview.widget.CardView>
  719. <androidx.constraintlayout.widget.ConstraintLayout
  720. android:layout_width="match_parent"
  721. android:layout_height="match_parent" />
  722. </androidx.constraintlayout.widget.ConstraintLayout>
  723. </androidx.constraintlayout.widget.ConstraintLayout>
  724. </androidx.constraintlayout.widget.ConstraintLayout>

 Java文件代码:

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import androidx.constraintlayout.widget.ConstraintLayout;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import java.math.BigDecimal;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class MainActivity extends AppCompatActivity {
  10. private StringBuilder show=new StringBuilder();
  11. private ArrayList calculate_equation;
  12. private int signal=0;//为0 时表示刚输入状态;为1时表示当前在输出结果上继续输入
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. show=new StringBuilder();
  18. calculate_equation=new ArrayList<>();
  19. //李木
  20. ConstraintLayout AC = findViewById(R.id.AC);
  21. ConstraintLayout Divide = findViewById(R.id.D);
  22. ConstraintLayout Multiplication = findViewById(R.id.X);
  23. ConstraintLayout Sub = findViewById(R.id.sub);
  24. ConstraintLayout Add = findViewById(R.id.add);
  25. ConstraintLayout Equal = findViewById(R.id.equal);
  26. ConstraintLayout Point = findViewById(R.id.point);
  27. ConstraintLayout one = findViewById(R.id.one);
  28. ConstraintLayout two = findViewById(R.id.two);
  29. ConstraintLayout three = findViewById(R.id.three);
  30. ConstraintLayout four = findViewById(R.id.four);
  31. ConstraintLayout five = findViewById(R.id.five);
  32. ConstraintLayout six = findViewById(R.id.six);
  33. ConstraintLayout seven = findViewById(R.id.seven);
  34. ConstraintLayout eight = findViewById(R.id.eight);
  35. ConstraintLayout nine = findViewById(R.id.nine);
  36. ConstraintLayout zero = findViewById(R.id.zero);
  37. EditText result=findViewById(R.id.editTextText2);
  38. AC.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. show.delete(0,show.length());
  42. show.append("");
  43. result.setText(show);
  44. result.setSelection(result.getText().length());
  45. signal=0;
  46. }
  47. });
  48. zero.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v){
  51. if(!(show.toString().equals("0"))){
  52. if(signal==0){
  53. show.append("0");
  54. result.setText(show);
  55. result.setSelection(result.getText().length());
  56. }else{
  57. show.delete(0,show.length());
  58. show.append("0");
  59. result.setText(show);
  60. result.setSelection(result.getText().length());
  61. signal=0;
  62. }
  63. }
  64. }
  65. });
  66. one.setOnClickListener(new View.OnClickListener() {
  67. @Override
  68. public void onClick(View v) {
  69. if(signal==0){
  70. show.append("1");
  71. result.setText(show);
  72. result.setSelection(result.getText().length());
  73. }else{
  74. show.delete(0,show.length());
  75. show.append("1");
  76. result.setText(show);
  77. result.setSelection(result.getText().length());
  78. signal=0;
  79. }
  80. }
  81. });
  82. two.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. if(signal==0){
  86. show.append("2");
  87. result.setText(show);
  88. result.setSelection(result.getText().length());
  89. }else{
  90. show.delete(0,show.length());
  91. show.append("2");
  92. result.setText(show);
  93. result.setSelection(result.getText().length());
  94. signal=0;
  95. }
  96. }
  97. });
  98. three.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. if(signal==0){
  102. show.append("3");
  103. result.setText(show);
  104. result.setSelection(result.getText().length());
  105. }else{
  106. show.delete(0,show.length());
  107. show.append("3");
  108. result.setText(show);
  109. result.setSelection(result.getText().length());
  110. signal=0;
  111. }
  112. }
  113. });
  114. four.setOnClickListener(new View.OnClickListener() {
  115. @Override
  116. public void onClick(View v) {
  117. if(signal==0){
  118. show.append("4");
  119. result.setText(show);
  120. result.setSelection(result.getText().length());
  121. }else{
  122. show.delete(0,show.length());
  123. show.append("4");
  124. result.setText(show);
  125. result.setSelection(result.getText().length());
  126. signal=0;
  127. }
  128. }
  129. });
  130. five.setOnClickListener(new View.OnClickListener() {
  131. @Override
  132. public void onClick(View v) {
  133. if(signal==0){
  134. show.append("5");
  135. result.setText(show);
  136. result.setSelection(result.getText().length());
  137. }else{
  138. show.delete(0,show.length());
  139. show.append("5");
  140. result.setText(show);
  141. result.setSelection(result.getText().length());
  142. signal=0;
  143. }
  144. }
  145. });
  146. six.setOnClickListener(new View.OnClickListener() {
  147. @Override
  148. public void onClick(View v) {
  149. if(signal==0){
  150. show.append("6");
  151. result.setText(show);
  152. result.setSelection(result.getText().length());
  153. }else{
  154. show.delete(0,show.length());
  155. show.append("6");
  156. result.setText(show);
  157. result.setSelection(result.getText().length());
  158. signal=0;
  159. }
  160. }
  161. });
  162. seven.setOnClickListener(new View.OnClickListener() {
  163. @Override
  164. public void onClick(View v) {
  165. if(signal==0){
  166. show.append("7");
  167. result.setText(show);
  168. result.setSelection(result.getText().length());
  169. }else{
  170. show.delete(0,show.length());
  171. show.append("7");
  172. result.setText(show);
  173. result.setSelection(result.getText().length());
  174. signal=0;
  175. }
  176. }
  177. });
  178. eight.setOnClickListener(new View.OnClickListener() {
  179. @Override
  180. public void onClick(View v) {
  181. if(signal==0){
  182. show.append("8");
  183. result.setText(show);
  184. result.setSelection(result.getText().length());
  185. }else{
  186. show.delete(0,show.length());
  187. show.append("8");
  188. result.setText(show);
  189. result.setSelection(result.getText().length());
  190. signal=0;
  191. }
  192. }
  193. });
  194. nine.setOnClickListener(new View.OnClickListener() {
  195. @Override
  196. public void onClick(View v) {
  197. if(signal==0){
  198. show.append("9");
  199. result.setText(show);
  200. result.setSelection(result.getText().length());
  201. }else{
  202. show.delete(0,show.length());
  203. show.append("9");
  204. result.setText(show);
  205. result.setSelection(result.getText().length());
  206. signal=0;
  207. }
  208. }
  209. });
  210. Point.setOnClickListener(new View.OnClickListener() {
  211. @Override
  212. public void onClick(View v) {
  213. if(signal==0){
  214. String a=show.toString();
  215. if(a.equals("")){
  216. show.append(".");
  217. result.setText(show);
  218. result.setSelection(result.getText().length());
  219. }else{
  220. int i;
  221. char t='0';
  222. for(i=a.length();i>0;i--){
  223. t=a.charAt(i-1);
  224. if(t=='.'||t=='+'||t=='-'||t=='*'||t=='/')
  225. break;
  226. }
  227. if(i==0){
  228. show.append(".");
  229. result.setText(show);
  230. result.setSelection(result.getText().length());
  231. }else if(t=='+'||t=='-'||t=='*'||t=='/'){
  232. show.append(".");
  233. result.setText(show);
  234. result.setSelection(result.getText().length());
  235. }
  236. }
  237. }else{
  238. show.delete(0,show.length());
  239. show.append(".");
  240. result.setText(".");
  241. result.setSelection(result.getText().length());
  242. signal=0;
  243. }
  244. }
  245. });
  246. Equal.setOnClickListener(new View.OnClickListener() {
  247. @Override
  248. public void onClick(View v) {
  249. //判断用户是否输入了内容
  250. if(!show.toString().equals("")){
  251. signal=1;
  252. char temp=show.charAt(show.length()-1);
  253. if(show.charAt(0)=='-')
  254. show.insert(0,"0");
  255. if(temp=='+'||temp=='-')
  256. show.append("0");
  257. if(temp=='*'||temp=='/')
  258. show.append("1");
  259. StringBuilder temp1=new StringBuilder();
  260. for(int i=0;i<show.length();i++){
  261. if(show.charAt(i)>='0'&&show.charAt(i)<='9'||show.charAt(i)=='.'){
  262. temp1.append(String.valueOf(show.charAt(i)));
  263. }else if(show.charAt(i)=='N'){
  264. calculate_equation.add("NaN");
  265. i=i+2;
  266. }else if(show.charAt(i)=='∞'){
  267. calculate_equation.add("∞");
  268. }
  269. else
  270. {
  271. if(temp1.length()!=0){
  272. calculate_equation.add(temp1.toString());
  273. temp1.delete(0,temp1.length());
  274. }
  275. calculate_equation.add(String.valueOf(show.charAt(i)));
  276. }
  277. }
  278. if(temp1.length()!=0){
  279. calculate_equation.add(temp1.toString());
  280. }
  281. calculate_equation.add("#");
  282. String temp8=calculate(calculate_equation);
  283. result.setText(temp8);
  284. result.setSelection(result.getText().length());
  285. show.delete(0,show.length());
  286. calculate_equation.clear();
  287. show.append(temp8);
  288. }
  289. }
  290. });
  291. Add.setOnClickListener(new View.OnClickListener() {
  292. @Override
  293. public void onClick(View v) {
  294. //判断用户是否输入了内容
  295. if(!(show.toString().equals(""))) {
  296. signal=0;
  297. char temp=show.charAt(show.length()-1);
  298. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  299. {
  300. show.deleteCharAt(show.length()-1);
  301. show.append("+");
  302. }
  303. else
  304. show.append("+");
  305. result.setText(show);
  306. result.setSelection(result.getText().length());
  307. }
  308. }
  309. });
  310. Sub.setOnClickListener(new View.OnClickListener() {
  311. @Override
  312. public void onClick(View v) {
  313. //判断用户是否输入了内容
  314. if(!(show.toString().equals(""))) {
  315. signal=0;
  316. char temp=show.charAt(show.length()-1);
  317. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  318. {
  319. show.deleteCharAt(show.length()-1);
  320. show.append("-");
  321. }
  322. else
  323. show.append("-");
  324. result.setText(show);
  325. result.setSelection(result.getText().length());
  326. }
  327. }
  328. });
  329. Multiplication.setOnClickListener(new View.OnClickListener() {
  330. @Override
  331. public void onClick(View v) {
  332. //判断用户是否输入了内容
  333. if(!(show.toString().equals(""))) {
  334. signal=0;
  335. char temp=show.charAt(show.length()-1);
  336. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  337. {
  338. show.deleteCharAt(show.length()-1);
  339. show.append("*");
  340. }
  341. else
  342. show.append("*");
  343. result.setText(show);
  344. result.setSelection(result.getText().length());
  345. }
  346. }
  347. });
  348. Divide.setOnClickListener(new View.OnClickListener() {
  349. @Override
  350. public void onClick(View v) {
  351. //判断用户是否输入了内容
  352. if(!(show.toString().equals(""))) {
  353. signal=0;
  354. char temp=show.charAt(show.length()-1);
  355. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  356. {
  357. show.deleteCharAt(show.length()-1);
  358. show.append("/");
  359. }
  360. else
  361. show.append("/");
  362. result.setText(show);
  363. result.setSelection(result.getText().length());
  364. }
  365. }
  366. });
  367. }
  368. protected boolean operatorPriorityCompare(char operator1,char operator2)
  369. {
  370. int o1=0;
  371. int o2=0;
  372. switch (operator1){
  373. case '+':{o1=0;break;}
  374. case '-':{o1=0;break;}
  375. case '*':{o1=1;break;}
  376. case '/':{o1=1;break;}
  377. }
  378. switch (operator2){
  379. case '+':{o2=0;break;}
  380. case '-':{o2=0;break;}
  381. case '*':{o2=1;break;}
  382. case '/':{o2=1;break;}
  383. }
  384. if(o1<=o2)
  385. {
  386. return false;
  387. }
  388. else
  389. return true;
  390. }
  391. //相加
  392. public static Double Add(Double d1,Double d2) {
  393. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  394. return d1+d2;
  395. }
  396. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  397. return d1+d2;
  398. }
  399. BigDecimal b1 = new BigDecimal(Double.toString(d1));
  400. BigDecimal b2 = new BigDecimal(Double.toString(d2));
  401. return b1.add(b2).doubleValue();
  402. }
  403. //相减
  404. public static Double Sub(Double d1,Double d2){
  405. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  406. return d1-d2;
  407. }
  408. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  409. return d1-d2;
  410. }
  411. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  412. return d1*d2;
  413. }
  414. BigDecimal b1=new BigDecimal(Double.toString(d1));
  415. BigDecimal b2=new BigDecimal(Double.toString(d2));
  416. return b1.subtract(b2).doubleValue();
  417. }
  418. //相乘
  419. public static Double Mul(Double d1,Double d2){
  420. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  421. return d1*d2;
  422. }
  423. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  424. return d1*d2;
  425. }
  426. BigDecimal b1=new BigDecimal(Double.toString(d1));
  427. BigDecimal b2=new BigDecimal(Double.toString(d2));
  428. return b1.multiply(b2).setScale(8).doubleValue();
  429. }
  430. //相除
  431. public static Double Div(Double d1,Double d2){
  432. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  433. return d1/d2;
  434. }
  435. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  436. return d1/d2;
  437. }
  438. if(d1==0.0&&d2==0.0){
  439. return Double.NaN;
  440. }
  441. if(d2==0.0){
  442. return d1/d2;
  443. }
  444. BigDecimal b1=new BigDecimal(Double.toString(d1));
  445. BigDecimal b2=new BigDecimal(Double.toString(d2));
  446. return b1.divide(b2,8,BigDecimal.ROUND_HALF_UP).doubleValue();
  447. }
  448. protected String calculate(ArrayList equation){
  449. Double temp2;
  450. Double temp3;
  451. Double result;
  452. List operator=new ArrayList();
  453. List<Double> operand=new ArrayList();
  454. for(int i=0;i<equation.size();i++)
  455. {
  456. String temp4=(String) equation.get(i);
  457. if(temp4.equals("+")||temp4.equals("-")||temp4.equals("*")||temp4.equals("/"))
  458. {
  459. if(operator.size()>0)
  460. {
  461. String temp5=operator.get(operator.size()-1).toString();
  462. while(!(operatorPriorityCompare(temp4.charAt(0),temp5.charAt(0)))&&operator.size()>0)
  463. {
  464. operator.remove(operator.size()-1);
  465. temp3=operand.get(operand.size()-1);
  466. operand.remove(operand.size()-1);
  467. temp2=operand.get(operand.size()-1);
  468. operand.remove(operand.size()-1);
  469. switch (temp5.charAt(0)){
  470. case '+':{result=Add(temp2,temp3);operand.add(result);break;}
  471. case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
  472. case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
  473. case '/':{result=Div(temp2,temp3);operand.add(result);break;}
  474. }
  475. if(operator.size()>0)
  476. {
  477. temp5=operator.get(operator.size()-1).toString();
  478. }
  479. else
  480. break;
  481. }
  482. operator.add(temp4);
  483. }
  484. else
  485. operator.add(temp4);
  486. }
  487. else if(temp4.equals("#"))
  488. {
  489. while(operator.size()>0)
  490. {
  491. String temp6=(String)operator.get(operator.size()-1);
  492. operator.remove(operator.size()-1);
  493. temp3=operand.get(operand.size()-1);
  494. operand.remove(operand.size()-1);
  495. temp2=operand.get(operand.size()-1);
  496. operand.remove(operand.size()-1);
  497. switch (temp6.charAt(0)){
  498. case '+':{result=Add(temp2,temp3);operand.add(result);break;}
  499. case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
  500. case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
  501. case '/':{result=Div(temp2,temp3);operand.add(result);break;}
  502. }
  503. }
  504. }
  505. else
  506. {
  507. if(temp4.equals("NaN")){
  508. operand.add(Double.NaN);
  509. }else if(temp4.equals("∞")){
  510. operand.add(Double.POSITIVE_INFINITY);
  511. }else{
  512. operand.add(Double.parseDouble(temp4));
  513. }
  514. }
  515. }
  516. if(operand.get(0)==Double.NEGATIVE_INFINITY) return "-∞";
  517. if(operand.get(0)==Double.POSITIVE_INFINITY) return "∞";
  518. return operand.get(0).toString();
  519. }
  520. }
  521. /*
  522. * csdn 波士顿o泡果奶 版权所有 */

注:

xml文件中有一些图片,若完全粘贴xml代码,可将图片换成自己的图片,图片资源放到哪里,这里我就不多说了,可以参考下面的那些博客安卓studio图片资源放到哪里- CSDN搜索icon-default.png?t=N7T8https://so.csdn.net/so/search?q=%E5%AE%89%E5%8D%93studio%E5%9B%BE%E7%89%87%E8%B5%84%E6%BA%90%E6%94%BE%E5%88%B0%E5%93%AA%E9%87%8C&t=&u=&urw=

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/899478
推荐阅读
相关标签
  

闽ICP备14008679号