安卓简易编辑器

自己做了个简易的小计算器,目前仅实现加减乘除操作,后续还未完善,只是简单记录一下自己的学习历程

计算器制作布局很简单,只是运用的算法清晰即可制作,我是运用了中缀表达式转化成后缀表达式,然后来的出计算器的算法。

首先是

(1)若为数字直接输出,并且储存在pre_out数组中,

(2)遇到运算符,若栈为空,则进栈,不然先弹出优先级较高的运算符到pre_out,再将运算符弹入栈中

(3)当读取完字符串,就将栈里面的元素都弹出,此时pre_out就是一个后缀表达式

(4)然后对pre_out进行操作,当遇到操作数,则直接加入栈中,当遇到运算符,则从栈中弹出两个元素进行运算,但是要注意元素的先后顺序和运算符的种类,即减法和除法要注意元素弹出的顺序,后弹出的应该作为被除数和被减数。

(5)在操作过程中,如果遇到运算符但是栈中只有一个元素,那么说明无法进行运算,表达式为错误表达式,我们就用Toast提示用户重新输入

(6)对的表达式最后应该栈中只有一个元素,那个就是结果取出输出即可

  1 /**
  2  * author Cc_Elvis
  3  */
  4 package com.hcc.my_calculate;
  5
  6 import java.util.LinkedList;
  7 import java.util.Stack;
  8
  9 import android.R.integer;
 10 import android.R.layout;
 11 import android.app.Activity;
 12 import android.os.Bundle;
 13 import android.view.Menu;
 14 import android.view.MenuItem;
 15 import android.view.View;
 16 import android.view.View.OnClickListener;
 17 import android.widget.Button;
 18 import android.widget.TextView;
 19 import android.widget.Toast;
 20
 21 public class MainActivity extends Activity
 22 {
 23     private TextView screenView;
 24     private Button zeroBtn;
 25     private Button oneBtn;
 26     private Button twoBtn;
 27     private Button threeBtn;
 28     private Button fourBtn;
 29     private Button fiveBtn;
 30     private Button sixBtn;
 31     private Button sevenBtn;
 32     private Button eightBtn;
 33     private Button nineBtn;
 34     private Button plusBtn;
 35     private Button minusBtn;
 36     private Button divideBtn;
 37     private Button MultiBtn;
 38     private Button equalBtn;
 39     private Button resBtn;
 40     private LinkedList<String> NumList=new LinkedList<String>();
 41     private String showString="";
 42     private String[] pre_out=new String[100];
 43     private int pre_out_Num=0;
 44     boolean illegal_flag=true;
 45     boolean isString=true;
 46     @Override
 47     protected void onCreate(Bundle savedInstanceState)
 48     {
 49         super.onCreate(savedInstanceState);
 50         setContentView(R.layout.myview);
 51         (oneBtn=(Button)findViewById(R.id.One)).setOnClickListener(listen);
 52         (twoBtn=(Button)findViewById(R.id.Two)).setOnClickListener(listen);
 53         (threeBtn=(Button)findViewById(R.id.Three)).setOnClickListener(listen);
 54         (fourBtn=(Button)findViewById(R.id.Four)).setOnClickListener(listen);
 55         (fiveBtn=(Button)findViewById(R.id.Five)).setOnClickListener(listen);
 56         (sixBtn=(Button)findViewById(R.id.Six)).setOnClickListener(listen);
 57         (sevenBtn=(Button)findViewById(R.id.Seven)).setOnClickListener(listen);
 58         (eightBtn=(Button)findViewById(R.id.Eight)).setOnClickListener(listen);
 59         (nineBtn=(Button)findViewById(R.id.Nine)).setOnClickListener(listen);
 60         (equalBtn=(Button)findViewById(R.id.equal)).setOnClickListener(listen);
 61         (divideBtn=(Button)findViewById(R.id.Division)).setOnClickListener(listen);
 62         (minusBtn=(Button)findViewById(R.id.Minus)).setOnClickListener(listen);
 63         (plusBtn=(Button)findViewById(R.id.plus)).setOnClickListener(listen);
 64         (MultiBtn=(Button)findViewById(R.id.Multip)).setOnClickListener(listen);
 65         (resBtn=(Button)findViewById(R.id.restart)).setOnClickListener(listen);
 66         (zeroBtn=(Button)findViewById(R.id.Zero)).setOnClickListener(listen);
 67         screenView=(TextView)findViewById(R.id.Screen);
 68         Init();
 69     }
 70     private OnClickListener listen=new OnClickListener()
 71     {
 72
 73         @Override
 74         public void onClick(View v)
 75         {
 76            Button btn=(Button)v;
 77            if( btn.getText().equals("1") || btn.getText().equals("2") ||
 78                btn.getText().equals("3") || btn.getText().equals("4") ||
 79                btn.getText().equals("5") || btn.getText().equals("6") ||
 80                btn.getText().equals("7") || btn.getText().equals("8") ||
 81                btn.getText().equals("9") || btn.getText().equals("0"))//如果为数字,直接输出并储存在pre_out数组里以转化成后缀表达式
 82               {
 83                              String c=btn.getText().toString();
 84                              addString(c);//add to TextEdit
 85                              if(isString)
 86                              {
 87                                pre_out[pre_out_Num]+=c;//add to Stack
 88                                isString=true;
 89                              }
 90                              else
 91                              {
 92                                  pre_out_Num++;
 93                                  pre_out[pre_out_Num]=c;
 94                                  isString=true;
 95                              }
 96               }
 97            else if(btn.getText().equals("AC"))//reset
 98            {
 99                restart();
100            }
101            else if(btn.getText().equals("="))//遇到‘=’则对栈开始进行清空并且输出到pre_out里面
102            {
103                while(!NumList.isEmpty())
104                {
105                    pre_out[++pre_out_Num]=NumList.removeFirst();
106                }
107                if(!GetResult())//judge if result true?
108                {
109                    Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_LONG).show();
110                }
111                else
112                {
113                 String restString=screenView.getText().toString();
114                 restart();
115                 screenView.setText(restString);
116                }
117            }
118            else if (btn.getText().equals("+") || btn.getText().equals("-") ||
119                       btn.getText().equals("*") || btn.getText().equals("/"))
120            {
121                isString=false;
122                String c=btn.getText().toString();
123                addString(c);//输出到TextView上面
124                if(NumList.isEmpty())//若栈为空则先将运算符压入
125                {
126                   NumList.addFirst(c);
127                }
128                else
129                {
130                    while(NumList.size()>=1&&get_prior(c)<=get_prior(NumList.getFirst().toString()))////先弹出优先级较高的运算符
131                    {
132                        if(pre_out_Num>0)
133                        pre_out[++pre_out_Num]=NumList.removeFirst();//转为后缀表达式序列
134                        else pre_out[pre_out_Num]=NumList.removeFirst();
135                    }
136                    NumList.addFirst(c);//再将此运算符入栈
137                }
138            }
139         }
140     };
141     private void restart()//重新开始
142     {
143         Init();
144         NumList.clear();
145         pre_out_Num=0;
146         screenView.setText(""+0);
147         showString="";
148     }
149     private void addString(String c)//显示结果框的字符串
150     {
151         showString+=c;
152          screenView.setText(showString);
153     }
154     private boolean GetResult()//获取结果
155     {
156       int length=0;
157       while(pre_out[length]!="")
158       {
159           String c=pre_out[length];
160           if(get_prior(c)==0)//数字直接压入栈
161           {
162               NumList.addFirst(c);
163               length++;
164           }
165           else
166           {
167              compute(c);
168              length++;
169           }
170       }
171       if(NumList.size()>1||get_prior(NumList.getFirst())!=0||illegal_flag==false)
172           return false;
173       if(!NumList.isEmpty())
174       {
175       int result=Integer.parseInt(NumList.removeFirst());
176       screenView.setText(""+result);
177       return true;
178       }
179       else return false;
180     }
181     private int get_prior(String c)
182     {
183         if(c.equals("+")||c.equals("-"))
184             return 1;
185         else if(c.equals("*")||c.equals("/"))
186             return 2;
187         else return 0;
188     }
189     private void compute(String c)//计算每一次的结果值
190     {
191         int sum =0,a,b;
192         if(NumList.size()>=2)//如果只剩下一个数,那么肯定不合法
193         {
194         a=Integer.parseInt(NumList.removeFirst());
195         b=Integer.parseInt(NumList.removeFirst());
196         }
197         else
198         {
199             illegal_flag=false;
200             return ;
201         } ;
202         if(c.equals("+"))
203         {
204             sum=a+b;
205         }
206         else if(c.equals("*"))
207         {
208             sum=a*b;
209         }
210         else if(c.equals("-"))
211         {
212             sum=b-a;
213         }
214         else if(c.equals("/"))
215         {
216             sum=b/a;
217         }
218         String s=""+sum;
219         NumList.addFirst(s);
220     }
221     private void Init()//初始化表达式
222     {
223         for(int i=0;i<100;i++)
224             pre_out[i]="";
225     }
226 }

