網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Android?studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的編寫(xiě)_Android
作者:Zzq_Fighting ? 更新時(shí)間: 2022-07-17 編程語(yǔ)言本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
話不多說(shuō),首先附上代碼:
MainActivity.java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
? ? EditText edit = null;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? edit = findViewById(R.id.edit_textview);
? ? }
? ? public void btnClick(View view) {
? ? ? ? switch (view.getId()){
? ? ? ? ? ? case R.id.btn0:
? ? ? ? ? ? ? ? edit.append("0");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn1:
? ? ? ? ? ? ? ? edit.append("1");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn2:
? ? ? ? ? ? ? ? edit.append("2");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn3:
? ? ? ? ? ? ? ? edit.append("3");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn4:
? ? ? ? ? ? ? ? edit.append("4");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn5:
? ? ? ? ? ? ? ? edit.append("5");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn6:
? ? ? ? ? ? ? ? edit.append("6");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn7:
? ? ? ? ? ? ? ? edit.append("7");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn8:
? ? ? ? ? ? ? ? edit.append("8");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn9:
? ? ? ? ? ? ? ? edit.append("9");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnPlus:
? ? ? ? ? ? ? ? edit.append("+");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnSubtract:
? ? ? ? ? ? ? ? edit.append("-");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnMultiply:
? ? ? ? ? ? ? ? edit.append("*");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnDivide:
? ? ? ? ? ? ? ? edit.append("/");
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? public void btnEqual(View view) {
? ? ? ? String str = edit.getText().toString();//1+2
? ? ? ? String res="";
? ? ? ? //Java計(jì)算代碼
? ? ? ? String result = calculate(str);
? ? ? ? edit.setText(result);
? ? }
? ? private static int number(char[] arr,int start,int end){
? ? ? ? StringBuilder buffer = new StringBuilder();
? ? ? ? for(int i=start;i<=end;i++){
? ? ? ? ? ? buffer.append(arr[i]);
? ? ? ? }
? ? ? ? return Integer.parseInt(buffer.toString());
? ? }
? ? // 待實(shí)現(xiàn)函數(shù),在此函數(shù)中填入答題代碼
? ? private static int comp(String op){
? ? ? ? if(op.equals("+") || op.equals("-"))
? ? ? ? ? ? return 1;
? ? ? ? if(op.equals("*") || op.equals("/"))
? ? ? ? ? ? return 2;
? ? ? ? return 0;
? ? }
? ? private static String compute(Integer a,Integer b,String op){
? ? ? ? Integer res;
? ? ? ? if(op.equals("+")) {
? ? ? ? ? ? res = a + b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("-")) {
? ? ? ? ? ? res= a - b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("*")) {
? ? ? ? ? ? res = a * b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("/") && b!=0) {
? ? ? ? ? ? res= a / b;
? ? ? ? ? ? return res.toString();
? ? ? ? }else{
? ? ? ? ? ? return "error";
? ? ? ? }
? ? }
? ? private static String calculate(String source) {
? ? ? ? Stack<Integer> numbers=new Stack<>();
? ? ? ? Stack<String> operator=new Stack<>();
? ? ? ? operator.push(".");
? ? ? ? char[] exps=source.toCharArray();
? ? ? ? int start=0;
? ? ? ? if(exps[0]=='-') numbers.push(0);
? ? ? ? for(int j=0;j<exps.length;j++){
? ? ? ? ? ? if(exps[j]=='+' || exps[j]=='*' || exps[j]=='/' || exps[j]=='-'){
? ? ? ? ? ? ? ? if (start <= j - 1) {
? ? ? ? ? ? ? ? ? ? numbers.push(number(exps,start,j-1));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? start=j+1;
? ? ? ? ? ? ? ? while (comp(operator.peek())>=comp(String.valueOf(exps[j]))){
? ? ? ? ? ? ? ? ? ? Integer two=numbers.peek();numbers.pop();
? ? ? ? ? ? ? ? ? ? Integer one=numbers.peek();numbers.pop();
? ? ? ? ? ? ? ? ? ? String result=compute(one,two,operator.peek());operator.pop();
? ? ? ? ? ? ? ? ? ? if (result.equals("error")) {
? ? ? ? ? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? numbers.push(Integer.parseInt(result));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? operator.push(String.valueOf(exps[j]));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? numbers.push(number(exps,start,exps.length-1));
? ? ? ? while (operator.size()>1){
? ? ? ? ? ? Integer two=numbers.peek();numbers.pop();
? ? ? ? ? ? Integer one =numbers.peek();numbers.pop();
? ? ? ? ? ? String op=operator.peek();operator.pop();
? ? ? ? ? ? String value = compute(one, two, op);
? ? ? ? ? ? if (value.equals("error")) {
? ? ? ? ? ? ? ? return value;
? ? ? ? ? ? }
? ? ? ? ? ? numbers.push(Integer.parseInt(value));
? ? ? ? }
? ? ? ? return numbers.peek().toString();
? ? }
? ? public void btnClear(View view) {
? ? ? ? edit.setText("");
? ? }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? <EditText ? ? ? ? android:id="@+id/edit_textview" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"/> ? ? <TableLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:stretchColumns="0,1,2,3"> ? ? ? ? <TableRow> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn7" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:onClick="btnClick" ? ? ? ? ? ? ? ? android:text="7" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn8" ? ? ? ? ? ? ? ? android:text="8" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn9" ? ? ? ? ? ? ? ? android:text="9" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnDivide" ? ? ? ? ? ? ? ? android:text="÷" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? </TableRow> ? ? ? ? <TableRow> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn4" ? ? ? ? ? ? ? ? android:text="4" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn5" ? ? ? ? ? ? ? ? android:text="5" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn6" ? ? ? ? ? ? ? ? android:text="6" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnMultiply" ? ? ? ? ? ? ? ? android:text="×" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? </TableRow> ? ? ? ? <TableRow> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn1" ? ? ? ? ? ? ? ? android:text="1" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn2" ? ? ? ? ? ? ? ? android:text="2" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn3" ? ? ? ? ? ? ? ? android:text="3" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnSubtract" ? ? ? ? ? ? ? ? android:text="-" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? </TableRow> ? ? ? ? <TableRow> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnClear" ? ? ? ? ? ? ? ? android:text="C" ? ? ? ? ? ? ? ? android:onClick="btnClear"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn0" ? ? ? ? ? ? ? ? android:text="0" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnEqual" ? ? ? ? ? ? ? ? android:text="=" ? ? ? ? ? ? ? ? android:onClick="btnEqual"/> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btnPlus" ? ? ? ? ? ? ? ? android:text="+" ? ? ? ? ? ? ? ? android:onClick="btnClick"/> ? ? ? ? </TableRow> ? ? </TableLayout> </LinearLayout>
計(jì)算器界面:
原文鏈接:https://blog.csdn.net/qq_45566213/article/details/123951205
相關(guān)推薦
- 2022-08-31 Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的解決方法_python
- 2023-12-19 CentOS和Ubuntu中防火墻相關(guān)命令
- 2022-05-18 ASP.NET?MVC自定義操作過(guò)濾器_實(shí)用技巧
- 2022-05-31 .NET?MAUI項(xiàng)目中創(chuàng)建超鏈接_實(shí)用技巧
- 2022-08-02 Golang中panic與recover的區(qū)別_Golang
- 2022-03-28 Go實(shí)現(xiàn)用戶每日限額的方法(例一天只能領(lǐng)三次福利)_Golang
- 2022-04-26 SQL?Server中Sequence對(duì)象用法_MsSql
- 2023-04-02 深入了解Go語(yǔ)言中web框架的中間件運(yùn)行機(jī)制_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支