XML

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="fill_parent"
  4     android:layout_height="fill_parent"
  5     android:orientation="vertical" >
  6     <TextView
  7     android:layout_marginTop="40dp"
  8     android:layout_width="fill_parent"
  9     android:layout_height="50dp"
 10     android:textSize="40dp"
 11     android:text="0"
 12     android:gravity="right"
 13     android:id="@+id/Screen"
 14     />
 15     <GridLayout
 16     android:layout_marginLeft="10dp"
 17     android:layout_marginTop="50dp"
 18     android:layout_height="wrap_content"
 19     android:layout_width="wrap_content"
 20     android:columnCount="4"
 21     android:rowCount="4">
 22         <Button
 23         android:layout_marginLeft="10dp"
 24         android:layout_marginTop="10dp"
 25         android:layout_width="wrap_content"
 26         android:layout_height="wrap_content"
 27         android:text="7"
 28         android:id="@+id/Seven"
 29         />
 30        <Button
 31         android:layout_marginLeft="10dp"
 32         android:layout_marginTop="10dp"
 33         android:layout_width="wrap_content"
 34         android:layout_height="wrap_content"
 35         android:text="8"
 36         android:id="@+id/Eight"
 37         />
 38        <Button
 39         android:layout_marginLeft="10dp"
 40         android:layout_marginTop="10dp"
 41         android:layout_width="wrap_content"
 42         android:layout_height="wrap_content"
 43         android:text="9"
 44         android:id="@+id/Nine"
 45         />
 46        <Button
 47         android:layout_marginLeft="10dp"
 48         android:layout_marginTop="10dp"
 49         android:layout_width="wrap_content"
 50         android:layout_height="wrap_content"
 51         android:text="/"
 52         android:id="@+id/Division"
 53         />
 54        <Button
 55         android:layout_marginLeft="10dp"
 56         android:layout_marginTop="10dp"
 57         android:layout_width="wrap_content"
 58         android:layout_height="wrap_content"
 59         android:text="4"
 60         android:id="@+id/Four"
 61         />
 62        <Button
 63         android:layout_marginLeft="10dp"
 64         android:layout_marginTop="10dp"
 65         android:layout_width="wrap_content"
 66         android:layout_height="wrap_content"
 67         android:text="5"
 68         android:id="@+id/Five"
 69         />
 70        <Button
 71         android:layout_marginLeft="10dp"
 72         android:layout_marginTop="10dp"
 73         android:layout_width="wrap_content"
 74         android:layout_height="wrap_content"
 75         android:text="6"
 76         android:id="@+id/Six"
 77         />
 78        <Button
 79         android:layout_marginLeft="10dp"
 80         android:layout_marginTop="10dp"
 81         android:layout_width="wrap_content"
 82         android:layout_height="wrap_content"
 83         android:text="@string/Multi"
 84         android:id="@+id/Multip"
 85         />
 86        <Button
 87         android:layout_marginLeft="10dp"
 88         android:layout_marginTop="10dp"
 89         android:layout_width="wrap_content"
 90         android:layout_height="wrap_content"
 91         android:text="1"
 92         android:id="@+id/One"
 93         />
 94        <Button
 95         android:layout_marginLeft="10dp"
 96         android:layout_marginTop="10dp"
 97         android:layout_width="wrap_content"
 98         android:layout_height="wrap_content"
 99         android:text="2"
100         android:id="@+id/Two"
101         />
102        <Button
103        android:layout_marginLeft="10dp"
104         android:layout_marginTop="10dp"
105         android:layout_width="wrap_content"
106         android:layout_height="wrap_content"
107         android:text="3"
108         android:id="@+id/Three"
109         />
110        <Button
111         android:layout_marginLeft="10dp"
112         android:layout_marginTop="10dp"
113         android:layout_width="wrap_content"
114         android:layout_height="wrap_content"
115         android:text="-"
116         android:id="@+id/Minus"
117         />
118         <Button
119         android:layout_marginLeft="10dp"
120         android:layout_marginTop="10dp"
121         android:layout_width="wrap_content"
122         android:layout_height="wrap_content"
123         android:text="0"
124         android:id="@+id/Zero"
125         />
126        <Button
127            android:layout_marginLeft="10dp"
128            android:layout_marginTop="10dp"
129            android:id="@+id/equal"
130            android:layout_gravity="fill"
131            android:text="="
132         />
133        <Button
134            android:layout_marginLeft="10dp"
135            android:layout_marginTop="10dp"
136            android:id="@+id/restart"
137            android:text="@string/ac"
138         />
139
140        <Button
141         android:layout_marginLeft="10dp"
142         android:layout_marginTop="10dp"
143         android:layout_width="wrap_content"
144         android:layout_height="wrap_content"
145         android:text="+"
146         android:id="@+id/plus"
147         />
148 </GridLayout>
149
150     <TextView
151         android:layout_width="fill_parent"
152         android:layout_height="wrap_content"
153         android:layout_marginBottom="10dp"
154         android:text="This is CC‘s Calculator" />
155
156 </LinearLayout>

代码量较小,健壮性还有待完善。。。。

---恢复内容结束---

时间: 2024-08-10 00:01:52

安卓简易编辑器的相关文章

java简易编辑器

1 package peng_jun; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import java.util.*; 8 import java.io.*; 9 import javax.swing.filechooser.*; 10 import javax.swing.filechooser.FileFilter; 11

安卓简易计算器

layout:  activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height=&

实现一个简易的富文本编辑器(二):给富文本添加自定义事件

为富文本添加一个提交按钮,点击按钮可以获取富文本内容.但是在提交前或者提交后我想做一些操作,比如内容校验内容清空等等. 我们直接在该按钮上绑定点击事件同样可以达到目的,但是为了组件化,所以本例打算为提交按钮自定义beforeSubmit.afterSubmit两个事件. 1.创建发布订阅者对象 前文说到,事件系统是发布-订阅模式的一个实现,模式给事件发布函数与事件处理函数进行解耦,使得两者无直接调用关系. 简易发布订阅者对象实现如下: var Event = { // _cachePool :

安卓手机修改host

 电脑修改法 1.手机必须先root,小米可以安卓开发版系统即可 2.安卓 adb工具(android debug bridge) 3.依次执行下面的命令 1.adb root 获得root权限 会自动启动server 2.adb remount 设置/system为可读写: 3.adb pull /system/etc/hosts 将手机的hosts文件复制到PC的C盘根目录 4.PC机上打开hosts文件,修改完成后,保存 5.adb push hosts /system/etc/ 将PC机

使用UE4发布安卓平台游戏

使用了几天的UE4 ,总算是将游戏在安卓平台运行起来了.其中遇到很多问题,并且最终依然有一些问题没能解决.总体感觉是UE4这款引擎目前还不够成熟,问题较多.没有unity使用起来方便.但是既然开放了,发展必然迅猛,值得期待.其代码也值得游戏开发编程人员研究. 我们来看看具体步骤: 首先,编译出引擎.对于学习目的来说,从网上下载一份就够了,编译步骤简单的描述一下:我们把下载的代码解压到指定的目录,包括UnrealEngine-4.*.*-release.7z,Required_1of2.7z,Re

[译] 开始使用 wxPython [Getting started with wxPython]

原文:http://wiki.wxpython.org/Getting%20Started 1. 第一个应用程序:Hello World 按照“国际惯例”,我们先写一个“Hello World”的应用程序,下面是代码: 1 #!/usr/bin/env python 2 import wx 3 4 app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window. 5 frame = wx.Frame

Android开发中Eclipse里的智能提示设置

今天开始学习一下Android开发,直接在Android Developers下载的一个开发工具包,然后再下了一个JDK,配置完环境变量等一系列的工作后环境就搭建好了,在新建好第一个Android项目 后,唉?写代码没有智能提示?对于用惯了VS的.NET开发者来说,简直不能接受啊,肯定哪里有设置对不对! 百度了一下,设置如下:1.java文件中智能提示打 开Eclipse 依次选择 Window > Preferences > Java > Editor - Content Assist

Linux入门之一步一步安装系统

1引言 2安装前的准备工作 下载vmware workstation 下载gentoo所需要的文件 知识点 1 我们下载的是基于x86架构的安装包在这里我们可以学习到用什么来区分架 构例如X86SPARC MIPS等这些标识主要是用来区分cpu的指令集的不同体系 不同型号的cpu有不同的指令集因此我们选择安装包时就需要选择和cpu指令匹配的包 2 几乎所有的个人电脑PC都是X86X86_64的 3 64位和32位的区别是cpu总线的宽度不同cpu总线又有数据总线控制总线和地址总线之分数据总线位数

【收藏】75个很有用的开源移动工具

据皮尤研究中心声称,现在68%的美国成年人拥有智能手机,只比拥有台式机或笔记本电脑的用户少5%.而美国45%的成年人现在拥有平板电脑. 考虑到移动设备如此普遍,开源社区日益致力于与移动有关的项目也就不足为奇了.本月,我们盘点了75款这样的工具――比去年我们更新这份列表时添加的工具整整多出了25款.又由于现在有如此多的开源项目与移动有关,我们缩小了一点范围,只关注企业组织可能感兴趣的那些项目.因而,本文介绍了对企业员工来说实用的一大批移动开发工具.安全及隐私解决方案和应用程序. 与往常一样,如果你